import.c: Count and (optionally) print max downtime depth
[nagios-reports-module.git] / module.c
blob5788baa081b4fcad5aa480011c3e9b597e516302
1 /*
2 * Author: Andreas Ericsson <ae@op5.se>
4 * Copyright(C) 2005 op5 AB
5 * All rights reserved.
7 */
9 #define NSCORE 1
10 #include "ndbneb.h"
11 #include <signal.h>
12 #include "module.h"
13 #include "cfgfile.h"
14 #include "sql.h"
15 #include "hooks.h"
16 #include <nagios/nebstructs.h>
17 #include <nagios/objects.h>
18 #include <nagios/statusdata.h>
20 /* Nagios stuff goes below */
21 NEB_API_VERSION(CURRENT_NEB_API_VERSION);
23 int cb_handler(int, void *);
25 static nebmodule *neb_handle;
26 extern int daemon_dumps_core;
27 extern int event_broker_options;
29 static struct callback_struct {
30 int type;
31 int (*hook)(int, void *);
32 } callback_table[] = {
33 { NEBCALLBACK_PROCESS_DATA, hook_process_data },
34 // { NEBCALLBACK_LOG_DATA, cb_handler },
35 // { NEBCALLBACK_SYSTEM_COMMAND_DATA, cb_handler },
36 // { NEBCALLBACK_EVENT_HANDLER_DATA, cb_handler },
37 // { NEBCALLBACK_NOTIFICATION_DATA, cb_handler },
38 { NEBCALLBACK_SERVICE_CHECK_DATA, hook_service_result },
39 { NEBCALLBACK_HOST_CHECK_DATA, hook_host_result },
40 // { NEBCALLBACK_COMMENT_DATA, cb_handler },
41 { NEBCALLBACK_DOWNTIME_DATA, hook_downtime },
42 // { NEBCALLBACK_FLAPPING_DATA, cb_handler },
43 // { NEBCALLBACK_PROGRAM_STATUS_DATA, cb_handler },
44 // { NEBCALLBACK_HOST_STATUS_DATA, cb_handler },
45 // { NEBCALLBACK_SERVICE_STATUS_DATA, cb_handler },
48 #define SELF "ndbneb"
50 static int read_config(const char *arg)
52 struct cfg_comp *config;
53 struct cfg_var *v;
54 int ret = 0;
56 if (!arg || !*arg || !(config = cfg_parse_file(arg))) {
57 lerr("Failed to read config from %s", arg ? arg : "(no file)");
58 return -1;
61 while ((v = next_var(config)))
62 ret |= sql_config(v->key, v->value);
64 return ret;
67 int nebmodule_init(int flags, char *arg, nebmodule *handle)
69 char *home = NULL, cwd[PATH_MAX];
70 int i;
72 neb_handle = handle;
74 linfo("Loading %s", SELF);
76 if (read_config(arg) < 0)
77 return -1;
79 if (sql_init() < 0)
80 return -1;
82 if (hook_init() < 0)
83 return -1;
85 /* make sure we can catch whatever we want */
86 event_broker_options = BROKER_EVERYTHING;
88 ldebug("Forcing coredumps");
89 daemon_dumps_core = 1;
90 home = getenv("HOME");
91 if (!home)
92 home = "/tmp";
94 signal(SIGSEGV, SIG_DFL);
97 * if flags == -1, we're running from nebtest, so make sure
98 * coredumps end up in cwd. This *shouldn't* affect anything
99 * (famous last words, no?)
101 if (flags != -1)
102 chdir(home);
103 getcwd(cwd, sizeof(cwd));
104 linfo("Coredumps end up in %s", cwd);
106 for (i = 0; i < ARRAY_SIZE(callback_table); i++) {
107 struct callback_struct *cb = &callback_table[i];
109 neb_register_callback(cb->type, neb_handle, 0, cb->hook);
112 return 0;
115 int nebmodule_deinit(int flags, int reason)
117 int i;
118 linfo("Unloading %s", SELF);
120 /* flush junk to disk */
121 sync();
123 for (i = 0; i < ARRAY_SIZE(callback_table); i++) {
124 struct callback_struct *cb = &callback_table[i];
125 neb_deregister_callback(cb->type, cb->hook);
128 return sql_close();