2 * Copyright(C) 2005,2007 op5 AB
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
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)
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
)
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
);
49 "timestamp, event_type, host_name, state, "
51 ") VALUES(%lu, %d, '%s', %d, %d, %d, '%s')",
53 ds
->timestamp
.tv_sec
, ds
->type
, esc(ds
->host_name
), ds
->state
,
54 ds
->state_type
== HARD_STATE
, ds
->current_attempt
,
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
)
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
);
79 "timestamp, event_type, host_name, service_description, state, "
80 "hard, retry, output) "
81 "VALUES(%lu, %d, '%s', '%s', '%d', '%d', '%d', '%s')",
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
,
89 int hook_downtime(int cb
, void *data
)
91 nebstruct_downtime_data
*ds
= (nebstruct_downtime_data
*)data
;
95 case NEBTYPE_DOWNTIME_START
:
96 case NEBTYPE_DOWNTIME_STOP
:
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
))
106 ds
->type
= NEBTYPE_DOWNTIME_STOP
;
112 if (ds
->service_description
) {
113 depth
= dt_depth_svc(ds
->type
, ds
->host_name
, ds
->service_description
);
115 linfo("Inserting service downtime entry");
117 "(timestamp, event_type, host_name,"
118 "service_description, downtime_depth) "
119 "VALUES(%lu, %d, '%s', '%s', %d)",
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");
128 "(timestamp, event_type, host_name, downtime_depth)"
129 "VALUES(%lu, %d, '%s', %d)",
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
;
139 case NEBTYPE_PROCESS_START
:
140 case NEBTYPE_PROCESS_SHUTDOWN
:
142 case NEBTYPE_PROCESS_RESTART
:
143 ds
->type
= NEBTYPE_PROCESS_SHUTDOWN
;
149 linfo("Logging Monitor process %s event",
150 ds
->type
== NEBTYPE_PROCESS_START
? "START" : "STOP");
152 RQ("INSERT INTO %s(timestamp, event_type) "
154 sql_table_name(), ds
->timestamp
.tv_sec
, ds
->type
);