db_updater: Put parentheses back
[merlin.git] / state.c
blob6bff2bc6f817536b4efe70f553cda9fe54b70d70
1 #include <stdlib.h>
2 #include <string.h>
3 #include "state.h"
4 #include "mrln_logging.h"
5 #include <nagios/lib/dkhash.h>
7 #define HOST_STATES_HASH_BUCKETS 4096
8 #define SERVICE_STATES_HASH_BUCKETS (HOST_STATES_HASH_BUCKETS * 4)
9 static dkhash_table *host_states, *svc_states;
11 int state_init(void)
13 host_states = dkhash_create(HOST_STATES_HASH_BUCKETS);
14 if (!host_states)
15 return -1;
17 svc_states = dkhash_create(SERVICE_STATES_HASH_BUCKETS);
18 if (!svc_states) {
19 free(host_states);
20 host_states = NULL;
21 return -1;
24 return 0;
27 static inline int has_state_change(int *old, int state, int type)
30 * A state change is considered to consist of a change
31 * to either state_type or state, so we OR the two
32 * together to form a complete state. This will make
33 * the module log as follows:
34 * service foo;poo is HARD OK initially
35 * service foo;poo goes to SOFT WARN, attempt 1 (logged)
36 * service foo;poo goes to SOFT WARN, attempt 2 (not logged)
37 * service foo;poo goes to HARD WARN (logged)
39 state = CAT_STATE(state, type);
41 if (*old == state)
42 return 0;
44 *old = state;
45 return 1;
48 int host_has_new_state(char *host, int state, int type)
50 int *old_state;
52 if (!host) {
53 lerr("host_has_new_state() called with NULL host");
54 return 0;
56 old_state = dkhash_get(host_states, host, NULL);
57 if (!old_state) {
58 int *cur_state;
60 cur_state = malloc(sizeof(*cur_state));
61 *cur_state = CAT_STATE(state, type);
62 dkhash_insert(host_states, strdup(host), NULL, 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 if (!host) {
74 lerr("service_has_new_state() called with NULL host");
75 return 0;
77 if (!desc) {
78 lerr("service_has_new_state() called with NULL desc");
79 return 0;
81 old_state = dkhash_get(svc_states, host, desc);
82 if (!old_state) {
83 int *cur_state;
85 cur_state = malloc(sizeof(*cur_state));
86 *cur_state = CAT_STATE(state, type);
87 dkhash_insert(svc_states, strdup(host), strdup(desc), cur_state);
88 return 1;
91 return has_state_change(old_state, state, type);