From: Guilherme Maciel Ferreira Date: Tue, 22 Mar 2011 09:31:58 +0000 (+0100) Subject: Changed PingStatusNotifier interface, it is now an object shared among N hosts X-Git-Tag: v1.0~128 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=c1fff16a92499e4e786da880af4fcaac717d706b;p=pingcheck Changed PingStatusNotifier interface, it is now an object shared among N hosts - this shared property allows to know how many hosts are down, once we can keep track of all hosts - only one PingStatusNotifier instance for all PingAnalyzers objects, they pass the host to identify themselves within the notifier object --- diff --git a/src/main.cpp b/src/main.cpp index 1608ffd..3e128ec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,10 +2,12 @@ #include #include +#include #include "config/configurationreader.h" #include "config/host.h" #include "ping/pingscheduler.h" +#include "ping/pingstatusnotifier.h" using namespace std; using namespace boost; @@ -24,6 +26,13 @@ int main( int argc, char* argv[] ) int limit_ping_fail = config.get_limit_ping_fail(); + // TODO init_notifier and get_notifier + int limit_hosts_down = config.get_limit_hosts_down(); + string status_notifier_cmd = config.get_status_notifier_cmd(); + shared_ptr notifier( + new PingStatusNotifier( limit_hosts_down, status_notifier_cmd ) + ); + // TODO init_pingers() vector< HostItem > hosts = config.get_hosts(); vector< PingSchedulerItem > scheduler_list; @@ -34,7 +43,7 @@ int main( int argc, char* argv[] ) PingSchedulerItem scheduler( new PingScheduler( io_service, ping_address, ping_interval, - limit_ping_fail + limit_ping_fail, notifier ) ); scheduler_list.push_back( scheduler ); diff --git a/src/ping/pinganalyzer.cpp b/src/ping/pinganalyzer.cpp index b7f4acd..b51f40e 100644 --- a/src/ping/pinganalyzer.cpp +++ b/src/ping/pinganalyzer.cpp @@ -4,6 +4,8 @@ #include +#include "ping/pingstatusnotifier.h" + using namespace std; using namespace boost; @@ -13,9 +15,11 @@ using namespace boost; PingAnalyzer::PingAnalyzer( const string &host_address, - const int limit_ping_fail_percentage + const int limit_ping_fail_percentage, + shared_ptr notifier ) : - Notifier( host_address ), + HostAddress( host_address ), + Notifier( notifier ), LimitPingFailPercentage( limit_ping_fail_percentage ), ResolvedIpCount( 0 ), PingsPerformedCount( 0 ), @@ -64,36 +68,48 @@ void PingAnalyzer::update_ping_statistics( bool ping_success ) void PingAnalyzer::analyze() { BOOST_ASSERT( PingsPerformedCount == ResolvedIpCount ); - BOOST_ASSERT( ( 0 <= LimitPingFailPercentage ) && ( LimitPingFailPercentage <= 100 ) ); - BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) ); + BOOST_ASSERT( !HostAddress.empty() ); - int limit_ping_fail_absolute = LimitPingFailPercentage / 100; // TODO possible precision loss, check with care - - if ( PingsFailedCount > limit_ping_fail_absolute ) + // notify if the amount of pings that failed exceed the limit + if ( exceeded_ping_failed_count_limit() ) { - Notifier.alert_host_down(); + Notifier->alert_host_down( HostAddress ); } else { - Notifier.alert_host_up(); + Notifier->alert_host_up( HostAddress ); } } +void PingAnalyzer::reset_ping_counters() +{ + PingsPerformedCount = 0; + PingsFailedCount = 0; +} + void PingAnalyzer::increase_ping_performed_count() { ++PingsPerformedCount; + + BOOST_ASSERT( ( 0 <= PingsPerformedCount ) && ( PingsPerformedCount <= ResolvedIpCount ) ); } void PingAnalyzer::increase_ping_failed_count() { ++PingsFailedCount; + + BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) ); } -void PingAnalyzer::reset_ping_counters() +bool PingAnalyzer::exceeded_ping_failed_count_limit() { - PingsPerformedCount = 0; - PingsFailedCount = 0; + BOOST_ASSERT( ( 0 <= LimitPingFailPercentage ) && ( LimitPingFailPercentage <= 100 ) ); + BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) ); + + int limit_ping_fail_absolute = LimitPingFailPercentage / 100; // TODO possible precision loss, check with care + + return ( PingsFailedCount > limit_ping_fail_absolute ); } bool PingAnalyzer::tried_all_resolved_ip() const diff --git a/src/ping/pinganalyzer.h b/src/ping/pinganalyzer.h index e7033cc..6a97f56 100644 --- a/src/ping/pinganalyzer.h +++ b/src/ping/pinganalyzer.h @@ -5,7 +5,7 @@ #include -#include "ping/pingstatusnotifier.h" +class PingStatusNotifier; //----------------------------------------------------------------------------- // PingAnalyzer @@ -21,7 +21,8 @@ class PingAnalyzer public: PingAnalyzer( const std::string &host_address, - const int ping_fail_threshold_percentage + const int ping_fail_threshold_percentage, + boost::shared_ptr notifier ); virtual ~PingAnalyzer(); @@ -30,13 +31,15 @@ public: private: void analyze(); + void reset_ping_counters(); void increase_ping_performed_count(); void increase_ping_failed_count(); - void reset_ping_counters(); + bool exceeded_ping_failed_count_limit(); bool tried_all_resolved_ip() const; private: - PingStatusNotifier Notifier; + std::string HostAddress; + boost::shared_ptr Notifier; int LimitPingFailPercentage; int ResolvedIpCount; int PingsPerformedCount; diff --git a/src/ping/pingscheduler.cpp b/src/ping/pingscheduler.cpp index 38a7ee3..34ee5dd 100644 --- a/src/ping/pingscheduler.cpp +++ b/src/ping/pingscheduler.cpp @@ -6,6 +6,7 @@ #include "dns/dnsresolver.h" #include "ping/boostpinger.h" +#include "ping/pingstatusnotifier.h" using namespace std; using namespace boost; @@ -20,7 +21,8 @@ PingScheduler::PingScheduler( boost::asio::io_service &io_service, const string &ping_address, const int ping_interval_in_sec, - const int limit_ping_fail_percentage + const int limit_ping_fail_percentage, + shared_ptr notifier ) : IoService( io_service ), @@ -28,7 +30,7 @@ PingScheduler::PingScheduler( TimeSentLastPing( microsec_clock::universal_time() ), PingIntervalInSec( ping_interval_in_sec ), IpList( ping_address ), - Analyzer( ping_address, limit_ping_fail_percentage ) + Analyzer( ping_address, limit_ping_fail_percentage, notifier ) { } diff --git a/src/ping/pingscheduler.h b/src/ping/pingscheduler.h index d9b9d5a..01e1b3a 100644 --- a/src/ping/pingscheduler.h +++ b/src/ping/pingscheduler.h @@ -9,6 +9,8 @@ #include "dns/dnsresolver.h" #include "ping/pinganalyzer.h" +class PingStatusNotifier; + //----------------------------------------------------------------------------- // PingScheduler //----------------------------------------------------------------------------- @@ -25,7 +27,8 @@ public: boost::asio::io_service &io_service, const std::string &ping_address, const int ping_interval_in_sec, - const int limit_ping_fail_percentage + const int limit_ping_fail_percentage, + boost::shared_ptr notifier ); virtual ~PingScheduler(); diff --git a/src/ping/pingstatusnotifier.cpp b/src/ping/pingstatusnotifier.cpp index c21f3a3..e5da632 100644 --- a/src/ping/pingstatusnotifier.cpp +++ b/src/ping/pingstatusnotifier.cpp @@ -2,14 +2,20 @@ #include +#include + using namespace std; //----------------------------------------------------------------------------- // PingStatusNotifier //----------------------------------------------------------------------------- -PingStatusNotifier::PingStatusNotifier( const string &host_address ) : - HostAddress( host_address ) +PingStatusNotifier::PingStatusNotifier( + const int limit_hosts_down, + const std::string &status_notifier_cmd +) : + LimitHostsDown( limit_hosts_down ), + StatusNotifierCmd( status_notifier_cmd ) { } @@ -17,14 +23,39 @@ PingStatusNotifier::~PingStatusNotifier() { } -void PingStatusNotifier::alert_host_up() const +void PingStatusNotifier::alert_host_up( const string &host_address ) const { - // TODO call script - cerr << "- " << HostAddress << " - LINK IS UP /\\ " << endl; + BOOST_ASSERT( !host_address.empty() ); + + // TODO call StatusNotifierCmd + cerr << "- " << host_address << " - LINK IS UP /\\ " << endl; + } -void PingStatusNotifier::alert_host_down() const +void PingStatusNotifier::alert_host_down( const string &host_address ) const { - // TODO call script - cerr << "- " << HostAddress << " - LINK IS DOWN \\/" << endl; + BOOST_ASSERT( !host_address.empty() ); + + // TODO call StatusNotifierCmd + cerr << "- " << host_address << " - LINK IS DOWN \\/" << endl; + + /* + + Knows how many hosts we have + + 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 + + Must have a central system we can alert this lint is down, this central system + knows the number of hosts. + + After N systems have alert this central one, if this N is greater than + limit-hosts-down, then we call the script + + Must pass which hosts are down + + */ } diff --git a/src/ping/pingstatusnotifier.h b/src/ping/pingstatusnotifier.h index 5fa7d9e..fe0d52f 100644 --- a/src/ping/pingstatusnotifier.h +++ b/src/ping/pingstatusnotifier.h @@ -7,17 +7,25 @@ // PingStatusNotifier //----------------------------------------------------------------------------- +/** + * @brief This class is responsible for notify when a host is down or up. + * Scope: one object for many hosts. + */ class PingStatusNotifier { public: - PingStatusNotifier( const std::string &host_address ); + PingStatusNotifier( + const int limit_hosts_down, + const std::string &status_notifier_cmd + ); virtual ~PingStatusNotifier(); - void alert_host_up() const; - void alert_host_down() const; + void alert_host_up( const std::string &host_address ) const; + void alert_host_down( const std::string &host_address ) const; private: - const std::string HostAddress; + int LimitHostsDown; + std::string StatusNotifierCmd; };