state.c: Squelch warning about lacking prototype for 'free()'
[nagios-reports-module.git] / state.c
blobebf6898ab8ae5244c3caf4a7ee324e20b372c9a0
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(nebstruct_host_check_data *ds)
54 int *old_state;
56 old_state = hash_find(host_states, ds->host_name);
57 if (!old_state) {
58 int *state;
60 state = malloc(sizeof(*state));
61 *state = CAT_STATE(ds->state, ds->state_type);
62 hash_add(host_states, ds->host_name, state);
63 return 1;
66 return has_state_change(old_state, ds->state, ds->state_type);
69 int service_has_new_state(nebstruct_service_check_data *ds)
71 int *old_state;
73 old_state = hash_find2(svc_states, ds->host_name, ds->service_description);
74 if (!old_state) {
75 int *state;
77 state = malloc(sizeof(*state));
78 *state = CAT_STATE(ds->state, ds->state_type);
79 hash_add2(svc_states, ds->host_name, ds->service_description, state);
80 return 1;
83 return has_state_change(old_state, ds->state, ds->state_type);