Icinga-core 1.4.0
next gen monitoring
xdata/xpddefault.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002  *
00003  * XPDDEFAULT.C - Default performance data routines
00004  *
00005  * Copyright (c) 1999-2009 Ethan Galstad (egalstad@nagios.org)
00006  * Copyright (c) 2009-2011 Nagios Core Development Team and Community Contributors
00007  * Copyright (c) 2009-2011 Icinga Development Team (http://www.icinga.org)
00008  *
00009  * License:
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License version 2 as
00013  * published by the Free Software Foundation.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00023  *
00024  *****************************************************************************/
00025 
00026 
00027 /*********** COMMON HEADER FILES ***********/
00028 
00029 #include "../include/config.h"
00030 #include "../include/common.h"
00031 #include "../include/objects.h"
00032 #include "../include/macros.h"
00033 #include "../include/icinga.h"
00034 
00035 
00036 /**** DATA INPUT-SPECIFIC HEADER FILES ****/
00037 
00038 #include "xpddefault.h"
00039 
00040 
00041 int     xpddefault_perfdata_timeout;
00042 
00043 char    *xpddefault_host_perfdata_command=NULL;
00044 char    *xpddefault_service_perfdata_command=NULL;
00045 command *xpddefault_host_perfdata_command_ptr=NULL;
00046 command *xpddefault_service_perfdata_command_ptr=NULL;
00047 
00048 char    *xpddefault_host_perfdata_file_template=NULL;
00049 char    *xpddefault_service_perfdata_file_template=NULL;
00050 
00051 char    *xpddefault_host_perfdata_file=NULL;
00052 char    *xpddefault_service_perfdata_file=NULL;
00053 
00054 int     xpddefault_host_perfdata_file_append=TRUE;
00055 int     xpddefault_service_perfdata_file_append=TRUE;
00056 int     xpddefault_host_perfdata_file_pipe=FALSE;
00057 int     xpddefault_service_perfdata_file_pipe=FALSE;
00058 
00059 unsigned long xpddefault_host_perfdata_file_processing_interval=0L;
00060 unsigned long xpddefault_service_perfdata_file_processing_interval=0L;
00061 
00062 char    *xpddefault_host_perfdata_file_processing_command=NULL;
00063 char    *xpddefault_service_perfdata_file_processing_command=NULL;
00064 command *xpddefault_host_perfdata_file_processing_command_ptr=NULL;
00065 command *xpddefault_service_perfdata_file_processing_command_ptr=NULL;
00066 
00067 int     xpddefault_host_perfdata_process_empty_results=DEFAULT_HOST_PERFDATA_PROCESS_EMPTY_RESULTS;
00068 int     xpddefault_service_perfdata_process_empty_results=DEFAULT_SERVICE_PERFDATA_PROCESS_EMPTY_RESULTS;
00069 
00070 FILE    *xpddefault_host_perfdata_fp=NULL;
00071 FILE    *xpddefault_service_perfdata_fp=NULL;
00072 int     xpddefault_host_perfdata_fd=-1;
00073 int     xpddefault_service_perfdata_fd=-1;
00074 
00075 static pthread_mutex_t xpddefault_host_perfdata_fp_lock;
00076 static pthread_mutex_t xpddefault_service_perfdata_fp_lock;
00077 
00078 /******************************************************************/
00079 /***************** COMMON CONFIG INITIALIZATION  ******************/
00080 /******************************************************************/
00081 
00082 /* grabs configuration information from main config file */
00083 int xpddefault_grab_config_info(char *config_file){
00084         char *input=NULL;
00085         mmapfile *thefile=NULL;
00086 
00087         /* open the config file for reading */
00088         if((thefile=mmap_fopen(config_file))==NULL){
00089                 logit(NSLOG_CONFIG_ERROR,TRUE,"Error: Could not open main config file '%s' for reading performance variables!\n",config_file);
00090                 return ERROR;
00091                 }
00092 
00093         /* read in all lines from the config file */
00094         while(1){
00095 
00096                 /* free memory */
00097                 my_free(input);
00098 
00099                 /* read the next line */
00100                 if((input=mmap_fgets_multiline(thefile))==NULL)
00101                         break;
00102 
00103                 /* skip blank lines and comments */
00104                 if(input[0]=='#' || input[0]=='\x0')
00105                         continue;
00106 
00107                 strip(input);
00108 
00109                 xpddefault_grab_config_directives(input);
00110                 }
00111 
00112         /* free memory and close the file */
00113         my_free(input);
00114         mmap_fclose(thefile);
00115 
00116         return OK;
00117         }
00118 
00119 
00120 /* processes a single directive */
00121 int xpddefault_grab_config_directives(char *input){
00122         char *temp_ptr=NULL;
00123         char *varname=NULL;
00124         char *varvalue=NULL;
00125 
00126         /* get the variable name */
00127         if((temp_ptr=my_strtok(input,"="))==NULL)
00128                 return ERROR;
00129         if((varname=(char *)strdup(temp_ptr))==NULL)
00130                 return ERROR;
00131 
00132         /* get the variable value */
00133         if((temp_ptr=my_strtok(NULL,"\n"))==NULL){
00134                 my_free(varname);
00135                 return ERROR;
00136                 }
00137         if((varvalue=(char *)strdup(temp_ptr))==NULL){
00138                 my_free(varname);
00139                 return ERROR;
00140                 }
00141 
00142         if(!strcmp(varname,"perfdata_timeout")){
00143                 strip(varvalue);
00144                 xpddefault_perfdata_timeout=atoi(varvalue);
00145                 }
00146 
00147         else if(!strcmp(varname,"host_perfdata_command"))
00148                 xpddefault_host_perfdata_command=(char *)strdup(varvalue);
00149 
00150         else if(!strcmp(varname,"service_perfdata_command"))
00151                 xpddefault_service_perfdata_command=(char *)strdup(varvalue);
00152 
00153         else if(!strcmp(varname,"host_perfdata_file_template"))
00154                 xpddefault_host_perfdata_file_template=(char *)strdup(varvalue);
00155 
00156         else if(!strcmp(varname,"service_perfdata_file_template"))
00157                 xpddefault_service_perfdata_file_template=(char *)strdup(varvalue);
00158 
00159         else if(!strcmp(varname,"host_perfdata_file"))
00160                 xpddefault_host_perfdata_file=(char *)strdup(varvalue);
00161 
00162         else if(!strcmp(varname,"service_perfdata_file"))
00163                 xpddefault_service_perfdata_file=(char *)strdup(varvalue);
00164 
00165         else if(!strcmp(varname,"host_perfdata_file_mode")){
00166                 xpddefault_host_perfdata_file_pipe=FALSE;
00167 
00168                 if(strstr(varvalue,"p")!=NULL)
00169                         xpddefault_host_perfdata_file_pipe=TRUE;
00170                 else if(strstr(varvalue,"w")!=NULL)
00171                         xpddefault_host_perfdata_file_append=FALSE;
00172                 else
00173                         xpddefault_host_perfdata_file_append=TRUE;
00174                 }
00175 
00176         else if(!strcmp(varname,"service_perfdata_file_mode")){
00177                 xpddefault_service_perfdata_file_pipe=FALSE;
00178 
00179                 if(strstr(varvalue,"p")!=NULL)
00180                         xpddefault_service_perfdata_file_pipe=TRUE;
00181                 else if(strstr(varvalue,"w")!=NULL)
00182                         xpddefault_service_perfdata_file_append=FALSE;
00183                 else
00184                         xpddefault_service_perfdata_file_append=TRUE;
00185                 }
00186 
00187         else if(!strcmp(varname,"host_perfdata_file_processing_interval"))
00188                 xpddefault_host_perfdata_file_processing_interval=strtoul(varvalue,NULL,0);
00189 
00190         else if(!strcmp(varname,"service_perfdata_file_processing_interval"))
00191                 xpddefault_service_perfdata_file_processing_interval=strtoul(varvalue,NULL,0);
00192 
00193         else if(!strcmp(varname,"host_perfdata_file_processing_command"))
00194                 xpddefault_host_perfdata_file_processing_command=(char *)strdup(varvalue);
00195 
00196         else if(!strcmp(varname,"service_perfdata_file_processing_command"))
00197                 xpddefault_service_perfdata_file_processing_command=(char *)strdup(varvalue);
00198 
00199         else if(!strcmp(varname,"host_perfdata_process_empty_results"))
00200                 xpddefault_host_perfdata_process_empty_results=(atoi(varvalue)>0)?TRUE:FALSE;
00201 
00202         else if(!strcmp(varname,"service_perfdata_process_empty_results"))
00203                 xpddefault_service_perfdata_process_empty_results=(atoi(varvalue)>0)?TRUE:FALSE;
00204 
00205         /* free memory */
00206         my_free(varname);
00207         my_free(varvalue);
00208 
00209         return OK;
00210         }
00211 
00212 
00213 
00214 /******************************************************************/
00215 /************** INITIALIZATION & CLEANUP FUNCTIONS ****************/
00216 /******************************************************************/
00217 
00218 /* initializes performance data */
00219 int xpddefault_initialize_performance_data(char *config_file){
00220         char *buffer=NULL;
00221         char *temp_buffer=NULL;
00222         char *temp_command_name=NULL;
00223         command *temp_command=NULL;
00224         time_t current_time;
00225         icinga_macros *mac;
00226 
00227         mac = get_global_macros();
00228         time(&current_time);
00229 
00230         /* reset vars */
00231         xpddefault_host_perfdata_command_ptr=NULL;
00232         xpddefault_service_perfdata_command_ptr=NULL;
00233         xpddefault_host_perfdata_file_processing_command_ptr=NULL;
00234         xpddefault_service_perfdata_file_processing_command_ptr=NULL;
00235 
00236         /* grab config info from main config file */
00237         xpddefault_grab_config_info(config_file);
00238 
00239         /* make sure we have some templates defined */
00240         if(xpddefault_host_perfdata_file_template==NULL)
00241                 xpddefault_host_perfdata_file_template=(char *)strdup(DEFAULT_HOST_PERFDATA_FILE_TEMPLATE);
00242         if(xpddefault_service_perfdata_file_template==NULL)
00243                 xpddefault_service_perfdata_file_template=(char *)strdup(DEFAULT_SERVICE_PERFDATA_FILE_TEMPLATE);
00244 
00245         /* process special chars in templates */
00246         xpddefault_preprocess_file_templates(xpddefault_host_perfdata_file_template);
00247         xpddefault_preprocess_file_templates(xpddefault_service_perfdata_file_template);
00248 
00249         /* open the performance data files */
00250         xpddefault_open_host_perfdata_file();
00251         xpddefault_open_service_perfdata_file();
00252 
00253         /* verify that performance data commands are valid */
00254         if(xpddefault_host_perfdata_command!=NULL){
00255 
00256                 temp_buffer=(char *)strdup(xpddefault_host_perfdata_command);
00257 
00258                 /* get the command name, leave any arguments behind */
00259                 temp_command_name=my_strtok(temp_buffer,"!");
00260 
00261                 if((temp_command=find_command(temp_command_name))==NULL){
00262 
00263                         logit(NSLOG_RUNTIME_WARNING,TRUE,"Warning: Host performance command '%s' was not found - host performance data will not be processed!\n",temp_command_name);
00264 
00265                         my_free(xpddefault_host_perfdata_command);
00266                         }
00267 
00268                 my_free(temp_buffer);
00269 
00270                 /* save the command pointer for later */
00271                 xpddefault_host_perfdata_command_ptr=temp_command;
00272                 }
00273         if(xpddefault_service_perfdata_command!=NULL){
00274 
00275                 temp_buffer=(char *)strdup(xpddefault_service_perfdata_command);
00276 
00277                 /* get the command name, leave any arguments behind */
00278                 temp_command_name=my_strtok(temp_buffer,"!");
00279 
00280                 if((temp_command=find_command(temp_command_name))==NULL){
00281 
00282                         logit(NSLOG_RUNTIME_WARNING,TRUE,"Warning: Service performance command '%s' was not found - service performance data will not be processed!\n",temp_command_name);
00283 
00284                         my_free(xpddefault_service_perfdata_command);
00285                         }
00286 
00287                 /* free memory */
00288                 my_free(temp_buffer);
00289 
00290                 /* save the command pointer for later */
00291                 xpddefault_service_perfdata_command_ptr=temp_command;
00292                 }
00293         if(xpddefault_host_perfdata_file_processing_command!=NULL){
00294 
00295                 temp_buffer=(char *)strdup(xpddefault_host_perfdata_file_processing_command);
00296 
00297                 /* get the command name, leave any arguments behind */
00298                 temp_command_name=my_strtok(temp_buffer,"!");
00299 
00300                 if((temp_command=find_command(temp_command_name))==NULL){
00301 
00302                         logit(NSLOG_RUNTIME_WARNING,TRUE,"Warning: Host performance file processing command '%s' was not found - host performance data file will not be processed!\n",temp_command_name);
00303 
00304                         my_free(xpddefault_host_perfdata_file_processing_command);
00305                         }
00306 
00307                 /* free memory */
00308                 my_free(temp_buffer);
00309 
00310                 /* save the command pointer for later */
00311                 xpddefault_host_perfdata_file_processing_command_ptr=temp_command;
00312                 }
00313         if(xpddefault_service_perfdata_file_processing_command!=NULL){
00314 
00315                 temp_buffer=(char *)strdup(xpddefault_service_perfdata_file_processing_command);
00316 
00317                 /* get the command name, leave any arguments behind */
00318                 temp_command_name=my_strtok(temp_buffer,"!");
00319 
00320                 if((temp_command=find_command(temp_command_name))==NULL){
00321 
00322                         logit(NSLOG_RUNTIME_WARNING,TRUE,"Warning: Service performance file processing command '%s' was not found - service performance data file will not be processed!\n",temp_command_name);
00323 
00324                         my_free(xpddefault_service_perfdata_file_processing_command);
00325                         }
00326 
00327                 /* save the command pointer for later */
00328                 xpddefault_service_perfdata_file_processing_command_ptr=temp_command;
00329                 }
00330 
00331         /* periodically process the host perfdata file */
00332         if(xpddefault_host_perfdata_file_processing_interval>0 && xpddefault_host_perfdata_file_processing_command!=NULL)
00333                 schedule_new_event(EVENT_USER_FUNCTION,TRUE,current_time+xpddefault_host_perfdata_file_processing_interval,TRUE,xpddefault_host_perfdata_file_processing_interval,NULL,TRUE,(void *)xpddefault_process_host_perfdata_file,NULL,0);
00334 
00335         /* periodically process the service perfdata file */
00336         if(xpddefault_service_perfdata_file_processing_interval>0 && xpddefault_service_perfdata_file_processing_command!=NULL)
00337                 schedule_new_event(EVENT_USER_FUNCTION,TRUE,current_time+xpddefault_service_perfdata_file_processing_interval,TRUE,xpddefault_service_perfdata_file_processing_interval,NULL,TRUE,(void *)xpddefault_process_service_perfdata_file,NULL,0);
00338 
00339         /* save the host perf data file macro */
00340         my_free(mac->x[MACRO_HOSTPERFDATAFILE]);
00341         if(xpddefault_host_perfdata_file!=NULL){
00342                 if((mac->x[MACRO_HOSTPERFDATAFILE]=(char *)strdup(xpddefault_host_perfdata_file)))
00343                         strip(mac->x[MACRO_HOSTPERFDATAFILE]);
00344                 }
00345 
00346         /* save the service perf data file macro */
00347         my_free(mac->x[MACRO_SERVICEPERFDATAFILE]);
00348         if(xpddefault_service_perfdata_file!=NULL){
00349                 if((mac->x[MACRO_SERVICEPERFDATAFILE]=(char *)strdup(xpddefault_service_perfdata_file)))
00350                         strip(mac->x[MACRO_SERVICEPERFDATAFILE]);
00351                 }
00352 
00353         /* free memory */
00354         my_free(temp_buffer);
00355         my_free(buffer);
00356 
00357         return OK;
00358 }
00359 
00360 
00361 
00362 /* cleans up performance data */
00363 int xpddefault_cleanup_performance_data(char *config_file){
00364 
00365         /* free memory */
00366         my_free(xpddefault_host_perfdata_command);
00367         my_free(xpddefault_service_perfdata_command);
00368         my_free(xpddefault_host_perfdata_file_template);
00369         my_free(xpddefault_service_perfdata_file_template);
00370         my_free(xpddefault_host_perfdata_file);
00371         my_free(xpddefault_service_perfdata_file);
00372         my_free(xpddefault_host_perfdata_file_processing_command);
00373         my_free(xpddefault_service_perfdata_file_processing_command);
00374 
00375         /* close the files */
00376         xpddefault_close_host_perfdata_file();
00377         xpddefault_close_service_perfdata_file();
00378 
00379         return OK;
00380         }
00381 
00382 
00383 
00384 /******************************************************************/
00385 /****************** PERFORMANCE DATA FUNCTIONS ********************/
00386 /******************************************************************/
00387 
00388 
00389 /* updates service performance data */
00390 int xpddefault_update_service_performance_data(service *svc){
00391 
00392         icinga_macros mac;
00393         host *hst;
00394 
00395         /*
00396          * bail early if we've got nothing to do so we don't spend a lot
00397          * of time calculating macros that never get used
00398          * on distributed setups, empty perfdata results are required, so
00399          * only drop out if demanded via configs.
00400          */
00401         if(xpddefault_service_perfdata_process_empty_results==FALSE){
00402                 if (!svc || !svc->perf_data || !*svc->perf_data){
00403                         return OK;
00404                 }
00405                 if ((!xpddefault_service_perfdata_fp || !xpddefault_service_perfdata_file_template) && !xpddefault_service_perfdata_command){
00406                         return OK;
00407                 }
00408         }
00409 
00410         /*
00411          * we know we've got some work to do, so grab the necessary
00412          * macros and get busy
00413          */
00414         memset(&mac, 0, sizeof(mac));
00415         hst = find_host(svc->host_name);
00416         grab_host_macros_r(&mac, hst);
00417         grab_service_macros_r(&mac, svc);
00418 
00419         /* run the performance data command */
00420         xpddefault_run_service_performance_data_command(&mac, svc);
00421 
00422         /* get rid of used memory we won't need anymore */
00423         clear_argv_macros_r(&mac);
00424 
00425         /* update the performance data file */
00426         xpddefault_update_service_performance_data_file(&mac, svc);
00427 
00428         /* now free() it all */
00429         clear_volatile_macros_r(&mac);
00430 
00431         return OK;
00432 }
00433 
00434 
00435 /* updates host performance data */
00436 int xpddefault_update_host_performance_data(host *hst){
00437 
00438         icinga_macros mac;
00439 
00440         /*
00441          * bail early if we've got nothing to do so we don't spend a lot
00442          * of time calculating macros that never get used
00443          * on distributed setups, empty perfdata results are required, so
00444          * only drop out if demanded via configs.
00445          */
00446         if(xpddefault_host_perfdata_process_empty_results==FALSE){
00447                 if (!hst || !hst->perf_data || !*hst->perf_data) {
00448                         return OK;
00449                 }
00450                 if ((!xpddefault_host_perfdata_fp || !xpddefault_host_perfdata_file_template) && !xpddefault_host_perfdata_command) {
00451                         return OK;
00452                 }
00453         }
00454 
00455         /* set up macros and get to work */
00456         memset(&mac, 0, sizeof(mac));
00457         grab_host_macros_r(&mac, hst);
00458 
00459         /* run the performance data command */
00460         xpddefault_run_host_performance_data_command(&mac, hst);
00461 
00462         /* no more commands to run, so we won't need this any more */
00463         clear_argv_macros_r(&mac);
00464 
00465         /* update the performance data file */
00466         xpddefault_update_host_performance_data_file(&mac, hst);
00467 
00468         /* free() all */
00469         clear_volatile_macros_r(&mac);
00470 
00471         return OK;
00472 }
00473 
00474 
00475 
00476 
00477 /******************************************************************/
00478 /************** PERFORMANCE DATA COMMAND FUNCTIONS ****************/
00479 /******************************************************************/
00480 
00481 
00482 /* runs the service performance data command */
00483 int xpddefault_run_service_performance_data_command(icinga_macros *mac, service *svc){
00484         char *raw_command_line=NULL;
00485         char *processed_command_line=NULL;
00486         int early_timeout=FALSE;
00487         double exectime;
00488         int result=OK;
00489         int macro_options=STRIP_ILLEGAL_MACRO_CHARS|ESCAPE_MACRO_CHARS;
00490 
00491 
00492         log_debug_info(DEBUGL_FUNCTIONS,0,"run_service_performance_data_command()\n");
00493 
00494         if(svc==NULL)
00495                 return ERROR;
00496 
00497         /* we don't have a command */
00498         if(xpddefault_service_perfdata_command==NULL)
00499                 return OK;
00500 
00501         /* get the raw command line */
00502         get_raw_command_line_r(mac, xpddefault_service_perfdata_command_ptr,xpddefault_service_perfdata_command,&raw_command_line,macro_options);
00503         if(raw_command_line==NULL)
00504                 return ERROR;
00505 
00506         log_debug_info(DEBUGL_PERFDATA,2,"Raw service performance data command line: %s\n",raw_command_line);
00507 
00508         /* process any macros in the raw command line */
00509         process_macros_r(mac, raw_command_line,&processed_command_line,macro_options);
00510         if(processed_command_line==NULL)
00511                 return ERROR;
00512 
00513         log_debug_info(DEBUGL_PERFDATA,2,"Processed service performance data command line: %s\n",processed_command_line);
00514 
00515         /* run the command */
00516         my_system_r(mac, processed_command_line,xpddefault_perfdata_timeout,&early_timeout,&exectime,NULL,0);
00517 
00518         /* check to see if the command timed out */
00519         if(early_timeout==TRUE)
00520                 logit(NSLOG_RUNTIME_WARNING,TRUE,"Warning: Service performance data command '%s' for service '%s' on host '%s' timed out after %d seconds\n",processed_command_line,svc->description,svc->host_name,xpddefault_perfdata_timeout);
00521 
00522         /* free memory */
00523         my_free(raw_command_line);
00524         my_free(processed_command_line);
00525 
00526         return result;
00527 }
00528 
00529 
00530 /* runs the host performance data command */
00531 int xpddefault_run_host_performance_data_command(icinga_macros *mac, host *hst){
00532         char *raw_command_line=NULL;
00533         char *processed_command_line=NULL;
00534         int early_timeout=FALSE;
00535         double exectime;
00536         int result=OK;
00537         int macro_options=STRIP_ILLEGAL_MACRO_CHARS|ESCAPE_MACRO_CHARS;
00538 
00539         log_debug_info(DEBUGL_FUNCTIONS,0,"run_host_performance_data_command()\n");
00540 
00541         if(hst==NULL)
00542                 return ERROR;
00543 
00544         /* we don't have a command */
00545         if(xpddefault_host_perfdata_command==NULL)
00546                 return OK;
00547 
00548         /* get the raw command line */
00549         get_raw_command_line_r(mac, xpddefault_host_perfdata_command_ptr,xpddefault_host_perfdata_command,&raw_command_line,macro_options);
00550         if(raw_command_line==NULL)
00551                 return ERROR;
00552 
00553         log_debug_info(DEBUGL_PERFDATA,2,"Raw host performance data command line: %s\n",raw_command_line);
00554 
00555         /* process any macros in the raw command line */
00556         process_macros_r(mac, raw_command_line,&processed_command_line,macro_options);
00557 
00558         log_debug_info(DEBUGL_PERFDATA,2,"Processed host performance data command line: %s\n",processed_command_line);
00559 
00560         /* run the command */
00561         my_system_r(mac, processed_command_line,xpddefault_perfdata_timeout,&early_timeout,&exectime,NULL,0);
00562         if(processed_command_line==NULL)
00563                 return ERROR;
00564 
00565         /* check to see if the command timed out */
00566         if(early_timeout==TRUE)
00567                 logit(NSLOG_RUNTIME_WARNING,TRUE,"Warning: Host performance data command '%s' for host '%s' timed out after %d seconds\n",processed_command_line,hst->name,xpddefault_perfdata_timeout);
00568 
00569         /* free memory */
00570         my_free(raw_command_line);
00571         my_free(processed_command_line);
00572 
00573         return result;
00574 }
00575 
00576 
00577 
00578 /******************************************************************/
00579 /**************** FILE PERFORMANCE DATA FUNCTIONS *****************/
00580 /******************************************************************/
00581 
00582 /* open the host performance data file for writing */
00583 int xpddefault_open_host_perfdata_file(void){
00584 
00585         if(xpddefault_host_perfdata_file!=NULL){
00586 
00587                 if(xpddefault_host_perfdata_file_pipe==TRUE) {
00588                         /* must open read-write to avoid failure if the other end isn't ready yet */
00589                         xpddefault_host_perfdata_fd=open(xpddefault_host_perfdata_file,O_NONBLOCK | O_RDWR);
00590                         xpddefault_host_perfdata_fp=fdopen(xpddefault_host_perfdata_fd,"w");
00591                         }
00592                 else
00593                         xpddefault_host_perfdata_fp=fopen(xpddefault_host_perfdata_file,(xpddefault_host_perfdata_file_append==TRUE)?"a":"w");
00594 
00595                 if(xpddefault_host_perfdata_fp==NULL){
00596 
00597                         logit(NSLOG_RUNTIME_WARNING,TRUE,"Warning: File '%s' could not be opened - host performance data will not be written to file!\n",xpddefault_host_perfdata_file);
00598 
00599                         return ERROR;
00600                         }
00601                 }
00602 
00603         return OK;
00604         }
00605 
00606 
00607 /* open the service performance data file for writing */
00608 int xpddefault_open_service_perfdata_file(void){
00609 
00610         if(xpddefault_service_perfdata_file!=NULL){
00611                 if(xpddefault_service_perfdata_file_pipe==TRUE) {
00612                         /* must open read-write to avoid failure if the other end isn't ready yet */
00613                         xpddefault_service_perfdata_fd=open(xpddefault_service_perfdata_file,O_NONBLOCK | O_RDWR);
00614                         xpddefault_service_perfdata_fp=fdopen(xpddefault_service_perfdata_fd,"w");
00615                         }
00616                 else
00617                         xpddefault_service_perfdata_fp=fopen(xpddefault_service_perfdata_file,(xpddefault_service_perfdata_file_append==TRUE)?"a":"w");
00618 
00619                 if(xpddefault_service_perfdata_fp==NULL){
00620 
00621                         logit(NSLOG_RUNTIME_WARNING,TRUE,"Warning: File '%s' could not be opened - service performance data will not be written to file!\n",xpddefault_service_perfdata_file);
00622 
00623                         return ERROR;
00624                         }
00625                 }
00626 
00627         return OK;
00628         }
00629 
00630 
00631 /* close the host performance data file */
00632 int xpddefault_close_host_perfdata_file(void){
00633 
00634         if(xpddefault_host_perfdata_fp!=NULL)
00635                 fclose(xpddefault_host_perfdata_fp);
00636         if(xpddefault_host_perfdata_fd>=0){
00637                 close(xpddefault_host_perfdata_fd);
00638                 xpddefault_host_perfdata_fd=-1;
00639                 }
00640 
00641         return OK;
00642         }
00643 
00644 
00645 /* close the service performance data file */
00646 int xpddefault_close_service_perfdata_file(void){
00647 
00648         if(xpddefault_service_perfdata_fp!=NULL)
00649                 fclose(xpddefault_service_perfdata_fp);
00650         if(xpddefault_service_perfdata_fd>=0){
00651                 close(xpddefault_service_perfdata_fd);
00652                 xpddefault_service_perfdata_fd=-1;
00653                 }
00654 
00655         return OK;
00656         }
00657 
00658 
00659 /* processes delimiter characters in templates */
00660 int xpddefault_preprocess_file_templates(char *template){
00661         char *tempbuf;
00662         int x=0;
00663         int y=0;
00664 
00665         if(template==NULL)
00666                 return OK;
00667 
00668         /* allocate temporary buffer */
00669         tempbuf=(char *)malloc(strlen(template)+1);
00670         if(tempbuf==NULL)
00671                 return ERROR;
00672         strcpy(tempbuf,"");
00673 
00674         for(x=0,y=0;x<strlen(template);x++,y++){
00675                 if(template[x]=='\\'){
00676                         if(template[x+1]=='t'){
00677                                 tempbuf[y]='\t';
00678                                 x++;
00679                                 }
00680                         else if(template[x+1]=='r'){
00681                                 tempbuf[y]='\r';
00682                                 x++;
00683                                 }
00684                         else if(template[x+1]=='n'){
00685                                 tempbuf[y]='\n';
00686                                 x++;
00687                                 }
00688                         else
00689                                 tempbuf[y]=template[x];
00690                         }
00691                 else
00692                         tempbuf[y]=template[x];
00693                 }
00694         tempbuf[y]='\x0';
00695 
00696         strcpy(template,tempbuf);
00697         my_free(tempbuf);
00698 
00699         return OK;
00700         }
00701 
00702 
00703 /* updates service performance data file */
00704 int xpddefault_update_service_performance_data_file(icinga_macros *mac, service *svc){
00705         char *raw_output=NULL;
00706         char *processed_output=NULL;
00707         int result=OK;
00708 
00709         log_debug_info(DEBUGL_FUNCTIONS,0,"update_service_performance_data_file()\n");
00710 
00711         if(svc==NULL)
00712                 return ERROR;
00713 
00714         /* we don't have a file to write to*/
00715         if(xpddefault_service_perfdata_fp==NULL || xpddefault_service_perfdata_file_template==NULL)
00716                 return OK;
00717 
00718         /* get the raw line to write */
00719         raw_output=(char *)strdup(xpddefault_service_perfdata_file_template);
00720 
00721         log_debug_info(DEBUGL_PERFDATA,2,"Raw service performance data file output: %s\n",raw_output);
00722 
00723         /* process any macros in the raw output line */
00724         process_macros_r(mac, raw_output, &processed_output, 0);
00725         if(processed_output==NULL)
00726                 return ERROR;
00727 
00728         log_debug_info(DEBUGL_PERFDATA,2,"Processed service performance data file output: %s\n",processed_output);
00729 
00730         /* lock, write to and unlock host performance data file */
00731         pthread_mutex_lock(&xpddefault_service_perfdata_fp_lock);
00732         fputs(processed_output,xpddefault_service_perfdata_fp);
00733         fputc('\n',xpddefault_service_perfdata_fp);
00734         fflush(xpddefault_service_perfdata_fp);
00735         pthread_mutex_unlock(&xpddefault_service_perfdata_fp_lock);
00736 
00737         /* free memory */
00738         my_free(raw_output);
00739         my_free(processed_output);
00740 
00741         return result;
00742 }
00743 
00744 
00745 /* updates host performance data file */
00746 int xpddefault_update_host_performance_data_file(icinga_macros *mac, host *hst){
00747         char *raw_output=NULL;
00748         char *processed_output=NULL;
00749         int result=OK;
00750 
00751         log_debug_info(DEBUGL_FUNCTIONS,0,"update_host_performance_data_file()\n");
00752 
00753         if(hst==NULL)
00754                 return ERROR;
00755 
00756         /* we don't have a host perfdata file */
00757         if(xpddefault_host_perfdata_fp==NULL || xpddefault_host_perfdata_file_template==NULL)
00758                 return OK;
00759 
00760         /* get the raw output */
00761         raw_output=(char *)strdup(xpddefault_host_perfdata_file_template);
00762 
00763         log_debug_info(DEBUGL_PERFDATA,2,"Raw host performance file output: %s\n",raw_output);
00764 
00765         /* process any macros in the raw output */
00766         process_macros_r(mac, raw_output,&processed_output,0);
00767         if(processed_output==NULL)
00768                 return ERROR;
00769 
00770         log_debug_info(DEBUGL_PERFDATA,2,"Processed host performance data file output: %s\n",processed_output);
00771 
00772         /* lock, write to and unlock host performance data file */
00773         pthread_mutex_lock(&xpddefault_host_perfdata_fp_lock);
00774         fputs(processed_output, xpddefault_host_perfdata_fp);
00775         fputc('\n', xpddefault_host_perfdata_fp);
00776         fflush(xpddefault_host_perfdata_fp);
00777         pthread_mutex_unlock(&xpddefault_host_perfdata_fp_lock);
00778 
00779         /* free memory */
00780         my_free(raw_output);
00781         my_free(processed_output);
00782 
00783         return result;
00784         }
00785 
00786 
00787 /* periodically process the host perf data file */
00788 int xpddefault_process_host_perfdata_file(void){
00789         char *raw_command_line=NULL;
00790         char *processed_command_line=NULL;
00791         int early_timeout=FALSE;
00792         double exectime=0.0;
00793         int result=OK;
00794         int macro_options=STRIP_ILLEGAL_MACRO_CHARS|ESCAPE_MACRO_CHARS;
00795         icinga_macros mac;
00796 
00797         log_debug_info(DEBUGL_FUNCTIONS,0,"process_host_perfdata_file()\n");
00798 
00799         /* we don't have a command */
00800         if(xpddefault_host_perfdata_file_processing_command==NULL)
00801                 return OK;
00802 
00803         /* init macros */
00804         memset(&mac, 0, sizeof(mac));
00805 
00806         /* get the raw command line */
00807         get_raw_command_line_r(&mac, xpddefault_host_perfdata_file_processing_command_ptr,xpddefault_host_perfdata_file_processing_command,&raw_command_line,macro_options);
00808         if(raw_command_line==NULL) {
00809                 clear_volatile_macros_r(&mac);
00810                 return ERROR;
00811         }
00812 
00813         log_debug_info(DEBUGL_PERFDATA,2,"Raw host performance data file processing command line: %s\n",raw_command_line);
00814 
00815         /* process any macros in the raw command line */
00816         process_macros_r(&mac, raw_command_line,&processed_command_line,macro_options);
00817         if(processed_command_line==NULL) {
00818                 clear_volatile_macros_r(&mac);
00819                 return ERROR;
00820         }
00821 
00822         log_debug_info(DEBUGL_PERFDATA,2,"Processed host performance data file processing command line: %s\n",processed_command_line);
00823 
00824         /* lock and close the performance data file */
00825         pthread_mutex_lock(&xpddefault_host_perfdata_fp_lock);
00826         xpddefault_close_host_perfdata_file();
00827 
00828         /* run the command */
00829         my_system_r(&mac, processed_command_line,xpddefault_perfdata_timeout,&early_timeout,&exectime,NULL,0);
00830         clear_volatile_macros_r(&mac);
00831 
00832         /* re-open and unlock the performance data file */
00833         xpddefault_open_host_perfdata_file();
00834         pthread_mutex_unlock(&xpddefault_host_perfdata_fp_lock);
00835 
00836         /* check to see if the command timed out */
00837         if(early_timeout==TRUE)
00838                 logit(NSLOG_RUNTIME_WARNING,TRUE,"Warning: Host performance data file processing command '%s' timed out after %d seconds\n",processed_command_line,xpddefault_perfdata_timeout);
00839 
00840         /* free memory */
00841         my_free(raw_command_line);
00842         my_free(processed_command_line);
00843 
00844         return result;
00845 }
00846 
00847 
00848 /* periodically process the service perf data file */
00849 int xpddefault_process_service_perfdata_file(void){
00850         char *raw_command_line=NULL;
00851         char *processed_command_line=NULL;
00852         int early_timeout=FALSE;
00853         double exectime=0.0;
00854         int result=OK;
00855         int macro_options=STRIP_ILLEGAL_MACRO_CHARS|ESCAPE_MACRO_CHARS;
00856         icinga_macros mac;
00857 
00858         log_debug_info(DEBUGL_FUNCTIONS,0,"process_service_perfdata_file()\n");
00859 
00860         /* we don't have a command */
00861         if(xpddefault_service_perfdata_file_processing_command==NULL)
00862                 return OK;
00863 
00864         /* init macros */
00865         memset(&mac, 0, sizeof(mac));
00866 
00867         /* get the raw command line */
00868         get_raw_command_line_r(&mac, xpddefault_service_perfdata_file_processing_command_ptr,xpddefault_service_perfdata_file_processing_command,&raw_command_line,macro_options);
00869         if(raw_command_line==NULL) {
00870                 clear_volatile_macros_r(&mac);
00871                 return ERROR;
00872         }
00873 
00874         log_debug_info(DEBUGL_PERFDATA,2,"Raw service performance data file processing command line: %s\n",raw_command_line);
00875 
00876         /* process any macros in the raw command line */
00877         process_macros_r(&mac, raw_command_line,&processed_command_line,macro_options);
00878         if(processed_command_line==NULL) {
00879                 clear_volatile_macros_r(&mac);
00880                 return ERROR;
00881         }
00882 
00883         log_debug_info(DEBUGL_PERFDATA,2,"Processed service performance data file processing command line: %s\n",processed_command_line);
00884 
00885         /* lock and close the performance data file */
00886         pthread_mutex_lock(&xpddefault_service_perfdata_fp_lock);
00887         xpddefault_close_service_perfdata_file();
00888 
00889         /* run the command */
00890         my_system_r(&mac, processed_command_line,xpddefault_perfdata_timeout,&early_timeout,&exectime,NULL,0);
00891 
00892         /* re-open and unlock the performance data file */
00893         xpddefault_open_service_perfdata_file();
00894         pthread_mutex_unlock(&xpddefault_service_perfdata_fp_lock);
00895 
00896         clear_volatile_macros_r(&mac);
00897 
00898         /* check to see if the command timed out */
00899         if(early_timeout==TRUE)
00900                 logit(NSLOG_RUNTIME_WARNING,TRUE,"Warning: Service performance data file processing command '%s' timed out after %d seconds\n",processed_command_line,xpddefault_perfdata_timeout);
00901 
00902         /* free memory */
00903         my_free(raw_command_line);
00904         my_free(processed_command_line);
00905 
00906         return result;
00907         }
00908 
 All Data Structures Files Functions Variables Typedefs Defines