-
Notifications
You must be signed in to change notification settings - Fork 453
/
Copy pathchecks.c
3533 lines (2758 loc) · 115 KB
/
checks.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
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
/*****************************************************************************
*
* CHECKS.C - Service and host check functions for Nagios
*
*
* License:
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*****************************************************************************/
#include "../include/config.h"
#include "../include/comments.h"
#include "../include/common.h"
#include "../include/statusdata.h"
#include "../include/downtime.h"
#include "../include/macros.h"
#include "../include/nagios.h"
#include "../include/broker.h"
#include "../include/perfdata.h"
#include "../include/workers.h"
/*#define DEBUG_CHECKS*/
/*#define DEBUG_HOST_CHECKS 1*/
#define replace_semicolons(output, ptr) do { ptr = output; while ((ptr = strchr(ptr, ';')) != NULL) { * ptr = ':'; } } while (0)
#ifdef USE_EVENT_BROKER
#include "../include/neberrors.h"
#endif
/******************************************************************/
/********************** CHECK REAPER FUNCTIONS ********************/
/******************************************************************/
/* reaps host and service check results */
int reap_check_results(void)
{
int reaped_checks = 0;
log_debug_info(DEBUGL_FUNCTIONS, 0, "reap_check_results() start\n");
/* process files in the check result queue */
reaped_checks = process_check_result_queue(check_result_path);
log_debug_info(DEBUGL_FUNCTIONS, 0, "reap_check_results() reaped %d checks end\n", reaped_checks);
return OK;
}
/******************************************************************/
/****************** SERVICE MONITORING FUNCTIONS ******************/
/******************************************************************/
/* executes a scheduled service check */
int run_scheduled_service_check(service *svc, int check_options, double latency)
{
int result = OK;
time_t current_time = 0L;
time_t preferred_time = 0L;
time_t next_valid_time = 0L;
int time_is_valid = TRUE;
if (svc == NULL) {
return ERROR;
}
log_debug_info(DEBUGL_FUNCTIONS, 0, "run_scheduled_service_check() start\n");
log_debug_info(DEBUGL_CHECKS, 0, "Attempting to run scheduled check of service '%s' on host '%s': check options=%d, latency=%lf\n", svc->description, svc->host_name, check_options, latency);
/*
* reset the next_check_event so we know it's
* no longer in the scheduling queue
*/
svc->next_check_event = NULL;
/* attempt to run the check */
result = run_async_service_check(svc, check_options, latency, TRUE, TRUE, &time_is_valid, &preferred_time);
/* an error occurred, so reschedule the check */
if (result == ERROR) {
log_debug_info(DEBUGL_CHECKS, 1, "Unable to run scheduled service check at this time\n");
/* only attempt to (re)schedule checks that should get checked... */
if (svc->should_be_scheduled == TRUE) {
/* get current time */
time(¤t_time);
/* determine next time we should check the service if needed */
/* if service has no check interval, schedule it again for 5 minutes from now */
if (current_time >= preferred_time) {
preferred_time = current_time + ((svc->check_interval <= 0) ? 300 : (svc->check_interval * interval_length));
}
/* make sure we rescheduled the next service check at a valid time */
get_next_valid_time(preferred_time, &next_valid_time, svc->check_period_ptr);
/*
* If we really can't reschedule the service properly, we
* just push the check to preferred_time plus some reasonable
* random value and try again then.
*/
if (time_is_valid == FALSE && check_time_against_period(next_valid_time, svc->check_period_ptr) == ERROR) {
svc->next_check = preferred_time + ranged_urand(0, check_window(svc));
logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: Check of service '%s' on host '%s' could not be rescheduled properly. Scheduling check for %s...\n", svc->description, svc->host_name, ctime(&preferred_time));
log_debug_info(DEBUGL_CHECKS, 1, "Unable to find any valid times to reschedule the next service check!\n");
}
/* this service could be rescheduled... */
else {
svc->next_check = next_valid_time;
if (next_valid_time > preferred_time) {
/* Next valid time is further in the future because of
* timeperiod constraints. Add a random amount so we
* don't get all checks subject to that timeperiod
* constraint scheduled at the same time
*/
svc->next_check += ranged_urand(0, check_window(svc));
}
svc->should_be_scheduled = TRUE;
log_debug_info(DEBUGL_CHECKS, 1, "Rescheduled next service check for %s", ctime(&next_valid_time));
}
}
/*
* reschedule the next service check - unless we couldn't
* find a valid next check time, but keep original options
*/
if (svc->should_be_scheduled == TRUE) {
schedule_service_check(svc, svc->next_check, check_options);
}
/* update the status log */
update_service_status(svc, FALSE);
return ERROR;
}
return OK;
}
/* forks a child process to run a service check, but does not wait for the service check result */
int run_async_service_check(service *svc, int check_options, double latency, int scheduled_check, int reschedule_check, int *time_is_valid, time_t *preferred_time)
{
nagios_macros mac;
char *raw_command = NULL;
char *processed_command = NULL;
struct timeval start_time, end_time;
host *temp_host = NULL;
double old_latency = 0.0;
check_result *cr;
int runchk_result = OK;
int macro_options = STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS;
#ifdef USE_EVENT_BROKER
int neb_result = OK;
#endif
log_debug_info(DEBUGL_FUNCTIONS, 0, "run_async_service_check()\n");
/* make sure we have something */
if (svc == NULL) {
return ERROR;
}
/* is the service check viable at this time? */
if (check_service_check_viability(svc, check_options, time_is_valid, preferred_time) == ERROR) {
return ERROR;
}
/* find the host associated with this service */
if ((temp_host = svc->host_ptr) == NULL) {
return ERROR;
}
/******** GOOD TO GO FOR A REAL SERVICE CHECK AT THIS POINT ********/
#ifdef USE_EVENT_BROKER
/* initialize start/end times */
start_time.tv_sec = 0L;
start_time.tv_usec = 0L;
end_time.tv_sec = 0L;
end_time.tv_usec = 0L;
/* send data to event broker */
neb_result = broker_service_check(NEBTYPE_SERVICECHECK_ASYNC_PRECHECK, NEBFLAG_NONE, NEBATTR_NONE, svc, CHECK_TYPE_ACTIVE, start_time, end_time, svc->check_command, svc->latency, 0.0, 0, FALSE, 0, NULL, NULL, NULL);
/* neb module wants to cancel the service check - the check will be rescheduled for a later time by the scheduling logic */
if (neb_result == NEBERROR_CALLBACKCANCEL) {
log_debug_info(DEBUGL_CHECKS, 0, "Check of service '%s' on host '%s' (id=%u) was cancelled by a module\n", svc->description, svc->host_name, svc->id);
if (preferred_time) {
*preferred_time += (svc->check_interval * interval_length);
}
return ERROR;
}
/* neb module wants to override (or cancel) the service check - perhaps it will check the service itself */
/* NOTE: if a module does this, it has to do a lot of the stuff found below to make sure things don't get whacked out of shape! */
/* NOTE: if would be easier for modules to override checks when the NEBTYPE_SERVICECHECK_INITIATE event is called (later) */
if (neb_result == NEBERROR_CALLBACKOVERRIDE) {
log_debug_info(DEBUGL_CHECKS, 0, "Check of service '%s' on host '%s' (id=%u) was overridden by a module\n", svc->description, svc->host_name, svc->id);
return OK;
}
#endif
log_debug_info(DEBUGL_CHECKS, 0, "Checking service '%s' on host '%s'...\n", svc->description, svc->host_name);
/* clear check options - we don't want old check options retained */
/* only clear check options for scheduled checks - ondemand checks shouldn't affected retained check options */
if (scheduled_check == TRUE) {
svc->check_options = CHECK_OPTION_NONE;
}
/* update latency for macros, event broker, save old value for later */
old_latency = svc->latency;
svc->latency = latency;
/* grab the host and service macro variables */
memset(&mac, 0, sizeof(mac));
grab_host_macros_r(&mac, temp_host);
grab_service_macros_r(&mac, svc);
/* get the raw command line */
get_raw_command_line_r(&mac, svc->check_command_ptr, svc->check_command, &raw_command, macro_options);
if (raw_command == NULL) {
clear_volatile_macros_r(&mac);
log_debug_info(DEBUGL_CHECKS, 0, "Raw check command for service '%s' on host '%s' was NULL - aborting.\n", svc->description, svc->host_name);
if (preferred_time) {
*preferred_time += (svc->check_interval * interval_length);
}
svc->latency = old_latency;
return ERROR;
}
/* process any macros contained in the argument */
process_macros_r(&mac, raw_command, &processed_command, macro_options);
my_free(raw_command);
if (processed_command == NULL) {
clear_volatile_macros_r(&mac);
log_debug_info(DEBUGL_CHECKS, 0, "Processed check command for service '%s' on host '%s' was NULL - aborting.\n", svc->description, svc->host_name);
if (preferred_time) {
*preferred_time += (svc->check_interval * interval_length);
}
svc->latency = old_latency;
return ERROR;
}
/* get the command start time */
gettimeofday(&start_time, NULL);
cr = calloc(1, sizeof(*cr));
if (!cr) {
clear_volatile_macros_r(&mac);
svc->latency = old_latency;
my_free(processed_command);
return ERROR;
}
init_check_result(cr);
/* save check info */
cr->object_check_type = SERVICE_CHECK;
cr->check_type = CHECK_TYPE_ACTIVE;
cr->check_options = check_options;
cr->scheduled_check = scheduled_check;
cr->reschedule_check = reschedule_check;
cr->latency = latency;
cr->start_time = start_time;
cr->finish_time = start_time;
cr->early_timeout = FALSE;
cr->exited_ok = TRUE;
cr->return_code = STATE_OK;
cr->output = NULL;
cr->host_name = (char *)strdup(svc->host_name);
cr->service_description = (char *)strdup(svc->description);
#ifdef USE_EVENT_BROKER
/* send data to event broker */
neb_result = broker_service_check(NEBTYPE_SERVICECHECK_INITIATE, NEBFLAG_NONE, NEBATTR_NONE, svc, CHECK_TYPE_ACTIVE, start_time, end_time, svc->check_command, svc->latency, 0.0, service_check_timeout, FALSE, 0, processed_command, NULL, cr);
/* neb module wants to override the service check - perhaps it will check the service itself */
if (neb_result == NEBERROR_CALLBACKOVERRIDE) {
clear_volatile_macros_r(&mac);
svc->latency = old_latency;
free_check_result(cr);
my_free(processed_command);
return OK;
}
#endif
/* reset latency (permanent value will be set later) */
svc->latency = old_latency;
/* paw off the check to a worker to run */
runchk_result = wproc_run_check(cr, processed_command, &mac);
if (runchk_result == ERROR) {
logit(NSLOG_RUNTIME_ERROR, TRUE, "Unable to run check for service '%s' on host '%s'\n", svc->description, svc->host_name);
}
else {
/* do the book-keeping */
currently_running_service_checks++;
svc->is_executing = TRUE;
update_check_stats((scheduled_check == TRUE) ? ACTIVE_SCHEDULED_SERVICE_CHECK_STATS : ACTIVE_ONDEMAND_SERVICE_CHECK_STATS, start_time.tv_sec);
}
/* free memory */
my_free(processed_command);
clear_volatile_macros_r(&mac);
return runchk_result;
}
/* Start of inline helper functions for handle async host/service check result functions
BH 03 Dec 2017
The giant monolithic async helper functions were becoming difficult to maintain
So I broke them out into smaller manageable chunks where a side-by-side comparison
of the two functions is a bit more reasonable. This would have been a lot easier with
dynamic type casting, or even some macro magic - but at least this way it isn't as
messy as it would have been with macros.
When introducing new inline helper functions, the goal is so that both of the functions
can sit on the same screen at the same time - for obvious reasons (I hope).
Try to keep them concise */
/* Bit of logic for determining an adequate return code */
int get_service_check_return_code(service *svc, check_result *cr)
{
int rc;
log_debug_info(DEBUGL_FUNCTIONS, 0, "get_service_check_return_code()\n");
if (NULL == svc || NULL == cr) {
return STATE_UNKNOWN;
}
/* return now if it's a passive check */
if (cr->check_type != CHECK_TYPE_ACTIVE) {
return cr->return_code;
}
/* grab the return code */
rc = cr->return_code;
/* did the check result have an early timeout? */
if (cr->early_timeout == TRUE) {
my_free(svc->plugin_output);
my_free(svc->long_plugin_output);
my_free(svc->perf_data);
logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: Check of service '%s' on host '%s' timed out after %.3fs!\n", svc->description, svc->host_name, svc->execution_time);
asprintf(&svc->plugin_output, "(Service check timed out after %.2lf seconds)", svc->execution_time);
rc = service_check_timeout_state;
}
/* if there was some error running the command, just skip it (this shouldn't be happening) */
else if (cr->exited_ok == FALSE) {
my_free(svc->plugin_output);
my_free(svc->long_plugin_output);
my_free(svc->perf_data);
logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: Check of service '%s' on host '%s' did not exit properly!\n", svc->description, svc->host_name);
svc->plugin_output = (char *)strdup("(Service check did not exit properly)");
rc = STATE_CRITICAL;
}
/* 126 is a return code for non-executable */
else if (cr->return_code == 126) {
my_free(svc->plugin_output);
my_free(svc->long_plugin_output);
my_free(svc->perf_data);
logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: Return code of 126 for service '%s' on host '%s' may indicate a non-executable plugin.\n",
svc->description, svc->host_name);
svc->plugin_output = strdup("(Return code of 126 is out of bounds. Check if plugin is executable)");
rc = STATE_CRITICAL;
}
/* 127 is a return code for non-existent */
else if (cr->return_code == 127) {
my_free(svc->plugin_output);
my_free(svc->long_plugin_output);
my_free(svc->perf_data);
logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: Return code of 127 for service '%s' on host '%s' may indicate this plugin doesn't exist.\n",
svc->description, svc->host_name);
svc->plugin_output = strdup("(Return code of 127 is out of bounds. Check if plugin exists)");
rc = STATE_CRITICAL;
}
/* make sure the return code is within bounds */
else if (cr->return_code < 0 || cr->return_code > 3) {
logit(NSLOG_RUNTIME_WARNING, TRUE,
"Warning: Return code of %d for check of service '%s' on host '%s' was out of bounds.\n",
cr->return_code,
svc->description,
svc->host_name);
asprintf(&svc->plugin_output, "(Return code of %d for service '%s' on host '%s' was out of bounds)",
cr->return_code,
svc->description,
svc->host_name);
rc = STATE_CRITICAL;
}
return rc;
}
/* Bit of logic for determining an adequate return code */
int get_host_check_return_code(host *hst, check_result *cr)
{
int rc;
log_debug_info(DEBUGL_FUNCTIONS, 0, "get_host_check_return_code()\n");
if (hst == NULL || cr == NULL) {
return HOST_UNREACHABLE;
}
/* return now if it's a passive check */
if (cr->check_type != CHECK_TYPE_ACTIVE) {
return cr->return_code;
}
/* get the unprocessed return code */
rc = cr->return_code;
/* did the check result have an early timeout? */
if (cr->early_timeout) {
my_free(hst->plugin_output);
my_free(hst->long_plugin_output);
my_free(hst->perf_data);
logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: Check of host '%s' timed out after %.2lf seconds\n", hst->name, hst->execution_time);
asprintf(&hst->plugin_output, "(Host check timed out after %.2lf seconds)", hst->execution_time);
rc = HOST_UNREACHABLE;
}
/* if there was some error running the command, just skip it (this shouldn't be happening) */
else if (cr->exited_ok == FALSE) {
my_free(hst->plugin_output);
my_free(hst->long_plugin_output);
my_free(hst->perf_data);
logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: Check of host '%s' did not exit properly!\n", hst->name);
hst->plugin_output = (char *)strdup("(Host check did not exit properly)");
rc = HOST_UNREACHABLE;
}
/* 126 is a return code for non-executable */
else if (cr->return_code == 126) {
my_free(hst->plugin_output);
my_free(hst->long_plugin_output);
my_free(hst->perf_data);
logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: Return code of 126 for host '%s' may indicate a non-executable plugin.\n",
hst->name);
hst->plugin_output = strdup("(Return code of 126 is out of bounds. Check if plugin is executable)");
rc = HOST_UNREACHABLE;
}
/* 127 is a return code for non-existent */
else if (cr->return_code == 127) {
my_free(hst->plugin_output);
my_free(hst->long_plugin_output);
my_free(hst->perf_data);
logit(NSLOG_RUNTIME_WARNING, TRUE, "Warning: Return code of 127 for host '%s' may indicate this plugin doesn't exist.\n",
hst->name);
hst->plugin_output = strdup("(Return code of 127 is out of bounds. Check if plugin exists)");
rc = HOST_UNREACHABLE;
}
/* make sure the return code is within bounds */
else if (cr->return_code < 0 || cr->return_code > 3) {
my_free(hst->plugin_output);
my_free(hst->long_plugin_output);
my_free(hst->perf_data);
logit(NSLOG_RUNTIME_WARNING, TRUE,
"Warning: Return code of %d for check of host '%s' was out of bounds.\n",
cr->return_code,
hst->name);
asprintf(&hst->plugin_output, "(Return code of %d for host '%s' was out of bounds)",
cr->return_code,
hst->name);
rc = HOST_UNREACHABLE;
}
/* a NULL host check command means we should assume the host is UP */
if (hst->check_command == NULL) {
my_free(hst->plugin_output);
hst->plugin_output = (char *)strdup("(Host assumed to be UP)");
rc = HOST_UP;
}
/* if we're not doing aggressive host checking, let WARNING states indicate the host is up (fake the result to be HOST_UP) */
else if (use_aggressive_host_checking == FALSE && rc == STATE_WARNING) {
rc = HOST_UP;
}
/* any problem state indicates the host is not UP */
else if (rc != HOST_UP) {
rc = HOST_DOWN;
}
return rc;
}
/******************************************************************************
******* Calculate check result exec time
*****************************************************************************/
static inline double calculate_check_result_execution_time(check_result *cr)
{
double execution_time = 0.0;
if (cr != NULL) {
double start_s = cr->start_time.tv_sec;
double start_us = cr->start_time.tv_usec;
double finish_s = cr->finish_time.tv_sec;
double finish_us = cr->finish_time.tv_usec;
execution_time = ((finish_s - start_s) + ((finish_us - start_us) / 1000.0) / 1000.0);
if (execution_time < 0.0) {
execution_time = 0.0;
}
}
return execution_time;
}
/******************************************************************************
******* Last state ended
*****************************************************************************/
static inline void record_last_service_state_ended(service * svc)
{
switch(svc->last_state) {
case STATE_OK:
svc->last_time_ok = svc->last_check;
break;
case STATE_WARNING:
svc->last_time_warning = svc->last_check;
break;
case STATE_UNKNOWN:
svc->last_time_unknown = svc->last_check;
break;
case STATE_CRITICAL:
svc->last_time_critical = svc->last_check;
break;
default:
break;
}
}
/*****************************************************************************/
static inline void record_last_host_state_ended(host * hst)
{
switch(hst->last_state) {
case HOST_UP:
hst->last_time_up = hst->last_check;
break;
case HOST_DOWN:
hst->last_time_down = hst->last_check;
break;
case HOST_UNREACHABLE:
hst->last_time_unreachable = hst->last_check;
break;
default:
break;
}
}
/******************************************************************************
******* Logic chunks for when an object is passive
*****************************************************************************/
static inline int service_is_passive(service *svc, check_result *cr)
{
if (accept_passive_service_checks == FALSE) {
log_debug_info(DEBUGL_CHECKS, 0, "Discarding passive service check result because passive service checks are disabled globally.\n");
return FALSE;
}
if (svc->accept_passive_checks == FALSE) {
log_debug_info(DEBUGL_CHECKS, 0, "Discarding passive service check result because passive checks are disabled for this service.\n");
return FALSE;
}
svc->check_type = CHECK_TYPE_PASSIVE;
/* update check statistics for passive checks */
update_check_stats(PASSIVE_SERVICE_CHECK_STATS, cr->start_time.tv_sec);
/* log passive checks - we need to do this here, as some may bypass external commands by getting dropped in checkresults dir */
if (log_passive_checks == TRUE) {
logit(NSLOG_PASSIVE_CHECK, FALSE, "PASSIVE SERVICE CHECK: %s;%s;%d;%s\n",
svc->host_name,
svc->description,
svc->current_state,
svc->plugin_output);
}
return TRUE;
}
/*****************************************************************************/
static inline int host_is_passive(host *hst, check_result *cr)
{
if (accept_passive_host_checks == FALSE) {
log_debug_info(DEBUGL_CHECKS, 0, "Discarding passive host check result because passive host checks are disabled globally.\n");
return FALSE;
}
if (hst->accept_passive_checks == FALSE) {
log_debug_info(DEBUGL_CHECKS, 0, "Discarding passive host check result because passive checks are disabled for this host.\n");
return FALSE;
}
hst->check_type = CHECK_TYPE_PASSIVE;
/* update check stats for passive checks */
update_check_stats(PASSIVE_HOST_CHECK_STATS, cr->start_time.tv_sec);
/* log passive checks - we need to do this here as some may bypass external commands by getting dropped in checkresults dir */
/* todo - check if current_state is right - i don't think it is! */
if (log_passive_checks == TRUE) {
logit(NSLOG_PASSIVE_CHECK, FALSE, "PASSIVE HOST CHECK: %s;%d;%s\n",
hst->name,
hst->current_state,
hst->plugin_output);
}
return TRUE;
}
/******************************************************************************
******* Logic chunks for when an object is active
*****************************************************************************/
static inline void service_is_active(service *svc)
{
svc->check_type = CHECK_TYPE_ACTIVE;
/* decrement the number of service checks still out there... */
if (currently_running_service_checks > 0) {
currently_running_service_checks--;
}
svc->is_executing = FALSE;
}
/*****************************************************************************/
static inline void host_is_active(host *hst)
{
hst->check_type = CHECK_TYPE_ACTIVE;
/* decrement the number of host checks still out there... */
if (currently_running_host_checks > 0) {
currently_running_host_checks--;
}
hst->is_executing = FALSE;
}
/******************************************************************************
******* Generic debugging functions
*****************************************************************************/
static inline void debug_async_service(service *svc, check_result *cr)
{
log_debug_info(DEBUGL_CHECKS, 0, "** Handling %s async check result for service '%s' on host '%s' from '%s'...\n",
(cr->check_type == CHECK_TYPE_ACTIVE) ? "ACTIVE" : "PASSIVE",
svc->description,
svc->host_name,
check_result_source(cr));
log_debug_info(DEBUGL_CHECKS, 1,
" * OPTIONS: %d, SCHEDULED: %d, RESCHEDULE: %d, EXITED OK: %d, RETURN CODE: %d, OUTPUT:\n%s\n",
cr->check_options,
cr->scheduled_check,
cr->reschedule_check,
cr->exited_ok,
cr->return_code,
(cr == NULL) ? "NULL" : cr->output);
}
/*****************************************************************************/
static inline void debug_async_host(host *hst, check_result *cr)
{
log_debug_info(DEBUGL_CHECKS, 0, "** Handling %s async check result for host '%s' from '%s'...\n",
(cr->check_type == CHECK_TYPE_ACTIVE) ? "ACTIVE" : "PASSIVE",
hst->name,
check_result_source(cr));
log_debug_info(DEBUGL_CHECKS, 1,
" * OPTIONS: %d, SCHEDULED: %d, RESCHEDULE: %d, EXITED OK: %d, RETURN CODE: %d, OUTPUT:\n%s\n",
cr->check_options,
cr->scheduled_check,
cr->reschedule_check,
cr->exited_ok,
cr->return_code,
(cr == NULL) ? "NULL" : cr->output);
}
/******************************************************************************
******* Logic chunks for checking object freshness
*****************************************************************************/
static inline void service_fresh_check(service *svc, check_result *cr, time_t current_time)
{
/* check freshness */
if (cr->check_options & CHECK_OPTION_FRESHNESS_CHECK) {
/* DISCARD INVALID FRESHNESS CHECK RESULTS */
/* If a services goes stale, Nagios will initiate a forced check in order to freshen it. There is a race condition whereby a passive check
could arrive between the 1) initiation of the forced check and 2) the time when the forced check result is processed here. This would
make the service fresh again, so we do a quick check to make sure the service is still stale before we accept the check result. */
if (is_service_result_fresh(svc, current_time, FALSE) == TRUE) {
log_debug_info(DEBUGL_CHECKS, 0, "Discarding service freshness check result because the service is currently fresh (race condition avoided).\n");
return;
}
/* clear the freshening flag (it would have been set if this service was determined to be stale) */
svc->is_being_freshened = FALSE;
}
}
/*****************************************************************************/
static inline void host_fresh_check(host *hst, check_result *cr, time_t current_time)
{
/* check freshness */
if (cr->check_options & CHECK_OPTION_FRESHNESS_CHECK) {
/* DISCARD INVALID FRESHNESS CHECK RESULTS */
/* If a host goes stale, Nagios will initiate a forced check in order to freshen it. There is a race condition whereby a passive check
could arrive between the 1) initiation of the forced check and 2) the time when the forced check result is processed here. This would
make the host fresh again, so we do a quick check to make sure the host is still stale before we accept the check result. */
if (is_host_result_fresh(hst, current_time, FALSE) == TRUE) {
log_debug_info(DEBUGL_CHECKS, 0, "Discarding host freshness check result because the host is currently fresh (race condition avoided).\n");
return;
}
/* clear the freshening flag (it would have been set if this host was determined to be stale) */
hst->is_being_freshened = FALSE;
}
}
/******************************************************************************
******* Logic chunks for setting some of the initial flags, etc.
*****************************************************************************/
static inline void service_initial_handling(service *svc, check_result *cr, char **old_plugin_output)
{
char * temp_ptr = NULL;
/* save old plugin output */
if (svc->plugin_output) {
* old_plugin_output = (char *)strdup(svc->plugin_output);
}
my_free(svc->plugin_output);
my_free(svc->long_plugin_output);
my_free(svc->perf_data);
/* parse check output to get: (1) short output, (2) long output, (3) perf data */
parse_check_output(cr->output, &svc->plugin_output, &svc->long_plugin_output, &svc->perf_data, TRUE, FALSE);
/* make sure the plugin output isn't null */
if (svc->plugin_output == NULL) {
svc->plugin_output = (char *)strdup("(No output returned from plugin)");
}
/* otherwise replace the semicolons with colons */
else {
replace_semicolons(svc->plugin_output, temp_ptr);
}
log_debug_info(DEBUGL_CHECKS, 2,
"Parsing check output...\n"
"Short Output: %s\n"
"Long Output: %s\n"
"Perf Data: %s\n",
svc->plugin_output,
(svc->long_plugin_output == NULL) ? "NULL" : svc->long_plugin_output,
(svc->perf_data == NULL) ? "NULL" : svc->perf_data);
svc->latency = cr->latency;
svc->execution_time = calculate_check_result_execution_time(cr);
svc->last_check = cr->start_time.tv_sec;
svc->should_be_scheduled = cr->reschedule_check;
svc->last_state = svc->current_state;
svc->current_state = get_service_check_return_code(svc, cr);
}
/*****************************************************************************/
static inline void host_initial_handling(host *hst, check_result *cr, char **old_plugin_output)
{
char * temp_ptr = NULL;
/* save old plugin output */
if (hst->plugin_output) {
* old_plugin_output = (char *)strdup(hst->plugin_output);
}
my_free(hst->plugin_output);
my_free(hst->long_plugin_output);
my_free(hst->perf_data);
/* parse check output to get: (1) short output, (2) long output, (3) perf data */
parse_check_output(cr->output, &hst->plugin_output, &hst->long_plugin_output, &hst->perf_data, TRUE, FALSE);
/* make sure the plugin output isn't null */
if (hst->plugin_output == NULL) {
hst->plugin_output = (char *)strdup("(No output returned from host check)");
}
/* otherwise replace the semicolons with colons */
else {
replace_semicolons(hst->plugin_output, temp_ptr);
}
log_debug_info(DEBUGL_CHECKS, 2,
"Parsing check output...\n"
"Short Output: %s\n"
"Long Output: %s\n"
"Perf Data: %s\n",
hst->plugin_output,
(hst->long_plugin_output == NULL) ? "NULL" : hst->long_plugin_output,
(hst->perf_data == NULL) ? "NULL" : hst->perf_data);
hst->latency = cr->latency;
hst->execution_time = calculate_check_result_execution_time(cr);
hst->last_check = cr->start_time.tv_sec;
hst->should_be_scheduled = cr->reschedule_check;
hst->last_state = hst->current_state;
hst->current_state = get_host_check_return_code(hst, cr);
}
/******************************************************************************
******* Logic for when an object has a notable change
******* Removes acknowledgement, advances event_id, etc.
*****************************************************************************/
static inline void service_state_or_hard_state_type_change(service * svc, int state_change, int hard_state_change, int * log_event, int * handle_event)
{
int state_or_type_change = FALSE;
/* update the event and problem ids */
if (state_change == TRUE) {
svc->last_state_change = svc->last_check;
/* always update the event id on a state change */
svc->last_event_id = svc->current_event_id;
svc->current_event_id = next_event_id;
next_event_id++;
/* update the problem id when transitioning to a problem state */
if (svc->last_state == STATE_OK) {
/* don't reset last problem id, or it will be zero the next time a problem is encountered */
svc->current_problem_id = next_problem_id;
next_problem_id++;
}
svc->state_type = SOFT_STATE;
state_or_type_change = TRUE;
}
if (hard_state_change == TRUE) {
svc->last_hard_state_change = svc->last_check;
svc->last_state_change = svc->last_check;
svc->last_hard_state = svc->current_state;
svc->state_type = HARD_STATE;
state_or_type_change = TRUE;
}
if (state_or_type_change) {
/* check if service should go into downtime from flexible downtime */
if (svc->pending_flex_downtime > 0) {
check_pending_flex_service_downtime(svc);
}
/* reset notification times and suppression option */
svc->last_notification = (time_t)0;
svc->next_notification = (time_t)0;
svc->no_more_notifications = FALSE;
if ((svc->acknowledgement_type == ACKNOWLEDGEMENT_NORMAL && (state_change == TRUE || hard_state_change == FALSE))
|| (svc->acknowledgement_type == ACKNOWLEDGEMENT_STICKY && svc->current_state == STATE_OK)) {
/* remove any non-persistant comments associated with the ack */
svc->problem_has_been_acknowledged = FALSE;
svc->acknowledgement_type = ACKNOWLEDGEMENT_NONE;
delete_service_acknowledgement_comments(svc);
}
svc->should_be_scheduled = TRUE;
*log_event = TRUE;
*handle_event = TRUE;
}
}
/*****************************************************************************/
static inline void host_state_or_hard_state_type_change(host * hst, int state_change, int hard_state_change, int * log_event, int * handle_event, int * send_notification)
{
int state_or_type_change = FALSE;
/* check if we simulate a hard state change */
if (hst->check_type == CHECK_TYPE_PASSIVE && passive_host_checks_are_soft == FALSE) {
log_debug_info(DEBUGL_CHECKS, 2, "Check type passive and passive host checks aren't false\n");
if (state_change == TRUE) {
hst->current_attempt = 1;
hard_state_change = TRUE;
}
hst->state_type = HARD_STATE;
}
/* update event and problem ids */
if (state_change == TRUE) {
hst->last_state_change = hst->last_check;
/* always update the event id on a state change */
hst->last_event_id = hst->current_event_id;
hst->current_event_id = next_event_id;
next_event_id++;
/* update the problem id when transitioning to a problem state */
if (hst->last_state == HOST_UP) {
/* don't reset last problem id, or it will be zero the next time a problem is encountered */
hst->current_problem_id = next_problem_id;
next_problem_id++;
}
/* clear the problem id when transitioning from a problem state to an OK state */
else {
hst->last_problem_id = hst->current_problem_id;
hst->current_problem_id = 0L;
}
hst->state_type = SOFT_STATE;
state_or_type_change = TRUE;
}
if (hard_state_change == TRUE) {
hst->last_hard_state_change = hst->last_check;
hst->last_hard_state = hst->current_state;
hst->state_type = HARD_STATE;
state_or_type_change = TRUE;
/* this is in the host func, but not the service
because it can easily be missed if a passive check
comes in and passive_host_checks_are_soft == FALSE */
*send_notification = TRUE;
}
if (state_or_type_change) {
/* check if host should go into downtime from flexible downtime */
check_pending_flex_host_downtime(hst);
/* reset notification times and suppression option */
hst->last_notification = (time_t)0;