2017-05-19 22:22:40 +02:00
/*****************************************************************************
*
* OBJECTS . C - Object addition and search functions for Nagios
*
*
* License :
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include "../include/config.h"
# include "../include/common.h"
# include "../include/objects.h"
2017-05-19 23:37:19 +02:00
# include "../xdata/xodtemplate.h"
2017-05-19 22:22:40 +02:00
# ifdef NSCGI
# include "../include/cgiutils.h"
2017-05-19 23:37:19 +02:00
# else
# include "../include/nagios.h"
2017-05-19 22:22:40 +02:00
# endif
2017-05-19 23:37:19 +02:00
/*
* These get created in xdata / xodtemplate . c : xodtemplate_register_objects ( )
* Escalations are attached to the objects they belong to .
* Dependencies are attached to the dependent end of the object chain .
*/
dkhash_table * object_hash_tables [ NUM_OBJECT_SKIPLISTS ] ;
command * command_list = NULL ;
timeperiod * timeperiod_list = NULL ;
host * host_list = NULL ;
service * service_list = NULL ;
contact * contact_list = NULL ;
hostgroup * hostgroup_list = NULL ;
servicegroup * servicegroup_list = NULL ;
contactgroup * contactgroup_list = NULL ;
hostescalation * hostescalation_list = NULL ;
serviceescalation * serviceescalation_list = NULL ;
command * * command_ary = NULL ;
timeperiod * * timeperiod_ary = NULL ;
host * * host_ary = NULL ;
service * * service_ary = NULL ;
contact * * contact_ary = NULL ;
hostgroup * * hostgroup_ary = NULL ;
servicegroup * * servicegroup_ary = NULL ;
contactgroup * * contactgroup_ary = NULL ;
hostescalation * * hostescalation_ary = NULL ;
serviceescalation * * serviceescalation_ary = NULL ;
hostdependency * * hostdependency_ary = NULL ;
servicedependency * * servicedependency_ary = NULL ;
# ifndef NSCGI
int __nagios_object_structure_version = CURRENT_OBJECT_STRUCTURE_VERSION ;
struct flag_map {
int opt ;
int ch ;
const char * name ;
} ;
static const struct flag_map service_flag_map [ ] = {
{ OPT_WARNING , ' w ' , " warning " } ,
{ OPT_UNKNOWN , ' u ' , " unknown " } ,
{ OPT_CRITICAL , ' c ' , " critical " } ,
{ OPT_FLAPPING , ' f ' , " flapping " } ,
{ OPT_DOWNTIME , ' s ' , " downtime " } ,
{ OPT_OK , ' o ' , " ok " } ,
{ OPT_RECOVERY , ' r ' , " recovery " } ,
{ OPT_PENDING , ' p ' , " pending " } ,
{ 0 , 0 , NULL } ,
} ;
static const struct flag_map host_flag_map [ ] = {
{ OPT_DOWN , ' d ' , " down " } ,
{ OPT_UNREACHABLE , ' u ' , " unreachable " } ,
{ OPT_FLAPPING , ' f ' , " flapping " } ,
{ OPT_RECOVERY , ' r ' , " recovery " } ,
{ OPT_DOWNTIME , ' s ' , " downtime " } ,
{ OPT_PENDING , ' p ' , " pending " } ,
{ 0 , 0 , NULL } ,
} ;
static const char * opts2str ( int opts , const struct flag_map * map , char ok_char )
{
int i , pos = 0 ;
static char buf [ 16 ] ;
if ( ! opts )
return " n " ;
if ( opts = = OPT_ALL )
return " a " ;
if ( flag_isset ( opts , OPT_OK ) ) {
flag_unset ( opts , OPT_OK ) ;
buf [ pos + + ] = ok_char ;
buf [ pos + + ] = opts ? ' , ' : 0 ;
}
for ( i = 0 ; map [ i ] . name ; i + + ) {
if ( flag_isset ( opts , map [ i ] . opt ) ) {
buf [ pos + + ] = map [ i ] . ch ;
flag_unset ( opts , map [ i ] . opt ) ;
if ( ! opts )
break ;
buf [ pos + + ] = ' , ' ;
}
}
buf [ pos + + ] = 0 ;
return buf ;
}
2017-05-19 22:22:40 +02:00
# endif
2017-05-19 23:37:19 +02:00
unsigned int host_services_value ( host * h ) {
servicesmember * sm ;
unsigned int ret = 0 ;
for ( sm = h - > services ; sm ; sm = sm - > next ) {
ret + = sm - > service_ptr - > hourly_value ;
}
return ret ;
}
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
# ifndef NSCGI
/* Host/Service dependencies are not visible in Nagios CGIs, so we exclude them */
static int cmp_sdep ( const void * a_ , const void * b_ ) {
const servicedependency * a = * ( servicedependency * * ) a_ ;
const servicedependency * b = * ( servicedependency * * ) b_ ;
int ret ;
ret = a - > master_service_ptr - > id - b - > master_service_ptr - > id ;
return ret ? ret : ( int ) ( a - > dependent_service_ptr - > id - b - > dependent_service_ptr - > id ) ;
}
static int cmp_hdep ( const void * a_ , const void * b_ ) {
const hostdependency * a = * ( const hostdependency * * ) a_ ;
const hostdependency * b = * ( const hostdependency * * ) b_ ;
int ret ;
ret = a - > master_host_ptr - > id - b - > master_host_ptr - > id ;
return ret ? ret : ( int ) ( a - > dependent_host_ptr - > id - b - > dependent_host_ptr - > id ) ;
}
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
static int cmp_serviceesc ( const void * a_ , const void * b_ ) {
const serviceescalation * a = * ( const serviceescalation * * ) a_ ;
const serviceescalation * b = * ( const serviceescalation * * ) b_ ;
return a - > service_ptr - > id - b - > service_ptr - > id ;
}
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
static int cmp_hostesc ( const void * a_ , const void * b_ ) {
const hostescalation * a = * ( const hostescalation * * ) a_ ;
const hostescalation * b = * ( const hostescalation * * ) b_ ;
return a - > host_ptr - > id - b - > host_ptr - > id ;
}
2017-05-19 22:22:40 +02:00
# endif
2017-05-19 23:37:19 +02:00
static void post_process_object_config ( void ) {
objectlist * list ;
unsigned int i , slot ;
if ( hostdependency_ary )
free ( hostdependency_ary ) ;
if ( servicedependency_ary )
free ( servicedependency_ary ) ;
hostdependency_ary = calloc ( num_objects . hostdependencies , sizeof ( void * ) ) ;
servicedependency_ary = calloc ( num_objects . servicedependencies , sizeof ( void * ) ) ;
slot = 0 ;
for ( i = 0 ; slot < num_objects . servicedependencies & & i < num_objects . services ; i + + ) {
service * s = service_ary [ i ] ;
for ( list = s - > notify_deps ; list ; list = list - > next )
servicedependency_ary [ slot + + ] = ( servicedependency * ) list - > object_ptr ;
for ( list = s - > exec_deps ; list ; list = list - > next )
servicedependency_ary [ slot + + ] = ( servicedependency * ) list - > object_ptr ;
}
timing_point ( " Done post-processing servicedependencies \n " ) ;
slot = 0 ;
for ( i = 0 ; slot < num_objects . hostdependencies & & i < num_objects . hosts ; i + + ) {
host * h = host_ary [ i ] ;
for ( list = h - > notify_deps ; list ; list = list - > next )
hostdependency_ary [ slot + + ] = ( hostdependency * ) list - > object_ptr ;
for ( list = h - > exec_deps ; list ; list = list - > next )
hostdependency_ary [ slot + + ] = ( hostdependency * ) list - > object_ptr ;
}
timing_point ( " Done post-processing host dependencies \n " ) ;
# ifndef NSCGI
/* cgi's always get their objects in sorted order */
if ( servicedependency_ary )
qsort ( servicedependency_ary , num_objects . servicedependencies , sizeof ( servicedependency * ) , cmp_sdep ) ;
if ( hostdependency_ary )
qsort ( hostdependency_ary , num_objects . hostdependencies , sizeof ( hostdependency * ) , cmp_hdep ) ;
if ( hostescalation_ary )
qsort ( hostescalation_ary , num_objects . hostescalations , sizeof ( hostescalation * ) , cmp_hostesc ) ;
if ( serviceescalation_ary )
qsort ( serviceescalation_ary , num_objects . serviceescalations , sizeof ( serviceescalation * ) , cmp_serviceesc ) ;
timing_point ( " Done post-sorting slave objects \n " ) ;
# endif
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
timeperiod_list = timeperiod_ary ? * timeperiod_ary : NULL ;
command_list = command_ary ? * command_ary : NULL ;
hostgroup_list = hostgroup_ary ? * hostgroup_ary : NULL ;
contactgroup_list = contactgroup_ary ? * contactgroup_ary : NULL ;
servicegroup_list = servicegroup_ary ? * servicegroup_ary : NULL ;
contact_list = contact_ary ? * contact_ary : NULL ;
host_list = host_ary ? * host_ary : NULL ;
service_list = service_ary ? * service_ary : NULL ;
hostescalation_list = hostescalation_ary ? * hostescalation_ary : NULL ;
serviceescalation_list = serviceescalation_ary ? * serviceescalation_ary : NULL ;
}
/* simple state-name helpers, nifty to have all over the place */
const char * service_state_name ( int state )
{
switch ( state ) {
case STATE_OK : return " OK " ;
case STATE_WARNING : return " WARNING " ;
case STATE_CRITICAL : return " CRITICAL " ;
}
return " UNKNOWN " ;
}
const char * host_state_name ( int state )
{
switch ( state ) {
case HOST_UP : return " UP " ;
case HOST_DOWN : return " DOWN " ;
case HOST_UNREACHABLE : return " UNREACHABLE " ;
}
return " (unknown) " ;
}
const char * state_type_name ( int state_type )
{
return state_type = = HARD_STATE ? " HARD " : " SOFT " ;
}
const char * check_type_name ( int check_type )
{
return check_type = = CHECK_TYPE_PASSIVE ? " PASSIVE " : " ACTIVE " ;
}
2017-05-19 22:22:40 +02:00
/******************************************************************/
/******* TOP-LEVEL HOST CONFIGURATION DATA INPUT FUNCTION *********/
/******************************************************************/
/* read all host configuration data from external source */
2017-05-19 23:37:19 +02:00
int read_object_config_data ( const char * main_config_file , int options ) {
2017-05-19 22:22:40 +02:00
int result = OK ;
2017-05-19 23:37:19 +02:00
/* reset object counts */
memset ( & num_objects , 0 , sizeof ( num_objects ) ) ;
2017-05-19 22:22:40 +02:00
/* read in data from all text host config files (template-based) */
2017-05-19 23:37:19 +02:00
result = xodtemplate_read_config_data ( main_config_file , options ) ;
2017-05-19 22:22:40 +02:00
if ( result ! = OK )
return ERROR ;
2017-05-19 23:37:19 +02:00
/* handle any remaining config mangling */
post_process_object_config ( ) ;
timing_point ( " Done post-processing configuration \n " ) ;
2017-05-19 22:22:40 +02:00
return result ;
}
/******************************************************************/
/******************** SKIPLIST FUNCTIONS **************************/
/******************************************************************/
int skiplist_compare_text ( const char * val1a , const char * val1b , const char * val2a , const char * val2b ) {
int result = 0 ;
/* check first name */
if ( val1a = = NULL & & val2a = = NULL )
result = 0 ;
else if ( val1a = = NULL )
result = 1 ;
else if ( val2a = = NULL )
result = - 1 ;
else
result = strcmp ( val1a , val2a ) ;
/* check second name if necessary */
if ( result = = 0 ) {
if ( val1b = = NULL & & val2b = = NULL )
result = 0 ;
else if ( val1b = = NULL )
result = 1 ;
else if ( val2b = = NULL )
result = - 1 ;
else
result = strcmp ( val1b , val2b ) ;
}
return result ;
}
2017-05-19 23:37:19 +02:00
int get_host_count ( void ) {
return num_objects . hosts ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
int get_service_count ( void ) {
return num_objects . services ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
/******************************************************************/
/**************** OBJECT ADDITION FUNCTIONS ***********************/
/******************************************************************/
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
static int create_object_table ( const char * name , unsigned int elems , unsigned int size , void * * ptr )
{
void * ret ;
if ( ! elems ) {
* ptr = NULL ;
return OK ;
}
ret = calloc ( elems , size ) ;
if ( ! ret ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Failed to allocate %s table with %u elements \n " , name , elems ) ;
return ERROR ;
}
* ptr = ret ;
return OK ;
}
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
# define mktable(name, id) \
create_object_table ( # name , ocount [ id ] , sizeof ( name * ) , ( void * * ) & name # # _ary )
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* ocount is an array with NUM_OBJECT_TYPES members */
int create_object_tables ( unsigned int * ocount )
{
int i ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
for ( i = 0 ; i < NUM_HASHED_OBJECT_TYPES ; i + + ) {
const unsigned int hash_size = ocount [ i ] ;
if ( ! hash_size )
continue ;
object_hash_tables [ i ] = dkhash_create ( hash_size ) ;
if ( ! object_hash_tables [ i ] ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Failed to create hash table with %u entries \n " , hash_size ) ;
}
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
/*
* errors here will always lead to an early exit , so there ' s no need
* to free ( ) successful allocs when later ones fail
*/
if ( mktable ( timeperiod , TIMEPERIOD_SKIPLIST ) ! = OK )
return ERROR ;
if ( mktable ( command , COMMAND_SKIPLIST ) ! = OK )
return ERROR ;
if ( mktable ( host , HOST_SKIPLIST ) ! = OK )
return ERROR ;
if ( mktable ( service , SERVICE_SKIPLIST ) ! = OK )
return ERROR ;
if ( mktable ( contact , CONTACT_SKIPLIST ) ! = OK )
return ERROR ;
if ( mktable ( hostgroup , HOSTGROUP_SKIPLIST ) ! = OK )
return ERROR ;
if ( mktable ( servicegroup , SERVICEGROUP_SKIPLIST ) ! = OK )
return ERROR ;
if ( mktable ( contactgroup , CONTACTGROUP_SKIPLIST ) ! = OK )
return ERROR ;
if ( mktable ( hostescalation , HOSTESCALATION_SKIPLIST ) ! = OK )
return ERROR ;
if ( mktable ( hostdependency , HOSTDEPENDENCY_SKIPLIST ) ! = OK )
return ERROR ;
if ( mktable ( serviceescalation , SERVICEESCALATION_SKIPLIST ) ! = OK )
return ERROR ;
if ( mktable ( servicedependency , SERVICEDEPENDENCY_SKIPLIST ) ! = OK )
return ERROR ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
return OK ;
}
2017-05-19 22:22:40 +02:00
/* add a new timeperiod to the list in memory */
timeperiod * add_timeperiod ( char * name , char * alias ) {
timeperiod * new_timeperiod = NULL ;
int result = OK ;
/* make sure we have the data we need */
if ( ( name = = NULL | | ! strcmp ( name , " " ) ) | | ( alias = = NULL | | ! strcmp ( alias , " " ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Name or alias for timeperiod is NULL \n " ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
new_timeperiod = calloc ( 1 , sizeof ( * new_timeperiod ) ) ;
if ( ! new_timeperiod )
2017-05-19 22:22:40 +02:00
return NULL ;
/* copy string vars */
2017-05-19 23:37:19 +02:00
new_timeperiod - > name = name ;
new_timeperiod - > alias = alias ? alias : name ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* add new timeperiod to hash table */
2017-05-19 22:22:40 +02:00
if ( result = = OK ) {
2017-05-19 23:37:19 +02:00
result = dkhash_insert ( object_hash_tables [ TIMEPERIOD_SKIPLIST ] , new_timeperiod - > name , NULL , new_timeperiod ) ;
2017-05-19 22:22:40 +02:00
switch ( result ) {
2017-05-19 23:37:19 +02:00
case DKHASH_EDUPE :
2017-05-19 22:22:40 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Timeperiod '%s' has already been defined \n " , name ) ;
result = ERROR ;
break ;
2017-05-19 23:37:19 +02:00
case DKHASH_OK :
2017-05-19 22:22:40 +02:00
result = OK ;
break ;
default :
2017-05-19 23:37:19 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Could not add timeperiod '%s' to hash table \n " , name ) ;
2017-05-19 22:22:40 +02:00
result = ERROR ;
break ;
}
}
/* handle errors */
if ( result = = ERROR ) {
2017-05-19 23:37:19 +02:00
free ( new_timeperiod ) ;
2017-05-19 22:22:40 +02:00
return NULL ;
}
2017-05-19 23:37:19 +02:00
new_timeperiod - > id = num_objects . timeperiods + + ;
if ( new_timeperiod - > id )
timeperiod_ary [ new_timeperiod - > id - 1 ] - > next = new_timeperiod ;
timeperiod_ary [ new_timeperiod - > id ] = new_timeperiod ;
2017-05-19 22:22:40 +02:00
return new_timeperiod ;
}
/* adds a new exclusion to a timeperiod */
timeperiodexclusion * add_exclusion_to_timeperiod ( timeperiod * period , char * name ) {
timeperiodexclusion * new_timeperiodexclusion = NULL ;
/* make sure we have enough data */
if ( period = = NULL | | name = = NULL )
return NULL ;
if ( ( new_timeperiodexclusion = ( timeperiodexclusion * ) malloc ( sizeof ( timeperiodexclusion ) ) ) = = NULL )
return NULL ;
new_timeperiodexclusion - > timeperiod_name = ( char * ) strdup ( name ) ;
new_timeperiodexclusion - > next = period - > exclusions ;
period - > exclusions = new_timeperiodexclusion ;
return new_timeperiodexclusion ;
}
/* add a new timerange to a timeperiod */
timerange * add_timerange_to_timeperiod ( timeperiod * period , int day , unsigned long start_time , unsigned long end_time ) {
2017-05-19 23:37:19 +02:00
timerange * prev = NULL , * tr , * new_timerange = NULL ;
2017-05-19 22:22:40 +02:00
/* make sure we have the data we need */
if ( period = = NULL )
return NULL ;
if ( day < 0 | | day > 6 ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Day %d is not valid for timeperiod '%s' \n " , day , period - > name ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
if ( start_time > 86400 ) {
2017-05-19 22:22:40 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Start time %lu on day %d is not valid for timeperiod '%s' \n " , start_time , day , period - > name ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
if ( end_time > 86400 ) {
2017-05-19 22:22:40 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: End time %lu on day %d is not value for timeperiod '%s' \n " , end_time , day , period - > name ) ;
return NULL ;
}
/* allocate memory for the new time range */
if ( ( new_timerange = malloc ( sizeof ( timerange ) ) ) = = NULL )
return NULL ;
new_timerange - > range_start = start_time ;
new_timerange - > range_end = end_time ;
2017-05-19 23:37:19 +02:00
/* insertion-sort the new time range into the list for this day */
if ( ! period - > days [ day ] | | period - > days [ day ] - > range_start > new_timerange - > range_start ) {
new_timerange - > next = period - > days [ day ] ;
period - > days [ day ] = new_timerange ;
return new_timerange ;
}
for ( tr = period - > days [ day ] ; tr ; tr = tr - > next ) {
if ( new_timerange - > range_start < tr - > range_start ) {
new_timerange - > next = tr ;
prev - > next = new_timerange ;
break ;
}
if ( ! tr - > next ) {
tr - > next = new_timerange ;
new_timerange - > next = NULL ;
break ;
}
prev = tr ;
}
2017-05-19 22:22:40 +02:00
return new_timerange ;
}
/* add a new exception to a timeperiod */
daterange * add_exception_to_timeperiod ( timeperiod * period , int type , int syear , int smon , int smday , int swday , int swday_offset , int eyear , int emon , int emday , int ewday , int ewday_offset , int skip_interval ) {
daterange * new_daterange = NULL ;
/* make sure we have the data we need */
if ( period = = NULL )
return NULL ;
/* allocate memory for the date range range */
if ( ( new_daterange = malloc ( sizeof ( daterange ) ) ) = = NULL )
return NULL ;
new_daterange - > times = NULL ;
new_daterange - > next = NULL ;
new_daterange - > type = type ;
new_daterange - > syear = syear ;
new_daterange - > smon = smon ;
new_daterange - > smday = smday ;
new_daterange - > swday = swday ;
new_daterange - > swday_offset = swday_offset ;
new_daterange - > eyear = eyear ;
new_daterange - > emon = emon ;
new_daterange - > emday = emday ;
new_daterange - > ewday = ewday ;
new_daterange - > ewday_offset = ewday_offset ;
new_daterange - > skip_interval = skip_interval ;
/* add the new date range to the head of the range list for this exception type */
new_daterange - > next = period - > exceptions [ type ] ;
period - > exceptions [ type ] = new_daterange ;
return new_daterange ;
}
/* add a new timerange to a daterange */
timerange * add_timerange_to_daterange ( daterange * drange , unsigned long start_time , unsigned long end_time ) {
timerange * new_timerange = NULL ;
/* make sure we have the data we need */
if ( drange = = NULL )
return NULL ;
2017-05-19 23:37:19 +02:00
if ( start_time > 86400 ) {
2017-05-19 22:22:40 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Start time %lu is not valid for timeperiod \n " , start_time ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
if ( end_time > 86400 ) {
2017-05-19 22:22:40 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: End time %lu is not value for timeperiod \n " , end_time ) ;
return NULL ;
}
/* allocate memory for the new time range */
if ( ( new_timerange = malloc ( sizeof ( timerange ) ) ) = = NULL )
return NULL ;
new_timerange - > range_start = start_time ;
new_timerange - > range_end = end_time ;
/* add the new time range to the head of the range list for this date range */
new_timerange - > next = drange - > times ;
drange - > times = new_timerange ;
return new_timerange ;
}
/* add a new host definition */
2017-05-19 23:37:19 +02:00
host * add_host ( char * name , char * display_name , char * alias , char * address , char * check_period , int initial_state , double check_interval , double retry_interval , int max_attempts , int notification_options , double notification_interval , double first_notification_delay , char * notification_period , int notifications_enabled , char * check_command , int checks_enabled , int accept_passive_checks , char * event_handler , int event_handler_enabled , int flap_detection_enabled , double low_flap_threshold , double high_flap_threshold , int flap_detection_options , int stalking_options , int process_perfdata , int check_freshness , int freshness_threshold , char * notes , char * notes_url , char * action_url , char * icon_image , char * icon_image_alt , char * vrml_image , char * statusmap_image , int x_2d , int y_2d , int have_2d_coords , double x_3d , double y_3d , double z_3d , int have_3d_coords , int should_be_drawn , int retain_status_information , int retain_nonstatus_information , int obsess , unsigned int hourly_value ) {
2017-05-19 22:22:40 +02:00
host * new_host = NULL ;
2017-05-19 23:37:19 +02:00
timeperiod * check_tp = NULL , * notify_tp = NULL ;
2017-05-19 22:22:40 +02:00
int result = OK ;
/* make sure we have the data we need */
2017-05-19 23:37:19 +02:00
if ( name = = NULL | | ! strcmp ( name , " " ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Host name is NULL \n " ) ;
2017-05-19 22:22:40 +02:00
return NULL ;
}
2017-05-19 23:37:19 +02:00
if ( check_period & & ! ( check_tp = find_timeperiod ( check_period ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Failed to locate check_period '%s' for host '%s'! \n " ,
check_period , name ) ;
return NULL ;
}
if ( notification_period & & ! ( notify_tp = find_timeperiod ( notification_period ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Failed to locate notification_period '%s' for host '%s'! \n " ,
notification_period , name ) ;
return NULL ;
}
2017-05-19 22:22:40 +02:00
/* check values */
if ( max_attempts < = 0 ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Invalid max_check_attempts value for host '%s' \n " , name ) ;
return NULL ;
}
if ( check_interval < 0 ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Invalid check_interval value for host '%s' \n " , name ) ;
return NULL ;
}
if ( notification_interval < 0 ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Invalid notification_interval value for host '%s' \n " , name ) ;
return NULL ;
}
if ( first_notification_delay < 0 ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Invalid first_notification_delay value for host '%s' \n " , name ) ;
return NULL ;
}
if ( freshness_threshold < 0 ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Invalid freshness_threshold value for host '%s' \n " , name ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
new_host = calloc ( 1 , sizeof ( * new_host ) ) ;
/* assign string vars */
new_host - > name = name ;
new_host - > display_name = display_name ? display_name : new_host - > name ;
new_host - > alias = alias ? alias : new_host - > name ;
new_host - > address = address ? address : new_host - > name ;
new_host - > check_period = check_tp ? ( char * ) strdup ( check_tp - > name ) : NULL ;
new_host - > notification_period = notify_tp ? ( char * ) strdup ( notify_tp - > name ) : NULL ;
new_host - > notification_period_ptr = notify_tp ;
new_host - > check_period_ptr = check_tp ;
new_host - > check_command = check_command ;
new_host - > event_handler = event_handler ;
new_host - > notes = notes ;
new_host - > notes_url = notes_url ;
new_host - > action_url = action_url ;
new_host - > icon_image = icon_image ;
new_host - > icon_image_alt = icon_image_alt ;
new_host - > vrml_image = vrml_image ;
new_host - > statusmap_image = statusmap_image ;
2017-05-19 22:22:40 +02:00
/* duplicate non-string vars */
2017-05-19 23:37:19 +02:00
new_host - > hourly_value = hourly_value ;
2017-05-19 22:22:40 +02:00
new_host - > max_attempts = max_attempts ;
new_host - > check_interval = check_interval ;
new_host - > retry_interval = retry_interval ;
new_host - > notification_interval = notification_interval ;
new_host - > first_notification_delay = first_notification_delay ;
2017-05-19 23:37:19 +02:00
new_host - > notification_options = notification_options ;
2017-05-19 22:22:40 +02:00
new_host - > flap_detection_enabled = ( flap_detection_enabled > 0 ) ? TRUE : FALSE ;
new_host - > low_flap_threshold = low_flap_threshold ;
new_host - > high_flap_threshold = high_flap_threshold ;
2017-05-19 23:37:19 +02:00
new_host - > flap_detection_options = flap_detection_options ;
new_host - > stalking_options = stalking_options ;
2017-05-19 22:22:40 +02:00
new_host - > process_performance_data = ( process_perfdata > 0 ) ? TRUE : FALSE ;
new_host - > check_freshness = ( check_freshness > 0 ) ? TRUE : FALSE ;
new_host - > freshness_threshold = freshness_threshold ;
new_host - > checks_enabled = ( checks_enabled > 0 ) ? TRUE : FALSE ;
2017-05-19 23:37:19 +02:00
new_host - > accept_passive_checks = ( accept_passive_checks > 0 ) ? TRUE : FALSE ;
2017-05-19 22:22:40 +02:00
new_host - > event_handler_enabled = ( event_handler_enabled > 0 ) ? TRUE : FALSE ;
new_host - > x_2d = x_2d ;
new_host - > y_2d = y_2d ;
new_host - > have_2d_coords = ( have_2d_coords > 0 ) ? TRUE : FALSE ;
new_host - > x_3d = x_3d ;
new_host - > y_3d = y_3d ;
new_host - > z_3d = z_3d ;
new_host - > have_3d_coords = ( have_3d_coords > 0 ) ? TRUE : FALSE ;
new_host - > should_be_drawn = ( should_be_drawn > 0 ) ? TRUE : FALSE ;
2017-05-19 23:37:19 +02:00
new_host - > obsess = ( obsess > 0 ) ? TRUE : FALSE ;
2017-05-19 22:22:40 +02:00
new_host - > retain_status_information = ( retain_status_information > 0 ) ? TRUE : FALSE ;
new_host - > retain_nonstatus_information = ( retain_nonstatus_information > 0 ) ? TRUE : FALSE ;
# ifdef NSCORE
new_host - > current_state = initial_state ;
new_host - > last_state = initial_state ;
new_host - > last_hard_state = initial_state ;
2017-05-19 23:37:19 +02:00
new_host - > check_type = CHECK_TYPE_ACTIVE ;
2017-05-19 22:22:40 +02:00
new_host - > should_be_scheduled = TRUE ;
new_host - > current_attempt = ( initial_state = = HOST_UP ) ? 1 : max_attempts ;
new_host - > state_type = HARD_STATE ;
new_host - > acknowledgement_type = ACKNOWLEDGEMENT_NONE ;
new_host - > notifications_enabled = ( notifications_enabled > 0 ) ? TRUE : FALSE ;
new_host - > check_options = CHECK_OPTION_NONE ;
# endif
2017-05-19 23:37:19 +02:00
/* add new host to hash table */
2017-05-19 22:22:40 +02:00
if ( result = = OK ) {
2017-05-19 23:37:19 +02:00
result = dkhash_insert ( object_hash_tables [ HOST_SKIPLIST ] , new_host - > name , NULL , new_host ) ;
2017-05-19 22:22:40 +02:00
switch ( result ) {
2017-05-19 23:37:19 +02:00
case DKHASH_EDUPE :
2017-05-19 22:22:40 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Host '%s' has already been defined \n " , name ) ;
result = ERROR ;
break ;
2017-05-19 23:37:19 +02:00
case DKHASH_OK :
2017-05-19 22:22:40 +02:00
result = OK ;
break ;
default :
2017-05-19 23:37:19 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Could not add host '%s' to hash table \n " , name ) ;
2017-05-19 22:22:40 +02:00
result = ERROR ;
break ;
}
}
/* handle errors */
if ( result = = ERROR ) {
my_free ( new_host ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
new_host - > id = num_objects . hosts + + ;
host_ary [ new_host - > id ] = new_host ;
if ( new_host - > id )
host_ary [ new_host - > id - 1 ] - > next = new_host ;
2017-05-19 22:22:40 +02:00
return new_host ;
}
hostsmember * add_parent_host_to_host ( host * hst , char * host_name ) {
hostsmember * new_hostsmember = NULL ;
int result = OK ;
/* make sure we have the data we need */
if ( hst = = NULL | | host_name = = NULL | | ! strcmp ( host_name , " " ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Host is NULL or parent host name is NULL \n " ) ;
return NULL ;
}
/* a host cannot be a parent/child of itself */
if ( ! strcmp ( host_name , hst - > name ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Host '%s' cannot be a child/parent of itself \n " , hst - > name ) ;
return NULL ;
}
/* allocate memory */
if ( ( new_hostsmember = ( hostsmember * ) calloc ( 1 , sizeof ( hostsmember ) ) ) = = NULL )
return NULL ;
/* duplicate string vars */
if ( ( new_hostsmember - > host_name = ( char * ) strdup ( host_name ) ) = = NULL )
result = ERROR ;
/* handle errors */
if ( result = = ERROR ) {
my_free ( new_hostsmember - > host_name ) ;
my_free ( new_hostsmember ) ;
return NULL ;
}
/* add the parent host entry to the host definition */
new_hostsmember - > next = hst - > parent_hosts ;
hst - > parent_hosts = new_hostsmember ;
return new_hostsmember ;
}
2017-05-19 23:37:19 +02:00
servicesmember * add_parent_service_to_service ( service * svc , char * host_name , char * description ) {
servicesmember * sm ;
if ( ! svc | | ! host_name | | ! description | | ! * host_name | | ! * description )
return NULL ;
if ( ( sm = calloc ( 1 , sizeof ( * sm ) ) ) = = NULL )
return NULL ;
if ( strcmp ( svc - > host_name , host_name ) = = 0 & & strcmp ( svc - > description , description ) = = 0 ) {
logit ( NSLOG_CONFIG_ERROR , TRUE ,
" Error: Host '%s' Service '%s' cannot be a child/parent of itself \n " ,
svc - > host_name , svc - > description ) ;
return NULL ;
}
if ( ( sm - > host_name = strdup ( host_name ) ) = = NULL | | ( sm - > service_description = strdup ( description ) ) = = NULL ) {
/* there was an error copying (description is NULL now) */
my_free ( sm - > host_name ) ;
free ( sm ) ;
return NULL ;
}
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
sm - > next = svc - > parents ;
svc - > parents = sm ;
return sm ;
}
2017-05-19 22:22:40 +02:00
hostsmember * add_child_link_to_host ( host * hst , host * child_ptr ) {
hostsmember * new_hostsmember = NULL ;
/* make sure we have the data we need */
if ( hst = = NULL | | child_ptr = = NULL )
return NULL ;
/* allocate memory */
if ( ( new_hostsmember = ( hostsmember * ) malloc ( sizeof ( hostsmember ) ) ) = = NULL )
return NULL ;
2017-05-19 23:37:19 +02:00
/* assign values */
new_hostsmember - > host_name = child_ptr - > name ;
2017-05-19 22:22:40 +02:00
new_hostsmember - > host_ptr = child_ptr ;
/* add the child entry to the host definition */
new_hostsmember - > next = hst - > child_hosts ;
hst - > child_hosts = new_hostsmember ;
return new_hostsmember ;
}
servicesmember * add_service_link_to_host ( host * hst , service * service_ptr ) {
servicesmember * new_servicesmember = NULL ;
/* make sure we have the data we need */
if ( hst = = NULL | | service_ptr = = NULL )
return NULL ;
/* allocate memory */
if ( ( new_servicesmember = ( servicesmember * ) calloc ( 1 , sizeof ( servicesmember ) ) ) = = NULL )
return NULL ;
2017-05-19 23:37:19 +02:00
/* assign values */
new_servicesmember - > host_name = service_ptr - > host_name ;
new_servicesmember - > service_description = service_ptr - > description ;
2017-05-19 22:22:40 +02:00
new_servicesmember - > service_ptr = service_ptr ;
2017-05-19 23:37:19 +02:00
# ifndef NSCGI
hst - > total_services + + ;
2017-05-19 22:22:40 +02:00
# endif
/* add the child entry to the host definition */
new_servicesmember - > next = hst - > services ;
hst - > services = new_servicesmember ;
return new_servicesmember ;
}
2017-05-19 23:37:19 +02:00
servicesmember * add_child_link_to_service ( service * svc , service * child_ptr ) {
servicesmember * new_servicesmember = NULL ;
2017-05-19 22:22:40 +02:00
/* make sure we have the data we need */
2017-05-19 23:37:19 +02:00
if ( svc = = NULL | | child_ptr = = NULL )
2017-05-19 22:22:40 +02:00
return NULL ;
2017-05-19 23:37:19 +02:00
/* allocate memory */
if ( ( new_servicesmember = ( servicesmember * ) malloc ( sizeof ( servicesmember ) ) )
= = NULL ) return NULL ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* assign values */
new_servicesmember - > host_name = child_ptr - > host_name ;
new_servicesmember - > service_description = child_ptr - > description ;
new_servicesmember - > service_ptr = child_ptr ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* add the child entry to the host definition */
new_servicesmember - > next = svc - > children ;
svc - > children = new_servicesmember ;
return new_servicesmember ;
}
static contactgroupsmember * add_contactgroup_to_object ( contactgroupsmember * * cg_list , const char * group_name ) {
contactgroupsmember * cgm ;
contactgroup * cg ;
if ( ! group_name | | ! * group_name ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Contact name is NULL \n " ) ;
return NULL ;
}
if ( ! ( cg = find_contactgroup ( group_name ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Contactgroup '%s' is not defined anywhere \n " , group_name ) ;
2017-05-19 22:22:40 +02:00
return NULL ;
}
2017-05-19 23:37:19 +02:00
if ( ! ( cgm = malloc ( sizeof ( * cgm ) ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Could not allocate memory for contactgroup \n " ) ;
return NULL ;
}
cgm - > group_name = cg - > group_name ;
cgm - > group_ptr = cg ;
cgm - > next = * cg_list ;
* cg_list = cgm ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
return cgm ;
}
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* add a new contactgroup to a host */
contactgroupsmember * add_contactgroup_to_host ( host * hst , char * group_name ) {
return add_contactgroup_to_object ( & hst - > contact_groups , group_name ) ;
2017-05-19 22:22:40 +02:00
}
/* adds a contact to a host */
contactsmember * add_contact_to_host ( host * hst , char * contact_name ) {
return add_contact_to_object ( & hst - > contacts , contact_name ) ;
}
/* adds a custom variable to a host */
customvariablesmember * add_custom_variable_to_host ( host * hst , char * varname , char * varvalue ) {
return add_custom_variable_to_object ( & hst - > custom_variables , varname , varvalue ) ;
}
/* add a new host group to the list in memory */
hostgroup * add_hostgroup ( char * name , char * alias , char * notes , char * notes_url , char * action_url ) {
hostgroup * new_hostgroup = NULL ;
int result = OK ;
/* make sure we have the data we need */
if ( name = = NULL | | ! strcmp ( name , " " ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Hostgroup name is NULL \n " ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
new_hostgroup = calloc ( 1 , sizeof ( * new_hostgroup ) ) ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* assign vars */
new_hostgroup - > group_name = name ;
new_hostgroup - > alias = alias ? alias : name ;
new_hostgroup - > notes = notes ;
new_hostgroup - > notes_url = notes_url ;
new_hostgroup - > action_url = action_url ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* add new host group to hash table */
2017-05-19 22:22:40 +02:00
if ( result = = OK ) {
2017-05-19 23:37:19 +02:00
result = dkhash_insert ( object_hash_tables [ HOSTGROUP_SKIPLIST ] , new_hostgroup - > group_name , NULL , new_hostgroup ) ;
2017-05-19 22:22:40 +02:00
switch ( result ) {
2017-05-19 23:37:19 +02:00
case DKHASH_EDUPE :
2017-05-19 22:22:40 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Hostgroup '%s' has already been defined \n " , name ) ;
result = ERROR ;
break ;
2017-05-19 23:37:19 +02:00
case DKHASH_OK :
2017-05-19 22:22:40 +02:00
result = OK ;
break ;
default :
2017-05-19 23:37:19 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Could not add hostgroup '%s' to hash table \n " , name ) ;
2017-05-19 22:22:40 +02:00
result = ERROR ;
break ;
}
}
/* handle errors */
if ( result = = ERROR ) {
2017-05-19 23:37:19 +02:00
free ( new_hostgroup ) ;
2017-05-19 22:22:40 +02:00
return NULL ;
}
2017-05-19 23:37:19 +02:00
new_hostgroup - > id = num_objects . hostgroups + + ;
hostgroup_ary [ new_hostgroup - > id ] = new_hostgroup ;
if ( new_hostgroup - > id )
hostgroup_ary [ new_hostgroup - > id - 1 ] - > next = new_hostgroup ;
2017-05-19 22:22:40 +02:00
return new_hostgroup ;
}
/* add a new host to a host group */
hostsmember * add_host_to_hostgroup ( hostgroup * temp_hostgroup , char * host_name ) {
hostsmember * new_member = NULL ;
hostsmember * last_member = NULL ;
hostsmember * temp_member = NULL ;
2017-05-19 23:37:19 +02:00
struct host * h ;
2017-05-19 22:22:40 +02:00
/* make sure we have the data we need */
if ( temp_hostgroup = = NULL | | ( host_name = = NULL | | ! strcmp ( host_name , " " ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Hostgroup or group member is NULL \n " ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
if ( ! ( h = find_host ( host_name ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Failed to locate host '%s' for hostgroup '%s' \n " , host_name , temp_hostgroup - > group_name ) ;
return NULL ;
}
2017-05-19 22:22:40 +02:00
/* allocate memory for a new member */
if ( ( new_member = calloc ( 1 , sizeof ( hostsmember ) ) ) = = NULL )
return NULL ;
2017-05-19 23:37:19 +02:00
/* assign vars */
new_member - > host_name = h - > name ;
new_member - > host_ptr = h ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* add (unsorted) link from the host to its group */
prepend_object_to_objectlist ( & h - > hostgroups_ptr , ( void * ) temp_hostgroup ) ;
2017-05-19 22:22:40 +02:00
/* add the new member to the member list, sorted by host name */
2017-05-19 23:37:19 +02:00
# ifndef NSCGI
if ( use_large_installation_tweaks = = TRUE ) {
new_member - > next = temp_hostgroup - > members ;
temp_hostgroup - > members = new_member ;
return new_member ;
}
# endif
2017-05-19 22:22:40 +02:00
last_member = temp_hostgroup - > members ;
for ( temp_member = temp_hostgroup - > members ; temp_member ! = NULL ; temp_member = temp_member - > next ) {
if ( strcmp ( new_member - > host_name , temp_member - > host_name ) < 0 ) {
new_member - > next = temp_member ;
if ( temp_member = = temp_hostgroup - > members )
temp_hostgroup - > members = new_member ;
else
last_member - > next = new_member ;
break ;
}
else
last_member = temp_member ;
}
if ( temp_hostgroup - > members = = NULL ) {
new_member - > next = NULL ;
temp_hostgroup - > members = new_member ;
}
else if ( temp_member = = NULL ) {
new_member - > next = NULL ;
last_member - > next = new_member ;
}
return new_member ;
}
/* add a new service group to the list in memory */
servicegroup * add_servicegroup ( char * name , char * alias , char * notes , char * notes_url , char * action_url ) {
servicegroup * new_servicegroup = NULL ;
int result = OK ;
/* make sure we have the data we need */
if ( name = = NULL | | ! strcmp ( name , " " ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Servicegroup name is NULL \n " ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
new_servicegroup = calloc ( 1 , sizeof ( * new_servicegroup ) ) ;
2017-05-19 22:22:40 +02:00
/* duplicate vars */
2017-05-19 23:37:19 +02:00
new_servicegroup - > group_name = name ;
new_servicegroup - > alias = alias ? alias : name ;
new_servicegroup - > notes = notes ;
new_servicegroup - > notes_url = notes_url ;
new_servicegroup - > action_url = action_url ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* add new service group to hash table */
2017-05-19 22:22:40 +02:00
if ( result = = OK ) {
2017-05-19 23:37:19 +02:00
result = dkhash_insert ( object_hash_tables [ SERVICEGROUP_SKIPLIST ] , new_servicegroup - > group_name , NULL , new_servicegroup ) ;
2017-05-19 22:22:40 +02:00
switch ( result ) {
2017-05-19 23:37:19 +02:00
case DKHASH_EDUPE :
2017-05-19 22:22:40 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Servicegroup '%s' has already been defined \n " , name ) ;
result = ERROR ;
break ;
2017-05-19 23:37:19 +02:00
case DKHASH_OK :
2017-05-19 22:22:40 +02:00
result = OK ;
break ;
default :
2017-05-19 23:37:19 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Could not add servicegroup '%s' to hash table \n " , name ) ;
2017-05-19 22:22:40 +02:00
result = ERROR ;
break ;
}
}
/* handle errors */
if ( result = = ERROR ) {
my_free ( new_servicegroup ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
new_servicegroup - > id = num_objects . servicegroups + + ;
servicegroup_ary [ new_servicegroup - > id ] = new_servicegroup ;
if ( new_servicegroup - > id )
servicegroup_ary [ new_servicegroup - > id - 1 ] - > next = new_servicegroup ;
2017-05-19 22:22:40 +02:00
return new_servicegroup ;
}
/* add a new service to a service group */
servicesmember * add_service_to_servicegroup ( servicegroup * temp_servicegroup , char * host_name , char * svc_description ) {
servicesmember * new_member = NULL ;
servicesmember * last_member = NULL ;
servicesmember * temp_member = NULL ;
2017-05-19 23:37:19 +02:00
struct service * svc ;
2017-05-19 22:22:40 +02:00
/* make sure we have the data we need */
if ( temp_servicegroup = = NULL | | ( host_name = = NULL | | ! strcmp ( host_name , " " ) ) | | ( svc_description = = NULL | | ! strcmp ( svc_description , " " ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Servicegroup or group member is NULL \n " ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
if ( ! ( svc = find_service ( host_name , svc_description ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Failed to locate service '%s' on host '%s' for servicegroup '%s' \n " , svc_description , host_name , temp_servicegroup - > group_name ) ;
return NULL ;
}
2017-05-19 22:22:40 +02:00
/* allocate memory for a new member */
if ( ( new_member = calloc ( 1 , sizeof ( servicesmember ) ) ) = = NULL )
return NULL ;
2017-05-19 23:37:19 +02:00
/* assign vars */
new_member - > host_name = svc - > host_name ;
new_member - > service_description = svc - > description ;
new_member - > service_ptr = svc ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* add (unsorted) link from the service to its groups */
prepend_object_to_objectlist ( & svc - > servicegroups_ptr , temp_servicegroup ) ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/*
* add new member to member list , sorted by host name then
* service description , unless we ' re a large installation , in
* which case insertion - sorting will take far too long
*/
# ifndef NSCGI
if ( use_large_installation_tweaks = = TRUE ) {
new_member - > next = temp_servicegroup - > members ;
temp_servicegroup - > members = new_member ;
return new_member ;
}
# endif
2017-05-19 22:22:40 +02:00
last_member = temp_servicegroup - > members ;
for ( temp_member = temp_servicegroup - > members ; temp_member ! = NULL ; temp_member = temp_member - > next ) {
if ( strcmp ( new_member - > host_name , temp_member - > host_name ) < 0 ) {
new_member - > next = temp_member ;
if ( temp_member = = temp_servicegroup - > members )
temp_servicegroup - > members = new_member ;
else
last_member - > next = new_member ;
break ;
}
else if ( strcmp ( new_member - > host_name , temp_member - > host_name ) = = 0 & & strcmp ( new_member - > service_description , temp_member - > service_description ) < 0 ) {
new_member - > next = temp_member ;
if ( temp_member = = temp_servicegroup - > members )
temp_servicegroup - > members = new_member ;
else
last_member - > next = new_member ;
break ;
}
else
last_member = temp_member ;
}
if ( temp_servicegroup - > members = = NULL ) {
new_member - > next = NULL ;
temp_servicegroup - > members = new_member ;
}
else if ( temp_member = = NULL ) {
new_member - > next = NULL ;
last_member - > next = new_member ;
}
return new_member ;
}
/* add a new contact to the list in memory */
2017-05-19 23:37:19 +02:00
contact * add_contact ( char * name , char * alias , char * email , char * pager , char * * addresses , char * svc_notification_period , char * host_notification_period , int service_notification_options , int host_notification_options , int host_notifications_enabled , int service_notifications_enabled , int can_submit_commands , int retain_status_information , int retain_nonstatus_information , unsigned int minimum_value ) {
2017-05-19 22:22:40 +02:00
contact * new_contact = NULL ;
2017-05-19 23:37:19 +02:00
timeperiod * htp = NULL , * stp = NULL ;
2017-05-19 22:22:40 +02:00
int x = 0 ;
int result = OK ;
/* make sure we have the data we need */
2017-05-19 23:37:19 +02:00
if ( name = = NULL | | ! * name ) {
2017-05-19 22:22:40 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Contact name is NULL \n " ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
if ( svc_notification_period & & ! ( stp = find_timeperiod ( svc_notification_period ) ) ) {
logit ( NSLOG_VERIFICATION_ERROR , TRUE , " Error: Service notification period '%s' specified for contact '%s' is not defined anywhere! \n " ,
svc_notification_period , name ) ;
2017-05-19 22:22:40 +02:00
return NULL ;
}
2017-05-19 23:37:19 +02:00
if ( host_notification_period & & ! ( htp = find_timeperiod ( host_notification_period ) ) ) {
logit ( NSLOG_VERIFICATION_ERROR , TRUE , " Error: Host notification period '%s' specified for contact '%s' is not defined anywhere! \n " ,
host_notification_period , name ) ;
return NULL ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
new_contact = calloc ( 1 , sizeof ( * new_contact ) ) ;
if ( ! new_contact )
return NULL ;
new_contact - > host_notification_period = htp ? ( char * ) strdup ( htp - > name ) : NULL ;
new_contact - > service_notification_period = stp ? ( char * ) strdup ( stp - > name ) : NULL ;
new_contact - > host_notification_period_ptr = htp ;
new_contact - > service_notification_period_ptr = stp ;
new_contact - > name = name ;
new_contact - > alias = alias ? alias : name ;
new_contact - > email = email ;
new_contact - > pager = pager ;
2017-05-19 22:22:40 +02:00
if ( addresses ) {
2017-05-19 23:37:19 +02:00
for ( x = 0 ; x < MAX_CONTACT_ADDRESSES ; x + + )
new_contact - > address [ x ] = addresses [ x ] ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
new_contact - > minimum_value = minimum_value ;
new_contact - > service_notification_options = service_notification_options ;
new_contact - > host_notification_options = host_notification_options ;
2017-05-19 22:22:40 +02:00
new_contact - > host_notifications_enabled = ( host_notifications_enabled > 0 ) ? TRUE : FALSE ;
new_contact - > service_notifications_enabled = ( service_notifications_enabled > 0 ) ? TRUE : FALSE ;
new_contact - > can_submit_commands = ( can_submit_commands > 0 ) ? TRUE : FALSE ;
new_contact - > retain_status_information = ( retain_status_information > 0 ) ? TRUE : FALSE ;
new_contact - > retain_nonstatus_information = ( retain_nonstatus_information > 0 ) ? TRUE : FALSE ;
2017-05-19 23:37:19 +02:00
/* add new contact to hash table */
2017-05-19 22:22:40 +02:00
if ( result = = OK ) {
2017-05-19 23:37:19 +02:00
result = dkhash_insert ( object_hash_tables [ CONTACT_SKIPLIST ] , new_contact - > name , NULL , new_contact ) ;
2017-05-19 22:22:40 +02:00
switch ( result ) {
2017-05-19 23:37:19 +02:00
case DKHASH_EDUPE :
2017-05-19 22:22:40 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Contact '%s' has already been defined \n " , name ) ;
result = ERROR ;
break ;
2017-05-19 23:37:19 +02:00
case DKHASH_OK :
2017-05-19 22:22:40 +02:00
result = OK ;
break ;
default :
2017-05-19 23:37:19 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Could not add contact '%s' to hash table \n " , name ) ;
2017-05-19 22:22:40 +02:00
result = ERROR ;
break ;
}
}
/* handle errors */
if ( result = = ERROR ) {
2017-05-19 23:37:19 +02:00
free ( new_contact ) ;
2017-05-19 22:22:40 +02:00
return NULL ;
}
2017-05-19 23:37:19 +02:00
new_contact - > id = num_objects . contacts + + ;
contact_ary [ new_contact - > id ] = new_contact ;
if ( new_contact - > id )
contact_ary [ new_contact - > id - 1 ] - > next = new_contact ;
2017-05-19 22:22:40 +02:00
return new_contact ;
}
/* adds a host notification command to a contact definition */
commandsmember * add_host_notification_command_to_contact ( contact * cntct , char * command_name ) {
commandsmember * new_commandsmember = NULL ;
int result = OK ;
/* make sure we have the data we need */
if ( cntct = = NULL | | ( command_name = = NULL | | ! strcmp ( command_name , " " ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Contact or host notification command is NULL \n " ) ;
return NULL ;
}
/* allocate memory */
if ( ( new_commandsmember = calloc ( 1 , sizeof ( commandsmember ) ) ) = = NULL )
return NULL ;
/* duplicate vars */
if ( ( new_commandsmember - > command = ( char * ) strdup ( command_name ) ) = = NULL )
result = ERROR ;
/* handle errors */
if ( result = = ERROR ) {
my_free ( new_commandsmember - > command ) ;
my_free ( new_commandsmember ) ;
return NULL ;
}
/* add the notification command */
new_commandsmember - > next = cntct - > host_notification_commands ;
cntct - > host_notification_commands = new_commandsmember ;
return new_commandsmember ;
}
/* adds a service notification command to a contact definition */
commandsmember * add_service_notification_command_to_contact ( contact * cntct , char * command_name ) {
commandsmember * new_commandsmember = NULL ;
int result = OK ;
/* make sure we have the data we need */
if ( cntct = = NULL | | ( command_name = = NULL | | ! strcmp ( command_name , " " ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Contact or service notification command is NULL \n " ) ;
return NULL ;
}
/* allocate memory */
if ( ( new_commandsmember = calloc ( 1 , sizeof ( commandsmember ) ) ) = = NULL )
return NULL ;
/* duplicate vars */
if ( ( new_commandsmember - > command = ( char * ) strdup ( command_name ) ) = = NULL )
result = ERROR ;
/* handle errors */
if ( result = = ERROR ) {
my_free ( new_commandsmember - > command ) ;
my_free ( new_commandsmember ) ;
return NULL ;
}
/* add the notification command */
new_commandsmember - > next = cntct - > service_notification_commands ;
cntct - > service_notification_commands = new_commandsmember ;
return new_commandsmember ;
}
/* adds a custom variable to a contact */
customvariablesmember * add_custom_variable_to_contact ( contact * cntct , char * varname , char * varvalue ) {
return add_custom_variable_to_object ( & cntct - > custom_variables , varname , varvalue ) ;
}
/* add a new contact group to the list in memory */
contactgroup * add_contactgroup ( char * name , char * alias ) {
contactgroup * new_contactgroup = NULL ;
int result = OK ;
/* make sure we have the data we need */
if ( name = = NULL | | ! strcmp ( name , " " ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Contactgroup name is NULL \n " ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
new_contactgroup = calloc ( 1 , sizeof ( * new_contactgroup ) ) ;
if ( ! new_contactgroup )
2017-05-19 22:22:40 +02:00
return NULL ;
2017-05-19 23:37:19 +02:00
/* assign vars */
new_contactgroup - > group_name = name ;
new_contactgroup - > alias = alias ? alias : name ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* add new contact group to hash table */
2017-05-19 22:22:40 +02:00
if ( result = = OK ) {
2017-05-19 23:37:19 +02:00
result = dkhash_insert ( object_hash_tables [ CONTACTGROUP_SKIPLIST ] , new_contactgroup - > group_name , NULL , new_contactgroup ) ;
2017-05-19 22:22:40 +02:00
switch ( result ) {
2017-05-19 23:37:19 +02:00
case DKHASH_EDUPE :
2017-05-19 22:22:40 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Contactgroup '%s' has already been defined \n " , name ) ;
result = ERROR ;
break ;
2017-05-19 23:37:19 +02:00
case DKHASH_OK :
2017-05-19 22:22:40 +02:00
result = OK ;
break ;
default :
2017-05-19 23:37:19 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Could not add contactgroup '%s' to hash table \n " , name ) ;
2017-05-19 22:22:40 +02:00
result = ERROR ;
break ;
}
}
/* handle errors */
if ( result = = ERROR ) {
2017-05-19 23:37:19 +02:00
free ( new_contactgroup ) ;
2017-05-19 22:22:40 +02:00
return NULL ;
}
2017-05-19 23:37:19 +02:00
new_contactgroup - > id = num_objects . contactgroups + + ;
contactgroup_ary [ new_contactgroup - > id ] = new_contactgroup ;
if ( new_contactgroup - > id )
contactgroup_ary [ new_contactgroup - > id - 1 ] - > next = new_contactgroup ;
2017-05-19 22:22:40 +02:00
return new_contactgroup ;
}
/* add a new member to a contact group */
contactsmember * add_contact_to_contactgroup ( contactgroup * grp , char * contact_name ) {
contactsmember * new_contactsmember = NULL ;
2017-05-19 23:37:19 +02:00
struct contact * c ;
2017-05-19 22:22:40 +02:00
/* make sure we have the data we need */
if ( grp = = NULL | | ( contact_name = = NULL | | ! strcmp ( contact_name , " " ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Contactgroup or contact name is NULL \n " ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
if ( ! ( c = find_contact ( contact_name ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Failed to locate contact '%s' for contactgroup '%s' \n " , contact_name , grp - > group_name ) ;
return NULL ;
}
2017-05-19 22:22:40 +02:00
/* allocate memory for a new member */
if ( ( new_contactsmember = calloc ( 1 , sizeof ( contactsmember ) ) ) = = NULL )
return NULL ;
2017-05-19 23:37:19 +02:00
/* assign vars */
new_contactsmember - > contact_name = c - > name ;
new_contactsmember - > contact_ptr = c ;
2017-05-19 22:22:40 +02:00
/* add the new member to the head of the member list */
new_contactsmember - > next = grp - > members ;
grp - > members = new_contactsmember ;
2017-05-19 23:37:19 +02:00
prepend_object_to_objectlist ( & c - > contactgroups_ptr , ( void * ) grp ) ;
2017-05-19 22:22:40 +02:00
return new_contactsmember ;
}
/* add a new service to the list in memory */
2017-05-19 23:37:19 +02:00
service * add_service ( char * host_name , char * description , char * display_name , char * check_period , int initial_state , int max_attempts , int parallelize , int accept_passive_checks , double check_interval , double retry_interval , double notification_interval , double first_notification_delay , char * notification_period , int notification_options , int notifications_enabled , int is_volatile , char * event_handler , int event_handler_enabled , char * check_command , int checks_enabled , int flap_detection_enabled , double low_flap_threshold , double high_flap_threshold , int flap_detection_options , int stalking_options , int process_perfdata , int check_freshness , int freshness_threshold , char * notes , char * notes_url , char * action_url , char * icon_image , char * icon_image_alt , int retain_status_information , int retain_nonstatus_information , int obsess , unsigned int hourly_value ) {
host * h ;
timeperiod * cp = NULL , * np = NULL ;
2017-05-19 22:22:40 +02:00
service * new_service = NULL ;
int result = OK ;
/* make sure we have everything we need */
2017-05-19 23:37:19 +02:00
if ( host_name = = NULL | | description = = NULL | | ! * description | | check_command = = NULL | | ! * check_command ) {
2017-05-19 22:22:40 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Service description, host name, or check command is NULL \n " ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
if ( ! ( h = find_host ( host_name ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Unable to locate host '%s' for service '%s' \n " ,
host_name , description ) ;
return NULL ;
}
if ( notification_period & & ! ( np = find_timeperiod ( notification_period ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: notification_period '%s' for service '%s' on host '%s' could not be found! \n " ,
notification_period , description , host_name ) ;
return NULL ;
}
if ( check_period & & ! ( cp = find_timeperiod ( check_period ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: check_period '%s' for service '%s' on host '%s' not found! \n " ,
check_period , description , host_name ) ;
return NULL ;
}
2017-05-19 22:22:40 +02:00
/* check values */
if ( max_attempts < = 0 | | check_interval < 0 | | retry_interval < = 0 | | notification_interval < 0 ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Invalid max_attempts, check_interval, retry_interval, or notification_interval value for service '%s' on host '%s' \n " , description , host_name ) ;
return NULL ;
}
if ( first_notification_delay < 0 ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Invalid first_notification_delay value for service '%s' on host '%s' \n " , description , host_name ) ;
return NULL ;
}
/* allocate memory */
2017-05-19 23:37:19 +02:00
new_service = calloc ( 1 , sizeof ( * new_service ) ) ;
if ( ! new_service )
2017-05-19 22:22:40 +02:00
return NULL ;
2017-05-19 23:37:19 +02:00
/* duplicate vars, but assign what we can */
new_service - > notification_period_ptr = np ;
new_service - > check_period_ptr = cp ;
new_service - > host_ptr = h ;
new_service - > check_period = cp ? ( char * ) strdup ( cp - > name ) : NULL ;
new_service - > notification_period = np ? ( char * ) strdup ( np - > name ) : NULL ;
new_service - > host_name = h - > name ;
2017-05-19 22:22:40 +02:00
if ( ( new_service - > description = ( char * ) strdup ( description ) ) = = NULL )
result = ERROR ;
2017-05-19 23:37:19 +02:00
if ( display_name ) {
if ( ( new_service - > display_name = ( char * ) strdup ( display_name ) ) = = NULL )
2017-05-19 22:22:40 +02:00
result = ERROR ;
}
2017-05-19 23:37:19 +02:00
else {
new_service - > display_name = new_service - > description ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
if ( ( new_service - > check_command = ( char * ) strdup ( check_command ) ) = = NULL )
result = ERROR ;
if ( event_handler ) {
if ( ( new_service - > event_handler = ( char * ) strdup ( event_handler ) ) = = NULL )
2017-05-19 22:22:40 +02:00
result = ERROR ;
}
if ( notes ) {
if ( ( new_service - > notes = ( char * ) strdup ( notes ) ) = = NULL )
result = ERROR ;
}
if ( notes_url ) {
if ( ( new_service - > notes_url = ( char * ) strdup ( notes_url ) ) = = NULL )
result = ERROR ;
}
if ( action_url ) {
if ( ( new_service - > action_url = ( char * ) strdup ( action_url ) ) = = NULL )
result = ERROR ;
}
if ( icon_image ) {
if ( ( new_service - > icon_image = ( char * ) strdup ( icon_image ) ) = = NULL )
result = ERROR ;
}
if ( icon_image_alt ) {
if ( ( new_service - > icon_image_alt = ( char * ) strdup ( icon_image_alt ) ) = = NULL )
result = ERROR ;
}
2017-05-19 23:37:19 +02:00
new_service - > hourly_value = hourly_value ;
2017-05-19 22:22:40 +02:00
new_service - > check_interval = check_interval ;
new_service - > retry_interval = retry_interval ;
new_service - > max_attempts = max_attempts ;
new_service - > parallelize = ( parallelize > 0 ) ? TRUE : FALSE ;
new_service - > notification_interval = notification_interval ;
new_service - > first_notification_delay = first_notification_delay ;
2017-05-19 23:37:19 +02:00
new_service - > notification_options = notification_options ;
2017-05-19 22:22:40 +02:00
new_service - > is_volatile = ( is_volatile > 0 ) ? TRUE : FALSE ;
new_service - > flap_detection_enabled = ( flap_detection_enabled > 0 ) ? TRUE : FALSE ;
new_service - > low_flap_threshold = low_flap_threshold ;
new_service - > high_flap_threshold = high_flap_threshold ;
2017-05-19 23:37:19 +02:00
new_service - > flap_detection_options = flap_detection_options ;
new_service - > stalking_options = stalking_options ;
2017-05-19 22:22:40 +02:00
new_service - > process_performance_data = ( process_perfdata > 0 ) ? TRUE : FALSE ;
new_service - > check_freshness = ( check_freshness > 0 ) ? TRUE : FALSE ;
new_service - > freshness_threshold = freshness_threshold ;
2017-05-19 23:37:19 +02:00
new_service - > accept_passive_checks = ( accept_passive_checks > 0 ) ? TRUE : FALSE ;
2017-05-19 22:22:40 +02:00
new_service - > event_handler_enabled = ( event_handler_enabled > 0 ) ? TRUE : FALSE ;
new_service - > checks_enabled = ( checks_enabled > 0 ) ? TRUE : FALSE ;
new_service - > retain_status_information = ( retain_status_information > 0 ) ? TRUE : FALSE ;
new_service - > retain_nonstatus_information = ( retain_nonstatus_information > 0 ) ? TRUE : FALSE ;
new_service - > notifications_enabled = ( notifications_enabled > 0 ) ? TRUE : FALSE ;
2017-05-19 23:37:19 +02:00
new_service - > obsess = ( obsess > 0 ) ? TRUE : FALSE ;
2017-05-19 22:22:40 +02:00
# ifdef NSCORE
new_service - > acknowledgement_type = ACKNOWLEDGEMENT_NONE ;
2017-05-19 23:37:19 +02:00
new_service - > check_type = CHECK_TYPE_ACTIVE ;
2017-05-19 22:22:40 +02:00
new_service - > current_attempt = ( initial_state = = STATE_OK ) ? 1 : max_attempts ;
new_service - > current_state = initial_state ;
new_service - > last_state = initial_state ;
new_service - > last_hard_state = initial_state ;
new_service - > state_type = HARD_STATE ;
new_service - > should_be_scheduled = TRUE ;
new_service - > check_options = CHECK_OPTION_NONE ;
# endif
2017-05-19 23:37:19 +02:00
/* add new service to hash table */
2017-05-19 22:22:40 +02:00
if ( result = = OK ) {
2017-05-19 23:37:19 +02:00
result = dkhash_insert ( object_hash_tables [ SERVICE_SKIPLIST ] , new_service - > host_name , new_service - > description , new_service ) ;
2017-05-19 22:22:40 +02:00
switch ( result ) {
2017-05-19 23:37:19 +02:00
case DKHASH_EDUPE :
2017-05-19 22:22:40 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Service '%s' on host '%s' has already been defined \n " , description , host_name ) ;
result = ERROR ;
break ;
2017-05-19 23:37:19 +02:00
case DKHASH_OK :
2017-05-19 22:22:40 +02:00
result = OK ;
break ;
default :
2017-05-19 23:37:19 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Could not add service '%s' on host '%s' to hash table \n " , description , host_name ) ;
2017-05-19 22:22:40 +02:00
result = ERROR ;
break ;
}
}
/* handle errors */
if ( result = = ERROR ) {
# ifdef NSCORE
my_free ( new_service - > perf_data ) ;
my_free ( new_service - > plugin_output ) ;
my_free ( new_service - > long_plugin_output ) ;
# endif
my_free ( new_service - > event_handler ) ;
2017-05-19 23:37:19 +02:00
my_free ( new_service - > check_command ) ;
2017-05-19 22:22:40 +02:00
my_free ( new_service - > description ) ;
2017-05-19 23:37:19 +02:00
if ( display_name )
my_free ( new_service - > display_name ) ;
2017-05-19 22:22:40 +02:00
return NULL ;
}
2017-05-19 23:37:19 +02:00
add_service_link_to_host ( h , new_service ) ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
new_service - > id = num_objects . services + + ;
service_ary [ new_service - > id ] = new_service ;
if ( new_service - > id )
service_ary [ new_service - > id - 1 ] - > next = new_service ;
2017-05-19 22:22:40 +02:00
return new_service ;
}
/* adds a contact group to a service */
contactgroupsmember * add_contactgroup_to_service ( service * svc , char * group_name ) {
2017-05-19 23:37:19 +02:00
return add_contactgroup_to_object ( & svc - > contact_groups , group_name ) ;
2017-05-19 22:22:40 +02:00
}
/* adds a contact to a service */
contactsmember * add_contact_to_service ( service * svc , char * contact_name ) {
return add_contact_to_object ( & svc - > contacts , contact_name ) ;
}
/* adds a custom variable to a service */
customvariablesmember * add_custom_variable_to_service ( service * svc , char * varname , char * varvalue ) {
return add_custom_variable_to_object ( & svc - > custom_variables , varname , varvalue ) ;
}
/* add a new command to the list in memory */
command * add_command ( char * name , char * value ) {
command * new_command = NULL ;
int result = OK ;
/* make sure we have the data we need */
if ( ( name = = NULL | | ! strcmp ( name , " " ) ) | | ( value = = NULL | | ! strcmp ( value , " " ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Command name of command line is NULL \n " ) ;
return NULL ;
}
/* allocate memory for the new command */
2017-05-19 23:37:19 +02:00
new_command = calloc ( 1 , sizeof ( * new_command ) ) ;
if ( ! new_command )
2017-05-19 22:22:40 +02:00
return NULL ;
2017-05-19 23:37:19 +02:00
/* assign vars */
new_command - > name = name ;
new_command - > command_line = value ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* add new command to hash table */
2017-05-19 22:22:40 +02:00
if ( result = = OK ) {
2017-05-19 23:37:19 +02:00
result = dkhash_insert ( object_hash_tables [ COMMAND_SKIPLIST ] , new_command - > name , NULL , new_command ) ;
2017-05-19 22:22:40 +02:00
switch ( result ) {
2017-05-19 23:37:19 +02:00
case DKHASH_EDUPE :
2017-05-19 22:22:40 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Command '%s' has already been defined \n " , name ) ;
result = ERROR ;
break ;
2017-05-19 23:37:19 +02:00
case DKHASH_OK :
2017-05-19 22:22:40 +02:00
result = OK ;
break ;
default :
2017-05-19 23:37:19 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Could not add command '%s' to hash table \n " , name ) ;
2017-05-19 22:22:40 +02:00
result = ERROR ;
break ;
}
}
/* handle errors */
if ( result = = ERROR ) {
my_free ( new_command ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
new_command - > id = num_objects . commands + + ;
command_ary [ new_command - > id ] = new_command ;
if ( new_command - > id )
command_ary [ new_command - > id - 1 ] - > next = new_command ;
2017-05-19 22:22:40 +02:00
return new_command ;
}
/* add a new service escalation to the list in memory */
2017-05-19 23:37:19 +02:00
serviceescalation * add_serviceescalation ( char * host_name , char * description , int first_notification , int last_notification , double notification_interval , char * escalation_period , int escalation_options ) {
2017-05-19 22:22:40 +02:00
serviceescalation * new_serviceescalation = NULL ;
2017-05-19 23:37:19 +02:00
service * svc ;
timeperiod * tp = NULL ;
2017-05-19 22:22:40 +02:00
/* make sure we have the data we need */
2017-05-19 23:37:19 +02:00
if ( host_name = = NULL | | ! * host_name | | description = = NULL | | ! * description ) {
2017-05-19 22:22:40 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Service escalation host name or description is NULL \n " ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
if ( ! ( svc = find_service ( host_name , description ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Service '%s' on host '%s' has an escalation but is not defined anywhere! \n " ,
host_name , description ) ;
return NULL ;
}
if ( escalation_period & & ! ( tp = find_timeperiod ( escalation_period ) ) ) {
logit ( NSLOG_VERIFICATION_ERROR , TRUE , " Error: Escalation period '%s' specified in service escalation for service '%s' on host '%s' is not defined anywhere! \n " ,
escalation_period , description , host_name ) ;
return NULL ;
}
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
new_serviceescalation = calloc ( 1 , sizeof ( * new_serviceescalation ) ) ;
if ( ! new_serviceescalation )
2017-05-19 22:22:40 +02:00
return NULL ;
2017-05-19 23:37:19 +02:00
if ( add_object_to_objectlist ( & svc - > escalation_list , new_serviceescalation ) ! = OK ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Could not add escalation to service '%s' on host '%s' \n " ,
svc - > host_name , svc - > description ) ;
return NULL ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
/* assign vars. object names are immutable, so no need to copy */
new_serviceescalation - > host_name = svc - > host_name ;
new_serviceescalation - > description = svc - > description ;
new_serviceescalation - > service_ptr = svc ;
new_serviceescalation - > escalation_period_ptr = tp ;
if ( tp )
new_serviceescalation - > escalation_period = ( char * ) strdup ( tp - > name ) ;
2017-05-19 22:22:40 +02:00
new_serviceescalation - > first_notification = first_notification ;
new_serviceescalation - > last_notification = last_notification ;
new_serviceescalation - > notification_interval = ( notification_interval < = 0 ) ? 0 : notification_interval ;
2017-05-19 23:37:19 +02:00
new_serviceescalation - > escalation_options = escalation_options ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
new_serviceescalation - > id = num_objects . serviceescalations + + ;
serviceescalation_ary [ new_serviceescalation - > id ] = new_serviceescalation ;
2017-05-19 22:22:40 +02:00
return new_serviceescalation ;
}
/* adds a contact group to a service escalation */
contactgroupsmember * add_contactgroup_to_serviceescalation ( serviceescalation * se , char * group_name ) {
2017-05-19 23:37:19 +02:00
return add_contactgroup_to_object ( & se - > contact_groups , group_name ) ;
2017-05-19 22:22:40 +02:00
}
/* adds a contact to a service escalation */
contactsmember * add_contact_to_serviceescalation ( serviceescalation * se , char * contact_name ) {
return add_contact_to_object ( & se - > contacts , contact_name ) ;
}
/* adds a service dependency definition */
2017-05-19 23:37:19 +02:00
servicedependency * add_service_dependency ( char * dependent_host_name , char * dependent_service_description , char * host_name , char * service_description , int dependency_type , int inherits_parent , int failure_options , char * dependency_period ) {
2017-05-19 22:22:40 +02:00
servicedependency * new_servicedependency = NULL ;
2017-05-19 23:37:19 +02:00
service * parent , * child ;
timeperiod * tp = NULL ;
int result ;
2017-05-19 22:22:40 +02:00
/* make sure we have what we need */
2017-05-19 23:37:19 +02:00
parent = find_service ( host_name , service_description ) ;
if ( ! parent ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Master service '%s' on host '%s' is not defined anywhere! \n " ,
service_description , host_name ) ;
return NULL ;
}
child = find_service ( dependent_host_name , dependent_service_description ) ;
if ( ! child ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Dependent service '%s' on host '%s' is not defined anywhere! \n " ,
dependent_service_description , dependent_host_name ) ;
2017-05-19 22:22:40 +02:00
return NULL ;
}
2017-05-19 23:37:19 +02:00
if ( dependency_period & & ! ( tp = find_timeperiod ( dependency_period ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Failed to locate timeperiod '%s' for dependency from service '%s' on host '%s' to service '%s' on host '%s' \n " ,
dependency_period , dependent_service_description , dependent_host_name , service_description , host_name ) ;
2017-05-19 22:22:40 +02:00
return NULL ;
}
/* allocate memory for a new service dependency entry */
2017-05-19 23:37:19 +02:00
if ( ( new_servicedependency = calloc ( 1 , sizeof ( * new_servicedependency ) ) ) = = NULL )
2017-05-19 22:22:40 +02:00
return NULL ;
2017-05-19 23:37:19 +02:00
new_servicedependency - > dependent_service_ptr = child ;
new_servicedependency - > master_service_ptr = parent ;
new_servicedependency - > dependency_period_ptr = tp ;
/* assign vars. object names are immutable, so no need to copy */
new_servicedependency - > dependent_host_name = child - > host_name ;
new_servicedependency - > dependent_service_description = child - > description ;
new_servicedependency - > host_name = parent - > host_name ;
new_servicedependency - > service_description = parent - > description ;
if ( tp )
new_servicedependency - > dependency_period = ( char * ) strdup ( tp - > name ) ;
2017-05-19 22:22:40 +02:00
new_servicedependency - > dependency_type = ( dependency_type = = EXECUTION_DEPENDENCY ) ? EXECUTION_DEPENDENCY : NOTIFICATION_DEPENDENCY ;
new_servicedependency - > inherits_parent = ( inherits_parent > 0 ) ? TRUE : FALSE ;
2017-05-19 23:37:19 +02:00
new_servicedependency - > failure_options = failure_options ;
/*
* add new service dependency to its respective services .
* Ordering doesn ' t matter here as we ' ll have to check them
* all anyway . We avoid adding dupes though , since we can
* apparently get zillion ' s and zillion ' s of them .
*/
if ( dependency_type = = NOTIFICATION_DEPENDENCY )
result = prepend_unique_object_to_objectlist ( & child - > notify_deps , new_servicedependency , sizeof ( * new_servicedependency ) ) ;
else
result = prepend_unique_object_to_objectlist ( & child - > exec_deps , new_servicedependency , sizeof ( * new_servicedependency ) ) ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
if ( result ! = OK ) {
free ( new_servicedependency ) ;
/* hack to avoid caller bombing out */
return result = = OBJECTLIST_DUPE ? ( void * ) 1 : NULL ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
num_objects . servicedependencies + + ;
2017-05-19 22:22:40 +02:00
return new_servicedependency ;
}
/* adds a host dependency definition */
2017-05-19 23:37:19 +02:00
hostdependency * add_host_dependency ( char * dependent_host_name , char * host_name , int dependency_type , int inherits_parent , int failure_options , char * dependency_period ) {
2017-05-19 22:22:40 +02:00
hostdependency * new_hostdependency = NULL ;
2017-05-19 23:37:19 +02:00
host * parent , * child ;
timeperiod * tp = NULL ;
int result ;
2017-05-19 22:22:40 +02:00
/* make sure we have what we need */
2017-05-19 23:37:19 +02:00
parent = find_host ( host_name ) ;
if ( ! parent ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Master host '%s' in hostdependency from '%s' to '%s' is not defined anywhere! \n " ,
host_name , dependent_host_name , host_name ) ;
return NULL ;
}
child = find_host ( dependent_host_name ) ;
if ( ! child ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Dependent host '%s' in hostdependency from '%s' to '%s' is not defined anywhere! \n " ,
dependent_host_name , dependent_host_name , host_name ) ;
2017-05-19 22:22:40 +02:00
return NULL ;
}
2017-05-19 23:37:19 +02:00
if ( dependency_period & & ! ( tp = find_timeperiod ( dependency_period ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Unable to locate dependency_period '%s' for %s->%s host dependency \n " ,
dependency_period , parent - > name , child - > name ) ;
return NULL ;
}
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
if ( ( new_hostdependency = calloc ( 1 , sizeof ( * new_hostdependency ) ) ) = = NULL )
2017-05-19 22:22:40 +02:00
return NULL ;
2017-05-19 23:37:19 +02:00
new_hostdependency - > dependent_host_ptr = child ;
new_hostdependency - > master_host_ptr = parent ;
new_hostdependency - > dependency_period_ptr = tp ;
/* assign vars. Objects are immutable, so no need to copy */
new_hostdependency - > dependent_host_name = child - > name ;
new_hostdependency - > host_name = parent - > name ;
if ( tp )
new_hostdependency - > dependency_period = ( char * ) strdup ( tp - > name ) ;
2017-05-19 22:22:40 +02:00
new_hostdependency - > dependency_type = ( dependency_type = = EXECUTION_DEPENDENCY ) ? EXECUTION_DEPENDENCY : NOTIFICATION_DEPENDENCY ;
new_hostdependency - > inherits_parent = ( inherits_parent > 0 ) ? TRUE : FALSE ;
2017-05-19 23:37:19 +02:00
new_hostdependency - > failure_options = failure_options ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
if ( dependency_type = = NOTIFICATION_DEPENDENCY )
result = prepend_unique_object_to_objectlist ( & child - > notify_deps , new_hostdependency , sizeof ( * new_hostdependency ) ) ;
else
result = prepend_unique_object_to_objectlist ( & child - > exec_deps , new_hostdependency , sizeof ( * new_hostdependency ) ) ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
if ( result ! = OK ) {
free ( new_hostdependency ) ;
/* hack to avoid caller bombing out */
return result = = OBJECTLIST_DUPE ? ( void * ) 1 : NULL ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
num_objects . hostdependencies + + ;
2017-05-19 22:22:40 +02:00
return new_hostdependency ;
}
/* add a new host escalation to the list in memory */
2017-05-19 23:37:19 +02:00
hostescalation * add_hostescalation ( char * host_name , int first_notification , int last_notification , double notification_interval , char * escalation_period , int escalation_options ) {
2017-05-19 22:22:40 +02:00
hostescalation * new_hostescalation = NULL ;
2017-05-19 23:37:19 +02:00
host * h ;
timeperiod * tp = NULL ;
2017-05-19 22:22:40 +02:00
/* make sure we have the data we need */
2017-05-19 23:37:19 +02:00
if ( host_name = = NULL | | ! * host_name ) {
2017-05-19 22:22:40 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Host escalation host name is NULL \n " ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
if ( ! ( h = find_host ( host_name ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Host '%s' has an escalation, but is not defined anywhere! \n " , host_name ) ;
return NULL ;
}
if ( escalation_period & & ! ( tp = find_timeperiod ( escalation_period ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Unable to locate timeperiod '%s' for hostescalation '%s' \n " ,
escalation_period , host_name ) ;
return NULL ;
}
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
new_hostescalation = calloc ( 1 , sizeof ( * new_hostescalation ) ) ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* add the escalation to its host */
if ( add_object_to_objectlist ( & h - > escalation_list , new_hostescalation ) ! = OK ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Could not add hostescalation to host '%s' \n " , host_name ) ;
free ( new_hostescalation ) ;
2017-05-19 22:22:40 +02:00
return NULL ;
}
2017-05-19 23:37:19 +02:00
/* assign vars. Object names are immutable, so no need to copy */
new_hostescalation - > host_name = h - > name ;
new_hostescalation - > host_ptr = h ;
new_hostescalation - > escalation_period = tp ? ( char * ) strdup ( tp - > name ) : NULL ;
new_hostescalation - > escalation_period_ptr = tp ;
2017-05-19 22:22:40 +02:00
new_hostescalation - > first_notification = first_notification ;
new_hostescalation - > last_notification = last_notification ;
new_hostescalation - > notification_interval = ( notification_interval < = 0 ) ? 0 : notification_interval ;
2017-05-19 23:37:19 +02:00
new_hostescalation - > escalation_options = escalation_options ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
new_hostescalation - > id = num_objects . hostescalations + + ;
hostescalation_ary [ new_hostescalation - > id ] = new_hostescalation ;
2017-05-19 22:22:40 +02:00
return new_hostescalation ;
}
/* adds a contact group to a host escalation */
contactgroupsmember * add_contactgroup_to_hostescalation ( hostescalation * he , char * group_name ) {
2017-05-19 23:37:19 +02:00
return add_contactgroup_to_object ( & he - > contact_groups , group_name ) ;
2017-05-19 22:22:40 +02:00
}
/* adds a contact to a host escalation */
contactsmember * add_contact_to_hostescalation ( hostescalation * he , char * contact_name ) {
return add_contact_to_object ( & he - > contacts , contact_name ) ;
}
/* adds a contact to an object */
contactsmember * add_contact_to_object ( contactsmember * * object_ptr , char * contactname ) {
contactsmember * new_contactsmember = NULL ;
2017-05-19 23:37:19 +02:00
contact * c ;
2017-05-19 22:22:40 +02:00
/* make sure we have the data we need */
if ( object_ptr = = NULL ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Contact object is NULL \n " ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
if ( contactname = = NULL | | ! * contactname ) {
2017-05-19 22:22:40 +02:00
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Contact name is NULL \n " ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
if ( ! ( c = find_contact ( contactname ) ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Contact '%s' is not defined anywhere! \n " , contactname ) ;
return NULL ;
}
2017-05-19 22:22:40 +02:00
/* allocate memory for a new member */
if ( ( new_contactsmember = malloc ( sizeof ( contactsmember ) ) ) = = NULL ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Could not allocate memory for contact \n " ) ;
return NULL ;
}
2017-05-19 23:37:19 +02:00
new_contactsmember - > contact_name = c - > name ;
2017-05-19 22:22:40 +02:00
/* set initial values */
2017-05-19 23:37:19 +02:00
new_contactsmember - > contact_ptr = c ;
2017-05-19 22:22:40 +02:00
/* add the new contact to the head of the contact list */
new_contactsmember - > next = * object_ptr ;
* object_ptr = new_contactsmember ;
return new_contactsmember ;
}
/* adds a custom variable to an object */
customvariablesmember * add_custom_variable_to_object ( customvariablesmember * * object_ptr , char * varname , char * varvalue ) {
customvariablesmember * new_customvariablesmember = NULL ;
/* make sure we have the data we need */
if ( object_ptr = = NULL ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Custom variable object is NULL \n " ) ;
return NULL ;
}
if ( varname = = NULL | | ! strcmp ( varname , " " ) ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Custom variable name is NULL \n " ) ;
return NULL ;
}
/* allocate memory for a new member */
if ( ( new_customvariablesmember = malloc ( sizeof ( customvariablesmember ) ) ) = = NULL ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Could not allocate memory for custom variable \n " ) ;
return NULL ;
}
if ( ( new_customvariablesmember - > variable_name = ( char * ) strdup ( varname ) ) = = NULL ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Could not allocate memory for custom variable name \n " ) ;
my_free ( new_customvariablesmember ) ;
return NULL ;
}
if ( varvalue ) {
if ( ( new_customvariablesmember - > variable_value = ( char * ) strdup ( varvalue ) ) = = NULL ) {
logit ( NSLOG_CONFIG_ERROR , TRUE , " Error: Could not allocate memory for custom variable value \n " ) ;
my_free ( new_customvariablesmember - > variable_name ) ;
my_free ( new_customvariablesmember ) ;
return NULL ;
}
}
else
new_customvariablesmember - > variable_value = NULL ;
/* set initial values */
new_customvariablesmember - > has_been_modified = FALSE ;
/* add the new member to the head of the member list */
new_customvariablesmember - > next = * object_ptr ;
* object_ptr = new_customvariablesmember ;
return new_customvariablesmember ;
}
/******************************************************************/
/******************** OBJECT SEARCH FUNCTIONS *********************/
/******************************************************************/
2017-05-19 23:37:19 +02:00
timeperiod * find_timeperiod ( const char * name ) {
return dkhash_get ( object_hash_tables [ TIMEPERIOD_SKIPLIST ] , name , NULL ) ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
host * find_host ( const char * name ) {
return dkhash_get ( object_hash_tables [ HOST_SKIPLIST ] , name , NULL ) ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
hostgroup * find_hostgroup ( const char * name ) {
return dkhash_get ( object_hash_tables [ HOSTGROUP_SKIPLIST ] , name , NULL ) ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
servicegroup * find_servicegroup ( const char * name ) {
return dkhash_get ( object_hash_tables [ SERVICEGROUP_SKIPLIST ] , name , NULL ) ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
contact * find_contact ( const char * name ) {
return dkhash_get ( object_hash_tables [ CONTACT_SKIPLIST ] , name , NULL ) ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
contactgroup * find_contactgroup ( const char * name ) {
return dkhash_get ( object_hash_tables [ CONTACTGROUP_SKIPLIST ] , name , NULL ) ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
command * find_command ( const char * name ) {
return dkhash_get ( object_hash_tables [ COMMAND_SKIPLIST ] , name , NULL ) ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
service * find_service ( const char * host_name , const char * svc_desc ) {
return dkhash_get ( object_hash_tables [ SERVICE_SKIPLIST ] , host_name , svc_desc ) ;
2017-05-19 22:22:40 +02:00
}
/* adds a object to a list of objects */
int add_object_to_objectlist ( objectlist * * list , void * object_ptr ) {
objectlist * temp_item = NULL ;
objectlist * new_item = NULL ;
if ( list = = NULL | | object_ptr = = NULL )
return ERROR ;
/* skip this object if its already in the list */
for ( temp_item = * list ; temp_item ; temp_item = temp_item - > next ) {
if ( temp_item - > object_ptr = = object_ptr )
break ;
}
if ( temp_item )
return OK ;
/* allocate memory for a new list item */
if ( ( new_item = ( objectlist * ) malloc ( sizeof ( objectlist ) ) ) = = NULL )
return ERROR ;
/* initialize vars */
new_item - > object_ptr = object_ptr ;
/* add new item to head of list */
new_item - > next = * list ;
* list = new_item ;
return OK ;
}
2017-05-19 23:37:19 +02:00
/* useful when we don't care if the object is unique or not */
int prepend_object_to_objectlist ( objectlist * * list , void * object_ptr )
{
objectlist * item ;
if ( list = = NULL | | object_ptr = = NULL )
return ERROR ;
if ( ( item = malloc ( sizeof ( * item ) ) ) = = NULL )
return ERROR ;
item - > next = * list ;
item - > object_ptr = object_ptr ;
* list = item ;
return OK ;
}
/* useful for adding dependencies to master objects */
int prepend_unique_object_to_objectlist ( objectlist * * list , void * object_ptr , size_t size ) {
objectlist * l ;
if ( list = = NULL | | object_ptr = = NULL )
return ERROR ;
for ( l = * list ; l ; l = l - > next ) {
if ( ! memcmp ( l - > object_ptr , object_ptr , size ) )
return OBJECTLIST_DUPE ;
}
return prepend_object_to_objectlist ( list , object_ptr ) ;
}
2017-05-19 22:22:40 +02:00
/* frees memory allocated to a temporary object list */
int free_objectlist ( objectlist * * temp_list ) {
objectlist * this_objectlist = NULL ;
objectlist * next_objectlist = NULL ;
if ( temp_list = = NULL )
return ERROR ;
/* free memory allocated to object list */
for ( this_objectlist = * temp_list ; this_objectlist ! = NULL ; this_objectlist = next_objectlist ) {
next_objectlist = this_objectlist - > next ;
my_free ( this_objectlist ) ;
}
* temp_list = NULL ;
return OK ;
}
/******************************************************************/
/********************* OBJECT QUERY FUNCTIONS *********************/
/******************************************************************/
/* determines whether or not a specific host is an immediate child of another host */
int is_host_immediate_child_of_host ( host * parent_host , host * child_host ) {
hostsmember * temp_hostsmember = NULL ;
/* not enough data */
if ( child_host = = NULL )
return FALSE ;
/* root/top-level hosts */
if ( parent_host = = NULL ) {
if ( child_host - > parent_hosts = = NULL )
return TRUE ;
}
/* mid-level/bottom hosts */
else {
for ( temp_hostsmember = child_host - > parent_hosts ; temp_hostsmember ! = NULL ; temp_hostsmember = temp_hostsmember - > next ) {
if ( temp_hostsmember - > host_ptr = = parent_host )
return TRUE ;
}
}
return FALSE ;
}
/* determines whether or not a specific host is an immediate parent of another host */
int is_host_immediate_parent_of_host ( host * child_host , host * parent_host ) {
2017-05-19 23:37:19 +02:00
if ( is_host_immediate_child_of_host ( parent_host , child_host ) = = TRUE )
return TRUE ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
return FALSE ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
# ifdef NSCGI
int hostsmember_elements ( hostsmember * list )
{
int elems = 0 ;
for ( ; list ; list = list - > next )
elems + + ;
return elems ;
}
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* returns a count of the immediate children for a given host */
2017-05-19 22:22:40 +02:00
/* NOTE: This function is only used by the CGIS */
2017-05-19 23:37:19 +02:00
int number_of_immediate_child_hosts ( host * hst ) {
2017-05-19 22:22:40 +02:00
int children = 0 ;
host * temp_host = NULL ;
2017-05-19 23:37:19 +02:00
if ( hst = = NULL ) {
for ( temp_host = host_list ; temp_host ! = NULL ;
temp_host = temp_host - > next ) {
if ( is_host_immediate_child_of_host ( hst , temp_host ) = = TRUE )
children + + ;
}
return children ;
}
else {
return hostsmember_elements ( hst - > child_hosts ) ;
2017-05-19 22:22:40 +02:00
}
}
/* get the number of immediate parent hosts for a given host */
/* NOTE: This function is only used by the CGIS */
int number_of_immediate_parent_hosts ( host * hst ) {
int parents = 0 ;
host * temp_host = NULL ;
2017-05-19 23:37:19 +02:00
if ( hst = = NULL ) {
for ( temp_host = host_list ; temp_host ! = NULL ;
temp_host = temp_host - > next ) {
if ( is_host_immediate_parent_of_host ( hst , temp_host ) = = TRUE ) {
parents + + ;
}
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
return parents ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
else {
return hostsmember_elements ( hst - > parent_hosts ) ;
2017-05-19 22:22:40 +02:00
}
}
2017-05-19 23:37:19 +02:00
# endif
2017-05-19 22:22:40 +02:00
/* tests whether a host is a member of a particular hostgroup */
/* NOTE: This function is only used by the CGIS */
int is_host_member_of_hostgroup ( hostgroup * group , host * hst ) {
hostsmember * temp_hostsmember = NULL ;
if ( group = = NULL | | hst = = NULL )
return FALSE ;
for ( temp_hostsmember = group - > members ; temp_hostsmember ! = NULL ; temp_hostsmember = temp_hostsmember - > next ) {
if ( temp_hostsmember - > host_ptr = = hst )
return TRUE ;
}
return FALSE ;
}
/* tests whether a host is a member of a particular servicegroup */
/* NOTE: This function is only used by the CGIS */
int is_host_member_of_servicegroup ( servicegroup * group , host * hst ) {
servicesmember * temp_servicesmember = NULL ;
if ( group = = NULL | | hst = = NULL )
return FALSE ;
for ( temp_servicesmember = group - > members ; temp_servicesmember ! = NULL ; temp_servicesmember = temp_servicesmember - > next ) {
if ( temp_servicesmember - > service_ptr ! = NULL & & temp_servicesmember - > service_ptr - > host_ptr = = hst )
return TRUE ;
}
return FALSE ;
}
/* tests whether a service is a member of a particular servicegroup */
/* NOTE: This function is only used by the CGIS */
int is_service_member_of_servicegroup ( servicegroup * group , service * svc ) {
servicesmember * temp_servicesmember = NULL ;
if ( group = = NULL | | svc = = NULL )
return FALSE ;
for ( temp_servicesmember = group - > members ; temp_servicesmember ! = NULL ; temp_servicesmember = temp_servicesmember - > next ) {
if ( temp_servicesmember - > service_ptr = = svc )
return TRUE ;
}
return FALSE ;
}
/*
* tests whether a contact is a member of a particular contactgroup .
* The mk - livestatus eventbroker module uses this , so it must hang
* around until 4.0 to prevent api breakage .
* The cgi ' s stopped using it quite long ago though , so we need only
* compile it if we ' re building the core
*/
int is_contact_member_of_contactgroup ( contactgroup * group , contact * cntct ) {
contactsmember * member ;
if ( ! group | | ! cntct )
return FALSE ;
/* search all contacts in this contact group */
for ( member = group - > members ; member ; member = member - > next ) {
2017-05-19 23:37:19 +02:00
if ( member - > contact_ptr = = cntct )
2017-05-19 22:22:40 +02:00
return TRUE ;
}
return FALSE ;
}
/* tests whether a contact is a contact for a particular host */
int is_contact_for_host ( host * hst , contact * cntct ) {
contactsmember * temp_contactsmember = NULL ;
contactgroupsmember * temp_contactgroupsmember = NULL ;
contactgroup * temp_contactgroup = NULL ;
if ( hst = = NULL | | cntct = = NULL ) {
return FALSE ;
}
/* search all individual contacts of this host */
for ( temp_contactsmember = hst - > contacts ; temp_contactsmember ! = NULL ; temp_contactsmember = temp_contactsmember - > next ) {
2017-05-19 23:37:19 +02:00
if ( temp_contactsmember - > contact_ptr = = cntct )
2017-05-19 22:22:40 +02:00
return TRUE ;
}
/* search all contactgroups of this host */
for ( temp_contactgroupsmember = hst - > contact_groups ; temp_contactgroupsmember ! = NULL ; temp_contactgroupsmember = temp_contactgroupsmember - > next ) {
temp_contactgroup = temp_contactgroupsmember - > group_ptr ;
if ( is_contact_member_of_contactgroup ( temp_contactgroup , cntct ) )
return TRUE ;
}
return FALSE ;
}
2017-05-19 23:37:19 +02:00
/* tests whether a contactgroup is a contactgroup for a particular host */
int is_contactgroup_for_host ( host * hst , contactgroup * group ) {
contactgroupsmember * temp_contactgroupsmember = NULL ;
if ( hst = = NULL | | group = = NULL ) {
return FALSE ;
}
/* search all contactgroups of this host */
for ( temp_contactgroupsmember = hst - > contact_groups ;
temp_contactgroupsmember ! = NULL ;
temp_contactgroupsmember = temp_contactgroupsmember - > next ) {
if ( temp_contactgroupsmember - > group_ptr = = group ) {
return TRUE ;
}
}
return FALSE ;
}
/* tests whether a contact is an escalated contact for a particular host */
2017-05-19 22:22:40 +02:00
int is_escalated_contact_for_host ( host * hst , contact * cntct ) {
hostescalation * temp_hostescalation = NULL ;
2017-05-19 23:37:19 +02:00
objectlist * list ;
/* search all host escalations */
for ( list = hst - > escalation_list ; list ; list = list - > next ) {
temp_hostescalation = ( hostescalation * ) list - > object_ptr ;
if ( is_contact_for_host_escalation ( temp_hostescalation , cntct ) = = TRUE ) {
return TRUE ;
}
}
return FALSE ;
}
/* tests whether a contact is an contact for a particular host escalation */
int is_contact_for_host_escalation ( hostescalation * escalation , contact * cntct ) {
contactsmember * temp_contactsmember = NULL ;
2017-05-19 22:22:40 +02:00
contactgroupsmember * temp_contactgroupsmember = NULL ;
contactgroup * temp_contactgroup = NULL ;
2017-05-19 23:37:19 +02:00
if ( escalation = = NULL | | cntct = = NULL ) {
return FALSE ;
}
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* search all contacts of this host escalation */
for ( temp_contactsmember = escalation - > contacts ;
temp_contactsmember ! = NULL ;
temp_contactsmember = temp_contactsmember - > next ) {
if ( temp_contactsmember - > contact_ptr = = cntct )
return TRUE ;
}
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* search all contactgroups of this host escalation */
for ( temp_contactgroupsmember = escalation - > contact_groups ;
temp_contactgroupsmember ! = NULL ;
temp_contactgroupsmember = temp_contactgroupsmember - > next ) {
temp_contactgroup = temp_contactgroupsmember - > group_ptr ;
if ( is_contact_member_of_contactgroup ( temp_contactgroup , cntct ) )
return TRUE ;
}
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
return FALSE ;
}
/* tests whether a contactgroup is a contactgroup for a particular
host escalation */
int is_contactgroup_for_host_escalation ( hostescalation * escalation ,
contactgroup * group ) {
contactgroupsmember * temp_contactgroupsmember = NULL ;
if ( escalation = = NULL | | group = = NULL ) {
return FALSE ;
}
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* search all contactgroups of this host escalation */
for ( temp_contactgroupsmember = escalation - > contact_groups ;
temp_contactgroupsmember ! = NULL ;
temp_contactgroupsmember = temp_contactgroupsmember - > next ) {
if ( temp_contactgroupsmember - > group_ptr = = group ) {
return TRUE ;
2017-05-19 22:22:40 +02:00
}
}
return FALSE ;
}
2017-05-19 23:37:19 +02:00
2017-05-19 22:22:40 +02:00
/* tests whether a contact is a contact for a particular service */
int is_contact_for_service ( service * svc , contact * cntct ) {
contactsmember * temp_contactsmember = NULL ;
contactgroupsmember * temp_contactgroupsmember = NULL ;
contactgroup * temp_contactgroup = NULL ;
if ( svc = = NULL | | cntct = = NULL )
return FALSE ;
/* search all individual contacts of this service */
for ( temp_contactsmember = svc - > contacts ; temp_contactsmember ! = NULL ; temp_contactsmember = temp_contactsmember - > next ) {
2017-05-19 23:37:19 +02:00
if ( temp_contactsmember - > contact_ptr = = cntct )
2017-05-19 22:22:40 +02:00
return TRUE ;
}
/* search all contactgroups of this service */
for ( temp_contactgroupsmember = svc - > contact_groups ; temp_contactgroupsmember ! = NULL ; temp_contactgroupsmember = temp_contactgroupsmember - > next ) {
temp_contactgroup = temp_contactgroupsmember - > group_ptr ;
if ( is_contact_member_of_contactgroup ( temp_contactgroup , cntct ) )
return TRUE ;
}
return FALSE ;
}
2017-05-19 23:37:19 +02:00
/* tests whether a contactgroup is a contactgroup for a particular service */
int is_contactgroup_for_service ( service * svc , contactgroup * group ) {
2017-05-19 22:22:40 +02:00
contactgroupsmember * temp_contactgroupsmember = NULL ;
2017-05-19 23:37:19 +02:00
if ( svc = = NULL | | group = = NULL )
return FALSE ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* search all contactgroups of this service */
for ( temp_contactgroupsmember = svc - > contact_groups ;
temp_contactgroupsmember ! = NULL ;
temp_contactgroupsmember = temp_contactgroupsmember - > next ) {
if ( temp_contactgroupsmember - > group_ptr = = group ) {
return TRUE ;
2017-05-19 22:22:40 +02:00
}
}
return FALSE ;
}
2017-05-19 23:37:19 +02:00
/* tests whether a contact is an escalated contact for a particular service */
int is_escalated_contact_for_service ( service * svc , contact * cntct ) {
serviceescalation * temp_serviceescalation = NULL ;
objectlist * list ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* search all the service escalations */
for ( list = svc - > escalation_list ; list ; list = list - > next ) {
temp_serviceescalation = ( serviceescalation * ) list - > object_ptr ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
if ( is_contact_for_service_escalation ( temp_serviceescalation ,
cntct ) = = TRUE ) {
2017-05-19 22:22:40 +02:00
return TRUE ;
}
}
2017-05-19 23:37:19 +02:00
return FALSE ;
}
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* tests whether a contact is an contact for a particular service escalation */
int is_contact_for_service_escalation ( serviceescalation * escalation ,
contact * cntct ) {
contactsmember * temp_contactsmember = NULL ;
contactgroupsmember * temp_contactgroupsmember = NULL ;
contactgroup * temp_contactgroup = NULL ;
/* search all contacts of this service escalation */
for ( temp_contactsmember = escalation - > contacts ;
temp_contactsmember ! = NULL ;
temp_contactsmember = temp_contactsmember - > next ) {
if ( temp_contactsmember - > contact_ptr = = cntct )
return TRUE ;
}
/* search all contactgroups of this service escalation */
for ( temp_contactgroupsmember = escalation - > contact_groups ;
temp_contactgroupsmember ! = NULL ;
temp_contactgroupsmember = temp_contactgroupsmember - > next ) {
temp_contactgroup = temp_contactgroupsmember - > group_ptr ;
if ( is_contact_member_of_contactgroup ( temp_contactgroup , cntct ) )
2017-05-19 22:22:40 +02:00
return TRUE ;
}
return FALSE ;
}
2017-05-19 23:37:19 +02:00
/* tests whether a contactgroup is a contactgroup for a particular
service escalation */
int is_contactgroup_for_service_escalation ( serviceescalation * escalation ,
contactgroup * group ) {
contactgroupsmember * temp_contactgroupsmember = NULL ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
if ( escalation = = NULL | | group = = NULL ) {
2017-05-19 22:22:40 +02:00
return FALSE ;
}
2017-05-19 23:37:19 +02:00
/* search all contactgroups of this service escalation */
for ( temp_contactgroupsmember = escalation - > contact_groups ;
temp_contactgroupsmember ! = NULL ;
temp_contactgroupsmember = temp_contactgroupsmember - > next ) {
if ( temp_contactgroupsmember - > group_ptr = = group ) {
2017-05-19 22:22:40 +02:00
return TRUE ;
2017-05-19 23:37:19 +02:00
}
2017-05-19 22:22:40 +02:00
}
return FALSE ;
}
/******************************************************************/
/******************* OBJECT DELETION FUNCTIONS ********************/
/******************************************************************/
/* free all allocated memory for objects */
int free_object_data ( void ) {
daterange * this_daterange = NULL ;
daterange * next_daterange = NULL ;
timerange * this_timerange = NULL ;
timerange * next_timerange = NULL ;
timeperiodexclusion * this_timeperiodexclusion = NULL ;
timeperiodexclusion * next_timeperiodexclusion = NULL ;
hostsmember * this_hostsmember = NULL ;
hostsmember * next_hostsmember = NULL ;
servicesmember * this_servicesmember = NULL ;
servicesmember * next_servicesmember = NULL ;
contactsmember * this_contactsmember = NULL ;
contactsmember * next_contactsmember = NULL ;
contactgroupsmember * this_contactgroupsmember = NULL ;
contactgroupsmember * next_contactgroupsmember = NULL ;
customvariablesmember * this_customvariablesmember = NULL ;
customvariablesmember * next_customvariablesmember = NULL ;
commandsmember * this_commandsmember = NULL ;
commandsmember * next_commandsmember = NULL ;
2017-05-19 23:37:19 +02:00
unsigned int i = 0 , x = 0 ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/*
* kill off hash tables so lingering modules don ' t look stuff up
* while we ' re busy removing it .
*/
for ( i = 0 ; i < ARRAY_SIZE ( object_hash_tables ) ; i + + ) {
dkhash_table * t = object_hash_tables [ i ] ;
object_hash_tables [ i ] = NULL ;
dkhash_destroy ( t ) ;
}
2017-05-19 22:22:40 +02:00
/**** free memory for the timeperiod list ****/
2017-05-19 23:37:19 +02:00
for ( i = 0 ; i < num_objects . timeperiods ; i + + ) {
timeperiod * this_timeperiod = timeperiod_ary [ i ] ;
2017-05-19 22:22:40 +02:00
/* free the exception time ranges contained in this timeperiod */
for ( x = 0 ; x < DATERANGE_TYPES ; x + + ) {
for ( this_daterange = this_timeperiod - > exceptions [ x ] ; this_daterange ! = NULL ; this_daterange = next_daterange ) {
next_daterange = this_daterange - > next ;
for ( this_timerange = this_daterange - > times ; this_timerange ! = NULL ; this_timerange = next_timerange ) {
next_timerange = this_timerange - > next ;
my_free ( this_timerange ) ;
}
my_free ( this_daterange ) ;
}
}
/* free the day time ranges contained in this timeperiod */
for ( x = 0 ; x < 7 ; x + + ) {
for ( this_timerange = this_timeperiod - > days [ x ] ; this_timerange ! = NULL ; this_timerange = next_timerange ) {
next_timerange = this_timerange - > next ;
my_free ( this_timerange ) ;
}
}
/* free exclusions */
for ( this_timeperiodexclusion = this_timeperiod - > exclusions ; this_timeperiodexclusion ! = NULL ; this_timeperiodexclusion = next_timeperiodexclusion ) {
next_timeperiodexclusion = this_timeperiodexclusion - > next ;
my_free ( this_timeperiodexclusion - > timeperiod_name ) ;
my_free ( this_timeperiodexclusion ) ;
}
2017-05-19 23:37:19 +02:00
if ( this_timeperiod - > alias ! = this_timeperiod - > name )
my_free ( this_timeperiod - > alias ) ;
2017-05-19 22:22:40 +02:00
my_free ( this_timeperiod - > name ) ;
my_free ( this_timeperiod ) ;
}
/* reset pointers */
2017-05-19 23:37:19 +02:00
my_free ( timeperiod_ary ) ;
2017-05-19 22:22:40 +02:00
/**** free memory for the host list ****/
2017-05-19 23:37:19 +02:00
for ( i = 0 ; i < num_objects . hosts ; i + + ) {
host * this_host = host_ary [ i ] ;
2017-05-19 22:22:40 +02:00
/* free memory for parent hosts */
this_hostsmember = this_host - > parent_hosts ;
while ( this_hostsmember ! = NULL ) {
next_hostsmember = this_hostsmember - > next ;
my_free ( this_hostsmember - > host_name ) ;
my_free ( this_hostsmember ) ;
this_hostsmember = next_hostsmember ;
}
/* free memory for child host links */
this_hostsmember = this_host - > child_hosts ;
while ( this_hostsmember ! = NULL ) {
next_hostsmember = this_hostsmember - > next ;
my_free ( this_hostsmember ) ;
this_hostsmember = next_hostsmember ;
}
/* free memory for service links */
this_servicesmember = this_host - > services ;
while ( this_servicesmember ! = NULL ) {
next_servicesmember = this_servicesmember - > next ;
my_free ( this_servicesmember ) ;
this_servicesmember = next_servicesmember ;
}
/* free memory for contact groups */
this_contactgroupsmember = this_host - > contact_groups ;
while ( this_contactgroupsmember ! = NULL ) {
next_contactgroupsmember = this_contactgroupsmember - > next ;
my_free ( this_contactgroupsmember ) ;
this_contactgroupsmember = next_contactgroupsmember ;
}
/* free memory for contacts */
this_contactsmember = this_host - > contacts ;
while ( this_contactsmember ! = NULL ) {
next_contactsmember = this_contactsmember - > next ;
my_free ( this_contactsmember ) ;
this_contactsmember = next_contactsmember ;
}
/* free memory for custom variables */
this_customvariablesmember = this_host - > custom_variables ;
while ( this_customvariablesmember ! = NULL ) {
next_customvariablesmember = this_customvariablesmember - > next ;
my_free ( this_customvariablesmember - > variable_name ) ;
my_free ( this_customvariablesmember - > variable_value ) ;
my_free ( this_customvariablesmember ) ;
this_customvariablesmember = next_customvariablesmember ;
}
2017-05-19 23:37:19 +02:00
if ( this_host - > display_name ! = this_host - > name )
my_free ( this_host - > display_name ) ;
if ( this_host - > alias ! = this_host - > name )
my_free ( this_host - > alias ) ;
if ( this_host - > address ! = this_host - > name )
my_free ( this_host - > address ) ;
2017-05-19 22:22:40 +02:00
my_free ( this_host - > name ) ;
# ifdef NSCORE
my_free ( this_host - > plugin_output ) ;
my_free ( this_host - > long_plugin_output ) ;
my_free ( this_host - > perf_data ) ;
# endif
2017-05-19 23:37:19 +02:00
free_objectlist ( & this_host - > hostgroups_ptr ) ;
free_objectlist ( & this_host - > notify_deps ) ;
free_objectlist ( & this_host - > exec_deps ) ;
free_objectlist ( & this_host - > escalation_list ) ;
my_free ( this_host - > check_command ) ;
2017-05-19 22:22:40 +02:00
my_free ( this_host - > event_handler ) ;
my_free ( this_host - > notes ) ;
my_free ( this_host - > notes_url ) ;
my_free ( this_host - > action_url ) ;
my_free ( this_host - > icon_image ) ;
my_free ( this_host - > icon_image_alt ) ;
my_free ( this_host - > vrml_image ) ;
my_free ( this_host - > statusmap_image ) ;
my_free ( this_host ) ;
}
/* reset pointers */
2017-05-19 23:37:19 +02:00
my_free ( host_ary ) ;
2017-05-19 22:22:40 +02:00
/**** free memory for the host group list ****/
2017-05-19 23:37:19 +02:00
for ( i = 0 ; i < num_objects . hostgroups ; i + + ) {
hostgroup * this_hostgroup = hostgroup_ary [ i ] ;
2017-05-19 22:22:40 +02:00
/* free memory for the group members */
this_hostsmember = this_hostgroup - > members ;
while ( this_hostsmember ! = NULL ) {
next_hostsmember = this_hostsmember - > next ;
my_free ( this_hostsmember ) ;
this_hostsmember = next_hostsmember ;
}
2017-05-19 23:37:19 +02:00
if ( this_hostgroup - > alias ! = this_hostgroup - > group_name )
my_free ( this_hostgroup - > alias ) ;
2017-05-19 22:22:40 +02:00
my_free ( this_hostgroup - > group_name ) ;
my_free ( this_hostgroup - > notes ) ;
my_free ( this_hostgroup - > notes_url ) ;
my_free ( this_hostgroup - > action_url ) ;
my_free ( this_hostgroup ) ;
}
/* reset pointers */
2017-05-19 23:37:19 +02:00
my_free ( hostgroup_ary ) ;
2017-05-19 22:22:40 +02:00
/**** free memory for the service group list ****/
2017-05-19 23:37:19 +02:00
for ( i = 0 ; i < num_objects . servicegroups ; i + + ) {
servicegroup * this_servicegroup = servicegroup_ary [ i ] ;
2017-05-19 22:22:40 +02:00
/* free memory for the group members */
this_servicesmember = this_servicegroup - > members ;
while ( this_servicesmember ! = NULL ) {
next_servicesmember = this_servicesmember - > next ;
my_free ( this_servicesmember ) ;
this_servicesmember = next_servicesmember ;
}
2017-05-19 23:37:19 +02:00
if ( this_servicegroup - > alias ! = this_servicegroup - > group_name )
my_free ( this_servicegroup - > alias ) ;
2017-05-19 22:22:40 +02:00
my_free ( this_servicegroup - > group_name ) ;
my_free ( this_servicegroup - > notes ) ;
my_free ( this_servicegroup - > notes_url ) ;
my_free ( this_servicegroup - > action_url ) ;
my_free ( this_servicegroup ) ;
}
/* reset pointers */
2017-05-19 23:37:19 +02:00
my_free ( servicegroup_ary ) ;
2017-05-19 22:22:40 +02:00
/**** free memory for the contact list ****/
2017-05-19 23:37:19 +02:00
for ( i = 0 ; i < num_objects . contacts ; i + + ) {
int j ;
contact * this_contact = contact_ary [ i ] ;
2017-05-19 22:22:40 +02:00
/* free memory for the host notification commands */
this_commandsmember = this_contact - > host_notification_commands ;
while ( this_commandsmember ! = NULL ) {
next_commandsmember = this_commandsmember - > next ;
if ( this_commandsmember - > command ! = NULL )
my_free ( this_commandsmember - > command ) ;
my_free ( this_commandsmember ) ;
this_commandsmember = next_commandsmember ;
}
/* free memory for the service notification commands */
this_commandsmember = this_contact - > service_notification_commands ;
while ( this_commandsmember ! = NULL ) {
next_commandsmember = this_commandsmember - > next ;
if ( this_commandsmember - > command ! = NULL )
my_free ( this_commandsmember - > command ) ;
my_free ( this_commandsmember ) ;
this_commandsmember = next_commandsmember ;
}
/* free memory for custom variables */
this_customvariablesmember = this_contact - > custom_variables ;
while ( this_customvariablesmember ! = NULL ) {
next_customvariablesmember = this_customvariablesmember - > next ;
my_free ( this_customvariablesmember - > variable_name ) ;
my_free ( this_customvariablesmember - > variable_value ) ;
my_free ( this_customvariablesmember ) ;
this_customvariablesmember = next_customvariablesmember ;
}
2017-05-19 23:37:19 +02:00
if ( this_contact - > alias ! = this_contact - > name )
my_free ( this_contact - > alias ) ;
2017-05-19 22:22:40 +02:00
my_free ( this_contact - > name ) ;
my_free ( this_contact - > email ) ;
my_free ( this_contact - > pager ) ;
2017-05-19 23:37:19 +02:00
for ( j = 0 ; j < MAX_CONTACT_ADDRESSES ; j + + )
my_free ( this_contact - > address [ j ] ) ;
2017-05-19 22:22:40 +02:00
free_objectlist ( & this_contact - > contactgroups_ptr ) ;
my_free ( this_contact ) ;
}
/* reset pointers */
2017-05-19 23:37:19 +02:00
my_free ( contact_ary ) ;
2017-05-19 22:22:40 +02:00
/**** free memory for the contact group list ****/
2017-05-19 23:37:19 +02:00
for ( i = 0 ; i < num_objects . contactgroups ; i + + ) {
contactgroup * this_contactgroup = contactgroup_ary [ i ] ;
2017-05-19 22:22:40 +02:00
/* free memory for the group members */
this_contactsmember = this_contactgroup - > members ;
while ( this_contactsmember ! = NULL ) {
next_contactsmember = this_contactsmember - > next ;
my_free ( this_contactsmember ) ;
this_contactsmember = next_contactsmember ;
}
2017-05-19 23:37:19 +02:00
if ( this_contactgroup - > alias ! = this_contactgroup - > group_name )
my_free ( this_contactgroup - > alias ) ;
2017-05-19 22:22:40 +02:00
my_free ( this_contactgroup - > group_name ) ;
my_free ( this_contactgroup ) ;
}
/* reset pointers */
2017-05-19 23:37:19 +02:00
my_free ( contactgroup_ary ) ;
2017-05-19 22:22:40 +02:00
/**** free memory for the service list ****/
2017-05-19 23:37:19 +02:00
for ( i = 0 ; i < num_objects . services ; i + + ) {
service * this_service = service_ary [ i ] ;
2017-05-19 22:22:40 +02:00
/* free memory for contact groups */
this_contactgroupsmember = this_service - > contact_groups ;
while ( this_contactgroupsmember ! = NULL ) {
next_contactgroupsmember = this_contactgroupsmember - > next ;
my_free ( this_contactgroupsmember ) ;
this_contactgroupsmember = next_contactgroupsmember ;
}
/* free memory for contacts */
this_contactsmember = this_service - > contacts ;
while ( this_contactsmember ! = NULL ) {
next_contactsmember = this_contactsmember - > next ;
my_free ( this_contactsmember ) ;
this_contactsmember = next_contactsmember ;
}
/* free memory for custom variables */
this_customvariablesmember = this_service - > custom_variables ;
while ( this_customvariablesmember ! = NULL ) {
next_customvariablesmember = this_customvariablesmember - > next ;
my_free ( this_customvariablesmember - > variable_name ) ;
my_free ( this_customvariablesmember - > variable_value ) ;
my_free ( this_customvariablesmember ) ;
this_customvariablesmember = next_customvariablesmember ;
}
2017-05-19 23:37:19 +02:00
if ( this_service - > display_name ! = this_service - > description )
my_free ( this_service - > display_name ) ;
2017-05-19 22:22:40 +02:00
my_free ( this_service - > description ) ;
2017-05-19 23:37:19 +02:00
my_free ( this_service - > check_command ) ;
2017-05-19 22:22:40 +02:00
# ifdef NSCORE
my_free ( this_service - > plugin_output ) ;
my_free ( this_service - > long_plugin_output ) ;
my_free ( this_service - > perf_data ) ;
my_free ( this_service - > event_handler_args ) ;
my_free ( this_service - > check_command_args ) ;
2017-05-19 23:37:19 +02:00
# endif
2017-05-19 22:22:40 +02:00
free_objectlist ( & this_service - > servicegroups_ptr ) ;
2017-05-19 23:37:19 +02:00
free_objectlist ( & this_service - > notify_deps ) ;
free_objectlist ( & this_service - > exec_deps ) ;
free_objectlist ( & this_service - > escalation_list ) ;
2017-05-19 22:22:40 +02:00
my_free ( this_service - > event_handler ) ;
my_free ( this_service - > notes ) ;
my_free ( this_service - > notes_url ) ;
my_free ( this_service - > action_url ) ;
my_free ( this_service - > icon_image ) ;
my_free ( this_service - > icon_image_alt ) ;
my_free ( this_service ) ;
}
/* reset pointers */
2017-05-19 23:37:19 +02:00
my_free ( service_ary ) ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/**** free command memory ****/
for ( i = 0 ; i < num_objects . commands ; i + + ) {
command * this_command = command_ary [ i ] ;
2017-05-19 22:22:40 +02:00
my_free ( this_command - > name ) ;
my_free ( this_command - > command_line ) ;
my_free ( this_command ) ;
}
/* reset pointers */
2017-05-19 23:37:19 +02:00
my_free ( command_ary ) ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/**** free service escalation memory ****/
for ( i = 0 ; i < num_objects . serviceescalations ; i + + ) {
serviceescalation * this_serviceescalation = serviceescalation_ary [ i ] ;
2017-05-19 22:22:40 +02:00
/* free memory for the contact group members */
this_contactgroupsmember = this_serviceescalation - > contact_groups ;
while ( this_contactgroupsmember ! = NULL ) {
next_contactgroupsmember = this_contactgroupsmember - > next ;
my_free ( this_contactgroupsmember ) ;
this_contactgroupsmember = next_contactgroupsmember ;
}
/* free memory for contacts */
this_contactsmember = this_serviceescalation - > contacts ;
while ( this_contactsmember ! = NULL ) {
next_contactsmember = this_contactsmember - > next ;
my_free ( this_contactsmember ) ;
this_contactsmember = next_contactsmember ;
}
my_free ( this_serviceescalation ) ;
}
/* reset pointers */
2017-05-19 23:37:19 +02:00
my_free ( serviceescalation_ary ) ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/**** free service dependency memory ****/
if ( servicedependency_ary ) {
for ( i = 0 ; i < num_objects . servicedependencies ; i + + )
my_free ( servicedependency_ary [ i ] ) ;
my_free ( servicedependency_ary ) ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
/**** free host dependency memory ****/
if ( hostdependency_ary ) {
for ( i = 0 ; i < num_objects . hostdependencies ; i + + )
my_free ( hostdependency_ary [ i ] ) ;
my_free ( hostdependency_ary ) ;
2017-05-19 22:22:40 +02:00
}
2017-05-19 23:37:19 +02:00
/**** free host escalation memory ****/
for ( i = 0 ; i < num_objects . hostescalations ; i + + ) {
hostescalation * this_hostescalation = hostescalation_ary [ i ] ;
2017-05-19 22:22:40 +02:00
/* free memory for the contact group members */
this_contactgroupsmember = this_hostescalation - > contact_groups ;
while ( this_contactgroupsmember ! = NULL ) {
next_contactgroupsmember = this_contactgroupsmember - > next ;
my_free ( this_contactgroupsmember ) ;
this_contactgroupsmember = next_contactgroupsmember ;
}
/* free memory for contacts */
this_contactsmember = this_hostescalation - > contacts ;
while ( this_contactsmember ! = NULL ) {
next_contactsmember = this_contactsmember - > next ;
my_free ( this_contactsmember ) ;
this_contactsmember = next_contactsmember ;
}
my_free ( this_hostescalation ) ;
}
/* reset pointers */
2017-05-19 23:37:19 +02:00
my_free ( hostescalation_ary ) ;
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
/* we no longer have any objects */
memset ( & num_objects , 0 , sizeof ( num_objects ) ) ;
2017-05-19 22:22:40 +02:00
return OK ;
}
2017-05-19 23:37:19 +02:00
/******************************************************************/
/*********************** CACHE FUNCTIONS **************************/
/******************************************************************/
# ifndef NSCGI
static const char * timerange2str ( const timerange * tr )
{
static char str [ 12 ] ;
int sh , sm , eh , em ;
if ( ! tr )
return " " ;
sh = tr - > range_start / 3600 ;
sm = ( tr - > range_start / 60 ) % 60 ;
eh = tr - > range_end / 3600 ;
em = ( tr - > range_end / 60 ) % 60 ;
sprintf ( str , " %02d:%02d-%02d:%02d " , sh , sm , eh , em ) ;
return str ;
}
void fcache_contactlist ( FILE * fp , const char * prefix , contactsmember * list )
{
if ( list ) {
contactsmember * l ;
fprintf ( fp , " %s " , prefix ) ;
for ( l = list ; l ; l = l - > next )
fprintf ( fp , " %s%c " , l - > contact_name , l - > next ? ' , ' : ' \n ' ) ;
}
}
void fcache_contactgrouplist ( FILE * fp , const char * prefix , contactgroupsmember * list )
{
if ( list ) {
contactgroupsmember * l ;
fprintf ( fp , " %s " , prefix ) ;
for ( l = list ; l ; l = l - > next )
fprintf ( fp , " %s%c " , l - > group_name , l - > next ? ' , ' : ' \n ' ) ;
}
}
void fcache_hostlist ( FILE * fp , const char * prefix , hostsmember * list )
{
if ( list ) {
hostsmember * l ;
fprintf ( fp , " %s " , prefix ) ;
for ( l = list ; l ; l = l - > next )
fprintf ( fp , " %s%c " , l - > host_name , l - > next ? ' , ' : ' \n ' ) ;
}
}
void fcache_customvars ( FILE * fp , customvariablesmember * cvlist )
{
if ( cvlist ) {
customvariablesmember * l ;
for ( l = cvlist ; l ; l = l - > next )
fprintf ( fp , " \t _%s \t %s \n " , l - > variable_name , ( l - > variable_value = = NULL ) ? XODTEMPLATE_NULL : l - > variable_value ) ;
}
}
void fcache_timeperiod ( FILE * fp , timeperiod * temp_timeperiod )
{
const char * days [ 7 ] = { " sunday " , " monday " , " tuesday " , " wednesday " , " thursday " , " friday " , " saturday " } ;
const char * months [ 12 ] = { " january " , " february " , " march " , " april " , " may " , " june " , " july " , " august " , " september " , " october " , " november " , " december " } ;
daterange * temp_daterange ;
timerange * tr ;
register int x ;
fprintf ( fp , " define timeperiod { \n " ) ;
fprintf ( fp , " \t timeperiod_name \t %s \n " , temp_timeperiod - > name ) ;
if ( temp_timeperiod - > alias )
fprintf ( fp , " \t alias \t %s \n " , temp_timeperiod - > alias ) ;
if ( temp_timeperiod - > exclusions ) {
timeperiodexclusion * exclude ;
fprintf ( fp , " \t exclude \t " ) ;
for ( exclude = temp_timeperiod - > exclusions ; exclude ; exclude = exclude - > next ) {
fprintf ( fp , " %s%c " , exclude - > timeperiod_name , exclude - > next ? ' , ' : ' \n ' ) ;
}
}
for ( x = 0 ; x < DATERANGE_TYPES ; x + + ) {
for ( temp_daterange = temp_timeperiod - > exceptions [ x ] ; temp_daterange ! = NULL ; temp_daterange = temp_daterange - > next ) {
/* skip null entries */
if ( temp_daterange - > times = = NULL )
continue ;
switch ( temp_daterange - > type ) {
case DATERANGE_CALENDAR_DATE :
fprintf ( fp , " \t %d-%02d-%02d " , temp_daterange - > syear , temp_daterange - > smon + 1 , temp_daterange - > smday ) ;
if ( ( temp_daterange - > smday ! = temp_daterange - > emday ) | | ( temp_daterange - > smon ! = temp_daterange - > emon ) | | ( temp_daterange - > syear ! = temp_daterange - > eyear ) )
fprintf ( fp , " - %d-%02d-%02d " , temp_daterange - > eyear , temp_daterange - > emon + 1 , temp_daterange - > emday ) ;
if ( temp_daterange - > skip_interval > 1 )
fprintf ( fp , " / %d " , temp_daterange - > skip_interval ) ;
break ;
case DATERANGE_MONTH_DATE :
fprintf ( fp , " \t %s %d " , months [ temp_daterange - > smon ] , temp_daterange - > smday ) ;
if ( ( temp_daterange - > smon ! = temp_daterange - > emon ) | | ( temp_daterange - > smday ! = temp_daterange - > emday ) ) {
fprintf ( fp , " - %s %d " , months [ temp_daterange - > emon ] , temp_daterange - > emday ) ;
if ( temp_daterange - > skip_interval > 1 )
fprintf ( fp , " / %d " , temp_daterange - > skip_interval ) ;
}
break ;
case DATERANGE_MONTH_DAY :
fprintf ( fp , " \t day %d " , temp_daterange - > smday ) ;
if ( temp_daterange - > smday ! = temp_daterange - > emday ) {
fprintf ( fp , " - %d " , temp_daterange - > emday ) ;
if ( temp_daterange - > skip_interval > 1 )
fprintf ( fp , " / %d " , temp_daterange - > skip_interval ) ;
}
break ;
case DATERANGE_MONTH_WEEK_DAY :
fprintf ( fp , " \t %s %d %s " , days [ temp_daterange - > swday ] , temp_daterange - > swday_offset , months [ temp_daterange - > smon ] ) ;
if ( ( temp_daterange - > smon ! = temp_daterange - > emon ) | | ( temp_daterange - > swday ! = temp_daterange - > ewday ) | | ( temp_daterange - > swday_offset ! = temp_daterange - > ewday_offset ) ) {
fprintf ( fp , " - %s %d %s " , days [ temp_daterange - > ewday ] , temp_daterange - > ewday_offset , months [ temp_daterange - > emon ] ) ;
if ( temp_daterange - > skip_interval > 1 )
fprintf ( fp , " / %d " , temp_daterange - > skip_interval ) ;
}
break ;
case DATERANGE_WEEK_DAY :
fprintf ( fp , " \t %s %d " , days [ temp_daterange - > swday ] , temp_daterange - > swday_offset ) ;
if ( ( temp_daterange - > swday ! = temp_daterange - > ewday ) | | ( temp_daterange - > swday_offset ! = temp_daterange - > ewday_offset ) ) {
fprintf ( fp , " - %s %d " , days [ temp_daterange - > ewday ] , temp_daterange - > ewday_offset ) ;
if ( temp_daterange - > skip_interval > 1 )
fprintf ( fp , " / %d " , temp_daterange - > skip_interval ) ;
}
break ;
default :
break ;
}
fputc ( ' \t ' , fp ) ;
for ( tr = temp_daterange - > times ; tr ; tr = tr - > next ) {
fprintf ( fp , " %s%c " , timerange2str ( tr ) , tr - > next ? ' , ' : ' \n ' ) ;
}
}
}
for ( x = 0 ; x < 7 ; x + + ) {
/* skip null entries */
if ( temp_timeperiod - > days [ x ] = = NULL )
continue ;
fprintf ( fp , " \t %s \t " , days [ x ] ) ;
for ( tr = temp_timeperiod - > days [ x ] ; tr ; tr = tr - > next ) {
fprintf ( fp , " %s%c " , timerange2str ( tr ) , tr - > next ? ' , ' : ' \n ' ) ;
}
}
fprintf ( fp , " \t } \n \n " ) ;
}
void fcache_command ( FILE * fp , command * temp_command )
{
fprintf ( fp , " define command { \n \t command_name \t %s \n \t command_line \t %s \n \t } \n \n " ,
temp_command - > name , temp_command - > command_line ) ;
}
void fcache_contactgroup ( FILE * fp , contactgroup * temp_contactgroup )
{
fprintf ( fp , " define contactgroup { \n " ) ;
fprintf ( fp , " \t contactgroup_name \t %s \n " , temp_contactgroup - > group_name ) ;
if ( temp_contactgroup - > alias )
fprintf ( fp , " \t alias \t %s \n " , temp_contactgroup - > alias ) ;
fcache_contactlist ( fp , " \t members \t " , temp_contactgroup - > members ) ;
fprintf ( fp , " \t } \n \n " ) ;
}
void fcache_hostgroup ( FILE * fp , hostgroup * temp_hostgroup )
{
fprintf ( fp , " define hostgroup { \n " ) ;
fprintf ( fp , " \t hostgroup_name \t %s \n " , temp_hostgroup - > group_name ) ;
if ( temp_hostgroup - > alias )
fprintf ( fp , " \t alias \t %s \n " , temp_hostgroup - > alias ) ;
if ( temp_hostgroup - > members ) {
hostsmember * list ;
fprintf ( fp , " \t members \t " ) ;
for ( list = temp_hostgroup - > members ; list ; list = list - > next )
fprintf ( fp , " %s%c " , list - > host_name , list - > next ? ' , ' : ' \n ' ) ;
}
if ( temp_hostgroup - > notes )
fprintf ( fp , " \t notes \t %s \n " , temp_hostgroup - > notes ) ;
if ( temp_hostgroup - > notes_url )
fprintf ( fp , " \t notes_url \t %s \n " , temp_hostgroup - > notes_url ) ;
if ( temp_hostgroup - > action_url )
fprintf ( fp , " \t action_url \t %s \n " , temp_hostgroup - > action_url ) ;
fprintf ( fp , " \t } \n \n " ) ;
}
void fcache_servicegroup ( FILE * fp , servicegroup * temp_servicegroup )
{
fprintf ( fp , " define servicegroup { \n " ) ;
fprintf ( fp , " \t servicegroup_name \t %s \n " , temp_servicegroup - > group_name ) ;
if ( temp_servicegroup - > alias )
fprintf ( fp , " \t alias \t %s \n " , temp_servicegroup - > alias ) ;
if ( temp_servicegroup - > members ) {
servicesmember * list ;
fprintf ( fp , " \t members \t " ) ;
for ( list = temp_servicegroup - > members ; list ; list = list - > next ) {
service * s = list - > service_ptr ;
fprintf ( fp , " %s,%s%c " , s - > host_name , s - > description , list - > next ? ' , ' : ' \n ' ) ;
}
}
if ( temp_servicegroup - > notes )
fprintf ( fp , " \t notes \t %s \n " , temp_servicegroup - > notes ) ;
if ( temp_servicegroup - > notes_url )
fprintf ( fp , " \t notes_url \t %s \n " , temp_servicegroup - > notes_url ) ;
if ( temp_servicegroup - > action_url )
fprintf ( fp , " \t action_url \t %s \n " , temp_servicegroup - > action_url ) ;
fprintf ( fp , " \t } \n \n " ) ;
}
void fcache_contact ( FILE * fp , contact * temp_contact )
{
commandsmember * list ;
int x ;
fprintf ( fp , " define contact { \n " ) ;
fprintf ( fp , " \t contact_name \t %s \n " , temp_contact - > name ) ;
if ( temp_contact - > alias )
fprintf ( fp , " \t alias \t %s \n " , temp_contact - > alias ) ;
if ( temp_contact - > service_notification_period )
fprintf ( fp , " \t service_notification_period \t %s \n " , temp_contact - > service_notification_period ) ;
if ( temp_contact - > host_notification_period )
fprintf ( fp , " \t host_notification_period \t %s \n " , temp_contact - > host_notification_period ) ;
fprintf ( fp , " \t service_notification_options \t %s \n " , opts2str ( temp_contact - > service_notification_options , service_flag_map , ' r ' ) ) ;
fprintf ( fp , " \t host_notification_options \t %s \n " , opts2str ( temp_contact - > host_notification_options , host_flag_map , ' r ' ) ) ;
if ( temp_contact - > service_notification_commands ) {
fprintf ( fp , " \t service_notification_commands \t " ) ;
for ( list = temp_contact - > service_notification_commands ; list ; list = list - > next ) {
fprintf ( fp , " %s%c " , list - > command , list - > next ? ' , ' : ' \n ' ) ;
}
}
if ( temp_contact - > host_notification_commands ) {
fprintf ( fp , " \t host_notification_commands \t " ) ;
for ( list = temp_contact - > host_notification_commands ; list ; list = list - > next ) {
fprintf ( fp , " %s%c " , list - > command , list - > next ? ' , ' : ' \n ' ) ;
}
}
if ( temp_contact - > email )
fprintf ( fp , " \t email \t %s \n " , temp_contact - > email ) ;
if ( temp_contact - > pager )
fprintf ( fp , " \t pager \t %s \n " , temp_contact - > pager ) ;
for ( x = 0 ; x < MAX_XODTEMPLATE_CONTACT_ADDRESSES ; x + + ) {
if ( temp_contact - > address [ x ] )
fprintf ( fp , " \t address%d \t %s \n " , x + 1 , temp_contact - > address [ x ] ) ;
}
fprintf ( fp , " \t minimum_importance \t %u \n " , temp_contact - > minimum_value ) ;
fprintf ( fp , " \t host_notifications_enabled \t %d \n " , temp_contact - > host_notifications_enabled ) ;
fprintf ( fp , " \t service_notifications_enabled \t %d \n " , temp_contact - > service_notifications_enabled ) ;
fprintf ( fp , " \t can_submit_commands \t %d \n " , temp_contact - > can_submit_commands ) ;
fprintf ( fp , " \t retain_status_information \t %d \n " , temp_contact - > retain_status_information ) ;
fprintf ( fp , " \t retain_nonstatus_information \t %d \n " , temp_contact - > retain_nonstatus_information ) ;
/* custom variables */
fcache_customvars ( fp , temp_contact - > custom_variables ) ;
fprintf ( fp , " \t } \n \n " ) ;
}
void fcache_host ( FILE * fp , host * temp_host )
{
fprintf ( fp , " define host { \n " ) ;
fprintf ( fp , " \t host_name \t %s \n " , temp_host - > name ) ;
if ( temp_host - > display_name ! = temp_host - > name )
fprintf ( fp , " \t display_name \t %s \n " , temp_host - > display_name ) ;
if ( temp_host - > alias )
fprintf ( fp , " \t alias \t %s \n " , temp_host - > alias ) ;
if ( temp_host - > address )
fprintf ( fp , " \t address \t %s \n " , temp_host - > address ) ;
fcache_hostlist ( fp , " \t parents \t " , temp_host - > parent_hosts ) ;
if ( temp_host - > check_period )
fprintf ( fp , " \t check_period \t %s \n " , temp_host - > check_period ) ;
if ( temp_host - > check_command )
fprintf ( fp , " \t check_command \t %s \n " , temp_host - > check_command ) ;
if ( temp_host - > event_handler )
fprintf ( fp , " \t event_handler \t %s \n " , temp_host - > event_handler ) ;
fcache_contactlist ( fp , " \t contacts \t " , temp_host - > contacts ) ;
fcache_contactgrouplist ( fp , " \t contact_groups \t " , temp_host - > contact_groups ) ;
if ( temp_host - > notification_period )
fprintf ( fp , " \t notification_period \t %s \n " , temp_host - > notification_period ) ;
fprintf ( fp , " \t initial_state \t " ) ;
if ( temp_host - > initial_state = = HOST_DOWN )
fprintf ( fp , " d \n " ) ;
else if ( temp_host - > initial_state = = HOST_UNREACHABLE )
fprintf ( fp , " u \n " ) ;
else
fprintf ( fp , " o \n " ) ;
fprintf ( fp , " \t importance \t %u \n " , temp_host - > hourly_value ) ;
fprintf ( fp , " \t check_interval \t %f \n " , temp_host - > check_interval ) ;
fprintf ( fp , " \t retry_interval \t %f \n " , temp_host - > retry_interval ) ;
fprintf ( fp , " \t max_check_attempts \t %d \n " , temp_host - > max_attempts ) ;
fprintf ( fp , " \t active_checks_enabled \t %d \n " , temp_host - > checks_enabled ) ;
fprintf ( fp , " \t passive_checks_enabled \t %d \n " , temp_host - > accept_passive_checks ) ;
fprintf ( fp , " \t obsess \t %d \n " , temp_host - > obsess ) ;
fprintf ( fp , " \t event_handler_enabled \t %d \n " , temp_host - > event_handler_enabled ) ;
fprintf ( fp , " \t low_flap_threshold \t %f \n " , temp_host - > low_flap_threshold ) ;
fprintf ( fp , " \t high_flap_threshold \t %f \n " , temp_host - > high_flap_threshold ) ;
fprintf ( fp , " \t flap_detection_enabled \t %d \n " , temp_host - > flap_detection_enabled ) ;
fprintf ( fp , " \t flap_detection_options \t %s \n " , opts2str ( temp_host - > flap_detection_options , host_flag_map , ' u ' ) ) ;
fprintf ( fp , " \t freshness_threshold \t %d \n " , temp_host - > freshness_threshold ) ;
fprintf ( fp , " \t check_freshness \t %d \n " , temp_host - > check_freshness ) ;
fprintf ( fp , " \t notification_options \t %s \n " , opts2str ( temp_host - > notification_options , host_flag_map , ' r ' ) ) ;
fprintf ( fp , " \t notifications_enabled \t %d \n " , temp_host - > notifications_enabled ) ;
fprintf ( fp , " \t notification_interval \t %f \n " , temp_host - > notification_interval ) ;
fprintf ( fp , " \t first_notification_delay \t %f \n " , temp_host - > first_notification_delay ) ;
fprintf ( fp , " \t stalking_options \t %s \n " , opts2str ( temp_host - > stalking_options , host_flag_map , ' u ' ) ) ;
fprintf ( fp , " \t process_perf_data \t %d \n " , temp_host - > process_performance_data ) ;
if ( temp_host - > icon_image )
fprintf ( fp , " \t icon_image \t %s \n " , temp_host - > icon_image ) ;
if ( temp_host - > icon_image_alt )
fprintf ( fp , " \t icon_image_alt \t %s \n " , temp_host - > icon_image_alt ) ;
if ( temp_host - > vrml_image )
fprintf ( fp , " \t vrml_image \t %s \n " , temp_host - > vrml_image ) ;
if ( temp_host - > statusmap_image )
fprintf ( fp , " \t statusmap_image \t %s \n " , temp_host - > statusmap_image ) ;
if ( temp_host - > have_2d_coords = = TRUE )
fprintf ( fp , " \t 2d_coords \t %d,%d \n " , temp_host - > x_2d , temp_host - > y_2d ) ;
if ( temp_host - > have_3d_coords = = TRUE )
fprintf ( fp , " \t 3d_coords \t %f,%f,%f \n " , temp_host - > x_3d , temp_host - > y_3d , temp_host - > z_3d ) ;
if ( temp_host - > notes )
fprintf ( fp , " \t notes \t %s \n " , temp_host - > notes ) ;
if ( temp_host - > notes_url )
fprintf ( fp , " \t notes_url \t %s \n " , temp_host - > notes_url ) ;
if ( temp_host - > action_url )
fprintf ( fp , " \t action_url \t %s \n " , temp_host - > action_url ) ;
fprintf ( fp , " \t retain_status_information \t %d \n " , temp_host - > retain_status_information ) ;
fprintf ( fp , " \t retain_nonstatus_information \t %d \n " , temp_host - > retain_nonstatus_information ) ;
/* custom variables */
fcache_customvars ( fp , temp_host - > custom_variables ) ;
fprintf ( fp , " \t } \n \n " ) ;
}
void fcache_service ( FILE * fp , service * temp_service )
{
fprintf ( fp , " define service { \n " ) ;
fprintf ( fp , " \t host_name \t %s \n " , temp_service - > host_name ) ;
fprintf ( fp , " \t service_description \t %s \n " , temp_service - > description ) ;
if ( temp_service - > display_name ! = temp_service - > description )
fprintf ( fp , " \t display_name \t %s \n " , temp_service - > display_name ) ;
if ( temp_service - > parents ) {
fprintf ( fp , " \t parents \t " ) ;
/* same-host, single-parent? */
if ( ! temp_service - > parents - > next & & temp_service - > parents - > service_ptr - > host_ptr = = temp_service - > host_ptr )
fprintf ( fp , " %s \n " , temp_service - > parents - > service_ptr - > description ) ;
else {
servicesmember * sm ;
for ( sm = temp_service - > parents ; sm ; sm = sm - > next ) {
fprintf ( fp , " %s,%s%c " , sm - > host_name , sm - > service_description , sm - > next ? ' , ' : ' \n ' ) ;
}
}
}
if ( temp_service - > check_period )
fprintf ( fp , " \t check_period \t %s \n " , temp_service - > check_period ) ;
if ( temp_service - > check_command )
fprintf ( fp , " \t check_command \t %s \n " , temp_service - > check_command ) ;
if ( temp_service - > event_handler )
fprintf ( fp , " \t event_handler \t %s \n " , temp_service - > event_handler ) ;
fcache_contactlist ( fp , " \t contacts \t " , temp_service - > contacts ) ;
fcache_contactgrouplist ( fp , " \t contact_groups \t " , temp_service - > contact_groups ) ;
if ( temp_service - > notification_period )
fprintf ( fp , " \t notification_period \t %s \n " , temp_service - > notification_period ) ;
fprintf ( fp , " \t initial_state \t " ) ;
if ( temp_service - > initial_state = = STATE_WARNING )
fprintf ( fp , " w \n " ) ;
else if ( temp_service - > initial_state = = STATE_UNKNOWN )
fprintf ( fp , " u \n " ) ;
else if ( temp_service - > initial_state = = STATE_CRITICAL )
fprintf ( fp , " c \n " ) ;
else
fprintf ( fp , " o \n " ) ;
fprintf ( fp , " \t importance \t %u \n " , temp_service - > hourly_value ) ;
fprintf ( fp , " \t check_interval \t %f \n " , temp_service - > check_interval ) ;
fprintf ( fp , " \t retry_interval \t %f \n " , temp_service - > retry_interval ) ;
fprintf ( fp , " \t max_check_attempts \t %d \n " , temp_service - > max_attempts ) ;
fprintf ( fp , " \t is_volatile \t %d \n " , temp_service - > is_volatile ) ;
fprintf ( fp , " \t parallelize_check \t %d \n " , temp_service - > parallelize ) ;
fprintf ( fp , " \t active_checks_enabled \t %d \n " , temp_service - > checks_enabled ) ;
fprintf ( fp , " \t passive_checks_enabled \t %d \n " , temp_service - > accept_passive_checks ) ;
fprintf ( fp , " \t obsess \t %d \n " , temp_service - > obsess ) ;
fprintf ( fp , " \t event_handler_enabled \t %d \n " , temp_service - > event_handler_enabled ) ;
fprintf ( fp , " \t low_flap_threshold \t %f \n " , temp_service - > low_flap_threshold ) ;
fprintf ( fp , " \t high_flap_threshold \t %f \n " , temp_service - > high_flap_threshold ) ;
fprintf ( fp , " \t flap_detection_enabled \t %d \n " , temp_service - > flap_detection_enabled ) ;
fprintf ( fp , " \t flap_detection_options \t %s \n " , opts2str ( temp_service - > flap_detection_options , service_flag_map , ' o ' ) ) ;
fprintf ( fp , " \t freshness_threshold \t %d \n " , temp_service - > freshness_threshold ) ;
fprintf ( fp , " \t check_freshness \t %d \n " , temp_service - > check_freshness ) ;
fprintf ( fp , " \t notification_options \t %s \n " , opts2str ( temp_service - > notification_options , service_flag_map , ' r ' ) ) ;
fprintf ( fp , " \t notifications_enabled \t %d \n " , temp_service - > notifications_enabled ) ;
fprintf ( fp , " \t notification_interval \t %f \n " , temp_service - > notification_interval ) ;
fprintf ( fp , " \t first_notification_delay \t %f \n " , temp_service - > first_notification_delay ) ;
fprintf ( fp , " \t stalking_options \t %s \n " , opts2str ( temp_service - > stalking_options , service_flag_map , ' o ' ) ) ;
fprintf ( fp , " \t process_perf_data \t %d \n " , temp_service - > process_performance_data ) ;
if ( temp_service - > icon_image )
fprintf ( fp , " \t icon_image \t %s \n " , temp_service - > icon_image ) ;
if ( temp_service - > icon_image_alt )
fprintf ( fp , " \t icon_image_alt \t %s \n " , temp_service - > icon_image_alt ) ;
if ( temp_service - > notes )
fprintf ( fp , " \t notes \t %s \n " , temp_service - > notes ) ;
if ( temp_service - > notes_url )
fprintf ( fp , " \t notes_url \t %s \n " , temp_service - > notes_url ) ;
if ( temp_service - > action_url )
fprintf ( fp , " \t action_url \t %s \n " , temp_service - > action_url ) ;
fprintf ( fp , " \t retain_status_information \t %d \n " , temp_service - > retain_status_information ) ;
fprintf ( fp , " \t retain_nonstatus_information \t %d \n " , temp_service - > retain_nonstatus_information ) ;
/* custom variables */
fcache_customvars ( fp , temp_service - > custom_variables ) ;
fprintf ( fp , " \t } \n \n " ) ;
}
void fcache_servicedependency ( FILE * fp , servicedependency * temp_servicedependency )
{
fprintf ( fp , " define servicedependency { \n " ) ;
fprintf ( fp , " \t host_name \t %s \n " , temp_servicedependency - > host_name ) ;
fprintf ( fp , " \t service_description \t %s \n " , temp_servicedependency - > service_description ) ;
fprintf ( fp , " \t dependent_host_name \t %s \n " , temp_servicedependency - > dependent_host_name ) ;
fprintf ( fp , " \t dependent_service_description \t %s \n " , temp_servicedependency - > dependent_service_description ) ;
if ( temp_servicedependency - > dependency_period )
fprintf ( fp , " \t dependency_period \t %s \n " , temp_servicedependency - > dependency_period ) ;
fprintf ( fp , " \t inherits_parent \t %d \n " , temp_servicedependency - > inherits_parent ) ;
fprintf ( fp , " \t %s_failure_options \t %s \n " ,
temp_servicedependency - > dependency_type = = NOTIFICATION_DEPENDENCY ? " notification " : " execution " ,
opts2str ( temp_servicedependency - > failure_options , service_flag_map , ' o ' ) ) ;
fprintf ( fp , " \t } \n \n " ) ;
}
void fcache_serviceescalation ( FILE * fp , serviceescalation * temp_serviceescalation )
{
fprintf ( fp , " define serviceescalation { \n " ) ;
fprintf ( fp , " \t host_name \t %s \n " , temp_serviceescalation - > host_name ) ;
fprintf ( fp , " \t service_description \t %s \n " , temp_serviceescalation - > description ) ;
fprintf ( fp , " \t first_notification \t %d \n " , temp_serviceescalation - > first_notification ) ;
fprintf ( fp , " \t last_notification \t %d \n " , temp_serviceescalation - > last_notification ) ;
fprintf ( fp , " \t notification_interval \t %f \n " , temp_serviceescalation - > notification_interval ) ;
if ( temp_serviceescalation - > escalation_period )
fprintf ( fp , " \t escalation_period \t %s \n " , temp_serviceescalation - > escalation_period ) ;
fprintf ( fp , " \t escalation_options \t %s \n " , opts2str ( temp_serviceescalation - > escalation_options , service_flag_map , ' r ' ) ) ;
if ( temp_serviceescalation - > contacts ) {
contactsmember * cl ;
fprintf ( fp , " \t contacts \t " ) ;
for ( cl = temp_serviceescalation - > contacts ; cl ; cl = cl - > next )
fprintf ( fp , " %s%c " , cl - > contact_ptr - > name , cl - > next ? ' , ' : ' \n ' ) ;
}
if ( temp_serviceescalation - > contact_groups ) {
contactgroupsmember * cgl ;
fprintf ( fp , " \t contact_groups \t " ) ;
for ( cgl = temp_serviceescalation - > contact_groups ; cgl ; cgl = cgl - > next )
fprintf ( fp , " %s%c " , cgl - > group_name , cgl - > next ? ' , ' : ' \n ' ) ;
}
fprintf ( fp , " \t } \n \n " ) ;
}
void fcache_hostdependency ( FILE * fp , hostdependency * temp_hostdependency )
{
fprintf ( fp , " define hostdependency { \n " ) ;
fprintf ( fp , " \t host_name \t %s \n " , temp_hostdependency - > host_name ) ;
fprintf ( fp , " \t dependent_host_name \t %s \n " , temp_hostdependency - > dependent_host_name ) ;
if ( temp_hostdependency - > dependency_period )
fprintf ( fp , " \t dependency_period \t %s \n " , temp_hostdependency - > dependency_period ) ;
fprintf ( fp , " \t inherits_parent \t %d \n " , temp_hostdependency - > inherits_parent ) ;
fprintf ( fp , " \t %s_failure_options \t %s \n " ,
temp_hostdependency - > dependency_type = = NOTIFICATION_DEPENDENCY ? " notification " : " execution " ,
opts2str ( temp_hostdependency - > failure_options , host_flag_map , ' o ' ) ) ;
fprintf ( fp , " \t } \n \n " ) ;
}
void fcache_hostescalation ( FILE * fp , hostescalation * temp_hostescalation )
{
fprintf ( fp , " define hostescalation { \n " ) ;
fprintf ( fp , " \t host_name \t %s \n " , temp_hostescalation - > host_name ) ;
fprintf ( fp , " \t first_notification \t %d \n " , temp_hostescalation - > first_notification ) ;
fprintf ( fp , " \t last_notification \t %d \n " , temp_hostescalation - > last_notification ) ;
fprintf ( fp , " \t notification_interval \t %f \n " , temp_hostescalation - > notification_interval ) ;
if ( temp_hostescalation - > escalation_period )
fprintf ( fp , " \t escalation_period \t %s \n " , temp_hostescalation - > escalation_period ) ;
fprintf ( fp , " \t escalation_options \t %s \n " , opts2str ( temp_hostescalation - > escalation_options , host_flag_map , ' r ' ) ) ;
fcache_contactlist ( fp , " \t contacts \t " , temp_hostescalation - > contacts ) ;
fcache_contactgrouplist ( fp , " \t contact_groups \t " , temp_hostescalation - > contact_groups ) ;
fprintf ( fp , " \t } \n \n " ) ;
}
/* writes cached object definitions for use by web interface */
int fcache_objects ( char * cache_file ) {
FILE * fp = NULL ;
time_t current_time = 0L ;
unsigned int i ;
/* some people won't want to cache their objects */
if ( ! cache_file | | ! strcmp ( cache_file , " /dev/null " ) )
return OK ;
time ( & current_time ) ;
/* open the cache file for writing */
fp = fopen ( cache_file , " w " ) ;
if ( fp = = NULL ) {
logit ( NSLOG_CONFIG_WARNING , TRUE , " Warning: Could not open object cache file '%s' for writing! \n " , cache_file ) ;
return ERROR ;
}
/* write header to cache file */
fprintf ( fp , " ######################################## \n " ) ;
fprintf ( fp , " # NAGIOS OBJECT CACHE FILE \n " ) ;
fprintf ( fp , " # \n " ) ;
fprintf ( fp , " # THIS FILE IS AUTOMATICALLY GENERATED \n " ) ;
fprintf ( fp , " # BY NAGIOS. DO NOT MODIFY THIS FILE! \n " ) ;
fprintf ( fp , " # \n " ) ;
fprintf ( fp , " # Created: %s " , ctime ( & current_time ) ) ;
fprintf ( fp , " ######################################## \n \n " ) ;
/* cache timeperiods */
for ( i = 0 ; i < num_objects . timeperiods ; i + + )
fcache_timeperiod ( fp , timeperiod_ary [ i ] ) ;
/* cache commands */
for ( i = 0 ; i < num_objects . commands ; i + + )
fcache_command ( fp , command_ary [ i ] ) ;
/* cache contactgroups */
for ( i = 0 ; i < num_objects . contactgroups ; i + + )
fcache_contactgroup ( fp , contactgroup_ary [ i ] ) ;
/* cache hostgroups */
for ( i = 0 ; i < num_objects . hostgroups ; i + + )
fcache_hostgroup ( fp , hostgroup_ary [ i ] ) ;
/* cache servicegroups */
for ( i = 0 ; i < num_objects . servicegroups ; i + + )
fcache_servicegroup ( fp , servicegroup_ary [ i ] ) ;
/* cache contacts */
for ( i = 0 ; i < num_objects . contacts ; i + + )
fcache_contact ( fp , contact_ary [ i ] ) ;
/* cache hosts */
for ( i = 0 ; i < num_objects . hosts ; i + + )
fcache_host ( fp , host_ary [ i ] ) ;
/* cache services */
for ( i = 0 ; i < num_objects . services ; i + + )
fcache_service ( fp , service_ary [ i ] ) ;
/* cache service dependencies */
for ( i = 0 ; i < num_objects . servicedependencies ; i + + )
fcache_servicedependency ( fp , servicedependency_ary [ i ] ) ;
/* cache service escalations */
for ( i = 0 ; i < num_objects . serviceescalations ; i + + )
fcache_serviceescalation ( fp , serviceescalation_ary [ i ] ) ;
/* cache host dependencies */
for ( i = 0 ; i < num_objects . hostdependencies ; i + + )
fcache_hostdependency ( fp , hostdependency_ary [ i ] ) ;
/* cache host escalations */
for ( i = 0 ; i < num_objects . hostescalations ; i + + )
fcache_hostescalation ( fp , hostescalation_ary [ i ] ) ;
fclose ( fp ) ;
return OK ;
}
# endif