From d52d036ac891029fa5614a14d0d66a666e12efd5 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Tue, 22 Mar 2011 09:49:21 +0100 Subject: [PATCH] Added status-notifier-cmd options to indicate which command to call when the system is down/up. --- Readme | 5 ++++- conf/pingcheck.conf | 3 ++- src/config/configuration.cpp | 13 +++++++++++++ src/config/configuration.h | 4 ++++ src/config/configurationreader.cpp | 18 +++++++++++++++++- src/config/configurationreader.h | 2 ++ 6 files changed, 42 insertions(+), 3 deletions(-) diff --git a/Readme b/Readme index c197b94..b14940a 100644 --- a/Readme +++ b/Readme @@ -173,13 +173,16 @@ This configurations are shared among and affect all the hosts. - limit-ping-fail: percentage of pings to a host that can fail. If the percentage of failed pings to a host exceed this number, then the host is considered down. +- status-notifier-cmd: the command line that is called when a host is down, or + up. Accepted variables are ${host}, ${status} 4.2. Host --------------------------------------- -- address: the DNS or IP of the host to ping. Take in consideration that, if a +- name: the DNS or IP of the host to ping. Take in consideration that, if a DNS is given, the application pings all IPs in the look up table, however, if IP is provide, it is the only which will be pinged. +- interval: the host will be pinged every "interval" seconds. diff --git a/conf/pingcheck.conf b/conf/pingcheck.conf index 799a7c8..e018d55 100644 --- a/conf/pingcheck.conf +++ b/conf/pingcheck.conf @@ -1,5 +1,6 @@ -limit-hosts-down=5 +limit-hosts-down=4 limit-ping-fail=40 +status-notifier-cmd=echo ${host} is ${status} [host] name=www.intra2net.com diff --git a/src/config/configuration.cpp b/src/config/configuration.cpp index 468993f..006ab51 100644 --- a/src/config/configuration.cpp +++ b/src/config/configuration.cpp @@ -16,6 +16,7 @@ Configuration::Configuration() : LimitPingFail( 0 ), MinLimitPingFail( 0 ), MaxLimitPingFail( 100 ), + StatusNotifierCmd( "" ), Hosts() { } @@ -58,6 +59,18 @@ void Configuration::set_limit_ping_fail( const int limit_ping_fail ) LimitPingFail = limit_ping_fail; } +string Configuration::get_status_notifier_cmd() const +{ + return StatusNotifierCmd; +} + +void Configuration::set_status_notifier_cmd( const string &status_notifier_cmd ) +{ + BOOST_ASSERT( !status_notifier_cmd.empty() ); + + StatusNotifierCmd = status_notifier_cmd; +} + vector< HostItem > Configuration::get_hosts() const { return Hosts; diff --git a/src/config/configuration.h b/src/config/configuration.h index fa8d0a7..b06093a 100644 --- a/src/config/configuration.h +++ b/src/config/configuration.h @@ -27,6 +27,9 @@ public: int get_limit_ping_fail() const; void set_limit_ping_fail( const int limit_ping_fail ); + std::string get_status_notifier_cmd() const; + void set_status_notifier_cmd( const std::string &status_notifier_cmd ); + std::vector get_hosts() const; void set_hosts( const std::vector &hosts_list ); @@ -38,6 +41,7 @@ private: int LimitPingFail; const int MinLimitPingFail; const int MaxLimitPingFail; + std::string StatusNotifierCmd; std::vector Hosts; }; diff --git a/src/config/configurationreader.cpp b/src/config/configurationreader.cpp index 06c2d0f..7a63808 100644 --- a/src/config/configurationreader.cpp +++ b/src/config/configurationreader.cpp @@ -29,6 +29,8 @@ ConfigurationReader::ConfigurationReader() : DefaultLimitPingFail( 50 ), LimitPingFailCmdStr( "limit-ping-fail" ), LimitPingFailCmdDesc( "Maximum percentage of pings that can fail for a given host." ), + StatusNotifierCmdCmdStr( "status-notifier-cmd" ), + StatusNotifierCmdCmdDesc( "The command to execute to alert about host status." ), HostNameCmdStr( "host.name" ), HostNameCmdDesc( "Host address" ), DefaultHostInterval( 1 ), @@ -114,6 +116,7 @@ options_description ConfigurationReader::get_configuration_options() const options.add_options() ( LimitHostsDownCmdStr.c_str(), value()->default_value( DefaultLimitHostsDown ), LimitHostsDownCmdDesc.c_str() ) ( LimitPingFailCmdStr.c_str(), value()->default_value( DefaultLimitPingFail ), LimitPingFailCmdDesc.c_str() ) + ( StatusNotifierCmdCmdStr.c_str(), value(), StatusNotifierCmdCmdDesc.c_str() ) ( HostNameCmdStr.c_str(), value< vector >(), HostNameCmdDesc.c_str() ) ( HostIntervalCmdStr.c_str(), value< vector >(), HostIntervalCmdDesc.c_str() ) ; @@ -133,9 +136,10 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm ) } // limit-hosts-down + int limit_host_down = 0; if ( vm.count( LimitHostsDownCmdStr ) ) { - int limit_host_down = vm[ LimitHostsDownCmdStr ].as (); + limit_host_down = vm[ LimitHostsDownCmdStr ].as (); Config.set_limit_hosts_down( limit_host_down ); cout << LimitHostsDownCmdStr << "=" << limit_host_down << endl; @@ -150,6 +154,15 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm ) cout << LimitPingFailCmdStr << "=" << limit_ping_fail << endl; } + // status-notifier-cmd + if ( vm.count( StatusNotifierCmdCmdStr ) ) + { + string status_notifier_cmd = vm[ StatusNotifierCmdCmdStr ].as(); + Config.set_status_notifier_cmd( status_notifier_cmd ); + + cout << StatusNotifierCmdCmdStr << "=" << status_notifier_cmd << endl; + } + // [host] name int hosts_names_total = 0; if ( vm.count( HostNameCmdStr ) ) @@ -168,6 +181,8 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm ) Config.set_hosts( hosts_list ); hosts_names_total = hosts_names.size(); + + BOOST_ASSERT( hosts_names_total >= limit_host_down ); } // [host] interval @@ -243,6 +258,7 @@ bool ConfigurationReader::process_command_line( bool ConfigurationReader::process_configuration_file( variables_map &vm ) { string config_file_name = ""; + // parse the command line options to retrieve the config file name if ( parse_configuration_options( vm ) ) config_file_name = Config.get_config_file_name(); diff --git a/src/config/configurationreader.h b/src/config/configurationreader.h index f586892..75fb163 100644 --- a/src/config/configurationreader.h +++ b/src/config/configurationreader.h @@ -67,6 +67,8 @@ private: const int DefaultLimitPingFail; const std::string LimitPingFailCmdStr; const std::string LimitPingFailCmdDesc; + const std::string StatusNotifierCmdCmdStr; + const std::string StatusNotifierCmdCmdDesc; const std::string HostNameCmdStr; const std::string HostNameCmdDesc; const int DefaultHostInterval; -- 1.7.1