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
:
27 printf("! %s: Callback %d is unhandled by nebtest!\n",
28 caller_func
, callback_type
);
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
;
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
;
63 static int check_callbacks(void)
67 for (i
= 0; i
< NEBCALLBACK_NUMITEMS
; i
++) {
70 printf("! hook %d not unregistered by module unload function\n", i
);
77 static int check_symbols(void *dso
)
81 const char *syms
[] = {
82 "__neb_api_version", "nebmodule_init", "nebmodule_deinit",
86 for (i
= 0; syms
[i
]; i
++) {
87 const char *sym
= syms
[i
];
90 printf(" %-20s OK\n", sym
);
92 printf(" ! %-20s FAILED: %s\n", sym
, dlerror());
100 static char *__host_name
= "ndb, host";
101 static char *__service_description
= "ndb, service";
104 memset(&x, 0, sizeof(x)); \
105 x.timestamp.tv_sec = time(NULL); \
106 x.host_name = __host_name; \
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");
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");
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");
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 *);
168 nebstruct_downtime_data ds
;
169 int cb
= NEBCALLBACK_DOWNTIME_DATA
;
171 if (!(hook
= hooks
[cb
])) {
172 printf(" ! Missing downtime data hook\n");
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
)))
187 printf("Testing service downtime insertion\n");
189 if ((err
= hook(cb
, &ds
)))
199 static int test_sql_inserts(void)
201 int ret
= 0, errors
= 0;
203 printf("Testing hooks\n");
204 errors
= test_sql_host_insert();
206 printf(" ! host check hook failed\n");
208 printf(" host check hook checks out ok\n");
211 if ((errors
= test_sql_service_insert()))
212 printf(" ! service check hook failed\n");
214 printf(" service check hook checks out ok\n");
217 if ((errors
= test_sql_downtime_insert()))
218 printf(" ! downtime hook failed\n");
220 printf(" downtime hook checks out ok\n");
223 if ((errors
= test_sql_process_data_insert()))
224 printf(" ! process data hook failed\n");
226 printf(" process data hook checks out ok\n");
232 static nebmodule
*neb
;
233 static int test_one_module(char *arg
)
235 static void *dso
= NULL
;
237 int result
= 0, retval
;
238 static int (*init_func
)(int, const char *, nebmodule
*);
239 static int (*deinit_func
)(int, int);
244 if (strchr(arg
, '/'))
247 int len
= strlen(arg
);
248 path
= calloc(len
+ 3, 1);
251 memcpy(path
+ 2, arg
, len
);
254 dso
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
256 printf("dlopen() failed: %s\n", dlerror());
259 printf("Checking symbols in '%s'\n", path
);
261 retval
= check_symbols(dso
);
266 init_func
= dlsym(dso
, "nebmodule_init");
267 retval
= init_func(-1, neb
->args
, neb
);
269 printf("Failed to run init function from %s\n", arg
);
275 retval
= test_sql_inserts();
280 deinit_func
= dlsym(dso
, "nebmodule_deinit");
281 result
|= deinit_func(0, 0);
282 result
|= check_callbacks();
285 printf("! There were errors with registering callbacks\n");
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
)
300 neb
= calloc(sizeof(*neb
), 1);
302 for (i
= 1; i
< argc
; i
++) {
305 if (*arg
== '-' && arg
[1] == 'f' && i
< argc
- 1) {
306 neb
->args
= argv
[++i
];
310 err
|= test_one_module(arg
);