showlog: Get rid of EVT_INITIAL
[nagios-reports-module.git] / state.c
blob457bc5e7ca43bd1cd419be384350c87b4cd8d68f
1 #include "state.h"
2 #include "hash.h"
3 #include "utils.h"
4 #include <stdlib.h>
5 #include <string.h>
7 #define HOST_STATES_HASH_BUCKETS 4096
8 #define SERVICE_STATES_HASH_BUCKETS (HOST_STATES_HASH_BUCKETS * 4)
11 * The hooks are called from broker.c in Nagios.
13 static hash_table *host_states;
14 static hash_table *svc_states;
16 int state_init(void)
18 host_states = hash_init(HOST_STATES_HASH_BUCKETS);
19 if (!host_states)
20 return -1;
22 svc_states = hash_init(SERVICE_STATES_HASH_BUCKETS);
23 if (!svc_states) {
24 free(host_states);
25 host_states = NULL;
26 return -1;
29 return prime_initial_states(host_states, svc_states);
32 static inline int has_state_change(int *old, int state, int type)
35 * A state change is considered to consist of a change
36 * to either state_type or state, so we OR the two
37 * together to form a complete state. This will make
38 * the module log as follows:
39 * service foo;poo is HARD OK initially
40 * service foo;poo goes to SOFT WARN, attempt 1 (logged)
41 * service foo;poo goes to SOFT WARN, attempt 2 (not logged)
42 * service foo;poo goes to HARD WARN (logged)
44 state = CAT_STATE(state, type);
46 if (*old == state)
47 return 0;
49 *old = state;
50 return 1;
53 int host_has_new_state(char *host, int state, int type)
55 int *old_state;
57 old_state = hash_find(host_states, host);
58 if (!old_state) {
59 int *cur_state;
61 cur_state = malloc(sizeof(*cur_state));
62 *cur_state = CAT_STATE(state, type);
63 hash_add(host_states, strdup(host), cur_state);
64 return 1;
67 return has_state_change(old_state, state, type);
70 int service_has_new_state(char *host, char *desc, int state, int type)
72 int *old_state;
74 old_state = hash_find2(svc_states, host, desc);
75 if (!old_state) {
76 int *cur_state;
78 cur_state = malloc(sizeof(*cur_state));
79 *cur_state = CAT_STATE(state, type);
80 hash_add2(svc_states, strdup(host), strdup(desc), cur_state);
81 return 1;
84 return has_state_change(old_state, state, type);