auth lib: Minor fixes
[nagios-reports-module.git] / module.c
blob53b1c172d918402951be2052c59797c3069a6e01
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;
71 nebstruct_process_data proc_start;
73 neb_handle = handle;
75 linfo("Loading %s", SELF);
77 if (read_config(arg) < 0)
78 return -1;
80 if (sql_init() < 0)
81 return -1;
83 if (hook_init() < 0)
84 return -1;
86 proc_start.type = NEBTYPE_PROCESS_START;
87 proc_start.timestamp.tv_sec = time(NULL);
88 if (hook_process_data(NEBCALLBACK_PROCESS_DATA, &proc_start) < 0)
89 return -1;
91 /* make sure we can catch whatever we want */
92 event_broker_options = BROKER_EVERYTHING;
94 ldebug("Forcing coredumps");
95 daemon_dumps_core = 1;
96 home = getenv("HOME");
97 if (!home)
98 home = "/tmp";
100 signal(SIGSEGV, SIG_DFL);
103 * if flags == -1, we're running from nebtest, so make sure
104 * coredumps end up in cwd. This *shouldn't* affect anything
105 * (famous last words, no?)
107 if (flags != -1)
108 chdir(home);
109 getcwd(cwd, sizeof(cwd));
110 linfo("Coredumps end up in %s", cwd);
112 for (i = 0; i < ARRAY_SIZE(callback_table); i++) {
113 struct callback_struct *cb = &callback_table[i];
115 neb_register_callback(cb->type, neb_handle, 0, cb->hook);
118 return 0;
121 int nebmodule_deinit(int flags, int reason)
123 int i;
124 linfo("Unloading %s", SELF);
126 /* flush junk to disk */
127 sync();
129 log_deinit();
131 for (i = 0; i < ARRAY_SIZE(callback_table); i++) {
132 struct callback_struct *cb = &callback_table[i];
133 neb_deregister_callback(cb->type, cb->hook);
136 return sql_close();