Break out {host,service}_has_new_state() to their own file
[nagios-reports-module.git] / hooks.c
blobfb9419b1a6ec7ccc45deff3bb02a4068afc70bf9
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)) {
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)) {
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 default:
99 return 0;
102 if (ds->service_description) {
103 depth = dt_depth_svc(ds->type, ds->host_name, ds->service_description);
105 linfo("Inserting service downtime entry");
106 RQ("INSERT INTO %s"
107 "(timestamp, event_type, host_name,"
108 "service_description, downtime_depth) "
109 "VALUES(%lu, %d, '%s', '%s', %d)",
110 sql_table_name(),
111 ds->timestamp.tv_sec, ds->type, esc(ds->host_name),
112 esc(ds->service_description), depth);
115 depth = dt_depth_host(ds->type, ds->host_name);
116 linfo("Inserting host downtime_data");
117 RQ("INSERT INTO %s"
118 "(timestamp, event_type, host_name, downtime_depth)"
119 "VALUES(%lu, %d, '%s', %d)",
120 sql_table_name(),
121 ds->timestamp.tv_sec, ds->type, esc(ds->host_name), depth);
124 int hook_process_data(int cb, void *data)
126 nebstruct_process_data *ds = (nebstruct_process_data *)data;
128 switch(ds->type) {
129 case NEBTYPE_PROCESS_START:
130 case NEBTYPE_PROCESS_SHUTDOWN:
131 break;
132 case NEBTYPE_PROCESS_RESTART:
133 ds->type = NEBTYPE_PROCESS_SHUTDOWN;
134 break;
135 default:
136 return 0;
139 linfo("Logging Monitor process %s event",
140 ds->type == NEBTYPE_PROCESS_START ? "START" : "STOP");
142 RQ("INSERT INTO %s(timestamp, event_type) "
143 "VALUES(%lu, %d)",
144 sql_table_name(), ds->timestamp.tv_sec, ds->type);