showlog: Add pretty-print things for notification events
[nagios-reports-module.git] / nebtest.c
blob68008e82940077ba73e41072440b20895ad965bb
1 #include "ndbneb.h"
2 #include <string.h>
3 #include <stdlib.h>
4 #include <dlfcn.h>
5 #include <nagios/nebstructs.h>
6 #include <nagios/nebmodules.h>
7 #include <nagios/nebmods.h>
8 #include <nagios/broker.h>
10 static int (*hooks[NEBCALLBACK_NUMITEMS])(int, void *);
11 static int reg_errors;
12 static int dereg_errors;
14 int event_broker_options = 0;
15 int daemon_dumps_core = 0;
17 int is_tested(int callback_type, const char *caller_func)
19 switch (callback_type) {
20 case NEBCALLBACK_SERVICE_CHECK_DATA:
21 case NEBCALLBACK_HOST_CHECK_DATA:
22 case NEBCALLBACK_DOWNTIME_DATA:
23 case NEBCALLBACK_PROCESS_DATA:
24 return 1;
27 printf("! %s: Callback %d is unhandled by nebtest!\n",
28 caller_func, callback_type);
29 return 0;
32 int neb_register_callback(int callback_type, void *mod_handle,
33 int priority, int (*callback_func)(int,void *))
35 if (!is_tested(callback_type, __func__))
36 reg_errors |= 1 << callback_type;
38 if (hooks[callback_type]) {
39 reg_errors |= 1 << callback_type;
40 printf("! Registering a second hook for %d!", callback_type);
43 hooks[callback_type] = callback_func;
45 return 0;
48 int neb_deregister_callback(int callback_type, int (*callback_func)(int, void *))
50 if (!is_tested(callback_type, __func__))
51 dereg_errors |= 1 << callback_type;
53 if (!hooks[callback_type]) {
54 dereg_errors |= 1 << callback_type;
55 printf("! Trying to unregister an unregistered hook!\n");
58 hooks[callback_type] = NULL;
60 return 0;
63 static int check_callbacks(void)
65 int i, ret = 0;
67 for (i = 0; i < NEBCALLBACK_NUMITEMS; i++) {
68 if (hooks[i]) {
69 ret = -1;
70 printf("! hook %d not unregistered by module unload function\n", i);
74 return ret;
77 static int check_symbols(void *dso)
79 int i, result = 0;
81 const char *syms[] = {
82 "__neb_api_version", "nebmodule_init", "nebmodule_deinit",
83 NULL,
86 for (i = 0; syms[i]; i++) {
87 const char *sym = syms[i];
89 if (dlsym(dso, sym))
90 printf(" %-20s OK\n", sym);
91 else {
92 printf(" ! %-20s FAILED: %s\n", sym, dlerror());
93 result = -1;
97 return result;
100 static char *__host_name = "ndb, host";
101 static char *__service_description = "ndb, service";
102 #define init_ds(x) \
103 do { \
104 memset(&x, 0, sizeof(x)); \
105 x.timestamp.tv_sec = time(NULL); \
106 x.host_name = __host_name; \
107 } while(0)
108 #define set_service(x) x.service_description = __service_description
110 static int test_sql_host_insert(void)
112 int (*hook)(int, void *);
113 nebstruct_host_check_data ds;
114 int cb = NEBCALLBACK_HOST_CHECK_DATA;
116 if (!(hook = hooks[cb])) {
117 printf(" ! Missing host_check hook\n");
118 return -1;
121 init_ds(ds);
122 ds.type = NEBTYPE_HOSTCHECK_PROCESSED;
123 ds.output = "nebtest host check";
124 return hook(cb, &ds);
127 static int test_sql_service_insert(void)
129 int (*hook)(int, void *);
130 nebstruct_service_check_data ds;
131 int cb = NEBCALLBACK_SERVICE_CHECK_DATA;
133 if (!(hook = hooks[cb])) {
134 printf(" ! Missing service_check hook\n");
135 return -1;
138 init_ds(ds);
139 set_service(ds);
140 ds.type = NEBTYPE_SERVICECHECK_PROCESSED;
141 ds.output = strdup("nebtest service check");
143 return hook(cb, &ds);
146 static int test_sql_process_data_insert(void)
148 int (*hook)(int, void *);
149 nebstruct_process_data ds;
150 int cb = NEBCALLBACK_PROCESS_DATA;
152 if (!(hook = hooks[cb])) {
153 printf(" ! Missing process data hook\n");
154 return -1;
157 memset(&ds, 0, sizeof(ds));
158 ds.timestamp.tv_sec = time(NULL);
159 ds.type = NEBTYPE_PROCESS_START;
161 return hook(cb, &ds);
164 static int test_sql_downtime_insert(void)
166 int (*hook)(int, void *);
167 int result = 0, err;
168 nebstruct_downtime_data ds;
169 int cb = NEBCALLBACK_DOWNTIME_DATA;
171 if (!(hook = hooks[cb])) {
172 printf(" ! Missing downtime data hook\n");
173 return -1;
176 init_ds(ds);
177 ds.type = NEBTYPE_DOWNTIME_START;
178 ds.comment_data = "nebtest downtime";
180 printf("Testing host downtime insertion\n");
181 if ((err = hook(cb, &ds)))
182 puts(" ! failed");
183 else
184 puts(" OK");
185 result |= err;
187 printf("Testing service downtime insertion\n");
188 set_service(ds);
189 if ((err = hook(cb, &ds)))
190 puts(" ! failed");
191 else
192 puts(" OK");
193 result |= err;
195 return result;
199 static int test_sql_inserts(void)
201 int ret = 0, errors = 0;
203 printf("Testing hooks\n");
204 errors = test_sql_host_insert();
205 if (errors)
206 printf(" ! host check hook failed\n");
207 else
208 printf(" host check hook checks out ok\n");
209 ret |= errors;
211 if ((errors = test_sql_service_insert()))
212 printf(" ! service check hook failed\n");
213 else
214 printf(" service check hook checks out ok\n");
215 ret |= errors;
217 if ((errors = test_sql_downtime_insert()))
218 printf(" ! downtime hook failed\n");
219 else
220 printf(" downtime hook checks out ok\n");
221 ret |= errors;
223 if ((errors = test_sql_process_data_insert()))
224 printf(" ! process data hook failed\n");
225 else
226 printf(" process data hook checks out ok\n");
227 ret |= errors;
229 return ret;
232 static nebmodule *neb;
233 static int test_one_module(char *arg)
235 static void *dso = NULL;
236 char *path;
237 int result = 0, retval;
238 static int (*init_func)(int, const char *, nebmodule *);
239 static int (*deinit_func)(int, int);
241 if (dso)
242 dlclose(dso);
244 if (strchr(arg, '/'))
245 path = arg;
246 else {
247 int len = strlen(arg);
248 path = calloc(len + 3, 1);
249 path[0] = '.';
250 path[1] = '/';
251 memcpy(path + 2, arg, len);
254 dso = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
255 if (!dso) {
256 printf("dlopen() failed: %s\n", dlerror());
257 return -1;
259 printf("Checking symbols in '%s'\n", path);
261 retval = check_symbols(dso);
262 result |= retval;
263 if (retval)
264 return result;
266 init_func = dlsym(dso, "nebmodule_init");
267 retval = init_func(-1, neb->args, neb);
268 if (retval) {
269 printf("Failed to run init function from %s\n", arg);
270 return -1;
273 result |= retval;
275 retval = test_sql_inserts();
276 result |= retval;
277 if (retval)
278 return result;
280 deinit_func = dlsym(dso, "nebmodule_deinit");
281 result |= deinit_func(0, 0);
282 result |= check_callbacks();
284 if (reg_errors)
285 printf("! There were errors with registering callbacks\n");
286 if (dereg_errors)
287 printf("! There were errors de-registering callbacks\n");
288 if (reg_errors | dereg_errors && reg_errors == dereg_errors)
289 printf("? Errors may cancel out\n");
290 printf("### Module %s %s\n", path, result ? "failed" : "checked out ok");
292 return result | reg_errors | dereg_errors;
295 int main(int argc, char **argv)
297 int i;
298 int err = 0;
300 neb = calloc(sizeof(*neb), 1);
302 for (i = 1; i < argc; i++) {
303 char *arg = argv[i];
305 if (*arg == '-' && arg[1] == 'f' && i < argc - 1) {
306 neb->args = argv[++i];
307 continue;
310 err |= test_one_module(arg);
313 if (err)
314 return 1;
316 return 0;