2017-05-19 22:22:40 +02:00
/*****************************************************************************
*
* XPDDEFAULT . C - Default performance data routines
*
*
* 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 0213 9 , USA .
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*********** COMMON HEADER FILES ***********/
# include "../include/config.h"
# include "../include/common.h"
# include "../include/objects.h"
# include "../include/macros.h"
# include "../include/nagios.h"
2017-05-19 23:37:19 +02:00
# include "../include/workers.h"
2017-05-19 22:22:40 +02:00
# include "xpddefault.h"
2017-05-19 23:37:19 +02:00
static command * host_perfdata_command_ptr = NULL ;
static command * service_perfdata_command_ptr = NULL ;
static command * host_perfdata_file_processing_command_ptr = NULL ;
static command * service_perfdata_file_processing_command_ptr = NULL ;
static FILE * host_perfdata_fp = NULL ;
static FILE * service_perfdata_fp = NULL ;
static int host_perfdata_fd = - 1 ;
static int service_perfdata_fd = - 1 ;
2017-05-19 22:22:40 +02:00
/******************************************************************/
/************** INITIALIZATION & CLEANUP FUNCTIONS ****************/
/******************************************************************/
/* initializes performance data */
2017-05-19 23:37:19 +02:00
int xpddefault_initialize_performance_data ( const char * cfgfile ) {
2017-05-19 22:22:40 +02:00
char * buffer = NULL ;
char * temp_buffer = NULL ;
char * temp_command_name = NULL ;
command * temp_command = NULL ;
time_t current_time ;
nagios_macros * mac ;
mac = get_global_macros ( ) ;
time ( & current_time ) ;
/* reset vars */
2017-05-19 23:37:19 +02:00
host_perfdata_command_ptr = NULL ;
service_perfdata_command_ptr = NULL ;
host_perfdata_file_processing_command_ptr = NULL ;
service_perfdata_file_processing_command_ptr = NULL ;
2017-05-19 22:22:40 +02:00
/* make sure we have some templates defined */
2017-05-19 23:37:19 +02:00
if ( host_perfdata_file_template = = NULL )
host_perfdata_file_template = ( char * ) strdup ( DEFAULT_HOST_PERFDATA_FILE_TEMPLATE ) ;
if ( service_perfdata_file_template = = NULL )
service_perfdata_file_template = ( char * ) strdup ( DEFAULT_SERVICE_PERFDATA_FILE_TEMPLATE ) ;
2017-05-19 22:22:40 +02:00
/* process special chars in templates */
2017-05-19 23:37:19 +02:00
xpddefault_preprocess_file_templates ( host_perfdata_file_template ) ;
xpddefault_preprocess_file_templates ( service_perfdata_file_template ) ;
2017-05-19 22:22:40 +02:00
/* open the performance data files */
xpddefault_open_host_perfdata_file ( ) ;
xpddefault_open_service_perfdata_file ( ) ;
/* verify that performance data commands are valid */
2017-05-19 23:37:19 +02:00
if ( host_perfdata_command ! = NULL ) {
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
temp_buffer = ( char * ) strdup ( host_perfdata_command ) ;
2017-05-19 22:22:40 +02:00
/* get the command name, leave any arguments behind */
temp_command_name = my_strtok ( temp_buffer , " ! " ) ;
if ( ( temp_command = find_command ( temp_command_name ) ) = = NULL ) {
logit ( NSLOG_RUNTIME_WARNING , TRUE , " Warning: Host performance command '%s' was not found - host performance data will not be processed! \n " , temp_command_name ) ;
2017-05-19 23:37:19 +02:00
my_free ( host_perfdata_command ) ;
2017-05-19 22:22:40 +02:00
}
my_free ( temp_buffer ) ;
/* save the command pointer for later */
2017-05-19 23:37:19 +02:00
host_perfdata_command_ptr = temp_command ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
if ( service_perfdata_command ! = NULL ) {
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
temp_buffer = ( char * ) strdup ( service_perfdata_command ) ;
2017-05-19 22:22:40 +02:00
/* get the command name, leave any arguments behind */
temp_command_name = my_strtok ( temp_buffer , " ! " ) ;
if ( ( temp_command = find_command ( temp_command_name ) ) = = NULL ) {
logit ( NSLOG_RUNTIME_WARNING , TRUE , " Warning: Service performance command '%s' was not found - service performance data will not be processed! \n " , temp_command_name ) ;
2017-05-19 23:37:19 +02:00
my_free ( service_perfdata_command ) ;
2017-05-19 22:22:40 +02:00
}
/* free memory */
my_free ( temp_buffer ) ;
/* save the command pointer for later */
2017-05-19 23:37:19 +02:00
service_perfdata_command_ptr = temp_command ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
if ( host_perfdata_file_processing_command ! = NULL ) {
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
temp_buffer = ( char * ) strdup ( host_perfdata_file_processing_command ) ;
2017-05-19 22:22:40 +02:00
/* get the command name, leave any arguments behind */
temp_command_name = my_strtok ( temp_buffer , " ! " ) ;
if ( ( temp_command = find_command ( temp_command_name ) ) = = NULL ) {
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 ) ;
2017-05-19 23:37:19 +02:00
my_free ( host_perfdata_file_processing_command ) ;
2017-05-19 22:22:40 +02:00
}
/* free memory */
my_free ( temp_buffer ) ;
/* save the command pointer for later */
2017-05-19 23:37:19 +02:00
host_perfdata_file_processing_command_ptr = temp_command ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
if ( service_perfdata_file_processing_command ! = NULL ) {
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
temp_buffer = ( char * ) strdup ( service_perfdata_file_processing_command ) ;
2017-05-19 22:22:40 +02:00
/* get the command name, leave any arguments behind */
temp_command_name = my_strtok ( temp_buffer , " ! " ) ;
if ( ( temp_command = find_command ( temp_command_name ) ) = = NULL ) {
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 ) ;
2017-05-19 23:37:19 +02:00
my_free ( service_perfdata_file_processing_command ) ;
2017-05-19 22:22:40 +02:00
}
/* save the command pointer for later */
2017-05-19 23:37:19 +02:00
service_perfdata_file_processing_command_ptr = temp_command ;
2017-05-19 22:22:40 +02:00
}
/* periodically process the host perfdata file */
2017-05-19 23:37:19 +02:00
if ( host_perfdata_file_processing_interval > 0 & & host_perfdata_file_processing_command ! = NULL )
schedule_new_event ( EVENT_USER_FUNCTION , TRUE , current_time + host_perfdata_file_processing_interval , TRUE , host_perfdata_file_processing_interval , NULL , TRUE , ( void * ) xpddefault_process_host_perfdata_file , NULL , 0 ) ;
2017-05-19 22:22:40 +02:00
/* periodically process the service perfdata file */
2017-05-19 23:37:19 +02:00
if ( service_perfdata_file_processing_interval > 0 & & service_perfdata_file_processing_command ! = NULL )
schedule_new_event ( EVENT_USER_FUNCTION , TRUE , current_time + service_perfdata_file_processing_interval , TRUE , service_perfdata_file_processing_interval , NULL , TRUE , ( void * ) xpddefault_process_service_perfdata_file , NULL , 0 ) ;
2017-05-19 22:22:40 +02:00
/* save the host perf data file macro */
my_free ( mac - > x [ MACRO_HOSTPERFDATAFILE ] ) ;
2017-05-19 23:37:19 +02:00
if ( host_perfdata_file ! = NULL ) {
if ( ( mac - > x [ MACRO_HOSTPERFDATAFILE ] = ( char * ) strdup ( host_perfdata_file ) ) )
2017-05-19 22:22:40 +02:00
strip ( mac - > x [ MACRO_HOSTPERFDATAFILE ] ) ;
}
/* save the service perf data file macro */
my_free ( mac - > x [ MACRO_SERVICEPERFDATAFILE ] ) ;
2017-05-19 23:37:19 +02:00
if ( service_perfdata_file ! = NULL ) {
if ( ( mac - > x [ MACRO_SERVICEPERFDATAFILE ] = ( char * ) strdup ( service_perfdata_file ) ) )
2017-05-19 22:22:40 +02:00
strip ( mac - > x [ MACRO_SERVICEPERFDATAFILE ] ) ;
}
/* free memory */
my_free ( temp_buffer ) ;
my_free ( buffer ) ;
return OK ;
}
/* cleans up performance data */
2017-05-19 23:37:19 +02:00
int xpddefault_cleanup_performance_data ( void ) {
2017-05-19 22:22:40 +02:00
/* free memory */
2017-05-19 23:37:19 +02:00
my_free ( host_perfdata_command ) ;
my_free ( service_perfdata_command ) ;
my_free ( host_perfdata_file_template ) ;
my_free ( service_perfdata_file_template ) ;
my_free ( host_perfdata_file ) ;
my_free ( service_perfdata_file ) ;
my_free ( host_perfdata_file_processing_command ) ;
my_free ( service_perfdata_file_processing_command ) ;
2017-05-19 22:22:40 +02:00
/* close the files */
xpddefault_close_host_perfdata_file ( ) ;
xpddefault_close_service_perfdata_file ( ) ;
return OK ;
}
/******************************************************************/
/****************** PERFORMANCE DATA FUNCTIONS ********************/
/******************************************************************/
/* updates service performance data */
int xpddefault_update_service_performance_data ( service * svc ) {
nagios_macros mac ;
host * hst ;
/*
* bail early if we ' ve got nothing to do so we don ' t spend a lot
* of time calculating macros that never get used
* on distributed setups , empty perfdata results are required , so
* only drop out if demanded via configs .
*/
2017-05-19 23:37:19 +02:00
if ( service_perfdata_process_empty_results = = FALSE ) {
if ( ! svc | | ! svc - > perf_data | | ! * svc - > perf_data ) {
return OK ;
}
if ( ( ! service_perfdata_fp | | ! service_perfdata_file_template ) & & ! service_perfdata_command ) {
return OK ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
}
2017-05-19 22:22:40 +02:00
/*
* we know we ' ve got some work to do , so grab the necessary
* macros and get busy
*/
memset ( & mac , 0 , sizeof ( mac ) ) ;
hst = find_host ( svc - > host_name ) ;
grab_host_macros_r ( & mac , hst ) ;
grab_service_macros_r ( & mac , svc ) ;
2017-05-19 23:37:19 +02:00
grab_argv_macros_r ( & mac , svc - > check_command ) ;
2017-05-19 22:22:40 +02:00
/* run the performance data command */
xpddefault_run_service_performance_data_command ( & mac , svc ) ;
/* update the performance data file */
xpddefault_update_service_performance_data_file ( & mac , svc ) ;
2017-05-19 23:37:19 +02:00
/* get rid of used memory we won't need anymore */
clear_argv_macros_r ( & mac ) ;
2017-05-19 22:22:40 +02:00
/* now free() it all */
clear_volatile_macros_r ( & mac ) ;
return OK ;
}
/* updates host performance data */
int xpddefault_update_host_performance_data ( host * hst ) {
nagios_macros mac ;
/*
* bail early if we ' ve got nothing to do so we don ' t spend a lot
* of time calculating macros that never get used
2017-05-19 23:37:19 +02:00
* on distributed setups , empty perfdata results are required , so
* only drop out if demanded via configs .
2017-05-19 22:22:40 +02:00
*/
2017-05-19 23:37:19 +02:00
if ( host_perfdata_process_empty_results = = FALSE ) {
2017-05-19 22:22:40 +02:00
if ( ! hst | | ! hst - > perf_data | | ! * hst - > perf_data ) {
return OK ;
}
2017-05-19 23:37:19 +02:00
if ( ( ! host_perfdata_fp | | ! host_perfdata_file_template ) & & ! host_perfdata_command ) {
2017-05-19 22:22:40 +02:00
return OK ;
}
}
/* set up macros and get to work */
memset ( & mac , 0 , sizeof ( mac ) ) ;
grab_host_macros_r ( & mac , hst ) ;
2017-05-19 23:37:19 +02:00
grab_argv_macros_r ( & mac , hst - > check_command ) ;
2017-05-19 22:22:40 +02:00
/* run the performance data command */
xpddefault_run_host_performance_data_command ( & mac , hst ) ;
/* no more commands to run, so we won't need this any more */
clear_argv_macros_r ( & mac ) ;
/* update the performance data file */
xpddefault_update_host_performance_data_file ( & mac , hst ) ;
/* free() all */
clear_volatile_macros_r ( & mac ) ;
return OK ;
}
/******************************************************************/
/************** PERFORMANCE DATA COMMAND FUNCTIONS ****************/
/******************************************************************/
/* runs the service performance data command */
int xpddefault_run_service_performance_data_command ( nagios_macros * mac , service * svc ) {
char * raw_command_line = NULL ;
char * processed_command_line = NULL ;
int result = OK ;
int macro_options = STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS ;
log_debug_info ( DEBUGL_FUNCTIONS , 0 , " run_service_performance_data_command() \n " ) ;
if ( svc = = NULL )
return ERROR ;
/* we don't have a command */
2017-05-19 23:37:19 +02:00
if ( service_perfdata_command = = NULL )
2017-05-19 22:22:40 +02:00
return OK ;
/* get the raw command line */
2017-05-19 23:37:19 +02:00
get_raw_command_line_r ( mac , service_perfdata_command_ptr , service_perfdata_command , & raw_command_line , macro_options ) ;
2017-05-19 22:22:40 +02:00
if ( raw_command_line = = NULL )
return ERROR ;
log_debug_info ( DEBUGL_PERFDATA , 2 , " Raw service performance data command line: %s \n " , raw_command_line ) ;
/* process any macros in the raw command line */
process_macros_r ( mac , raw_command_line , & processed_command_line , macro_options ) ;
my_free ( raw_command_line ) ;
if ( processed_command_line = = NULL )
return ERROR ;
log_debug_info ( DEBUGL_PERFDATA , 2 , " Processed service performance data command line: %s \n " , processed_command_line ) ;
/* run the command */
2017-05-19 23:37:19 +02:00
wproc_run ( WPJOB_SVC_PERFDATA , processed_command_line , perfdata_timeout , NULL ) ;
2017-05-19 22:22:40 +02:00
/* free memory */
my_free ( processed_command_line ) ;
return result ;
}
/* runs the host performance data command */
int xpddefault_run_host_performance_data_command ( nagios_macros * mac , host * hst ) {
char * raw_command_line = NULL ;
char * processed_command_line = NULL ;
int result = OK ;
int macro_options = STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS ;
log_debug_info ( DEBUGL_FUNCTIONS , 0 , " run_host_performance_data_command() \n " ) ;
if ( hst = = NULL )
return ERROR ;
/* we don't have a command */
2017-05-19 23:37:19 +02:00
if ( host_perfdata_command = = NULL )
2017-05-19 22:22:40 +02:00
return OK ;
/* get the raw command line */
2017-05-19 23:37:19 +02:00
get_raw_command_line_r ( mac , host_perfdata_command_ptr , host_perfdata_command , & raw_command_line , macro_options ) ;
2017-05-19 22:22:40 +02:00
if ( raw_command_line = = NULL )
return ERROR ;
log_debug_info ( DEBUGL_PERFDATA , 2 , " Raw host performance data command line: %s \n " , raw_command_line ) ;
/* process any macros in the raw command line */
process_macros_r ( mac , raw_command_line , & processed_command_line , macro_options ) ;
my_free ( raw_command_line ) ;
2017-05-19 23:37:19 +02:00
if ( ! processed_command_line )
2017-05-19 22:22:40 +02:00
return ERROR ;
log_debug_info ( DEBUGL_PERFDATA , 2 , " Processed host performance data command line: %s \n " , processed_command_line ) ;
/* run the command */
2017-05-19 23:37:19 +02:00
wproc_run ( WPJOB_HOST_PERFDATA , processed_command_line , perfdata_timeout , NULL ) ;
2017-05-19 22:22:40 +02:00
/* free memory */
my_free ( processed_command_line ) ;
return result ;
}
/******************************************************************/
/**************** FILE PERFORMANCE DATA FUNCTIONS *****************/
/******************************************************************/
/* open the host performance data file for writing */
int xpddefault_open_host_perfdata_file ( void ) {
2017-05-19 23:37:19 +02:00
if ( host_perfdata_file ! = NULL ) {
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
if ( host_perfdata_file_pipe = = TRUE ) {
2017-05-19 22:22:40 +02:00
/* must open read-write to avoid failure if the other end isn't ready yet */
2017-05-19 23:37:19 +02:00
host_perfdata_fd = open ( host_perfdata_file , O_NONBLOCK | O_RDWR | O_CREAT , 0644 ) ;
host_perfdata_fp = fdopen ( host_perfdata_fd , " w " ) ;
2017-05-19 22:22:40 +02:00
}
else
2017-05-19 23:37:19 +02:00
host_perfdata_fp = fopen ( host_perfdata_file , ( host_perfdata_file_append = = TRUE ) ? " a " : " w " ) ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
if ( host_perfdata_fp = = NULL ) {
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
logit ( NSLOG_RUNTIME_WARNING , TRUE , " Warning: File '%s' could not be opened - host performance data will not be written to file! \n " , host_perfdata_file ) ;
2017-05-19 22:22:40 +02:00
return ERROR ;
}
}
return OK ;
}
/* open the service performance data file for writing */
int xpddefault_open_service_perfdata_file ( void ) {
2017-05-19 23:37:19 +02:00
if ( service_perfdata_file ! = NULL ) {
if ( service_perfdata_file_pipe = = TRUE ) {
2017-05-19 22:22:40 +02:00
/* must open read-write to avoid failure if the other end isn't ready yet */
2017-05-19 23:37:19 +02:00
service_perfdata_fd = open ( service_perfdata_file , O_NONBLOCK | O_RDWR ) ;
service_perfdata_fp = fdopen ( service_perfdata_fd , " w " ) ;
2017-05-19 22:22:40 +02:00
}
else
2017-05-19 23:37:19 +02:00
service_perfdata_fp = fopen ( service_perfdata_file , ( service_perfdata_file_append = = TRUE ) ? " a " : " w " ) ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
if ( service_perfdata_fp = = NULL ) {
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
logit ( NSLOG_RUNTIME_WARNING , TRUE , " Warning: File '%s' could not be opened - service performance data will not be written to file! \n " , service_perfdata_file ) ;
2017-05-19 22:22:40 +02:00
return ERROR ;
}
}
return OK ;
}
/* close the host performance data file */
int xpddefault_close_host_perfdata_file ( void ) {
2017-05-19 23:37:19 +02:00
if ( host_perfdata_fp ! = NULL )
fclose ( host_perfdata_fp ) ;
if ( host_perfdata_fd > = 0 ) {
close ( host_perfdata_fd ) ;
host_perfdata_fd = - 1 ;
2017-05-19 22:22:40 +02:00
}
return OK ;
}
/* close the service performance data file */
int xpddefault_close_service_perfdata_file ( void ) {
2017-05-19 23:37:19 +02:00
if ( service_perfdata_fp ! = NULL )
fclose ( service_perfdata_fp ) ;
if ( service_perfdata_fd > = 0 ) {
close ( service_perfdata_fd ) ;
service_perfdata_fd = - 1 ;
2017-05-19 22:22:40 +02:00
}
return OK ;
}
/* processes delimiter characters in templates */
int xpddefault_preprocess_file_templates ( char * template ) {
char * tempbuf ;
2017-05-19 23:37:19 +02:00
unsigned int x , y ;
2017-05-19 22:22:40 +02:00
if ( template = = NULL )
return OK ;
/* allocate temporary buffer */
tempbuf = ( char * ) malloc ( strlen ( template ) + 1 ) ;
if ( tempbuf = = NULL )
return ERROR ;
strcpy ( tempbuf , " " ) ;
for ( x = 0 , y = 0 ; x < strlen ( template ) ; x + + , y + + ) {
if ( template [ x ] = = ' \\ ' ) {
if ( template [ x + 1 ] = = ' t ' ) {
tempbuf [ y ] = ' \t ' ;
x + + ;
}
else if ( template [ x + 1 ] = = ' r ' ) {
tempbuf [ y ] = ' \r ' ;
x + + ;
}
else if ( template [ x + 1 ] = = ' n ' ) {
tempbuf [ y ] = ' \n ' ;
x + + ;
}
else
tempbuf [ y ] = template [ x ] ;
}
else
tempbuf [ y ] = template [ x ] ;
}
tempbuf [ y ] = ' \x0 ' ;
strcpy ( template , tempbuf ) ;
my_free ( tempbuf ) ;
return OK ;
}
/* updates service performance data file */
int xpddefault_update_service_performance_data_file ( nagios_macros * mac , service * svc ) {
char * raw_output = NULL ;
char * processed_output = NULL ;
int result = OK ;
log_debug_info ( DEBUGL_FUNCTIONS , 0 , " update_service_performance_data_file() \n " ) ;
if ( svc = = NULL )
return ERROR ;
/* we don't have a file to write to*/
2017-05-19 23:37:19 +02:00
if ( service_perfdata_fp = = NULL | | service_perfdata_file_template = = NULL )
2017-05-19 22:22:40 +02:00
return OK ;
/* get the raw line to write */
2017-05-19 23:37:19 +02:00
raw_output = ( char * ) strdup ( service_perfdata_file_template ) ;
2017-05-19 22:22:40 +02:00
log_debug_info ( DEBUGL_PERFDATA , 2 , " Raw service performance data file output: %s \n " , raw_output ) ;
/* process any macros in the raw output line */
process_macros_r ( mac , raw_output , & processed_output , 0 ) ;
if ( processed_output = = NULL )
return ERROR ;
log_debug_info ( DEBUGL_PERFDATA , 2 , " Processed service performance data file output: %s \n " , processed_output ) ;
2017-05-19 23:37:19 +02:00
/* write to host performance data file */
fputs ( processed_output , service_perfdata_fp ) ;
fputc ( ' \n ' , service_perfdata_fp ) ;
fflush ( service_perfdata_fp ) ;
2017-05-19 22:22:40 +02:00
/* free memory */
my_free ( raw_output ) ;
my_free ( processed_output ) ;
return result ;
}
/* updates host performance data file */
int xpddefault_update_host_performance_data_file ( nagios_macros * mac , host * hst ) {
char * raw_output = NULL ;
char * processed_output = NULL ;
int result = OK ;
log_debug_info ( DEBUGL_FUNCTIONS , 0 , " update_host_performance_data_file() \n " ) ;
if ( hst = = NULL )
return ERROR ;
/* we don't have a host perfdata file */
2017-05-19 23:37:19 +02:00
if ( host_perfdata_fp = = NULL | | host_perfdata_file_template = = NULL )
2017-05-19 22:22:40 +02:00
return OK ;
/* get the raw output */
2017-05-19 23:37:19 +02:00
raw_output = ( char * ) strdup ( host_perfdata_file_template ) ;
2017-05-19 22:22:40 +02:00
log_debug_info ( DEBUGL_PERFDATA , 2 , " Raw host performance file output: %s \n " , raw_output ) ;
/* process any macros in the raw output */
process_macros_r ( mac , raw_output , & processed_output , 0 ) ;
if ( processed_output = = NULL )
return ERROR ;
log_debug_info ( DEBUGL_PERFDATA , 2 , " Processed host performance data file output: %s \n " , processed_output ) ;
2017-05-19 23:37:19 +02:00
/* write to host performance data file */
fputs ( processed_output , host_perfdata_fp ) ;
fputc ( ' \n ' , host_perfdata_fp ) ;
fflush ( host_perfdata_fp ) ;
2017-05-19 22:22:40 +02:00
/* free memory */
my_free ( raw_output ) ;
my_free ( processed_output ) ;
return result ;
}
/* periodically process the host perf data file */
int xpddefault_process_host_perfdata_file ( void ) {
char * raw_command_line = NULL ;
char * processed_command_line = NULL ;
int early_timeout = FALSE ;
double exectime = 0.0 ;
int result = OK ;
int macro_options = STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS ;
nagios_macros mac ;
log_debug_info ( DEBUGL_FUNCTIONS , 0 , " process_host_perfdata_file() \n " ) ;
/* we don't have a command */
2017-05-19 23:37:19 +02:00
if ( host_perfdata_file_processing_command = = NULL )
2017-05-19 22:22:40 +02:00
return OK ;
/* init macros */
memset ( & mac , 0 , sizeof ( mac ) ) ;
/* get the raw command line */
2017-05-19 23:37:19 +02:00
get_raw_command_line_r ( & mac , host_perfdata_file_processing_command_ptr , host_perfdata_file_processing_command , & raw_command_line , macro_options ) ;
2017-05-19 22:22:40 +02:00
if ( raw_command_line = = NULL ) {
clear_volatile_macros_r ( & mac ) ;
return ERROR ;
}
log_debug_info ( DEBUGL_PERFDATA , 2 , " Raw host performance data file processing command line: %s \n " , raw_command_line ) ;
/* process any macros in the raw command line */
process_macros_r ( & mac , raw_command_line , & processed_command_line , macro_options ) ;
my_free ( raw_command_line ) ;
if ( processed_command_line = = NULL ) {
clear_volatile_macros_r ( & mac ) ;
return ERROR ;
}
log_debug_info ( DEBUGL_PERFDATA , 2 , " Processed host performance data file processing command line: %s \n " , processed_command_line ) ;
2017-05-19 23:37:19 +02:00
/* close the performance data file */
2017-05-19 22:22:40 +02:00
xpddefault_close_host_perfdata_file ( ) ;
/* run the command */
2017-05-19 23:37:19 +02:00
my_system_r ( & mac , processed_command_line , perfdata_timeout , & early_timeout , & exectime , NULL , 0 ) ;
2017-05-19 22:22:40 +02:00
clear_volatile_macros_r ( & mac ) ;
2017-05-19 23:37:19 +02:00
/* re-open the performance data file */
2017-05-19 22:22:40 +02:00
xpddefault_open_host_perfdata_file ( ) ;
/* check to see if the command timed out */
if ( early_timeout = = TRUE )
2017-05-19 23:37:19 +02:00
logit ( NSLOG_RUNTIME_WARNING , TRUE , " Warning: Host performance data file processing command '%s' timed out after %d seconds \n " , processed_command_line , perfdata_timeout ) ;
2017-05-19 22:22:40 +02:00
/* free memory */
my_free ( processed_command_line ) ;
return result ;
}
/* periodically process the service perf data file */
int xpddefault_process_service_perfdata_file ( void ) {
char * raw_command_line = NULL ;
char * processed_command_line = NULL ;
int early_timeout = FALSE ;
double exectime = 0.0 ;
int result = OK ;
int macro_options = STRIP_ILLEGAL_MACRO_CHARS | ESCAPE_MACRO_CHARS ;
nagios_macros mac ;
log_debug_info ( DEBUGL_FUNCTIONS , 0 , " process_service_perfdata_file() \n " ) ;
/* we don't have a command */
2017-05-19 23:37:19 +02:00
if ( service_perfdata_file_processing_command = = NULL )
2017-05-19 22:22:40 +02:00
return OK ;
/* init macros */
memset ( & mac , 0 , sizeof ( mac ) ) ;
/* get the raw command line */
2017-05-19 23:37:19 +02:00
get_raw_command_line_r ( & mac , service_perfdata_file_processing_command_ptr , service_perfdata_file_processing_command , & raw_command_line , macro_options ) ;
2017-05-19 22:22:40 +02:00
if ( raw_command_line = = NULL ) {
clear_volatile_macros_r ( & mac ) ;
return ERROR ;
}
log_debug_info ( DEBUGL_PERFDATA , 2 , " Raw service performance data file processing command line: %s \n " , raw_command_line ) ;
/* process any macros in the raw command line */
process_macros_r ( & mac , raw_command_line , & processed_command_line , macro_options ) ;
my_free ( raw_command_line ) ;
if ( processed_command_line = = NULL ) {
clear_volatile_macros_r ( & mac ) ;
return ERROR ;
}
log_debug_info ( DEBUGL_PERFDATA , 2 , " Processed service performance data file processing command line: %s \n " , processed_command_line ) ;
2017-05-19 23:37:19 +02:00
/* close the performance data file */
2017-05-19 22:22:40 +02:00
xpddefault_close_service_perfdata_file ( ) ;
/* run the command */
2017-05-19 23:37:19 +02:00
my_system_r ( & mac , processed_command_line , perfdata_timeout , & early_timeout , & exectime , NULL , 0 ) ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* re-open the performance data file */
2017-05-19 22:22:40 +02:00
xpddefault_open_service_perfdata_file ( ) ;
clear_volatile_macros_r ( & mac ) ;
/* check to see if the command timed out */
if ( early_timeout = = TRUE )
2017-05-19 23:37:19 +02:00
logit ( NSLOG_RUNTIME_WARNING , TRUE , " Warning: Service performance data file processing command '%s' timed out after %d seconds \n " , processed_command_line , perfdata_timeout ) ;
2017-05-19 22:22:40 +02:00
/* free memory */
my_free ( processed_command_line ) ;
return result ;
}