Makefile: Add target to run sql-dependant tests
[nagios-reports-module.git] / hooks.c
blobb4b7694ee2d81370209eb07ab0999d4297976c4e
1 /*
2 * Copyright(C) 2005,2007 op5 AB
3 * All rights reserved.
5 * See LICENSE.GPL for license details
7 * This code is taken from the mrm project and modified to suit
8 * the ndbneb database logger
9 */
11 #include "module.h"
12 #include "sql.h"
13 #include "hooks.h"
14 #include "utils.h"
15 #include "state.h"
16 #include "nagios/broker.h"
18 * sql_query() arguments are usually both numerous and extremely long,
19 * and the idiom is common enough that we want to shorten the syntax
20 * as much as possible, so do that with this macro
22 #define RQ return sql_query
23 #define esc(s) sql_escape(s)
25 int hook_init(void)
27 return state_init();
30 int hook_host_result(int cb, void *data)
32 nebstruct_host_check_data *ds = (nebstruct_host_check_data *)data;
34 /* ignore unprocessed and passive checks */
35 if (ds->type != NEBTYPE_HOSTCHECK_PROCESSED ||
36 cb != NEBCALLBACK_HOST_CHECK_DATA)
38 return 0;
41 linfo("Check result processed for host '%s'", ds->host_name);
43 if (!host_has_new_state(ds->host_name, ds->state, ds->state_type)) {
44 linfo("state not changed for host '%s'", ds->host_name);
45 return 0;
48 RQ("INSERT INTO %s ("
49 "timestamp, event_type, host_name, state, "
50 "hard, retry, output"
51 ") VALUES(%lu, %d, '%s', %d, %d, %d, '%s')",
52 sql_table_name(),
53 ds->timestamp.tv_sec, ds->type, esc(ds->host_name), ds->state,
54 ds->state_type == HARD_STATE, ds->current_attempt,
55 esc(ds->output));
58 int hook_service_result(int cb, void *data)
60 nebstruct_service_check_data *ds = (nebstruct_service_check_data *)data;
62 /* ignore unprocessed and passive checks */
63 if (ds->type != NEBTYPE_SERVICECHECK_PROCESSED
64 || cb != NEBCALLBACK_SERVICE_CHECK_DATA)
66 return 0;
69 linfo("Check result processed for service '%s' on host '%s'",
70 ds->service_description, ds->host_name);
72 if (!service_has_new_state(ds->host_name, ds->service_description, ds->state, ds->state_type)) {
73 linfo("state not changed for service '%s' on host '%s'",
74 ds->service_description, ds->host_name);
75 return 0;
78 RQ("INSERT INTO %s ("
79 "timestamp, event_type, host_name, service_description, state, "
80 "hard, retry, output) "
81 "VALUES(%lu, %d, '%s', '%s', '%d', '%d', '%d', '%s')",
82 sql_table_name(),
83 ds->timestamp.tv_sec, ds->type, esc(ds->host_name),
84 esc(ds->service_description), ds->state,
85 ds->state_type == HARD_STATE, ds->current_attempt,
86 esc(ds->output));
89 int hook_downtime(int cb, void *data)
91 nebstruct_downtime_data *ds = (nebstruct_downtime_data *)data;
92 int depth;
94 switch (ds->type) {
95 case NEBTYPE_DOWNTIME_START:
96 case NEBTYPE_DOWNTIME_STOP:
97 break;
98 case NEBTYPE_DOWNTIME_DELETE:
100 * if we're deleting a downtime that hasn't started yet, nothing
101 * should be added to the database. Otherwise, transform it to a
102 * NEBTYPE_DOWNTIME_STOP event to mark the downtime as stopped.
104 if (ds->start_time > time(NULL))
105 return 0;
106 ds->type = NEBTYPE_DOWNTIME_STOP;
107 break;
108 default:
109 return 0;
112 if (ds->service_description) {
113 depth = dt_depth_svc(ds->type, ds->host_name, ds->service_description);
115 linfo("Inserting service downtime entry");
116 RQ("INSERT INTO %s"
117 "(timestamp, event_type, host_name,"
118 "service_description, downtime_depth) "
119 "VALUES(%lu, %d, '%s', '%s', %d)",
120 sql_table_name(),
121 ds->timestamp.tv_sec, ds->type, esc(ds->host_name),
122 esc(ds->service_description), depth);
125 depth = dt_depth_host(ds->type, ds->host_name);
126 linfo("Inserting host downtime_data");
127 RQ("INSERT INTO %s"
128 "(timestamp, event_type, host_name, downtime_depth)"
129 "VALUES(%lu, %d, '%s', %d)",
130 sql_table_name(),
131 ds->timestamp.tv_sec, ds->type, esc(ds->host_name), depth);
134 int hook_process_data(int cb, void *data)
136 nebstruct_process_data *ds = (nebstruct_process_data *)data;
138 switch(ds->type) {
139 case NEBTYPE_PROCESS_START:
140 case NEBTYPE_PROCESS_SHUTDOWN:
141 break;
142 case NEBTYPE_PROCESS_RESTART:
143 ds->type = NEBTYPE_PROCESS_SHUTDOWN;
144 break;
145 default:
146 return 0;
149 linfo("Logging Monitor process %s event",
150 ds->type == NEBTYPE_PROCESS_START ? "START" : "STOP");
152 RQ("INSERT INTO %s(timestamp, event_type) "
153 "VALUES(%lu, %d)",
154 sql_table_name(), ds->timestamp.tv_sec, ds->type);