-
Notifications
You must be signed in to change notification settings - Fork 7
/
nagios-zmq.c
368 lines (288 loc) · 12.7 KB
/
nagios-zmq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <json/json.h>
#include <pthread.h>
#include <uuid/uuid.h>
#include "include/zhelpers.h"
#include "include/nagios/objects.h"
#include "include/nagios/nagios.h"
#include "include/nagios/nebstructs.h"
#include "include/nagios/broker.h"
#include "include/nagios/nebmodules.h"
#include "include/nagios/nebcallbacks.h"
#define DEFAULT_ZMQ_IN_PORT 5555
#define DEFAULT_ZMQ_OUT_PORT 6666
#define LG_INFO 262144
#define LG_WARN LOG_INFO
#define LG_ERR LOG_INFO
#define LG_CRIT LOG_INFO
#define LG_DEBUG LOG_INFO
#define LG_ALERT LOG_INFO
#define MAX_MESSAGE 1024*1024
void *g_nagios_zmq_handle = NULL;
void *g_nagios_zmq_context;
void *g_nagios_zmq_publisher;
int g_nagios_zmq_in_port = DEFAULT_ZMQ_IN_PORT;
int g_nagios_zmq_out_port = DEFAULT_ZMQ_OUT_PORT;
pthread_t g_nagios_zmq_forwarder_thread;
pthread_t g_nagios_zmq_publisher_thread;
NEB_API_VERSION(CURRENT_NEB_API_VERSION)
/*
It's important to namespace functions and variables, otherwise they might conflict with other NEB's functions
Information about the different NEB structs can be found at:
- http://nagios.svn.sourceforge.net/viewvc/nagios/nagioscore/trunk/include/nebstructs.h?&view=markup
The different callbacks can be found at:
- http://nagios.svn.sourceforge.net/viewvc/nagios/nagioscore/trunk/include/nebcallbacks.h?&view=markup
*/
void nagios_zmq_logger(int priority, const char *loginfo, ...)
{
char buffer[8192];
snprintf(buffer, 20, "message queue: ");
va_list ap;
va_start(ap, loginfo);
vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), loginfo, ap);
va_end(ap);
write_to_all_logs(buffer, priority);
}
void nagios_zmq_parse_arguments(const char *args) {
char arguments[1024];
char *arg_term;
// no arguments
if(!args) return;
strncpy(arguments, args, 1024);
arg_term = strtok(arguments, " =");
while(arg_term != NULL) {
char *key, *value;
key = arg_term;
arg_term = strtok(NULL, " =");
value = arg_term;
if(!strncmp(key, "inport", 4)) {
g_nagios_zmq_in_port = atoi(value);
}
if(!strncmp(key, "outport", 4)) {
g_nagios_zmq_out_port = atoi(value);
}
arg_term = strtok(NULL, " =");
}
}
void *nagios_zmq_publisher_start() {
nagios_zmq_logger(LG_INFO, "start zmq publisher.");
// init socket
g_nagios_zmq_publisher = zmq_socket(g_nagios_zmq_context, ZMQ_PUB);
zmq_connect(g_nagios_zmq_publisher, "tcp://localhost:5555");
}
void *nagios_zmq_forwarder_start() {
nagios_zmq_logger(LG_INFO, "start zmq forwarder.");
// Socket facing clients
void *incoming = zmq_socket(g_nagios_zmq_context, ZMQ_SUB);
zmq_bind(incoming, "tcp://*:5555");
zmq_setsockopt(incoming, ZMQ_SUBSCRIBE, "", 0);
// Socket facing services
void *outgoing = zmq_socket(g_nagios_zmq_context, ZMQ_PUB);
zmq_bind(outgoing, "tcp://*:6666");
// Start built-in device
zmq_device(ZMQ_FORWARDER, incoming, outgoing);
// We never get here…
zmq_close(incoming);
zmq_close(outgoing);
zmq_term(g_nagios_zmq_context);
}
void nagios_zmq_start_threads() {
// init zmq context
g_nagios_zmq_context = zmq_init(1);
// start forwarder thread
pthread_create(&g_nagios_zmq_forwarder_thread, 0, nagios_zmq_forwarder_start, (void *)0);
pthread_detach(g_nagios_zmq_forwarder_thread);
// start publisher thread
pthread_create(&g_nagios_zmq_publisher_thread, 0, nagios_zmq_publisher_start, (void *)0);
pthread_detach(g_nagios_zmq_publisher_thread);
}
json_object * json_add_pair(json_object *jobj, char *key, char *value) {
json_object *jstring = json_object_new_string(value);
json_object_object_add(jobj, key, jstring);
return jobj;
}
/* Generate an UUID string. */
char * nagios_zmq_create_uuid() {
char *string = malloc(37);
uuid_t uuid;
uuid_generate(uuid);
uuid_unparse(uuid, string);
return string;
}
int nagios_zmq_send_servicecheck(nebstruct_service_check_data *check_data) {
time_t ts = time(NULL);
char message_buffer[MAX_MESSAGE];
char cast_buffer[1024];
char *uuid = nagios_zmq_create_uuid();
json_object * jevent = json_object_new_object();
sprintf(cast_buffer, "%s", uuid);
json_add_pair(jevent, "id", cast_buffer);
free(uuid);
json_add_pair(jevent, "context", "SERVICECHECK");
json_add_pair(jevent, "source", "NAGIOS");
sprintf(cast_buffer, "%i", (int)ts);
json_add_pair(jevent, "timestamp", cast_buffer);
json_object * jobj = json_object_new_object();
sprintf(cast_buffer, "%i", check_data->current_attempt);
json_add_pair(jobj, "current_attempt", cast_buffer);
sprintf(cast_buffer, "%i", check_data->max_attempts);
json_add_pair(jobj, "max_attempts", cast_buffer);
sprintf(cast_buffer, "%i", check_data->state_type);
json_add_pair(jobj, "state_type", cast_buffer);
sprintf(cast_buffer, "%i", check_data->state);
json_add_pair(jobj, "state", cast_buffer);
sprintf(cast_buffer, "%ld", check_data->timestamp.tv_sec);
json_add_pair(jobj, "timestamp", cast_buffer);
sprintf(cast_buffer, "%f", check_data->execution_time);
json_add_pair(jobj, "execution_time", cast_buffer);
json_add_pair(jobj, "hostname", check_data->host_name);
json_add_pair(jobj, "service", check_data->service_description);
json_add_pair(jobj, "output", check_data->output);
if(check_data->perf_data)
json_add_pair(jobj, "performance", check_data->perf_data);
json_object_object_add(jevent, "payload", jobj);
nagios_zmq_logger(LG_INFO, "'%s'\n", json_object_to_json_string(jevent));
sprintf(message_buffer, "%s", json_object_to_json_string(jevent));
s_send(g_nagios_zmq_publisher, message_buffer);
json_object_put(jevent);
json_object_put(jobj);
return 0;
}
int nagios_zmq_send_hostcheck(nebstruct_host_check_data *check_data) {
time_t ts = time(NULL);
char message_buffer[MAX_MESSAGE];
char cast_buffer[1024];
char *uuid = nagios_zmq_create_uuid();
json_object * jevent = json_object_new_object();
sprintf(cast_buffer, "%s", uuid);
json_add_pair(jevent, "id", cast_buffer);
free(uuid);
json_add_pair(jevent, "context", "HOSTCHECK");
json_add_pair(jevent, "source", "NAGIOS");
sprintf(cast_buffer, "%i", (int)ts);
json_add_pair(jevent, "timestamp", cast_buffer);
json_object * jobj = json_object_new_object();
sprintf(cast_buffer, "%i", check_data->current_attempt);
json_add_pair(jobj, "current_attempt", cast_buffer);
sprintf(cast_buffer, "%i", check_data->max_attempts);
json_add_pair(jobj, "max_attempts", cast_buffer);
sprintf(cast_buffer, "%i", check_data->state_type);
json_add_pair(jobj, "state_type", cast_buffer);
sprintf(cast_buffer, "%i", check_data->state);
json_add_pair(jobj, "state", cast_buffer);
sprintf(cast_buffer, "%ld", check_data->timestamp.tv_sec);
json_add_pair(jobj, "timestamp", cast_buffer);
sprintf(cast_buffer, "%f", check_data->execution_time);
json_add_pair(jobj, "execution_time", cast_buffer);
json_add_pair(jobj, "hostname", check_data->host_name);
json_add_pair(jobj, "output", check_data->output);
if(check_data->perf_data)
json_add_pair(jobj, "performance", check_data->perf_data);
json_object_object_add(jevent, "payload", jobj);
nagios_zmq_logger(LG_INFO, "'%s'\n", json_object_to_json_string(jevent));
sprintf(message_buffer, "%s", json_object_to_json_string(jevent));
s_send(g_nagios_zmq_publisher, message_buffer);
json_object_put(jevent);
json_object_put(jobj);
return 0;
}
int nagios_zmq_send_notification(nebstruct_notification_data *notification_data) {
time_t ts = time(NULL);
char message_buffer[MAX_MESSAGE];
char cast_buffer[1024];
char *uuid = nagios_zmq_create_uuid();
json_object * jevent = json_object_new_object();
sprintf(cast_buffer, "%s", uuid);
json_add_pair(jevent, "id", cast_buffer);
free(uuid);
json_add_pair(jevent, "context", "NOTIFICATION");
json_add_pair(jevent, "source", "NAGIOS");
sprintf(cast_buffer, "%i", (int)ts);
json_add_pair(jevent, "timestamp", cast_buffer);
json_object * jobj = json_object_new_object();
json_add_pair(jobj, "hostname", notification_data->host_name);
sprintf(cast_buffer, "%ld", notification_data->start_time.tv_sec);
json_add_pair(jobj, "start_time", cast_buffer);
sprintf(cast_buffer, "%ld", notification_data->end_time.tv_sec);
json_add_pair(jobj, "end_time", cast_buffer);
json_add_pair(jobj, "service_description", notification_data->service_description);
sprintf(cast_buffer, "%i", notification_data->reason_type);
json_add_pair(jobj, "reason_type", cast_buffer);
sprintf(cast_buffer, "%i", notification_data->state);
json_add_pair(jobj, "state", cast_buffer);
json_add_pair(jobj, "output", notification_data->output);
if (notification_data->ack_author != NULL) {
json_add_pair(jobj, "ack_author", notification_data->ack_author);
}
if (notification_data->ack_data != NULL) {
json_add_pair(jobj, "ack_data", notification_data->ack_data);
}
sprintf(cast_buffer, "%i", notification_data->escalated);
json_add_pair(jobj, "escalated", cast_buffer);
sprintf(cast_buffer, "%i", notification_data->contacts_notified);
json_add_pair(jobj, "contacts_notified", cast_buffer);
json_object_object_add(jevent, "payload", jobj);
nagios_zmq_logger(LG_INFO, "'%s'\n", json_object_to_json_string(jevent));
sprintf(message_buffer, "%s", json_object_to_json_string(jevent));
s_send(g_nagios_zmq_publisher, message_buffer);
json_object_put(jevent);
json_object_put(jobj);
return 0;
}
int nagios_zmq_broker_check(int event_type, void *data) {
if (event_type == NEBCALLBACK_SERVICE_CHECK_DATA) {
nebstruct_service_check_data *c = (nebstruct_service_check_data *)data;
if (c->type == NEBTYPE_SERVICECHECK_PROCESSED) {
nagios_zmq_send_servicecheck(c);
}
}
else if (event_type == NEBCALLBACK_HOST_CHECK_DATA) {
nebstruct_host_check_data *c = (nebstruct_host_check_data *)data;
if (c->type == NEBTYPE_HOSTCHECK_PROCESSED) {
nagios_zmq_send_hostcheck(c);
}
}
return 0;
}
int nagios_zmq_broker_notification(int event_type __attribute__ ((__unused__)), void *data) {
nebstruct_notification_data *n = (nebstruct_notification_data *)data;
nagios_zmq_send_notification(n);
}
int nagios_zmq_broker_state(int event_type __attribute__ ((__unused__)), void *data __attribute__ ((__unused__))) {
return 0;
}
int nagios_zmq_broker_process(int event_type __attribute__ ((__unused__)), void *data) {
struct nebstruct_process_struct *ps = (struct nebstruct_process_struct *)data;
if (ps->type == NEBTYPE_PROCESS_EVENTLOOPSTART) {
nagios_zmq_start_threads();
}
return 0;
}
/* this function gets called when the module is loaded by the event broker */
int nebmodule_init(int flags __attribute__ ((__unused__)), char *args, void *handle) {
g_nagios_zmq_handle = handle;
nagios_zmq_logger(LG_INFO, "nagios-zmq by Marius Sturm");
nagios_zmq_parse_arguments(args);
neb_register_callback(NEBCALLBACK_STATE_CHANGE_DATA, g_nagios_zmq_handle, 0, nagios_zmq_broker_state);
neb_register_callback(NEBCALLBACK_SERVICE_CHECK_DATA, g_nagios_zmq_handle, 0, nagios_zmq_broker_check);
neb_register_callback(NEBCALLBACK_HOST_CHECK_DATA, g_nagios_zmq_handle, 0, nagios_zmq_broker_check);
neb_register_callback(NEBCALLBACK_NOTIFICATION_DATA, g_nagios_zmq_handle, 0, nagios_zmq_broker_notification);
// used for starting threads
neb_register_callback(NEBCALLBACK_PROCESS_DATA, g_nagios_zmq_handle, 0, nagios_zmq_broker_process);
nagios_zmq_logger(LG_INFO, "successfully finished initialization");
}
int nebmodule_deinit(int flags __attribute__ ((__unused__)), int reason __attribute__ ((__unused__))) {
nagios_zmq_logger(LG_INFO, "deinitializing");
neb_deregister_callback(NEBCALLBACK_STATE_CHANGE_DATA, nagios_zmq_broker_state);
neb_deregister_callback(NEBCALLBACK_SERVICE_CHECK_DATA, nagios_zmq_broker_check);
neb_deregister_callback(NEBCALLBACK_HOST_CHECK_DATA, nagios_zmq_broker_check);
neb_deregister_callback(NEBCALLBACK_NOTIFICATION_DATA, nagios_zmq_broker_notification);
neb_deregister_callback(NEBCALLBACK_PROCESS_DATA, nagios_zmq_broker_process);
//deinit zmq
zmq_close(g_nagios_zmq_publisher);
zmq_term(g_nagios_zmq_context);
return 0;
}