-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.c
More file actions
1110 lines (934 loc) · 29.1 KB
/
Copy pathmodule.c
File metadata and controls
1110 lines (934 loc) · 29.1 KB
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <nagios/lib/iobroker.h>
#include "module.h"
#include <nagios/nagios.h>
#include <nagios/objects.h>
#include <nagios/statusdata.h>
#include <nagios/macros.h>
#include <nagios/perfdata.h>
#include <nagios/comments.h>
#include <nagios/common.h>
#include <nagios/downtime.h>
merlin_node **host_check_node = NULL;
merlin_node **service_check_node = NULL;
merlin_node untracked_checks_node = {
.name = "untracked checks",
.type = MODE_INTERNAL,
.host_checks = 0,
.service_checks = 0,
};
extern iobroker_set *nagios_iobs;
time_t merlin_should_send_paths = 1;
/*
* nagios functions not included in almost-but-not-nearly-public
* functions. We're probably not meant to call them, but being a
* member of the Nagios core team has its benefits. Mwhahahahaha
*/
extern int xodtemplate_grab_config_info(char *main_config_file);
extern comment *comment_list;
/** code start **/
extern hostgroup *hostgroup_list;
static int mrm_reap_interval = 2;
static int merlin_sendpath_interval = MERLIN_SENDPATH_INTERVAL;
static int db_track_current = 0;
int merlin_net_event = 0;
/*
* user-defined filters, used as or-gate. Defaults to
* 'handle everything'. This only affects what events
* we register callbacks for. Received events will
* still be parsed in full.
* It's calculated thus:
* event_mask = handle_events & (~ignore_events);
* See grok_module_compound() for further details
*/
static uint32_t event_mask;
void set_host_check_node(merlin_node *node, host *h)
{
merlin_node *old;
old = host_check_node[h->id];
if(old == node)
return;
if (!old) {
old = &untracked_checks_node;
}
ldebug("Migrating hostcheck '%s' (id=%u) from %s '%s' (p-id=%u) to %s '%s' (p-id=%u)",
h->name, h->id,
node_type(old), old->name, old->peer_id,
node_type(node), node->name, node->peer_id);
old->host_checks--;
node->host_checks++;
host_check_node[h->id] = node;
}
void set_service_check_node(merlin_node *node, service *s)
{
merlin_node *old;
old = service_check_node[s->id];
if(old == node)
return;
if (!old) {
old = &untracked_checks_node;
}
ldebug("Migrating servicecheck '%s;%s' (id=%u) from %s '%s' (p-id=%u) to %s '%s (p-id=%u)",
s->host_name, s->description, s->id,
node_type(old), old->name, old->peer_id,
node_type(node), node->name, node->peer_id);
old->service_checks--;
node->service_checks++;
service_check_node[s->id] = node;
}
/*
* handle_{host,service}_result() is basically identical to
* handle_{host,service}_status(), with the added exception
* that the check result events also cause performance data
* to be handled by Nagios, so they're handled by the same
* routines.
*
* In essence, it would probably be enough to just send the
* check result events and ignore the rest
*/
static int handle_host_status(merlin_node *node, merlin_header *hdr, void *buf)
{
host *obj;
merlin_host_status *st_obj = (merlin_host_status *)buf;
struct tmp_net2mod_data tmp;
obj = find_host(st_obj->name);
if (!obj) {
lerr("Host '%s' not found. Ignoring %s event",
st_obj->name, callback_name(hdr->type));
return -1;
}
/* discard check results that are older than our latest */
if(obj->last_check > st_obj->state.last_check)
return 0;
NET2MOD_STATE_VARS(tmp, obj, st_obj->state);
if (hdr->type == NEBCALLBACK_HOST_CHECK_DATA) {
set_host_check_node(node, obj);
obj->check_source = node->source_name;
obj->has_been_checked = 1;
if (obj->perf_data) {
update_host_performance_data(obj);
}
}
return 0;
}
static int handle_service_status(merlin_node *node, merlin_header *hdr, void *buf)
{
service *obj;
merlin_service_status *st_obj = (merlin_service_status *)buf;
struct tmp_net2mod_data tmp;
obj = find_service(st_obj->host_name, st_obj->service_description);
if (!obj) {
lerr("Service '%s' on host '%s' not found. Ignoring %s event",
st_obj->service_description, st_obj->host_name,
callback_name(hdr->type));
return -1;
}
/* discard check results that are older than our latest */
if(obj->last_check > st_obj->state.last_check)
return 0;
NET2MOD_STATE_VARS(tmp, obj, st_obj->state);
if (hdr->type == NEBCALLBACK_SERVICE_CHECK_DATA) {
set_service_check_node(node, obj);
obj->check_source = node->source_name;
obj->has_been_checked = 1;
if (obj->perf_data) {
update_service_performance_data(obj);
}
}
return 0;
}
static int handle_external_command(merlin_node *node, void *buf)
{
nebstruct_external_command_data *ds = (nebstruct_external_command_data *)buf;
ldebug("EXTCMD: from %s: [%ld] %d;%s",
node->name, ds->entry_time, ds->command_type, ds->command_args);
switch (ds->command_type) {
case CMD_RESTART_PROCESS:
case CMD_SHUTDOWN_PROCESS:
/*
* These two are slightly dangerous, as they allow one
* compromised node to cause the shutdown of every
* node in the chain, so we simply ignore them here on
* the final receiving end.
*/
return 0;
case CMD_DEL_HOST_COMMENT:
case CMD_DEL_SVC_COMMENT:
/*
* these we block entirely to prevent deleting the wrong
* comment in case the comment_id's are not in sync between
* nodes. COMMENT_DELETE events and the info they contain
* are used instead.
*/
return 0;
}
process_external_command2(ds->command_type, ds->entry_time, ds->command_args);
return 1;
}
static int matching_comment(comment *cmnt, nebstruct_comment_data *ds)
{
/*
* hash collisions can cause comments from other objects
* (and other types of objects) to be listed here, so we
* must match on host_name and service_description as
* well and skip comments that don't match the type
*/
if (cmnt->comment_type == ds->comment_type &&
cmnt->entry_type == ds->entry_type &&
cmnt->source == ds->source &&
cmnt->expires == ds->expires &&
cmnt->expire_time == ds->expire_time &&
cmnt->entry_time == ds->entry_time &&
cmnt->persistent == ds->persistent &&
!strcmp(cmnt->author, ds->author_name) &&
!strcmp(cmnt->comment_data, ds->comment_data) &&
!strcmp(cmnt->host_name, ds->host_name) &&
(cmnt->service_description == ds->service_description ||
!strcmp(cmnt->service_description, ds->service_description)))
{
ldebug("CMNT: cmnt->host_name: %s; ds->host_name: %s",
cmnt->host_name, ds->host_name);
ldebug("CMNT: cmnt->author: %s; ds->author_name: %s",
cmnt->author, ds->author_name);
ldebug("CMNT: cmnt->comment_data: %s; ds->comment_data: %s",
cmnt->comment_data, ds->comment_data);
return 1;
}
return 0;
}
static int handle_comment_data(merlin_node *node, void *buf)
{
nebstruct_comment_data *ds = (nebstruct_comment_data *)buf;
unsigned long comment_id = 0;
if (!node) {
lerr("handle_comment_data() with NULL node? Impossible...");
return 0;
}
if (ds->type == NEBTYPE_COMMENT_DELETE) {
comment *cmnt, *next_cmnt;
if (ds->comment_type == HOST_COMMENT) {
cmnt = get_first_comment_by_host(ds->host_name);
for (; cmnt; cmnt = next_cmnt) {
next_cmnt = cmnt->nexthash;
if (matching_comment(cmnt, ds)) {
merlin_set_block_comment(ds);
delete_comment(cmnt->comment_type, cmnt->comment_id);
merlin_set_block_comment(NULL);
}
}
} else {
/* this is *really* expensive. Sort of wtf? */
for (cmnt = comment_list; cmnt; cmnt = next_cmnt) {
next_cmnt = cmnt->next;
if (matching_comment(cmnt, ds)) {
merlin_set_block_comment(ds);
delete_comment(cmnt->comment_type, cmnt->comment_id);
merlin_set_block_comment(NULL);
}
}
}
return 0;
}
/* we're adding a comment */
merlin_set_block_comment(ds);
add_new_comment(ds->comment_type, ds->entry_type,
ds->host_name, ds->service_description,
ds->entry_time, ds->author_name,
ds->comment_data, ds->persistent,
ds->source, ds->expires,
ds->expire_time, &comment_id);
merlin_set_block_comment(NULL);
return 0;
}
static int handle_downtime_data(merlin_node *node, void *buf)
{
nebstruct_downtime_data *ds = (nebstruct_downtime_data *)buf;
if (!node) {
lerr("handle_downtime_data() with NULL node");
return 0;
}
if (ds->type != NEBTYPE_DOWNTIME_DELETE && ds->type != NEBTYPE_DOWNTIME_STOP) {
lerr("forwarded downtime event is not a delete. not good.");
return 0;
}
/* the longest function name in the history of C programming... */
delete_downtime_by_hostname_service_description_start_time_comment
(ds->host_name, ds->service_description,
ds->start_time, ds->comment_data);
return 0;
}
#define otype_agnostic_flapping_handling(obj) \
do { \
if (!obj) \
return 0; \
obj->is_flapping = starting; \
if (!starting) { \
comment_id = obj->flapping_comment_id; \
} \
} while (0)
static int handle_flapping_data(merlin_node *node, void *buf)
{
nebstruct_flapping_data *ds = (nebstruct_flapping_data *)buf;
unsigned long comment_id = 0;
host *hst = NULL;
service *srv = NULL;
int starting, comment_type;
if (!node) {
lerr("handle_flapping_data() with NULL node? Weird stuff");
return 0;
}
starting = ds->type == NEBTYPE_FLAPPING_START;
if (ds->flapping_type == SERVICE_FLAPPING) {
srv = find_service(ds->host_name, ds->service_description);
otype_agnostic_flapping_handling(srv);
comment_type = SERVICE_COMMENT;
} else {
hst = find_host(ds->host_name);
otype_agnostic_flapping_handling(hst);
comment_type = HOST_COMMENT;
}
if (!starting && comment_id) {
delete_comment(comment_type, comment_id);
}
return 1;
}
/* events that require status updates return 1, others return 0 */
int handle_ipc_event(merlin_node *node, merlin_event *pkt)
{
int ret;
if (!pkt) {
lerr("MM: pkt is NULL in handle_ipc_event()");
return 0;
}
if (!pkt->body) {
lerr("MM: pkt->body is NULL in handle_ipc_event()");
return 0;
}
if (node) {
/*
* this node is obviously connected, so mark it as such,
* but warn about nodes with empty info that's sending
* us data.
*/
node_set_state(node, STATE_CONNECTED, "Data received");
if (!node->info.byte_order) {
lwarn("STATE: %s is sending event data but hasn't sent %s",
node->name, ctrl_name(CTRL_ACTIVE));
/* marker to prevent logspamming */
node->info.byte_order = -1;
}
node->stats.events.read++;
node->stats.bytes.read += packet_size(pkt);
node_log_event_count(node, 0);
}
/*
ldebug("Inbound %s event from %s. len %d, type %d",
callback_name(pkt->hdr.type),
node ? node->name : "local Merlin daemon",
pkt->hdr.len, *pkt->body);
*/
/* restore the pointers so the various handlers won't have to */
if (merlin_decode_event(node, pkt)) {
return 0;
}
/*
* check results and status updates are handled the same,
* with the exception that checkresults also cause performance
* data to be handled.
*/
merlin_net_event = 1;
switch (pkt->hdr.type) {
case NEBCALLBACK_HOST_CHECK_DATA:
case NEBCALLBACK_HOST_STATUS_DATA:
ret = handle_host_status(node, &pkt->hdr, pkt->body);
break;
case NEBCALLBACK_SERVICE_CHECK_DATA:
case NEBCALLBACK_SERVICE_STATUS_DATA:
ret = handle_service_status(node, &pkt->hdr, pkt->body);
break;
case NEBCALLBACK_EXTERNAL_COMMAND_DATA:
ret = handle_external_command(node, pkt->body);
break;
case NEBCALLBACK_COMMENT_DATA:
ret = handle_comment_data(node, pkt->body);
break;
case NEBCALLBACK_DOWNTIME_DATA:
ret = handle_downtime_data(node, pkt->body);
break;
case NEBCALLBACK_FLAPPING_DATA:
ret = handle_flapping_data(node, pkt->body);
break;
default:
lwarn("Ignoring unrecognized/unhandled callback type: %d (%s)",
pkt->hdr.type, callback_name(pkt->hdr.type));
}
merlin_net_event = 0;
return ret;
}
static int ipc_reaper(int sd, int events, void *arg)
{
merlin_node *source = (merlin_node *)arg;
int recv_result;
merlin_event *pkt;
struct timeval tv;
if ((recv_result = node_recv(source)) <= 0) {
return 1;
}
/* needed for latency and action time setting */
gettimeofday(&tv, NULL);
/* and then just loop over the received packets */
while ((pkt = node_get_event(source))) {
merlin_node *node = node_by_id(pkt->hdr.selection);
if (node) {
int type = pkt->hdr.type == CTRL_PACKET ? NEBCALLBACK_NUMITEMS : pkt->hdr.type;
node->latency = tv_delta_msec(&pkt->hdr.sent, &tv);
if (node->latency < 0) {
if (!(node->warn_flags & NODE_WARN_CLOCK))
lwarn("Warning: Clock skew of ~%lu seconds detected to %s",
tv.tv_sec - pkt->hdr.sent.tv_sec, node->name);
node->warn_flags |= NODE_WARN_CLOCK;
}
node->last_action = node->last_recv = tv.tv_sec;
node->stats.cb_count[type].in++;
ldebug("Received type %d event from %s node %s",
pkt->hdr.type, node_type(node), node->name);
}
/* control packets are handled separately */
if (pkt->hdr.type == CTRL_PACKET) {
handle_control(node, pkt);
} else {
handle_ipc_event(node, pkt);
}
}
return 0;
}
dkhash_table *host_hash_table;
node_selection *node_selection_by_hostname(const char *name)
{
return dkhash_get(host_hash_table, name, NULL);
}
static void setup_host_hash_tables(void)
{
hostgroup *hg;
int i, nsel;
int *num_ents = NULL;
nsel = get_num_selections();
/*
* only bother if we've got hostgroups, pollers and selections.
* Otherwise we'll just be wasting perfectly good memory
* for no good reason
*/
if (!hostgroup_list || !num_pollers || !nsel)
return;
linfo("Creating hash tables");
host_hash_table = dkhash_create(num_objects.hosts * 1.3);
if (!host_hash_table) {
lerr("Failed to initialize hash tables: Out of memory");
exit(1);
}
num_ents = calloc(nsel, sizeof(int));
/*
* we must loop each hostgroup once, or we'll log a lot of
* spurious warnings that aren't exactly accurate
*/
for (hg = hostgroup_list; hg; hg = hg->next) {
node_selection *sel = node_selection_by_name(hg->group_name);
hostsmember *m;
if (!sel)
continue;
for (m = hg->members; m; m = m->next) {
node_selection *cur = node_selection_by_hostname(m->host_name);
/*
* this should never happen, but if it does
* we just ignore it and move on
*/
if (cur == sel)
continue;
if (cur) {
lwarn("'%s' is checked by selection '%s', so can't add to selection '%s'",
m->host_name, cur->name, sel->name);
continue;
}
num_ents[sel->id]++;
dkhash_insert(host_hash_table, m->host_name, NULL, sel);
}
}
for (i = 0; i < nsel; i++) {
if (!num_ents[i])
lwarn("'%s' is a selection without hosts. Are you sure you want this?",
get_sel_name(i));
}
free(num_ents);
}
static int parse_event_filter(const char *orig_str, uint32_t *evt_mask)
{
uint32_t mask = 0;
char *base_str, *str, *comma;
if (!orig_str || !*orig_str)
return -1;
/*
* initializing 'mask' to the result storage means we
* can let users supply the variable more times than
* one and get an appending result, but that could
* quite easily be surprising, so we don't do that
* just yet.
*/
base_str = str = strdup(orig_str);
do {
int code;
while (!*str || *str == ',' || *str == ' ')
str++;
comma = strchr(str, ',');
if (comma)
*comma = 0;
if (!strcmp(str, "all"))
return ~0;
code = callback_id(str);
if (code >= 0 && code < 32) {
mask |= 1 << code;
} else {
lwarn("Unable to find a callback id for '%s'\n", str);
}
str = comma;
if (comma)
*comma = ',';
} while (str);
free(base_str);
*evt_mask = mask;
return 0;
}
static void grok_module_compound(struct cfg_comp *comp)
{
uint i;
uint32_t handle_events = ~0; /* events to filter in */
uint32_t ignore_events = 0; /* events to filter out */
for (i = 0; i < comp->vars; i++) {
struct cfg_var *v = comp->vlist[i];
if (!strcmp(v->key, "ipc_reap_interval")) {
char *endp;
mrm_reap_interval = (int)strtoul(v->value, &endp, 0);
if (mrm_reap_interval < 0 || *endp != '\0')
cfg_error(comp, v, "Illegal value for %s", v->key);
continue;
}
/* not very widely used, I should think */
if (!strcmp(v->key, "event_mask")) {
event_mask = strtoul(v->value, NULL, 0);
continue;
}
if (!strcmp(v->key, "handle_events")) {
if (parse_event_filter(v->value, &handle_events) < 0)
cfg_error(comp, v, "Illegal value for %s", v->key);
continue;
}
if (!strcmp(v->key, "ignore_events")) {
if (parse_event_filter(v->value, &ignore_events) < 0)
cfg_error(comp, v, "Illegal value for %s", v->key);
continue;
}
if (grok_common_var(comp, v))
continue;
if (log_grok_var(v->key, v->value))
continue;
if (ipc_grok_var(v->key, v->value))
continue;
cfg_error(comp, comp->vlist[i], "Unknown variable");
}
/* remove the ignored events from the handled ones */
event_mask = handle_events & (~ignore_events);
if (!mrm_reap_interval)
mrm_reap_interval = 2;
}
static void grok_daemon_compound(struct cfg_comp *comp)
{
uint i;
for (i = 0; i < comp->nested; i++) {
struct cfg_comp *c = comp->nest[i];
uint vi;
if (!prefixcmp(comp->nest[i]->name, "database")) {
use_database = 1;
for (vi = 0; vi < c->vars; vi++) {
struct cfg_var *v = c->vlist[vi];
if (!prefixcmp(v->key, "track_current")) {
db_track_current = strtobool(v->value);
}
}
break;
}
}
}
static void read_config(char *cfg_file)
{
uint i;
struct cfg_comp *config = cfg_parse_file(cfg_file);
if (!config) {
lwarn("Failed to read config file");
return;
}
for (i = 0; i < config->vars; i++)
grok_common_var(config, config->vlist[i]);
for (i = 0; i < config->nested; i++) {
struct cfg_comp *c = config->nest[i];
if (!prefixcmp(c->name, "module")) {
grok_module_compound(c);
continue;
}
/*
* we sneak a peak in here only to see if we're using a
* database or not, as it's an important heuristic to
*/
if (!prefixcmp(c->name, "daemon")) {
grok_daemon_compound(c);
continue;
}
}
/*
* parse all the nodes. This warns or errors out on
* config errors with illegal compounds as well
*/
node_grok_config(config);
cfg_destroy_compound(config);
}
/* Nagios stuff goes below */
NEB_API_VERSION(CURRENT_NEB_API_VERSION);
extern int daemon_dumps_core;
void *neb_handle = NULL;
extern char *config_file;
/*
* we send this every 15 seconds, just in case our nodes forget
* about us. It shouldn't happen, but there are stranger things
* than random bugs in computer programs.
*/
static int send_pulse(void *discard)
{
node_send_ctrl_active(&ipc, CTRL_GENERIC, &self, 100);
schedule_new_event(EVENT_USER_FUNCTION, TRUE,
time(NULL) + MERLIN_PULSE_INTERVAL, FALSE,
0, NULL, FALSE, send_pulse, NULL, 0);
return 0;
}
/*
* Sends the path to objects.cache and status.log to the
* daemon so it can import the necessary data into the
* database.
*/
/* check recent additions to Nagios for why these are nifty */
#define nagios_object_cache mac->x[MACRO_OBJECTCACHEFILE]
#define nagios_status_log mac->x[MACRO_STATUSDATAFILE]
int send_paths(void)
{
size_t config_path_len, cache_path_len;
char *cache_file, *status_log;
merlin_event pkt;
nagios_macros *mac;
/*
* delay sending paths until we're connected, or we'll always
* just hang around until the stall times out and we start
* sending more events later, thereby triggering a new connection
* attempt.
*/
if (!ipc_is_connected(0)) {
merlin_should_send_paths = 1;
return 0;
}
if (!merlin_should_send_paths || merlin_should_send_paths > time(NULL))
return 0;
mac = get_global_macros();
if (db_track_current)
cache_file = nagios_object_cache;
else
asprintf(&cache_file, "/tmp/timeperiods.cache");
status_log = nagios_status_log;
ldebug("config_file: %p; nagios_object_cache: %p; status_log: %p",
config_file, cache_file, status_log);
if (!config_file) {
/* this should never happen. It really shouldn't */
merlin_should_send_paths = time(NULL) + merlin_sendpath_interval;
return -1;
}
memset(&pkt, 0, sizeof(pkt));
pkt.hdr.type = CTRL_PACKET;
pkt.hdr.code = CTRL_PATHS;
pkt.hdr.protocol = MERLIN_PROTOCOL_VERSION;
/*
* Add the paths to pkt.body as nul-terminated strings.
* We simply rely on 32K bytes to be enough to hold the
* three paths we're interested in (and they are if we're
* on a unixy system, where PATH_MAX is normally 4096).
* We cheat a little and use pkt.hdr.len as an offset
* to the bytestream.
*/
config_path_len = strlen(config_file);
memcpy(pkt.body, config_file, config_path_len);
pkt.hdr.len = config_path_len;
if (cache_file && *cache_file) {
cache_path_len = strlen(cache_file);
memcpy(pkt.body + pkt.hdr.len + 1, cache_file, cache_path_len);
pkt.hdr.len += cache_path_len + 1;
if (status_log && *status_log) {
memcpy(pkt.body + pkt.hdr.len + 1, status_log, strlen(status_log));
pkt.hdr.len += strlen(status_log) + 1;
}
}
if (!db_track_current) {
free(cache_file);
cache_file = NULL;
}
/* nul-terminate and include the nul-char */
pkt.body[pkt.hdr.len++] = 0;
pkt.hdr.selection = 0;
/*
* if the event was successfully added to the binlog,
* we'll get 0 back, which means we can just let the
* event in the binlog be valid until the binlog gets
* full.
*/
if (ipc_send_event(&pkt) < 0)
return -1;
merlin_should_send_paths = 0;
/*
* start stalling immediately and keep doing so until
* the reaper thread reads a CTRL_RESUME event so we
* wait until the import is completed
*/
ctrl_stall_start();
return 0;
}
/*
* This function gets called before and after Nagios has read its config
* and written its objects.cache and status.log files.
* We want to setup object lists and such here, so we only care about the
* case where config has already been read.
*/
static int post_config_init(int cb, void *ds)
{
int result;
if (*(int *)ds != NEBTYPE_PROCESS_EVENTLOOPSTART)
return 0;
/* required for the 'nodeinfo' query through the query handler */
host_check_node = calloc(num_objects.hosts, sizeof(merlin_node *));
service_check_node = calloc(num_objects.services, sizeof(merlin_node *));
self.host_checks_handled = num_objects.hosts;
self.service_checks_handled = num_objects.services;
if (!db_track_current) {
char *cache_file = NULL;
FILE *fp = NULL;
time_t current_time = 0L;
unsigned int i;
time(¤t_time);
asprintf(&cache_file, "/tmp/timeperiods.cache");
/* open the cache file for writing */
fp = fopen(cache_file, "w");
if(fp != NULL) {
fprintf(fp, "########################################\n");
fprintf(fp, "# MERLIN TIMEPERIOD CACHE FILE\n");
fprintf(fp, "#\n");
fprintf(fp, "# Created: %s", ctime(¤t_time));
fprintf(fp, "########################################\n\n");
/* cache timeperiods */
for(i = 0; i < num_objects.timeperiods; i++)
fcache_timeperiod(fp, timeperiod_ary[i]);
fclose(fp);
}
free(cache_file);
}
/* only call this function once */
neb_deregister_callback(NEBCALLBACK_PROCESS_DATA, post_config_init);
linfo("Object configuration parsed.");
setup_host_hash_tables();
if((result = qh_register_handler("merlin", "Merlin information", 0, merlin_qh)) < 0)
lerr("Failed to register query handler: %s", strerror(-result));
else
linfo("merlin_qh registered with query handler");
/*
* it's safe to send the hash of the config we're using now that
* we know the local host could parse it properly.
*/
send_pulse(NULL);
/*
* now we register the hooks we're interested in, avoiding
* the huge initial burst of events Nagios otherwise spews
* at us when it's reading its status back in from the
* status.sav file (assuming state retention is enabled)
*/
register_merlin_hooks(event_mask);
send_paths();
/*
* this is the last event related to startup, so the regular mod hook
* must see it to be able to shove startup info into the database.
*/
merlin_mod_hook(cb, ds);
return 0;
}
/*
* This gets run when we create an ipc connection, or when that
* connection is lost. A CTRL_ACTIVE packet should always be
* the first to go through the ipc socket
*/
static int ipc_action_handler(merlin_node *node, int prev_state)
{
int ret;
ldebug("Running ipc action handler");
if (node != &ipc || ipc.state == prev_state) {
ldebug(" ipc_action_handler(): First exit");
return 0;
}
/*
* If we get disconnected while stalling, we immediately
* stop stalling and note that we should send paths again.
* Since we never received a CTRL_RESUME we can't know for
* sure that the module has actually imported anything.
* Better safe than sorry, iow.
*/
if (prev_state == STATE_CONNECTED && is_stalling()) {
ctrl_stall_stop();
merlin_should_send_paths = 1;
}
if (ipc.state == STATE_CONNECTED) {
ret = iobroker_register(nagios_iobs, ipc.sock, (void *)&ipc, ipc_reaper);
if (ret)
lerr(" ipc_action_handler(): iobroker_register(%p, %d, %p, %p) returned %d: %s",
nagios_iobs, ipc.sock, (void *)&ipc, ipc_reaper, ret, iobroker_strerror(ret));
} else {
ret = iobroker_unregister(nagios_iobs, ipc.sock);
if (ret)
ldebug(" ipc_action_handler(): iobroker_unregister(%p, %d) returned %d: %s",
nagios_iobs, ipc.sock, ret, iobroker_strerror(ret));
}
/*
* we must use node_send_ctrl_active() here or we'll
* end up in an infinite loop in ipc_ctrl(), rapidly
* devouring all available stack space. Since we
* know we're connected anyways, we don't really
* need the ipc_is_connected(0) call that ipc_ctrl
* adds before trying to send.
*/
if (node->state == STATE_CONNECTED)
return node_send_ctrl_active(&ipc, CTRL_GENERIC, &self, 100);
else {
int i;
/*
* if we went from connected to anything else, we must
* mark all other nodes as disconnected so that checks
* fail over properly
*/
for (i = 0; i < num_nodes; i++) {
merlin_node *node = node_table[i];
/* this handles peer-count and peer-id as well */
node_set_state(node, STATE_NONE, "Daemon disconnected");
memset(&node->info, 0, sizeof(node->info));
}
}
return 0;
}
/**
* Initialization routine for the eventbroker module. This
* function gets called by Nagios when it's done loading us
*/
int nebmodule_init(int flags, char *arg, nebmodule *handle)
{
char *home = NULL;
neb_handle = (void *)handle;
/*
* this must be zero'd out before we enter the node
* config parsing, or we'll clobber the values collected
* there and think we have no nodes configured
*/
memset(&self, 0, sizeof(self));
/*
* Solaris (among others) don't have MSG_NOSIGNAL, so we
* ignore SIGPIPE globally instead.
*/
signal(SIGPIPE, SIG_IGN);
/*
* this must come before reading configuration. It's a very
* cheap operation anyways and doesn't allocate any memory,