From: Guilherme Maciel Ferreira Date: Wed, 23 Mar 2011 10:37:30 +0000 (+0100) Subject: Added intelligence to know how many hosts are down X-Git-Tag: v1.0~122 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=1266407a9b4137797586de57772cbaa80618524b;p=pingcheck Added intelligence to know how many hosts are down --- diff --git a/src/ping/pingstatusnotifier.cpp b/src/ping/pingstatusnotifier.cpp index e5da632..02da7e7 100644 --- a/src/ping/pingstatusnotifier.cpp +++ b/src/ping/pingstatusnotifier.cpp @@ -15,47 +15,69 @@ PingStatusNotifier::PingStatusNotifier( const std::string &status_notifier_cmd ) : LimitHostsDown( limit_hosts_down ), - StatusNotifierCmd( status_notifier_cmd ) + StatusNotifierCmd( status_notifier_cmd ), + HostsDownList() { + BOOST_ASSERT( 0 <= limit_hosts_down ); + BOOST_ASSERT( !status_notifier_cmd.empty() ); } PingStatusNotifier::~PingStatusNotifier() { } -void PingStatusNotifier::alert_host_up( const string &host_address ) const +void PingStatusNotifier::alert_host_up( const string &host_address ) { BOOST_ASSERT( !host_address.empty() ); - // TODO call StatusNotifierCmd - cerr << "- " << host_address << " - LINK IS UP /\\ " << endl; + cerr << "- " << host_address << " - LINK IS UP /\\ " << endl; // TODO + add_host_up( host_address ); + + if ( !exceeded_host_down_count_limit() ) + { + // alert the system is up + cerr << "SYSTEM IS UP /\\" << endl; + // execute the script + } + + BOOST_ASSERT( HostsDownList.count( host_address ) == 0 ); } -void PingStatusNotifier::alert_host_down( const string &host_address ) const +void PingStatusNotifier::alert_host_down( const string &host_address ) { BOOST_ASSERT( !host_address.empty() ); - // TODO call StatusNotifierCmd - cerr << "- " << host_address << " - LINK IS DOWN \\/" << endl; - - /* + cerr << "- " << host_address << " - LINK IS DOWN \\/" << endl; // TODO - Knows how many hosts we have + add_host_down( host_address ); - create a set to hold the hosts, two sets, one with the hosts down, other - with hosts up - if the size of the list with hosts down exceed the limit-host-down, alert - the external script - make sure the same host cannot be in both lists + if ( exceeded_host_down_count_limit() ) + { + // alert the system is down. + cerr << "****** DANGER WILL ROBINSON! THE SYSTEM IS DOWN \\/ *******" + << endl; // TODO + // execute the script + } - Must have a central system we can alert this lint is down, this central system - knows the number of hosts. + BOOST_ASSERT( HostsDownList.count( host_address ) == 1 ); +} - After N systems have alert this central one, if this N is greater than - limit-hosts-down, then we call the script +void PingStatusNotifier::add_host_up( const string &host_address ) +{ + if ( HostsDownList.count( host_address ) > 0 ) + { + HostsDownList.erase( host_address ); + } +} - Must pass which hosts are down +void PingStatusNotifier::add_host_down( const string &host_address ) +{ + HostsDownList.insert( host_address ); +} - */ +bool PingStatusNotifier::exceeded_host_down_count_limit() const +{ + int host_down_count = HostsDownList.size(); + return ( host_down_count >= LimitHostsDown ); } diff --git a/src/ping/pingstatusnotifier.h b/src/ping/pingstatusnotifier.h index fe0d52f..59dda2c 100644 --- a/src/ping/pingstatusnotifier.h +++ b/src/ping/pingstatusnotifier.h @@ -1,6 +1,7 @@ #ifndef PINGSTATUSNOTIFIER_H #define PINGSTATUSNOTIFIER_H +#include #include //----------------------------------------------------------------------------- @@ -8,7 +9,8 @@ //----------------------------------------------------------------------------- /** - * @brief This class is responsible for notify when a host is down or up. + * @brief This class is responsible for notify when a host is down or up. And + * to keep track of which hosts are down. * Scope: one object for many hosts. */ class PingStatusNotifier @@ -20,12 +22,18 @@ public: ); virtual ~PingStatusNotifier(); - void alert_host_up( const std::string &host_address ) const; - void alert_host_down( const std::string &host_address ) const; + void alert_host_up( const std::string &host_address ); + void alert_host_down( const std::string &host_address ); + +private: + void add_host_up( const std::string &host_address ); + void add_host_down( const std::string &host_address ); + bool exceeded_host_down_count_limit() const; private: int LimitHostsDown; std::string StatusNotifierCmd; + std::set HostsDownList; };