-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatus.c
210 lines (173 loc) · 4.96 KB
/
status.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "daemon.h"
#include <nagios/broker.h>
#include <stdlib.h>
#include <string.h>
static struct object_state *object_states[2];
static size_t merlin_num_objects[2];
/*
* sort/search function for the state caches
*/
static int state_compare(const void *_a, const void *_b)
{
object_state *a, *b;
a = (object_state *)_a;
b = (object_state *)_b;
return strcmp(a->name, b->name);
}
/*
* Creates a state cache table from a dbi result set, sorts the
* table and then returns it. *count holds the number of items
* in the state cache
*/
static struct object_state *store_object_states(db_wrap_result * result, size_t *count)
{
int i = 0;
struct object_state *state_ary = NULL;
if (!result) {
lwarn("store_object_states() called with NULL result pointer (non-fatal)");
return NULL;
}
*count = 0;
result->api->num_rows(result, count);
if (!*count)
goto out;
state_ary = calloc(*count, sizeof(struct object_state));
if (!state_ary)
goto out;
while (0 == result->api->step(result)) {
int state, state_type;
struct object_state *os = &state_ary[i++];
char const * str = NULL;
result->api->get_string_ndx(result, 0, &str, NULL);
os->name = (str&&*str) ? strdup(str) : NULL;
/* reminder: os->name is leaked here! We should add an
atexit() handler to clean it up.*/
result->api->get_int32_ndx(result, 1, &state);
result->api->get_int32_ndx(result, 2, &state_type);
os->state = concat_state(state_type, state);
}
/*
* Some sql engines sort case-insensitively, but we can't
* use that since "FOO" and "foo" are both valid names, and
* we're not allowed to be agnostic about them.
* In order to be 100% safe, we sort the results ourself
* instead. Note that in most cases, this will just loop
* once over the objects without actually do anything.
*/
qsort(state_ary, *count, sizeof(object_state), state_compare);
out:
sql_free_result();
return state_ary;
}
/*
* Obtains a dbi result set for host states and passes it to the
* store_object_states() helper
*/
static int prime_host_states(size_t *count)
{
sql_query("SELECT host_name, current_state, state_type "
"FROM %s.host ORDER BY host_name", sql_db_name());
object_states[0] = store_object_states(sql_get_result(), count);
merlin_num_objects[0] = *count;
return object_states[0] != NULL;
}
/*
* Obtains a dbi result set for service states and passes it to the
* store_object_states() helper
*/
static int prime_service_states(size_t *count)
{
sql_query("SELECT CONCAT(host_name, ';', service_description) as name, current_state, state_type "
"FROM %s.service ORDER BY name", sql_db_name());
object_states[1] = store_object_states(sql_get_result(), count);
merlin_num_objects[1] = *count;
return object_states[1] != NULL;
}
/*
* Destroyt the state table *ostate, which should hold count items
*/
static void destroy_states(struct object_state *ostate, size_t count)
{
size_t i;
if (!ostate)
return;
for (i = 0; i < count; i++)
free(ostate[i].name);
free(ostate);
}
/*
* The public primer for the object state cache. This wipes both
* host and service state caches and then re-creates them from
* the database
*/
int prime_object_states(size_t *hosts, size_t *services)
{
if (!use_database)
return 0;
destroy_states(object_states[0], merlin_num_objects[0]);
destroy_states(object_states[1], merlin_num_objects[1]);
return prime_host_states(hosts) | prime_service_states(services);
}
/*
* Fetch a particular object state, using binary search on the
* alphabetically sorted tables
*/
object_state *get_object_state(const char *name, size_t id)
{
size_t mid, high, low = 0;
int result;
object_state *ary;
high = merlin_num_objects[id];
ary = object_states[id];
/* binary search in the alphabetically sorted array */
while (low < high) {
object_state *st;
mid = low + ((high - low) / 2);
st = &ary[mid];
result = strcmp(name, st->name);
if (result > 0) {
low = mid + 1;
continue;
}
if (result < 0) {
high = mid;
continue;
}
/* we hit the sweet spot */
return st;
}
return NULL;
}
/*
* Wrapper for get_object_state()
*/
object_state *get_host_state(const char *name)
{
return get_object_state(name, 0);
}
/*
* To avoid having to use a dual-key object_state structure, we cache
* services with 'hostname;servicedescription' type strings, relying
* on the fact that semi-colon will never be a valid object name char.
* This wrapper concatenates a hostname and a servicedescription thusly,
* making it possible for us to use a common helper for both host and
* service states
*/
object_state *get_service_state(const char *h_name, const char *s_name)
{
char name[4096];
snprintf(name, sizeof(name) - 1, "%s;%s", h_name, s_name);
return get_object_state(name, 1);
}
/*
* Primarily for debugging purposes
*/
size_t foreach_state(int id, int (*fn)(object_state *))
{
size_t i;
for (i = 0; i < merlin_num_objects[id]; i++) {
object_state *st = &object_states[id][i];
fn(st);
}
return i;
}