This repository has been archived by the owner on Jan 15, 2019. It is now read-only.
/
checks.c
3980 lines (2968 loc) · 150 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 Icinga
*
* Copyright (c) 1999-2009 Ethan Galstad (egalstad@nagios.org)
* Last Modified: 06-23-2009
*
* 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/icinga.h"
#include "../include/broker.h"
#include "../include/perfdata.h"
/*#define DEBUG_CHECKS*/
/*#define DEBUG_HOST_CHECKS 1*/
#ifdef EMBEDDEDPERL
#include "../include/epn_icinga.h"
#endif
#ifdef USE_EVENT_BROKER
#include "../include/neberrors.h"
#endif
extern int sigshutdown;
extern int sigrestart;
extern char *temp_file;
extern char *temp_path;
extern char *check_result_path;
extern int interval_length;
extern int command_check_interval;
extern int log_initial_states;
extern int log_passive_checks;
extern int service_check_timeout;
extern int host_check_timeout;
extern int check_reaper_interval;
extern int max_check_reaper_time;
extern int use_aggressive_host_checking;
extern unsigned long cached_host_check_horizon;
extern unsigned long cached_service_check_horizon;
extern int enable_predictive_host_dependency_checks;
extern int enable_predictive_service_dependency_checks;
extern int soft_state_dependencies;
extern int currently_running_service_checks;
extern int currently_running_host_checks;
extern int accept_passive_service_checks;
extern int execute_service_checks;
extern int accept_passive_host_checks;
extern int execute_host_checks;
extern int obsess_over_services;
extern int obsess_over_hosts;
extern int translate_passive_host_checks;
extern int passive_host_checks_are_soft;
extern int check_service_freshness;
extern int check_host_freshness;
extern int additional_freshness_latency;
extern int max_host_check_spread;
extern int max_service_check_spread;
extern int use_large_installation_tweaks;
extern int free_child_process_memory;
extern int child_processes_fork_twice;
extern time_t program_start;
extern time_t event_start;
extern timed_event *event_list_low;
extern timed_event *event_list_low_tail;
extern host *host_list;
extern service *service_list;
extern servicedependency *servicedependency_list;
extern hostdependency *hostdependency_list;
extern unsigned long next_event_id;
extern unsigned long next_problem_id;
extern check_result check_result_info;
extern check_result *check_result_list;
extern pthread_t worker_threads[TOTAL_WORKER_THREADS];
extern unsigned long max_debug_file_size;
#ifdef EMBEDDEDPERL
extern int use_embedded_perl;
#endif
/******************************************************************/
/********************** CHECK REAPER FUNCTIONS ********************/
/******************************************************************/
/* reaps host and service check results */
int reap_check_results(void){
check_result *queued_check_result=NULL;
service *temp_service=NULL;
host *temp_host=NULL;
time_t current_time=0L;
time_t reaper_start_time=0L;
int reaped_checks=0;
log_debug_info(DEBUGL_FUNCTIONS,0,"reap_check_results() start\n");
log_debug_info(DEBUGL_CHECKS,0,"Starting to reap check results.\n");
/* get the start time */
time(&reaper_start_time);
/* process files in the check result queue */
process_check_result_queue(check_result_path);
/* read all check results that have come in... */
while((queued_check_result=read_check_result())){
reaped_checks++;
log_debug_info(DEBUGL_CHECKS,2,"Found a check result (#%d) to handle...\n",reaped_checks);
/* service check */
if(queued_check_result->object_check_type==SERVICE_CHECK){
/* make sure the service exists */
if((temp_service=find_service(queued_check_result->host_name,queued_check_result->service_description))==NULL){
logit(NSLOG_RUNTIME_WARNING,TRUE,"Warning: Check result queue contained results for service '%s' on host '%s', but the service could not be found! Perhaps you forgot to define the service in your config files?\n",queued_check_result->service_description,queued_check_result->host_name);
/* delete the file that contains the check results, as well as the ok-to-go file */
delete_check_result_file(queued_check_result->output_file);
/* free memory */
free_check_result(queued_check_result);
my_free(queued_check_result);
/* TODO - add new service definition automatically */
continue;
}
log_debug_info(DEBUGL_CHECKS,1,"Handling check result for service '%s' on host '%s'...\n",temp_service->description,temp_service->host_name);
/* process the check result */
handle_async_service_check_result(temp_service,queued_check_result);
}
/* host check */
else{
if((temp_host=find_host(queued_check_result->host_name))==NULL){
/* make sure the host exists */
logit(NSLOG_RUNTIME_WARNING,TRUE,"Warning: Check result queue contained results for host '%s', but the host could not be found! Perhaps you forgot to define the host in your config files?\n",queued_check_result->host_name);
/* delete the file that contains the check results, as well as the ok-to-go file */
delete_check_result_file(queued_check_result->output_file);
/* free memory */
free_check_result(queued_check_result);
my_free(queued_check_result);
/* TODO - add new host definition automatically */
continue;
}
log_debug_info(DEBUGL_CHECKS,1,"Handling check result for host '%s'...\n",temp_host->name);
/* process the check result */
handle_async_host_check_result_3x(temp_host,queued_check_result);
}
/* delete the file that contains the check results, as well as the ok-to-go file */
/* files can contain multiple check results - in this case, the file will be removed when the first check result is processed */
delete_check_result_file(queued_check_result->output_file);
log_debug_info(DEBUGL_CHECKS|DEBUGL_IPC,1,"Deleted check result file '%s'\n",queued_check_result->output_file);
/* free allocated memory */
free_check_result(queued_check_result);
my_free(queued_check_result);
/* break out if we've been here too long (max_check_reaper_time seconds) */
time(¤t_time);
if((int)(current_time-reaper_start_time)>max_check_reaper_time){
log_debug_info(DEBUGL_CHECKS,0,"Breaking out of check result reaper: max reaper time exceeded\n");
break;
}
/* bail out if we encountered a signal */
if(sigshutdown==TRUE || sigrestart==TRUE){
log_debug_info(DEBUGL_CHECKS,0,"Breaking out of check result reaper: signal encountered\n");
break;
}
}
log_debug_info(DEBUGL_CHECKS,0,"Finished reaping %d check results\n",reaped_checks);
log_debug_info(DEBUGL_FUNCTIONS,0,"reap_check_results() end\n");
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);
/* 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);
/*
logit(NSLOG_RUNTIME_WARNING,TRUE,"Warning: Service '%s' on host '%s' timeperiod check failed...\n",svc->description,svc->host_name);
logit(NSLOG_RUNTIME_WARNING,TRUE,"Current time: %s",ctime(¤t_time));
logit(NSLOG_RUNTIME_WARNING,TRUE,"Preferred time: %s",ctime(&preferred_time));
logit(NSLOG_RUNTIME_WARNING,TRUE,"Next valid time: %s",ctime(&next_valid_time));
*/
/* the service could not be rescheduled properly - set the next check time for next week */
/*if(time_is_valid==FALSE && next_valid_time==preferred_time){*/
/* UPDATED 08/12/09 EG to reflect proper timeperod check logic */
if(time_is_valid==FALSE && check_time_against_period(next_valid_time,svc->check_period_ptr)==ERROR){
/*
svc->next_check=(time_t)(next_valid_time+(60*60*24*365));
svc->should_be_scheduled=FALSE;
*/
svc->next_check=(time_t)(next_valid_time+(60*60*24*7));
logit(NSLOG_RUNTIME_WARNING,TRUE,"Warning: Check of service '%s' on host '%s' could not be rescheduled properly. Scheduling check for next week...\n",svc->description,svc->host_name);
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;
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 */
/* 10/19/07 EG - keep original check 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){
char *raw_command=NULL;
char *processed_command=NULL;
char output_buffer[MAX_INPUT_BUFFER]="";
char *temp_buffer=NULL;
struct timeval start_time,end_time;
pid_t pid=0;
int fork_error=FALSE;
int wait_result=0;
host *temp_host=NULL;
FILE *fp=NULL;
int pclose_result=0;
mode_t new_umask=077;
mode_t old_umask;
char *output_file=NULL;
double old_latency=0.0;
dbuf checkresult_dbuf;
int dbuf_chunk=1024;
#ifdef USE_EVENT_BROKER
int neb_result=OK;
#endif
#ifdef EMBEDDEDPERL
char fname[512]="";
char *args[5]={"",DO_CLEAN, "", "", NULL };
char *perl_plugin_output=NULL;
SV *plugin_hndlr_cr;
STRLEN n_a ;
int count ;
int use_epn=FALSE;
#ifdef aTHX
dTHX;
#endif
dSP;
#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,SERVICE_CHECK_ACTIVE,start_time,end_time,svc->service_check_command,svc->latency,0.0,0,FALSE,0,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){
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)
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 */
clear_volatile_macros();
grab_host_macros(temp_host);
grab_service_macros(svc);
/* get the raw command line */
get_raw_command_line(svc->check_command_ptr,svc->service_check_command,&raw_command,0);
if(raw_command==NULL){
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(raw_command,&processed_command,0);
if(processed_command==NULL){
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;
my_free(raw_command);
return ERROR;
}
/* get the command start time */
gettimeofday(&start_time,NULL);
/* increment number of service checks that are currently running... */
currently_running_service_checks++;
/* set the execution flag */
svc->is_executing=TRUE;
/* start save check info */
check_result_info.object_check_type=SERVICE_CHECK;
check_result_info.check_type=SERVICE_CHECK_ACTIVE;
check_result_info.check_options=check_options;
check_result_info.scheduled_check=scheduled_check;
check_result_info.reschedule_check=reschedule_check;
check_result_info.start_time=start_time;
check_result_info.finish_time=start_time;
check_result_info.early_timeout=FALSE;
check_result_info.exited_ok=TRUE;
check_result_info.return_code=STATE_OK;
check_result_info.output=NULL;
#ifdef USE_EVENT_BROKER
/* send data to event broker */
neb_result=broker_service_check(NEBTYPE_SERVICECHECK_INITIATE,NEBFLAG_NONE,NEBATTR_NONE,svc,SERVICE_CHECK_ACTIVE,start_time,end_time,svc->service_check_command,svc->latency,0.0,service_check_timeout,FALSE,0,processed_command,NULL);
/* neb module wants to override the service check - perhaps it will check the service itself */
if(neb_result==NEBERROR_CALLBACKOVERRIDE){
svc->latency=old_latency;
my_free(processed_command);
my_free(raw_command);
return OK;
}
#endif
/* open a temp file for storing check output */
old_umask=umask(new_umask);
asprintf(&output_file,"%s/checkXXXXXX",temp_path);
check_result_info.output_file_fd=mkstemp(output_file);
if(check_result_info.output_file_fd>=0)
check_result_info.output_file_fp=fdopen(check_result_info.output_file_fd,"w");
else{
check_result_info.output_file_fp=NULL;
check_result_info.output_file_fd=-1;
}
umask(old_umask);
log_debug_info(DEBUGL_CHECKS|DEBUGL_IPC,1,"Check result output will be written to '%s' (fd=%d)\n",output_file,check_result_info.output_file_fd);
/* finish save check info */
check_result_info.host_name=(char *)strdup(svc->host_name);
check_result_info.service_description=(char *)strdup(svc->description);
check_result_info.output_file=(check_result_info.output_file_fd<0 || output_file==NULL)?NULL:strdup(output_file);
/* free memory */
my_free(output_file);
/* write start of check result file */
/* if things go really bad later on down the line, the user will at least have a partial file to help debug missing output results */
if(check_result_info.output_file_fp){
fprintf(check_result_info.output_file_fp,"### Active Check Result File ###\n");
fprintf(check_result_info.output_file_fp,"file_time=%lu\n",(unsigned long)check_result_info.start_time.tv_sec);
fprintf(check_result_info.output_file_fp,"\n");
fprintf(check_result_info.output_file_fp,"### Icinga Service Check Result ###\n");
fprintf(check_result_info.output_file_fp,"# Time: %s",ctime(&check_result_info.start_time.tv_sec));
fprintf(check_result_info.output_file_fp,"host_name=%s\n",check_result_info.host_name);
fprintf(check_result_info.output_file_fp,"service_description=%s\n",check_result_info.service_description);
fprintf(check_result_info.output_file_fp,"check_type=%d\n",check_result_info.check_type);
fprintf(check_result_info.output_file_fp,"check_options=%d\n",check_result_info.check_options);
fprintf(check_result_info.output_file_fp,"scheduled_check=%d\n",check_result_info.scheduled_check);
fprintf(check_result_info.output_file_fp,"reschedule_check=%d\n",check_result_info.reschedule_check);
fprintf(check_result_info.output_file_fp,"latency=%f\n",svc->latency);
fprintf(check_result_info.output_file_fp,"start_time=%lu.%lu\n",check_result_info.start_time.tv_sec,check_result_info.start_time.tv_usec);
/* flush output or it'll get written again when we fork() */
fflush(check_result_info.output_file_fp);
}
/* initialize dynamic buffer for storing plugin output */
dbuf_init(&checkresult_dbuf,dbuf_chunk);
/* reset latency (permanent value will be set later) */
svc->latency=old_latency;
/* update check statistics */
update_check_stats((scheduled_check==TRUE)?ACTIVE_SCHEDULED_SERVICE_CHECK_STATS:ACTIVE_ONDEMAND_SERVICE_CHECK_STATS,start_time.tv_sec);
#ifdef EMBEDDEDPERL
/* get"filename" component of command */
strncpy(fname,processed_command,strcspn(processed_command," "));
fname[strcspn(processed_command," ")]='\x0';
/* should we use the embedded Perl interpreter to run this script? */
use_epn=file_uses_embedded_perl(fname);
/* if yes, do some initialization */
if(use_epn==TRUE){
log_debug_info(DEBUGL_CHECKS,1,"** Using Embedded Perl interpreter to run service check...\n");
args[0]=fname;
args[2]="";
if(strchr(processed_command,' ')==NULL)
args[3]="";
else
args[3]=processed_command+strlen(fname)+1;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(args[0],0)));
XPUSHs(sv_2mortal(newSVpv(args[1],0)));
XPUSHs(sv_2mortal(newSVpv(args[2],0)));
XPUSHs(sv_2mortal(newSVpv(args[3],0)));
PUTBACK;
/* call our perl interpreter to compile and optionally cache the command */
call_pv("Embed::Persistent::eval_file", G_SCALAR | G_EVAL);
SPAGAIN ;
if( SvTRUE(ERRSV) ){
/*
* if SvTRUE(ERRSV)
* write failure to IPC pipe
* return
*/
/* remove the top element of the Perl stack (undef) */
(void) POPs ;
pclose_result=STATE_UNKNOWN;
perl_plugin_output=SvPVX(ERRSV);
log_debug_info(DEBUGL_CHECKS,0,"Embedded Perl failed to compile %s, compile error %s - skipping plugin\n",fname,perl_plugin_output);
/* save plugin output */
if(perl_plugin_output!=NULL){
temp_buffer=escape_newlines(perl_plugin_output);
dbuf_strcat(&checkresult_dbuf,temp_buffer);
my_free(temp_buffer);
}
/* get the check finish time */
gettimeofday(&end_time,NULL);
/* record check result info */
check_result_info.exited_ok=FALSE;
check_result_info.return_code=pclose_result;
check_result_info.finish_time=end_time;
/* write check result to file */
if(check_result_info.output_file_fp){
fprintf(check_result_info.output_file_fp,"finish_time=%lu.%lu\n",check_result_info.finish_time.tv_sec,check_result_info.finish_time.tv_usec);
fprintf(check_result_info.output_file_fp,"early_timeout=%d\n",check_result_info.early_timeout);
fprintf(check_result_info.output_file_fp,"exited_ok=%d\n",check_result_info.exited_ok);
fprintf(check_result_info.output_file_fp,"return_code=%d\n",check_result_info.return_code);
fprintf(check_result_info.output_file_fp,"output=%s\n",(checkresult_dbuf.buf==NULL)?"(null)":checkresult_dbuf.buf);
/* close the temp file */
fclose(check_result_info.output_file_fp);
/* move check result to queue directory */
move_check_result_to_queue(check_result_info.output_file);
}
/* free memory */
dbuf_free(&checkresult_dbuf);
/* free check result memory */
free_check_result(&check_result_info);
return;
}
else{
plugin_hndlr_cr=newSVsv(POPs);
log_debug_info(DEBUGL_CHECKS,1,"Embedded Perl successfully compiled %s and returned code ref to plugin handler\n",fname);
PUTBACK ;
FREETMPS ;
LEAVE ;
}
}
#endif
/* plugin is a C plugin or a Perl plugin _without_ compilation errors */
/* fork a child process */
pid=fork();
/* an error occurred while trying to fork */
if(pid==-1){
fork_error=TRUE;
logit(NSLOG_RUNTIME_WARNING,TRUE,"Warning: The check of service '%s' on host '%s' could not be performed due to a fork() error: '%s'. The check will be rescheduled.\n",svc->description,svc->host_name,strerror(errno));
log_debug_info(DEBUGL_CHECKS,0,"Check of service '%s' on host '%s' could not be performed due to a fork() error: '%s'!\n",svc->description,svc->host_name,strerror(errno));
}
/* if we are in the child process... */
else if(pid==0){
/* set environment variables */
set_all_macro_environment_vars(TRUE);
/* ADDED 11/12/07 EG */
/* close external command file and shut down worker thread */
close_command_file();
/* fork again if we're not in a large installation */
if(child_processes_fork_twice==TRUE){
/* fork again... */
pid=fork();
/* an error occurred while trying to fork again */
if(pid==-1)
exit(STATE_UNKNOWN);
}
/* the grandchild (or child if large install tweaks are enabled) process should run the service check... */
if(pid==0 || child_processes_fork_twice==FALSE){
/* reset signal handling */
reset_sighandler();
/* become the process group leader */
setpgid(0,0);
/* catch term signals at this process level */
signal(SIGTERM,service_check_sighandler);
/* catch plugins that don't finish in a timely manner */
signal(SIGALRM,service_check_sighandler);
alarm(service_check_timeout);
/* disable rotation of the debug file */
max_debug_file_size=0L;
/******** BEGIN EMBEDDED PERL INTERPRETER EXECUTION ********/
#ifdef EMBEDDEDPERL
if(use_epn==TRUE){
/* execute our previously compiled script - from call_pv("Embed::Persistent::eval_file",..) */
/* NB. args[2] is _now_ a code ref (to the Perl subroutine corresp to the plugin) returned by eval_file() */
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(args[0],0)));
XPUSHs(sv_2mortal(newSVpv(args[1],0)));
XPUSHs(plugin_hndlr_cr);
XPUSHs(sv_2mortal(newSVpv(args[3],0)));
PUTBACK;
count=call_pv("Embed::Persistent::run_package", G_ARRAY);
SPAGAIN;
perl_plugin_output = POPpx ;
pclose_result = POPi ;
/* NOTE: 07/16/07 This has to be done before FREETMPS statement below, or the POPpx pointer will be invalid (Hendrik B.) */
/* get perl plugin output - escape newlines */
if(perl_plugin_output!=NULL){
temp_buffer=escape_newlines(perl_plugin_output);
dbuf_strcat(&checkresult_dbuf,temp_buffer);
my_free(temp_buffer);
}
PUTBACK;
FREETMPS;
LEAVE;
log_debug_info(DEBUGL_CHECKS,1,"Embedded Perl ran %s: return code=%d, plugin output=%s\n",fname,pclose_result,(perl_plugin_output==NULL)?"NULL":checkresult_dbuf.buf);
/* reset the alarm */
alarm(0);
/* get the check finish time */
gettimeofday(&end_time,NULL);
/* record check result info */
check_result_info.return_code=pclose_result;
check_result_info.finish_time=end_time;
/* write check result to file */
if(check_result_info.output_file_fp){
fprintf(check_result_info.output_file_fp,"finish_time=%lu.%lu\n",check_result_info.finish_time.tv_sec,check_result_info.finish_time.tv_usec);
fprintf(check_result_info.output_file_fp,"early_timeout=%d\n",check_result_info.early_timeout);
fprintf(check_result_info.output_file_fp,"exited_ok=%d\n",check_result_info.exited_ok);
fprintf(check_result_info.output_file_fp,"return_code=%d\n",check_result_info.return_code);
fprintf(check_result_info.output_file_fp,"output=%s\n",(checkresult_dbuf.buf==NULL)?"(null)":checkresult_dbuf.buf);
/* close the temp file */
fclose(check_result_info.output_file_fp);
/* move check result to queue directory */
move_check_result_to_queue(check_result_info.output_file);
}
/* free memory */
dbuf_free(&checkresult_dbuf);
/* free check result memory */
free_check_result(&check_result_info);
/* return with plugin exit status - not really necessary... */
_exit(pclose_result);
}
#endif
/******** END EMBEDDED PERL INTERPRETER EXECUTION ********/
/* run the plugin check command */
fp=popen(processed_command,"r");
if(fp==NULL)
_exit(STATE_UNKNOWN);
/* initialize buffer */
strcpy(output_buffer,"");
/* get all lines of plugin output - escape newlines */
while(fgets(output_buffer,sizeof(output_buffer)-1,fp)){
temp_buffer=escape_newlines(output_buffer);
dbuf_strcat(&checkresult_dbuf,temp_buffer);
my_free(temp_buffer);
}
/* close the process */
pclose_result=pclose(fp);
/* reset the alarm */
alarm(0);
/* get the check finish time */
gettimeofday(&end_time,NULL);
/* record check result info */
check_result_info.finish_time=end_time;
check_result_info.early_timeout=FALSE;
/* test for execution error */
if(pclose_result==-1){
pclose_result=STATE_UNKNOWN;
check_result_info.return_code=STATE_CRITICAL;
check_result_info.exited_ok=FALSE;
}
else{
if(WEXITSTATUS(pclose_result)==0 && WIFSIGNALED(pclose_result))
check_result_info.return_code=128+WTERMSIG(pclose_result);
else
check_result_info.return_code=WEXITSTATUS(pclose_result);
}
/* write check result to file */
if(check_result_info.output_file_fp){
fprintf(check_result_info.output_file_fp,"finish_time=%lu.%lu\n",check_result_info.finish_time.tv_sec,check_result_info.finish_time.tv_usec);
fprintf(check_result_info.output_file_fp,"early_timeout=%d\n",check_result_info.early_timeout);
fprintf(check_result_info.output_file_fp,"exited_ok=%d\n",check_result_info.exited_ok);
fprintf(check_result_info.output_file_fp,"return_code=%d\n",check_result_info.return_code);
fprintf(check_result_info.output_file_fp,"output=%s\n",(checkresult_dbuf.buf==NULL)?"(null)":checkresult_dbuf.buf);
/* close the temp file */
fclose(check_result_info.output_file_fp);
/* move check result to queue directory */
move_check_result_to_queue(check_result_info.output_file);
}
/* free memory */
dbuf_free(&checkresult_dbuf);
my_free(raw_command);
my_free(processed_command);
/* free check result memory */
free_check_result(&check_result_info);
/* return with plugin exit status - not really necessary... */
_exit(pclose_result);
}
/* NOTE: this code is never reached if large install tweaks are enabled... */
/* unset environment variables */
set_all_macro_environment_vars(FALSE);
/* free allocated memory */
/* this needs to be done last, so we don't free memory for variables before they're used above */
if(free_child_process_memory==TRUE)
free_memory();
/* parent exits immediately - grandchild process is inherited by the INIT process, so we have no zombie problem... */
_exit(STATE_OK);
}
/* else the parent should wait for the first child to return... */
else if(pid>0){
log_debug_info(DEBUGL_CHECKS,2,"Service check is executing in child process (pid=%lu)\n",(unsigned long)pid);
/* parent should close output file */
if(check_result_info.output_file_fp)
fclose(check_result_info.output_file_fp);
/* should this be done in first child process (after spawning grandchild) as well? */
/* free memory allocated for IPC functionality */
free_check_result(&check_result_info);
/* free memory */
my_free(raw_command);
my_free(processed_command);
/* wait for the first child to return */
/* don't do this if large install tweaks are enabled - we'll clean up children in event loop */
if(child_processes_fork_twice==TRUE)
wait_result=waitpid(pid,NULL,0);
/* removed 06/28/2000 - caused problems under AIX */
/*
result=WEXITSTATUS(wait_result);
if(result==STATE_UNKNOWN)
fork_error=TRUE;
*/
}
/* see if we were able to run the check... */
if(fork_error==TRUE)
return ERROR;
return OK;
}
/* handles asynchronous service check results */
int handle_async_service_check_result(service *temp_service, check_result *queued_check_result){
host *temp_host=NULL;
time_t next_service_check=0L;
time_t preferred_time=0L;
time_t next_valid_time=0L;
int reschedule_check=FALSE;
int state_change=FALSE;
int hard_state_change=FALSE;
int first_host_check_initiated=FALSE;
int route_result=HOST_UP;
time_t current_time=0L;
int state_was_logged=FALSE;
char *old_plugin_output=NULL;
char *temp_plugin_output=NULL;
char *temp_ptr=NULL;
servicedependency *temp_dependency=NULL;
objectlist *check_servicelist=NULL;
objectlist *servicelist_item=NULL;
service *master_service=NULL;
int run_async_check=TRUE;
int state_changes_use_cached_state=TRUE; /* TODO - 09/23/07 move this to a global variable */
int flapping_check_done=FALSE;
void *ptr=NULL;
log_debug_info(DEBUGL_FUNCTIONS,0,"handle_async_service_check_result()\n");
/* make sure we have what we need */
if(temp_service==NULL || queued_check_result==NULL)
return ERROR;
/* get the current time */
time(¤t_time);
log_debug_info(DEBUGL_CHECKS,0,"** Handling check result for service '%s' on host '%s'...\n",temp_service->description,temp_service->host_name);
log_debug_info(DEBUGL_CHECKS,1,"HOST: %s, SERVICE: %s, CHECK TYPE: %s, OPTIONS: %d, SCHEDULED: %s, RESCHEDULE: %s, EXITED OK: %s, RETURN CODE: %d, OUTPUT: %s\n",temp_service->host_name,temp_service->description,(queued_check_result->check_type==SERVICE_CHECK_ACTIVE)?"Active":"Passive",queued_check_result->check_options,(queued_check_result->scheduled_check==TRUE)?"Yes":"No",(queued_check_result->reschedule_check==TRUE)?"Yes":"No",(queued_check_result->exited_ok==TRUE)?"Yes":"No",queued_check_result->return_code,queued_check_result->output);
/* decrement the number of service checks still out there... */
if(queued_check_result->check_type==SERVICE_CHECK_ACTIVE && currently_running_service_checks>0)
currently_running_service_checks--;
/* skip this service check results if its passive and we aren't accepting passive check results */
if(queued_check_result->check_type==SERVICE_CHECK_PASSIVE){
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 ERROR;
}
if(temp_service->accept_passive_service_checks==FALSE){
log_debug_info(DEBUGL_CHECKS,0,"Discarding passive service check result because passive checks are disabled for this service.\n");
return ERROR;
}
}
/* clear the freshening flag (it would have been set if this service was determined to be stale) */
if(queued_check_result->check_options & CHECK_OPTION_FRESHNESS_CHECK)
temp_service->is_being_freshened=FALSE;
/* clear the execution flag if this was an active check */
if(queued_check_result->check_type==SERVICE_CHECK_ACTIVE)
temp_service->is_executing=FALSE;
/* DISCARD INVALID FRESHNESS CHECK RESULTS */
/* If a services goes stale, Icinga 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((queued_check_result->check_options & CHECK_OPTION_FRESHNESS_CHECK) && is_service_result_fresh(temp_service,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 OK;
}
/* check latency is passed to us */
temp_service->latency=queued_check_result->latency;
/* update the execution time for this check (millisecond resolution) */
temp_service->execution_time=(double)((double)(queued_check_result->finish_time.tv_sec-queued_check_result->start_time.tv_sec)+(double)((queued_check_result->finish_time.tv_usec-queued_check_result->start_time.tv_usec)/1000.0)/1000.0);
if(temp_service->execution_time<0.0)
temp_service->execution_time=0.0;
/* get the last check time */
temp_service->last_check=queued_check_result->start_time.tv_sec;
/* was this check passive or active? */
temp_service->check_type=(queued_check_result->check_type==SERVICE_CHECK_ACTIVE)?SERVICE_CHECK_ACTIVE:SERVICE_CHECK_PASSIVE;
/* update check statistics for passive checks */
if(queued_check_result->check_type==SERVICE_CHECK_PASSIVE)
update_check_stats(PASSIVE_SERVICE_CHECK_STATS,queued_check_result->start_time.tv_sec);
/* should we reschedule the next service check? NOTE: This may be overridden later... */
reschedule_check=queued_check_result->reschedule_check;
/* save the old service status info */
temp_service->last_state=temp_service->current_state;
/* save old plugin output */
if(temp_service->plugin_output)
old_plugin_output=(char *)strdup(temp_service->plugin_output);
/* clear the old plugin output and perf data buffers */
my_free(temp_service->plugin_output);
my_free(temp_service->long_plugin_output);
my_free(temp_service->perf_data);
/* if there was some error running the command, just skip it (this shouldn't be happening) */
if(queued_check_result->exited_ok==FALSE){