state api: Make *_has_new_state() usable without a nebstruct type
[nagios-reports-module.git] / state.c
blob5e5c74caf13b2f7830b1bb5a603c601024acb904
1 #include "state.h"
2 #include "hash.h"
3 #include "utils.h"
4 #include <stdlib.h>
6 #define HOST_STATES_HASH_BUCKETS 4096
7 #define SERVICE_STATES_HASH_BUCKETS (HOST_STATES_HASH_BUCKETS * 4)
9 /*
10 * The hooks are called from broker.c in Nagios.
12 static hash_table *host_states;
13 static hash_table *svc_states;
15 int state_init(void)
17 host_states = hash_init(HOST_STATES_HASH_BUCKETS);
18 if (!host_states)
19 return -1;
21 svc_states = hash_init(SERVICE_STATES_HASH_BUCKETS);
22 if (!svc_states) {
23 free(host_states);
24 host_states = NULL;
25 return -1;
28 return prime_initial_states(host_states, svc_states);
31 static inline int has_state_change(int *old, int state, int type)
34 * A state change is considered to consist of a change
35 * to either state_type or state, so we OR the two
36 * together to form a complete state. This will make
37 * the module log as follows:
38 * service foo;poo is HARD OK initially
39 * service foo;poo goes to SOFT WARN, attempt 1 (logged)
40 * service foo;poo goes to SOFT WARN, attempt 2 (not logged)
41 * service foo;poo goes to HARD WARN (logged)
43 state = CAT_STATE(state, type);
45 if (*old == state)
46 return 0;
48 *old = state;
49 return 1;
52 int host_has_new_state(char *host, int state, int type)
54 int *old_state;
56 old_state = hash_find(host_states, host);
57 if (!old_state) {
58 int *cur_state;
60 cur_state = malloc(sizeof(*cur_state));
61 *cur_state = CAT_STATE(state, type);
62 hash_add(host_states, host, cur_state);
63 return 1;
66 return has_state_change(old_state, state, type);
69 int service_has_new_state(char *host, char *desc, int state, int type)
71 int *old_state;
73 old_state = hash_find2(svc_states, host, desc);
74 if (!old_state) {
75 int *cur_state;
77 cur_state = malloc(sizeof(*cur_state));
78 *cur_state = CAT_STATE(state, type);
79 hash_add2(svc_states, host, desc, cur_state);
80 return 1;
83 return has_state_change(old_state, state, type);