import: Only read last time from sql if --incremental lacks timestamp
[nagios-reports-module.git] / module.c
blob873b7cfbf46fa13ff3120cf0a59a3b04d3fc624e
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 sql_init();
81 if (hook_init() < 0)
82 return -1;
84 proc_start.type = NEBTYPE_PROCESS_START;
85 proc_start.timestamp.tv_sec = time(NULL);
86 if (hook_process_data(NEBCALLBACK_PROCESS_DATA, &proc_start) < 0)
87 return -1;
89 /* make sure we can catch whatever we want */
90 event_broker_options = BROKER_EVERYTHING;
92 ldebug("Forcing coredumps");
93 daemon_dumps_core = 1;
94 home = getenv("HOME");
95 if (!home)
96 home = "/tmp";
98 signal(SIGSEGV, SIG_DFL);
101 * if flags == -1, we're running from nebtest, so make sure
102 * coredumps end up in cwd. This *shouldn't* affect anything
103 * (famous last words, no?)
105 if (flags != -1)
106 chdir(home);
107 getcwd(cwd, sizeof(cwd));
108 linfo("Coredumps end up in %s", cwd);
110 for (i = 0; i < ARRAY_SIZE(callback_table); i++) {
111 struct callback_struct *cb = &callback_table[i];
113 neb_register_callback(cb->type, neb_handle, 0, cb->hook);
116 return 0;
119 int nebmodule_deinit(int flags, int reason)
121 int i;
122 linfo("Unloading %s", SELF);
124 /* flush junk to disk */
125 sync();
127 log_deinit();
129 for (i = 0; i < ARRAY_SIZE(callback_table); i++) {
130 struct callback_struct *cb = &callback_table[i];
131 neb_deregister_callback(cb->type, cb->hook);
134 return sql_close();