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)
25 #define HOST_STATES_HASH_BUCKETS 4096
26 #define SERVICE_STATES_HASH_BUCKETS (HOST_STATES_HASH_BUCKETS * 4)
29 * The hooks are called from broker.c in Nagios.
31 static hash_table
*host_states
;
32 static hash_table
*svc_states
;
36 host_states
= hash_init(HOST_STATES_HASH_BUCKETS
);
40 svc_states
= hash_init(SERVICE_STATES_HASH_BUCKETS
);
47 return prime_initial_states(host_states
, svc_states
);
50 static inline int has_state_change(int *old
, int state
, int type
)
53 * A state change is considered to consist of a change
54 * to either state_type or state, so we OR the two
55 * together to form a complete state. This will make
56 * the module log as follows:
57 * service foo;poo is HARD OK initially
58 * service foo;poo goes to SOFT WARN, attempt 1 (logged)
59 * service foo;poo goes to SOFT WARN, attempt 2 (not logged)
60 * service foo;poo goes to HARD WARN (logged)
62 state
= CAT_STATE(state
, type
);
71 static inline int host_has_new_state(nebstruct_host_check_data
*ds
)
75 old_state
= hash_find(host_states
, ds
->host_name
);
79 state
= malloc(sizeof(*state
));
80 *state
= CAT_STATE(ds
->state
, ds
->state_type
);
81 hash_add(host_states
, ds
->host_name
, state
);
85 return has_state_change(old_state
, ds
->state
, ds
->state_type
);
88 static inline int service_has_new_state(nebstruct_service_check_data
*ds
)
92 old_state
= hash_find2(svc_states
, ds
->host_name
, ds
->service_description
);
96 state
= malloc(sizeof(*state
));
97 *state
= CAT_STATE(ds
->state
, ds
->state_type
);
98 hash_add2(svc_states
, ds
->host_name
, ds
->service_description
, state
);
102 return has_state_change(old_state
, ds
->state
, ds
->state_type
);
105 int hook_host_result(int cb
, void *data
)
107 nebstruct_host_check_data
*ds
= (nebstruct_host_check_data
*)data
;
109 /* ignore unprocessed and passive checks */
110 if (ds
->type
!= NEBTYPE_HOSTCHECK_PROCESSED
||
111 cb
!= NEBCALLBACK_HOST_CHECK_DATA
)
116 linfo("Check result processed for host '%s'", ds
->host_name
);
118 if (!host_has_new_state(ds
)) {
119 linfo("state not changed for host '%s'", ds
->host_name
);
123 RQ("INSERT INTO %s ("
124 "timestamp, event_type, host_name, state, "
125 "hard, retry, output"
126 ") VALUES(%lu, %d, '%s', %d, %d, %d, '%s')",
128 ds
->timestamp
.tv_sec
, ds
->type
, esc(ds
->host_name
), ds
->state
,
129 ds
->state_type
== HARD_STATE
, ds
->current_attempt
,
133 int hook_service_result(int cb
, void *data
)
135 nebstruct_service_check_data
*ds
= (nebstruct_service_check_data
*)data
;
137 /* ignore unprocessed and passive checks */
138 if (ds
->type
!= NEBTYPE_SERVICECHECK_PROCESSED
139 || cb
!= NEBCALLBACK_SERVICE_CHECK_DATA
)
144 linfo("Check result processed for service '%s' on host '%s'",
145 ds
->service_description
, ds
->host_name
);
147 if (!service_has_new_state(ds
)) {
148 linfo("state not changed for service '%s' on host '%s'",
149 ds
->service_description
, ds
->host_name
);
153 RQ("INSERT INTO %s ("
154 "timestamp, event_type, host_name, service_description, state, "
155 "hard, retry, output) "
156 "VALUES(%lu, %d, '%s', '%s', '%d', '%d', '%d', '%s')",
158 ds
->timestamp
.tv_sec
, ds
->type
, esc(ds
->host_name
),
159 esc(ds
->service_description
), ds
->state
,
160 ds
->state_type
== HARD_STATE
, ds
->current_attempt
,
164 int hook_downtime(int cb
, void *data
)
166 nebstruct_downtime_data
*ds
= (nebstruct_downtime_data
*)data
;
170 case NEBTYPE_DOWNTIME_START
:
171 case NEBTYPE_DOWNTIME_STOP
:
177 if (ds
->service_description
) {
178 depth
= dt_depth_svc(ds
->type
, ds
->host_name
, ds
->service_description
);
180 linfo("Inserting service downtime entry");
182 "(timestamp, event_type, host_name,"
183 "service_description, downtime_depth) "
184 "VALUES(%lu, %d, '%s', '%s', %d)",
186 ds
->timestamp
.tv_sec
, ds
->type
, esc(ds
->host_name
),
187 esc(ds
->service_description
), depth
);
190 depth
= dt_depth_host(ds
->type
, ds
->host_name
);
191 linfo("Inserting host downtime_data");
193 "(timestamp, event_type, host_name, downtime_depth)"
194 "VALUES(%lu, %d, '%s', %d)",
196 ds
->timestamp
.tv_sec
, ds
->type
, esc(ds
->host_name
), depth
);
199 int hook_process_data(int cb
, void *data
)
201 nebstruct_process_data
*ds
= (nebstruct_process_data
*)data
;
204 case NEBTYPE_PROCESS_START
:
205 case NEBTYPE_PROCESS_SHUTDOWN
:
207 case NEBTYPE_PROCESS_RESTART
:
208 ds
->type
= NEBTYPE_PROCESS_SHUTDOWN
;
214 linfo("Logging Monitor process %s event",
215 ds
->type
== NEBTYPE_PROCESS_START
? "START" : "STOP");
217 RQ("INSERT INTO %s(timestamp, event_type) "
219 sql_table_name(), ds
->timestamp
.tv_sec
, ds
->type
);