Icinga-core 1.4.0
next gen monitoring
base/icinga.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002  *
00003  * ICINGA.C - Core Program Code For Icinga
00004  *
00005  * Program: Icinga
00006  * Version: 1.4.0
00007  * License: GPL
00008  * Copyright (c) 1999-2009 Ethan Galstad (http://www.nagios.org)
00009  * Copyright (c) 2009-2011 Nagios Core Development Team and Community Contributors
00010  * Copyright (c) 2009-2011 Icinga Development Team (http://www.icinga.org)
00011  *
00012  * Description:
00013  *
00014  * Icinga (Nagios) is a network monitoring tool that will check hosts and services
00015  * that you specify.  It has the ability to notify contacts via email, pager,
00016  * or other user-defined methods when a service or host goes down and
00017  * recovers.  Service and host monitoring is done through the use of external
00018  * plugins which can be developed independently of Icinga.
00019  *
00020  * License:
00021  *
00022  * This program is free software; you can redistribute it and/or modify
00023  * it under the terms of the GNU General Public License version 2 as
00024  * published by the Free Software Foundation.
00025  *
00026  * This program is distributed in the hope that it will be useful,
00027  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00028  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00029  * GNU General Public License for more details.
00030  *
00031  * You should have received a copy of the GNU General Public License
00032  * along with this program; if not, write to the Free Software
00033  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00034  *
00035  *****************************************************************************/
00036 
00037 #include "../include/config.h"
00038 #include "../include/common.h"
00039 #include "../include/objects.h"
00040 #include "../include/comments.h"
00041 #include "../include/downtime.h"
00042 #include "../include/statusdata.h"
00043 #include "../include/macros.h"
00044 #include "../include/icinga.h"
00045 #include "../include/sretention.h"
00046 #include "../include/perfdata.h"
00047 #include "../include/broker.h"
00048 #include "../include/nebmods.h"
00049 #include "../include/nebmodules.h"
00050 
00051 /* make sure gcc3 won't hit here */
00052 #ifndef GCCTOOOLD
00053 #include "../include/profiler.h"
00054 #endif
00055 
00056 /*#define DEBUG_MEMORY 1*/
00057 #ifdef DEBUG_MEMORY
00058 #include <mcheck.h>
00059 #endif
00060 
00061 
00062 char            *config_file=NULL;
00063 char            *log_file=NULL;
00064 char            *command_file=NULL;
00065 char            *temp_file=NULL;
00066 char            *temp_path=NULL;
00067 char            *check_result_path=NULL;
00068 char            *lock_file=NULL;
00069 char            *log_archive_path=NULL;
00070 char            *p1_file=NULL;    /**** EMBEDDED PERL ****/
00071 char            *auth_file=NULL;  /**** EMBEDDED PERL INTERPRETER AUTH FILE ****/
00072 char            *nagios_user=NULL;
00073 char            *nagios_group=NULL;
00074 
00075 char            *global_host_event_handler=NULL;
00076 char            *global_service_event_handler=NULL;
00077 command         *global_host_event_handler_ptr=NULL;
00078 command         *global_service_event_handler_ptr=NULL;
00079 
00080 char            *ocsp_command=NULL;
00081 char            *ochp_command=NULL;
00082 command         *ocsp_command_ptr=NULL;
00083 command         *ochp_command_ptr=NULL;
00084 
00085 char            *illegal_object_chars=NULL;
00086 char            *illegal_output_chars=NULL;
00087 
00088 int             use_regexp_matches=FALSE;
00089 int             use_true_regexp_matching=FALSE;
00090 
00091 int             use_daemon_log=DEFAULT_USE_DAEMON_LOG;
00092 int             use_syslog=DEFAULT_USE_SYSLOG;
00093 int             use_syslog_local_facility=DEFAULT_USE_SYSLOG_LOCAL_FACILITY;
00094 int             syslog_local_facility=DEFAULT_SYSLOG_LOCAL_FACILITY;
00095 int             log_notifications=DEFAULT_NOTIFICATION_LOGGING;
00096 int             log_service_retries=DEFAULT_LOG_SERVICE_RETRIES;
00097 int             log_host_retries=DEFAULT_LOG_HOST_RETRIES;
00098 int             log_event_handlers=DEFAULT_LOG_EVENT_HANDLERS;
00099 int             log_initial_states=DEFAULT_LOG_INITIAL_STATES;
00100 int             log_current_states=DEFAULT_LOG_CURRENT_STATES;
00101 int             log_external_commands=DEFAULT_LOG_EXTERNAL_COMMANDS;
00102 int             log_external_commands_user=DEFAULT_LOG_EXTERNAL_COMMANDS_USER;
00103 int             log_passive_checks=DEFAULT_LOG_PASSIVE_CHECKS;
00104 int             log_long_plugin_output=DEFAULT_LOG_LONG_PLUGIN_OUTPUT;
00105 
00106 unsigned long   logging_options=0;
00107 unsigned long   syslog_options=0;
00108 
00109 int             service_check_timeout=DEFAULT_SERVICE_CHECK_TIMEOUT;
00110 int             service_check_timeout_state=STATE_CRITICAL;
00111 int             host_check_timeout=DEFAULT_HOST_CHECK_TIMEOUT;
00112 int             event_handler_timeout=DEFAULT_EVENT_HANDLER_TIMEOUT;
00113 int             notification_timeout=DEFAULT_NOTIFICATION_TIMEOUT;
00114 int             ocsp_timeout=DEFAULT_OCSP_TIMEOUT;
00115 int             ochp_timeout=DEFAULT_OCHP_TIMEOUT;
00116 
00117 double          sleep_time=DEFAULT_SLEEP_TIME;
00118 int             interval_length=DEFAULT_INTERVAL_LENGTH;
00119 int             service_inter_check_delay_method=ICD_SMART;
00120 int             host_inter_check_delay_method=ICD_SMART;
00121 int             service_interleave_factor_method=ILF_SMART;
00122 int             max_host_check_spread=DEFAULT_HOST_CHECK_SPREAD;
00123 int             max_service_check_spread=DEFAULT_SERVICE_CHECK_SPREAD;
00124 
00125 int             command_check_interval=DEFAULT_COMMAND_CHECK_INTERVAL;
00126 int             check_reaper_interval=DEFAULT_CHECK_REAPER_INTERVAL;
00127 int             max_check_reaper_time=DEFAULT_MAX_REAPER_TIME;
00128 int             service_freshness_check_interval=DEFAULT_FRESHNESS_CHECK_INTERVAL;
00129 int             host_freshness_check_interval=DEFAULT_FRESHNESS_CHECK_INTERVAL;
00130 int             auto_rescheduling_interval=DEFAULT_AUTO_RESCHEDULING_INTERVAL;
00131 
00132 int             check_external_commands=DEFAULT_CHECK_EXTERNAL_COMMANDS;
00133 int             check_orphaned_services=DEFAULT_CHECK_ORPHANED_SERVICES;
00134 int             check_orphaned_hosts=DEFAULT_CHECK_ORPHANED_HOSTS;
00135 int             check_service_freshness=DEFAULT_CHECK_SERVICE_FRESHNESS;
00136 int             check_host_freshness=DEFAULT_CHECK_HOST_FRESHNESS;
00137 int             auto_reschedule_checks=DEFAULT_AUTO_RESCHEDULE_CHECKS;
00138 int             auto_rescheduling_window=DEFAULT_AUTO_RESCHEDULING_WINDOW;
00139 
00140 int             additional_freshness_latency=DEFAULT_ADDITIONAL_FRESHNESS_LATENCY;
00141 
00142 time_t          last_command_check=0L;
00143 time_t          last_command_status_update=0L;
00144 time_t          last_log_rotation=0L;
00145 
00146 int             use_aggressive_host_checking=DEFAULT_AGGRESSIVE_HOST_CHECKING;
00147 unsigned long   cached_host_check_horizon=DEFAULT_CACHED_HOST_CHECK_HORIZON;
00148 unsigned long   cached_service_check_horizon=DEFAULT_CACHED_SERVICE_CHECK_HORIZON;
00149 int             enable_predictive_host_dependency_checks=DEFAULT_ENABLE_PREDICTIVE_HOST_DEPENDENCY_CHECKS;
00150 int             enable_predictive_service_dependency_checks=DEFAULT_ENABLE_PREDICTIVE_SERVICE_DEPENDENCY_CHECKS;
00151 
00152 int             soft_state_dependencies=FALSE;
00153 
00154 int             retain_state_information=FALSE;
00155 int             retention_update_interval=DEFAULT_RETENTION_UPDATE_INTERVAL;
00156 int             use_retained_program_state=TRUE;
00157 int             use_retained_scheduling_info=FALSE;
00158 int             retention_scheduling_horizon=DEFAULT_RETENTION_SCHEDULING_HORIZON;
00159 unsigned long   modified_host_process_attributes=MODATTR_NONE;
00160 unsigned long   modified_service_process_attributes=MODATTR_NONE;
00161 unsigned long   retained_host_attribute_mask=0L;
00162 unsigned long   retained_service_attribute_mask=0L;
00163 unsigned long   retained_contact_host_attribute_mask=0L;
00164 unsigned long   retained_contact_service_attribute_mask=0L;
00165 unsigned long   retained_process_host_attribute_mask=0L;
00166 unsigned long   retained_process_service_attribute_mask=0L;
00167 
00168 unsigned long   next_comment_id=0L;
00169 unsigned long   next_downtime_id=0L;
00170 unsigned long   next_event_id=0L;
00171 unsigned long   next_problem_id=0L;
00172 unsigned long   next_notification_id=0L;
00173 
00174 int             log_rotation_method=LOG_ROTATION_NONE;
00175 
00176 int             sigshutdown=FALSE;
00177 int             sigrestart=FALSE;
00178 char            *sigs[35]={"EXIT","HUP","INT","QUIT","ILL","TRAP","ABRT","BUS","FPE","KILL","USR1","SEGV","USR2","PIPE","ALRM","TERM","STKFLT","CHLD","CONT","STOP","TSTP","TTIN","TTOU","URG","XCPU","XFSZ","VTALRM","PROF","WINCH","IO","PWR","UNUSED","ZERR","DEBUG",(char *)NULL};
00179 int             caught_signal=FALSE;
00180 int             sig_id=0;
00181 
00182 int             restarting=FALSE;
00183 
00184 int             verify_config=FALSE;
00185 int             verify_object_relationships=TRUE;
00186 int             verify_circular_paths=TRUE;
00187 int             test_scheduling=FALSE;
00188 int             show_schedule=FALSE;
00189 int             precache_objects=FALSE;
00190 int             use_precached_objects=FALSE;
00191 
00192 int             daemon_mode=FALSE;
00193 int             daemon_dumps_core=TRUE;
00194 
00195 int             max_parallel_service_checks=DEFAULT_MAX_PARALLEL_SERVICE_CHECKS;
00196 int             currently_running_service_checks=0;
00197 int             currently_running_host_checks=0;
00198 
00199 time_t          program_start=0L;
00200 time_t          event_start=0L;
00201 int             nagios_pid=0;
00202 int             enable_notifications=TRUE;
00203 int             execute_service_checks=TRUE;
00204 int             accept_passive_service_checks=TRUE;
00205 int             execute_host_checks=TRUE;
00206 int             accept_passive_host_checks=TRUE;
00207 int             enable_event_handlers=TRUE;
00208 int             obsess_over_services=FALSE;
00209 int             obsess_over_hosts=FALSE;
00210 int             enable_failure_prediction=TRUE;
00211 
00212 int             translate_passive_host_checks=DEFAULT_TRANSLATE_PASSIVE_HOST_CHECKS;
00213 int             passive_host_checks_are_soft=DEFAULT_PASSIVE_HOST_CHECKS_SOFT;
00214 
00215 int             aggregate_status_updates=TRUE;
00216 int             status_update_interval=DEFAULT_STATUS_UPDATE_INTERVAL;
00217 
00218 int             time_change_threshold=DEFAULT_TIME_CHANGE_THRESHOLD;
00219 
00220 unsigned long   event_broker_options=BROKER_NOTHING;
00221 
00222 int             process_performance_data=DEFAULT_PROCESS_PERFORMANCE_DATA;
00223 
00224 int             enable_flap_detection=DEFAULT_ENABLE_FLAP_DETECTION;
00225 
00226 double          low_service_flap_threshold=DEFAULT_LOW_SERVICE_FLAP_THRESHOLD;
00227 double          high_service_flap_threshold=DEFAULT_HIGH_SERVICE_FLAP_THRESHOLD;
00228 double          low_host_flap_threshold=DEFAULT_LOW_HOST_FLAP_THRESHOLD;
00229 double          high_host_flap_threshold=DEFAULT_HIGH_HOST_FLAP_THRESHOLD;
00230 
00231 int             use_large_installation_tweaks=DEFAULT_USE_LARGE_INSTALLATION_TWEAKS;
00232 int             enable_environment_macros=TRUE;
00233 int             free_child_process_memory=-1;
00234 int             child_processes_fork_twice=-1;
00235 
00236 int             enable_embedded_perl=DEFAULT_ENABLE_EMBEDDED_PERL;
00237 int             use_embedded_perl_implicitly=DEFAULT_USE_EMBEDDED_PERL_IMPLICITLY;
00238 int             embedded_perl_initialized=FALSE;
00239 
00240 int             stalking_event_handlers_for_hosts=DEFAULT_STALKING_EVENT_HANDLERS_FOR_HOSTS;
00241 int             stalking_event_handlers_for_services=DEFAULT_STALKING_EVENT_HANDLERS_FOR_SERVICES;
00242 
00243 int             date_format=DATE_FORMAT_US;
00244 char            *use_timezone=NULL;
00245 
00246 int             allow_empty_hostgroup_assignment=DEFAULT_ALLOW_EMPTY_HOSTGROUP_ASSIGNMENT;
00247 
00248 int             command_file_fd;
00249 FILE            *command_file_fp;
00250 int             command_file_created=FALSE;
00251 
00252 /* make sure gcc3 won't hit here */
00253 #ifndef GCCTOOOLD
00254 int             event_profiling_enabled=FALSE;
00255 #endif
00256 
00257 extern contact         *contact_list;
00258 extern contactgroup    *contactgroup_list;
00259 extern hostgroup       *hostgroup_list;
00260 extern command         *command_list;
00261 extern timeperiod      *timeperiod_list;
00262 extern serviceescalation *serviceescalation_list;
00263 extern module          *module_list;
00264 
00265 notification    *notification_list;
00266 
00267 check_result    check_result_info;
00268 check_result    *check_result_list=NULL;
00269 unsigned long   max_check_result_file_age=DEFAULT_MAX_CHECK_RESULT_AGE;
00270 
00271 dbuf            check_result_dbuf;
00272 
00273 circular_buffer external_command_buffer;
00274 circular_buffer check_result_buffer;
00275 pthread_t       worker_threads[TOTAL_WORKER_THREADS];
00276 int             external_command_buffer_slots=DEFAULT_EXTERNAL_COMMAND_BUFFER_SLOTS;
00277 
00278 check_stats     check_statistics[MAX_CHECK_STATS_TYPES];
00279 
00280 char            *debug_file;
00281 int             debug_level=DEFAULT_DEBUG_LEVEL;
00282 int             debug_verbosity=DEFAULT_DEBUG_VERBOSITY;
00283 unsigned long   max_debug_file_size=DEFAULT_MAX_DEBUG_FILE_SIZE;
00284 
00285 int dummy;      /* reduce compiler warnings */
00286 
00287 
00288 /* Following main() declaration required by older versions of Perl ut 5.00503 */
00289 int main(int argc, char **argv, char **env){
00290         int result;
00291         int error=FALSE;
00292         char *buffer=NULL;
00293         char *dummy_c=NULL;
00294         int display_license=FALSE;
00295         int display_help=FALSE;
00296         int c=0;
00297         struct tm *tm, tm_s;
00298         time_t now;
00299         char datestring[256];
00300         icinga_macros *mac;
00301 
00302 #ifdef HAVE_GETOPT_H
00303         int option_index=0;
00304         static struct option long_options[]=
00305         {
00306                 {"help",no_argument,0,'h'},
00307                 {"version",no_argument,0,'V'},
00308                 {"license",no_argument,0,'V'},
00309                 {"verify-config",no_argument,0,'v'},
00310                 {"daemon",no_argument,0,'d'},
00311                 {"test-scheduling",no_argument,0,'s'},
00312                 {"show-scheduling",no_argument,0,'S'},
00313                 {"dont-verify-objects",no_argument,0,'o'},
00314                 {"dont-verify-paths",no_argument,0,'x'},
00315                 {"precache-objects",no_argument,0,'p'},
00316                 {"use-precached-objects",no_argument,0,'u'},
00317                 {0,0,0,0}
00318         };
00319 #endif
00320 
00321         mac = get_global_macros();
00322 
00323         /* make sure we have the correct number of command line arguments */
00324         if(argc<2)
00325                 error=TRUE;
00326 
00327 
00328         /* get all command line arguments */
00329         while(1){
00330 
00331 #ifdef HAVE_GETOPT_H
00332                 c=getopt_long(argc,argv,"+hVvdsoxpuS",long_options,&option_index);
00333 #else
00334                 c=getopt(argc,argv,"+hVvdsoxpuS");
00335 #endif
00336 
00337                 if(c==-1 || c==EOF)
00338                         break;
00339 
00340                 switch(c){
00341 
00342                 case '?': /* usage */
00343                 case 'h':
00344                         display_help=TRUE;
00345                         break;
00346 
00347                 case 'V': /* version */
00348                         display_license=TRUE;
00349                         break;
00350 
00351                 case 'v': /* verify */
00352                         verify_config=TRUE;
00353                         break;
00354 
00355                 case 's': /* scheduling check */
00356                         test_scheduling=TRUE;
00357                         break;
00358 
00359                 case 'S': /* scheduling check and show queue*/
00360                         show_schedule=TRUE;
00361                         test_scheduling=TRUE;
00362                         break;
00363 
00364                 case 'd': /* daemon mode */
00365                         daemon_mode=TRUE;
00366                         break;
00367 
00368                 case 'o': /* don't verify objects */
00369                         /*
00370                         verify_object_relationships=FALSE;
00371                         */
00372                         break;
00373 
00374                 case 'x': /* don't verify circular paths */
00375                         verify_circular_paths=FALSE;
00376                         break;
00377 
00378                 case 'p': /* precache object config */
00379                         precache_objects=TRUE;
00380                         break;
00381 
00382                 case 'u': /* use precached object config */
00383                         use_precached_objects=TRUE;
00384                         break;
00385 
00386                 default:
00387                         break;
00388                         }
00389 
00390                 }
00391 
00392         /* make sure we have the right combination of arguments */
00393         if(precache_objects==TRUE && (test_scheduling==FALSE && verify_config==FALSE)){
00394                 error=TRUE;
00395                 display_help=TRUE;
00396                 }
00397 
00398 #ifdef DEBUG_MEMORY
00399         mtrace();
00400 #endif
00401 
00402         if(daemon_mode==FALSE){
00403                 printf("\n%s %s\n", PROGRAM_NAME ,PROGRAM_VERSION);
00404                 printf("Copyright (c) 2009-2011 Icinga Development Team (http://www.icinga.org)\n");
00405                 printf("Copyright (c) 2009-2011 Nagios Core Development Team and Community Contributors\n");
00406                 printf("Copyright (c) 1999-2009 Ethan Galstad\n");
00407                 printf("Last Modified: %s\n",PROGRAM_MODIFICATION_DATE);
00408                 printf("License: GPL\n\n");
00409                 }
00410 
00411         /* just display the license */
00412         if(display_license==TRUE){
00413 
00414                 printf("This program is free software; you can redistribute it and/or modify\n");
00415                 printf("it under the terms of the GNU General Public License version 2 as\n");
00416                 printf("published by the Free Software Foundation.\n\n");
00417                 printf("This program is distributed in the hope that it will be useful,\n");
00418                 printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
00419                 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
00420                 printf("GNU General Public License for more details.\n\n");
00421                 printf("You should have received a copy of the GNU General Public License\n");
00422                 printf("along with this program; if not, write to the Free Software\n");
00423                 printf("Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n\n");
00424 
00425                 exit(OK);
00426                 }
00427 
00428         /* make sure we got the main config file on the command line... */
00429         if(optind>=argc)
00430                 error=TRUE;
00431 
00432         /* if there are no command line options (or if we encountered an error), print usage */
00433         if(error==TRUE || display_help==TRUE){
00434 
00435                 printf("Usage: %s [options] <main_config_file>\n",argv[0]);
00436                 printf("\n");
00437                 printf("Options:\n");
00438                 printf("\n");
00439                 printf("  -v, --verify-config          Verify all configuration data\n");
00440                 printf("  -s, --test-scheduling        Shows projected/recommended check scheduling and other\n");
00441                 printf("                               diagnostic info based on the current configuration files.\n");
00442                 printf("  -S, --show-scheduling        Same as -s, but also show the scheduling queue\n");
00443                 /*printf("  -o, --dont-verify-objects    Don't verify object relationships - USE WITH CAUTION!\n");*/
00444                 printf("  -x, --dont-verify-paths      Don't check for circular object paths - USE WITH CAUTION!\n");
00445                 printf("  -p, --precache-objects       Precache object configuration - use with -v or -s options\n");
00446                 printf("  -u, --use-precached-objects  Use precached object config file\n");
00447                 printf("  -d, --daemon                 Starts %s in daemon mode, instead of as a foreground process\n", PROGRAM_NAME);
00448                 printf("\n");
00449                 printf("Visit the %s website at http://www.icinga.org/ for bug fixes, new\n", PROGRAM_NAME);
00450                 printf("releases, online documentation, FAQs, information on subscribing to\n");
00451                 printf("the mailing lists, and commercial support options for %s.\n", PROGRAM_NAME);
00452                 printf("\n");
00453 
00454                 exit(ERROR);
00455                 }
00456 
00457 
00458         /* config file is last argument specified */
00459         config_file=(char *)strdup(argv[optind]);
00460         if(config_file==NULL){
00461                 printf("Error allocating memory.\n");
00462                 exit(ERROR);
00463                 }
00464 
00465         /* make sure the config file uses an absolute path */
00466         if(config_file[0]!='/'){
00467 
00468                 /* save the name of the config file */
00469                 buffer=(char *)strdup(config_file);
00470 
00471                 /* reallocate a larger chunk of memory */
00472                 config_file=(char *)realloc(config_file,MAX_FILENAME_LENGTH);
00473                 if(config_file==NULL){
00474                         printf("Error allocating memory.\n");
00475                         exit(ERROR);
00476                         }
00477 
00478                 /* get absolute path of current working directory */
00479                 dummy_c=getcwd(config_file,MAX_FILENAME_LENGTH);
00480 
00481                 /* append a forward slash */
00482                 strncat(config_file,"/",1);
00483                 config_file[MAX_FILENAME_LENGTH-1]='\x0';
00484 
00485                 /* append the config file to the path */
00486                 strncat(config_file,buffer,MAX_FILENAME_LENGTH-strlen(config_file)-1);
00487                 config_file[MAX_FILENAME_LENGTH-1]='\x0';
00488 
00489                 my_free(buffer);
00490                 }
00491 
00492 
00493         /* we're just verifying the configuration... */
00494         if(verify_config==TRUE){
00495 
00496                 /* reset program variables */
00497                 reset_variables();
00498 
00499                 printf("Reading configuration data...\n");
00500 
00501                 /* read in the configuration files (main config file, resource and object config files) */
00502                 if((result=read_main_config_file(config_file))==OK){
00503 
00504                         printf("   Read main config file okay...\n");
00505 
00506                         /* drop privileges */
00507                         if((result=drop_privileges(nagios_user,nagios_group))==ERROR)
00508                                 printf("   Failed to drop privileges.  Aborting.");
00509                         else{
00510                                 /* read object config files */
00511                                 if((result=read_all_object_data(config_file))==OK)
00512                                         printf("   Read object config files okay...\n");
00513                                 else
00514                                         printf("   Error processing object config files!\n");
00515                                 }
00516                         }
00517                 else
00518                         printf("   Error processing main config file!\n\n");
00519 
00520                 printf("\n");
00521 
00522                 /* there was a problem reading the config files */
00523                 if(result!=OK){
00524 
00525                         /* if the config filename looks fishy, warn the user */
00526                         if(!strstr(config_file,"nagios.cfg") || !strstr(config_file,"icinga.cfg")){
00527                                 printf("\n***> The name of the main configuration file looks suspicious...\n");
00528                                 printf("\n");
00529                                 printf("     Make sure you are specifying the name of the MAIN configuration file on\n");
00530                                 printf("     the command line and not the name of another configuration file.  The\n");
00531                                 printf("     main configuration file is typically '/usr/local/icinga/etc/icinga.cfg'\n");
00532                                 }
00533 
00534                         printf("\n***> One or more problems was encountered while processing the config files...\n");
00535                         printf("\n");
00536                         printf("     Check your configuration file(s) to ensure that they contain valid\n");
00537                         printf("     directives and data definitions.  If you are upgrading from a previous\n");
00538                         printf("     version of %s, you should be aware that some variables/definitions\n", PROGRAM_NAME);
00539                         printf("     may have been removed or modified in this version.  Make sure to read\n");
00540                         printf("     the HTML documentation regarding the config files, as well as the\n");
00541                         printf("     'Whats New' section to find out what has changed.\n\n");
00542                         }
00543 
00544                 /* the config files were okay, so run the pre-flight check */
00545                 else{
00546 
00547                         printf("Running pre-flight check on configuration data...\n\n");
00548 
00549                         /* run the pre-flight check to make sure things look okay... */
00550                         result=pre_flight_check();
00551 
00552                         if(result==OK)
00553                                 printf("\nThings look okay - No serious problems were detected during the pre-flight check\n");
00554                         else{
00555                                 printf("\n***> One or more problems was encountered while running the pre-flight check...\n");
00556                                 printf("\n");
00557                                 printf("     Check your configuration file(s) to ensure that they contain valid\n");
00558                                 printf("     directives and data definitions.  If you are upgrading from a previous\n");
00559                                 printf("     version of %s, you should be aware that some variables/definitions\n", PROGRAM_NAME);
00560                                 printf("     may have been removed or modified in this version.  Make sure to read\n");
00561                                 printf("     the HTML documentation regarding the config files, as well as the\n");
00562                                 printf("     'Whats New' section to find out what has changed.\n\n");
00563                                 }
00564                         }
00565 
00566                 /* clean up after ourselves */
00567                 cleanup();
00568 
00569                 /* free config_file */
00570                 my_free(config_file);
00571 
00572                 /* exit */
00573                 exit(result);
00574                 }
00575 
00576 
00577         /* we're just testing scheduling... */
00578         else if(test_scheduling==TRUE){
00579 
00580                 /* reset program variables */
00581                 reset_variables();
00582 
00583                 /* read in the configuration files (main config file and all host config files) */
00584                 result=read_main_config_file(config_file);
00585 
00586                 /* drop privileges */
00587                 if(result==OK)
00588                         if((result=drop_privileges(nagios_user,nagios_group))==ERROR)
00589                                 printf("Failed to drop privileges.  Aborting.");
00590 
00591                 /* read object config files */
00592                 if(result==OK)
00593                         result=read_all_object_data(config_file);
00594 
00595                 /* read initial service and host state information */
00596                 if(result==OK){
00597                         initialize_retention_data(config_file);
00598                         read_initial_state_information();
00599                         }
00600 
00601                 if(result!=OK)
00602                         printf("***> One or more problems was encountered while reading configuration data...\n");
00603 
00604                 /* run the pre-flight check to make sure everything looks okay */
00605                 if(result==OK){
00606                         if((result=pre_flight_check())!=OK)
00607                                 printf("***> One or more problems was encountered while running the pre-flight check...\n");
00608                         }
00609 
00610                 if(result==OK){
00611 
00612                         /* initialize the event timing loop */
00613                         init_timing_loop();
00614 
00615                         /* display schedule */
00616                         if(show_schedule == TRUE)
00617                                 display_schedule();
00618 
00619                         /* display scheduling information */
00620                         display_scheduling_info();
00621 
00622                         if(precache_objects==TRUE){
00623                                 printf("\n");
00624                                 printf("OBJECT PRECACHING\n");
00625                                 printf("-----------------\n");
00626                                 printf("Object config files were precached.\n");
00627                                 }
00628                         }
00629 
00630 #undef TEST_TIMEPERIODS
00631 #ifdef TEST_TIMEPERIODS
00632                 /* DO SOME TIMEPERIOD TESTING - ADDED 08/11/2009 */
00633                 time_t now, pref_time, valid_time;
00634                 timeperiod *tp;
00635                 tp=find_timeperiod("247_exclusion");
00636                 time(&now);
00637                 pref_time=now;
00638                 get_next_valid_time(pref_time,&valid_time,tp);
00639                 printf("=====\n");
00640                 printf("CURRENT:   %lu = %s",(unsigned long)now,ctime(&now));
00641                 printf("PREFERRED: %lu = %s",(unsigned long)pref_time,ctime(&pref_time));
00642                 printf("NEXT:      %lu = %s",(unsigned long)valid_time,ctime(&valid_time));
00643                 printf("=====\n");
00644 #endif
00645 
00646                 /* clean up after ourselves */
00647                 cleanup();
00648 
00649                 /* exit */
00650                 exit(result);
00651                 }
00652 
00653 
00654         /* else start to monitor things... */
00655         else{
00656 
00657 /* make sure gcc3 won't hit here */
00658 #ifndef GCCTOOOLD
00659         /* This is Sparta! */
00660         if(event_profiling_enabled==TRUE)
00661                 profiler_init();
00662 #endif
00663 
00664                 /* keep monitoring things until we get a shutdown command */
00665                 do{
00666 
00667                         /* reset program variables */
00668                         reset_variables();
00669 
00670                         /* get PID */
00671                         nagios_pid=(int)getpid();
00672 
00673                         /* read in the configuration files (main and resource config files) */
00674                         result=read_main_config_file(config_file);
00675 
00676                         /* we need to read the modules in the first place as object configuration before neb modules are initialized/loaded */
00677                         result=read_object_config_data(config_file,READ_MODULES,FALSE,FALSE);
00678 
00679                         /* add modules to neb */
00680                         result=add_module_objects_to_neb();
00681 
00682                         /* NOTE 11/06/07 EG moved to after we read config files, as user may have overridden timezone offset */
00683                         /* get program (re)start time and save as macro */
00684                         program_start=time(NULL);
00685                         my_free(mac->x[MACRO_PROCESSSTARTTIME]);
00686                         dummy=asprintf(&mac->x[MACRO_PROCESSSTARTTIME],"%lu",(unsigned long)program_start);
00687 
00688                         /* open debug log */
00689                         open_debug_log();
00690 
00691                         /* drop privileges */
00692                         if(drop_privileges(nagios_user,nagios_group)==ERROR){
00693 
00694                                 logit(NSLOG_PROCESS_INFO | NSLOG_RUNTIME_ERROR | NSLOG_CONFIG_ERROR,TRUE,"Failed to drop privileges.  Aborting.");
00695 
00696                                 cleanup();
00697                                 exit(ERROR);
00698                                 }
00699 
00700 #ifdef USE_EVENT_BROKER
00701                         /* initialize modules */
00702                         neb_init_modules();
00703                         neb_init_callback_list();
00704 #endif
00705 
00706                         /* this must be logged after we read config data, as user may have changed location of main log file */
00707                         logit(NSLOG_PROCESS_INFO, TRUE, "%s %s starting... (PID=%d)\n", PROGRAM_NAME, PROGRAM_VERSION, (int)getpid() );
00708 
00709                         /* log the local time - may be different than clock time due to timezone offset */
00710                         now=time(NULL);
00711                         tm=localtime_r(&now, &tm_s);
00712                         strftime(datestring,sizeof(datestring),"%a %b %d %H:%M:%S %Z %Y",tm);
00713                         logit(NSLOG_PROCESS_INFO,TRUE,"Local time is %s",datestring);
00714 
00715                         /* write log version/info */
00716                         write_log_file_info(NULL);
00717 
00718 #ifdef USE_EVENT_BROKER
00719                         /* load modules */
00720                         neb_load_all_modules();
00721 
00722                         /* send program data to broker */
00723                         broker_program_state(NEBTYPE_PROCESS_PRELAUNCH,NEBFLAG_NONE,NEBATTR_NONE,NULL);
00724 #endif
00725 
00726                         /* read in all object config data */
00727                         if(result==OK)
00728                                 result=read_all_object_data(config_file);
00729 
00730                         /* there was a problem reading the config files */
00731                         if(result!=OK)
00732                                 logit(NSLOG_PROCESS_INFO | NSLOG_RUNTIME_ERROR | NSLOG_CONFIG_ERROR,TRUE,"Bailing out due to one or more errors encountered in the configuration files. Run %s from the command line with the -v option to verify your config before restarting. (PID=%d)", PROGRAM_NAME ,(int)getpid());
00733 
00734                         else{
00735 
00736                                 /* run the pre-flight check to make sure everything looks okay*/
00737                                 if((result=pre_flight_check())!=OK)
00738                                         logit(NSLOG_PROCESS_INFO | NSLOG_RUNTIME_ERROR | NSLOG_VERIFICATION_ERROR,TRUE,"Bailing out due to errors encountered while running the pre-flight check.  Run %s from the command line with the -v option to verify your config before restarting. (PID=%d)\n", PROGRAM_NAME ,(int)getpid());
00739                                 }
00740 
00741                         /* an error occurred that prevented us from (re)starting */
00742                         if(result!=OK){
00743 
00744                                 /* if we were restarting, we need to cleanup from the previous run */
00745                                 if(sigrestart==TRUE){
00746 
00747                                         /* clean up the status data */
00748                                         cleanup_status_data(config_file,TRUE);
00749 
00750                                         /* shutdown the external command worker thread */
00751                                         shutdown_command_file_worker_thread();
00752 
00753                                         /* close and delete the external command file FIFO */
00754                                         close_command_file();
00755 
00756                                         /* cleanup embedded perl interpreter */
00757                                         if(embedded_perl_initialized==TRUE)
00758                                                 deinit_embedded_perl();
00759                                         }
00760 
00761 #ifdef USE_EVENT_BROKER
00762                                 /* send program data to broker */
00763                                 broker_program_state(NEBTYPE_PROCESS_SHUTDOWN,NEBFLAG_PROCESS_INITIATED,NEBATTR_SHUTDOWN_ABNORMAL,NULL);
00764 #endif
00765                                 cleanup();
00766                                 exit(ERROR);
00767                                 }
00768 
00769 
00770 
00771                         /* initialize embedded Perl interpreter */
00772                         /* NOTE 02/15/08 embedded Perl must be initialized if compiled in, regardless of whether or not its enabled in the config file */
00773                         /* It compiled it, but not initialized, Icinga will segfault in readdir() calls, as libperl takes this function over */
00774                         if(embedded_perl_initialized==FALSE){
00775 /*                              if(enable_embedded_perl==TRUE){*/
00776 #ifdef EMBEDDEDPERL
00777                                 init_embedded_perl(env);
00778 #else
00779                                 init_embedded_perl(NULL);
00780 #endif
00781                                 embedded_perl_initialized=TRUE;
00782 /*                                      }*/
00783                                 }
00784 
00785                         /* handle signals (interrupts) */
00786                         setup_sighandler();
00787 
00788 
00789 #ifdef USE_EVENT_BROKER
00790                         /* send program data to broker */
00791                         broker_program_state(NEBTYPE_PROCESS_START,NEBFLAG_NONE,NEBATTR_NONE,NULL);
00792 #endif
00793 
00794                         /* enter daemon mode (unless we're restarting...) */
00795                         if(daemon_mode==TRUE && sigrestart==FALSE){
00796 
00797                                 result=daemon_init();
00798 
00799                                 /* we had an error daemonizing, so bail... */
00800                                 if(result==ERROR){
00801                                         logit(NSLOG_PROCESS_INFO | NSLOG_RUNTIME_ERROR,TRUE,"Bailing out due to failure to daemonize. (PID=%d)",(int)getpid());
00802 
00803 #ifdef USE_EVENT_BROKER
00804                                         /* send program data to broker */
00805                                         broker_program_state(NEBTYPE_PROCESS_SHUTDOWN,NEBFLAG_PROCESS_INITIATED,NEBATTR_SHUTDOWN_ABNORMAL,NULL);
00806 #endif
00807                                         cleanup();
00808                                         exit(ERROR);
00809                                         }
00810 
00811                                 dummy=asprintf(&buffer,"Finished daemonizing... (New PID=%d)\n",(int)getpid());
00812                                 write_to_all_logs(buffer,NSLOG_PROCESS_INFO);
00813                                 my_free(buffer);
00814 
00815                                 /* get new PID */
00816                                 nagios_pid=(int)getpid();
00817                                 }
00818 
00819                         /* open the command file (named pipe) for reading */
00820                         result=open_command_file();
00821                         if(result!=OK){
00822 
00823                                 logit(NSLOG_PROCESS_INFO | NSLOG_RUNTIME_ERROR,TRUE,"Bailing out due to errors encountered while trying to initialize the external command file... (PID=%d)\n",(int)getpid());
00824 
00825 #ifdef USE_EVENT_BROKER
00826                                 /* send program data to broker */
00827                                 broker_program_state(NEBTYPE_PROCESS_SHUTDOWN,NEBFLAG_PROCESS_INITIATED,NEBATTR_SHUTDOWN_ABNORMAL,NULL);
00828 #endif
00829                                 cleanup();
00830                                 exit(ERROR);
00831                                 }
00832 
00833                         /* initialize status data unless we're starting */
00834                         if(sigrestart==FALSE)
00835                                 initialize_status_data(config_file);
00836 
00837                         /* read initial service and host state information  */
00838                         initialize_retention_data(config_file);
00839                         read_initial_state_information();
00840                         sync_state_information();
00841 
00842                         /* initialize comment data */
00843                         initialize_comment_data(config_file);
00844 
00845                         /* initialize scheduled downtime data */
00846                         initialize_downtime_data(config_file);
00847 
00848                         /* initialize performance data */
00849                         initialize_performance_data(config_file);
00850 
00851                         /* initialize the event timing loop */
00852                         init_timing_loop();
00853 
00854                         /* initialize check statistics */
00855                         init_check_stats();
00856 
00857                         /* update all status data (with retained information) */
00858                         update_all_status_data();
00859 
00860                         /* log initial host and service state */
00861                         log_host_states(INITIAL_STATES,NULL);
00862                         log_service_states(INITIAL_STATES,NULL);
00863 
00864                         /* reset the restart flag */
00865                         sigrestart=FALSE;
00866 
00867 #ifdef USE_EVENT_BROKER
00868                         /* send program data to broker */
00869                         broker_program_state(NEBTYPE_PROCESS_EVENTLOOPSTART,NEBFLAG_NONE,NEBATTR_NONE,NULL);
00870 #endif
00871 
00872                         /* get event start time and save as macro */
00873                         event_start=time(NULL);
00874                         my_free(mac->x[MACRO_EVENTSTARTTIME]);
00875                         dummy=asprintf(&mac->x[MACRO_EVENTSTARTTIME],"%lu",(unsigned long)event_start);
00876 
00877                         /***** start monitoring all services *****/
00878                         /* (doesn't return until a restart or shutdown signal is encountered) */
00879                         event_execution_loop();
00880 
00881                         /* 03/01/2007 EG Moved from sighandler() to prevent FUTEX locking problems under NPTL */
00882                         /* 03/21/2007 EG SIGSEGV signals are still logged in sighandler() so we don't loose them */
00883                         /* did we catch a signal? */
00884                         if(caught_signal==TRUE){
00885 
00886                                 if(sig_id==SIGHUP)
00887                                         dummy=asprintf(&buffer,"Caught SIGHUP, restarting...\n");
00888                                 else if(sig_id!=SIGSEGV)
00889                                         dummy=asprintf(&buffer,"Caught SIG%s, shutting down...\n",sigs[sig_id]);
00890 
00891                                 write_to_all_logs(buffer,NSLOG_PROCESS_INFO);
00892                                 my_free(buffer);
00893                                 }
00894 
00895 #ifdef USE_EVENT_BROKER
00896                         /* send program data to broker */
00897                         broker_program_state(NEBTYPE_PROCESS_EVENTLOOPEND,NEBFLAG_NONE,NEBATTR_NONE,NULL);
00898                         if(sigshutdown==TRUE)
00899                                 broker_program_state(NEBTYPE_PROCESS_SHUTDOWN,NEBFLAG_USER_INITIATED,NEBATTR_SHUTDOWN_NORMAL,NULL);
00900                         else if(sigrestart==TRUE)
00901                                 broker_program_state(NEBTYPE_PROCESS_RESTART,NEBFLAG_USER_INITIATED,NEBATTR_RESTART_NORMAL,NULL);
00902 #endif
00903 
00904                         /* save service and host state information */
00905                         save_state_information(FALSE);
00906                         cleanup_retention_data(config_file);
00907 
00908                         /* clean up performance data */
00909                         cleanup_performance_data(config_file);
00910 
00911                         /* clean up the scheduled downtime data */
00912                         cleanup_downtime_data(config_file);
00913 
00914                         /* clean up the comment data */
00915                         cleanup_comment_data(config_file);
00916 
00917                         /* clean up the status data unless we're restarting */
00918                         if(sigrestart==FALSE)
00919                                 cleanup_status_data(config_file,TRUE);
00920 
00921                         /* close and delete the external command file FIFO unless we're restarting */
00922                         if(sigrestart==FALSE){
00923                                 shutdown_command_file_worker_thread();
00924                                 close_command_file();
00925                                 }
00926 
00927                         /* cleanup embedded perl interpreter */
00928                         if(sigrestart==FALSE)
00929                                 deinit_embedded_perl();
00930 
00931                         /* shutdown stuff... */
00932                         if(sigshutdown==TRUE){
00933 
00934                                 /* make sure lock file has been removed - it may not have been if we received a shutdown command */
00935                                 if(daemon_mode==TRUE)
00936                                         unlink(lock_file);
00937 
00938                                 /* log a shutdown message */
00939                                 logit(NSLOG_PROCESS_INFO,TRUE,"Successfully shutdown... (PID=%d)\n",(int)getpid());
00940                                 }
00941 
00942                         /* clean up after ourselves */
00943                         cleanup();
00944 
00945                         /* close debug log */
00946                         close_debug_log();
00947 
00948                         }while(sigrestart==TRUE && sigshutdown==FALSE);
00949 
00950                 /* free misc memory */
00951                 my_free(config_file);
00952                 }
00953 
00954         return OK;
00955         }
00956 
00957 
 All Data Structures Files Functions Variables Typedefs Defines