From c01a60230960db5609e61286b97474cd75f2298a Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Fri, 25 Mar 2011 16:58:20 +0100 Subject: [PATCH] Renamed PingAnalyzer to HostStatusAnalyzer - this describes better what the class is suppode to do, analyze the status of the host, using the pings. --- src/CMakeLists.txt | 2 +- src/ping/hoststatusanalyzer.cpp | 146 +++++++++++++++++++++++++++++++++++++++ src/ping/hoststatusanalyzer.h | 52 ++++++++++++++ src/ping/pinganalyzer.cpp | 129 ---------------------------------- src/ping/pinganalyzer.h | 52 -------------- src/ping/pingscheduler.h | 4 +- 6 files changed, 201 insertions(+), 184 deletions(-) create mode 100644 src/ping/hoststatusanalyzer.cpp create mode 100644 src/ping/hoststatusanalyzer.h delete mode 100644 src/ping/pinganalyzer.cpp delete mode 100644 src/ping/pinganalyzer.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d63f58f..fa0d756 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -30,7 +30,7 @@ set(SOURCES notify/statusnotifier.cpp notify/statusnotifiercommand.cpp ping/boostpinger.cpp - ping/pinganalyzer.cpp + ping/hoststatusanalyzer.cpp ping/pingscheduler.cpp main.cpp ) diff --git a/src/ping/hoststatusanalyzer.cpp b/src/ping/hoststatusanalyzer.cpp new file mode 100644 index 0000000..47118ea --- /dev/null +++ b/src/ping/hoststatusanalyzer.cpp @@ -0,0 +1,146 @@ +#include "ping/hoststatusanalyzer.h" + +#include + +#include + +#include "notify/statusnotifier.h" + +using namespace std; +using namespace boost; + +//----------------------------------------------------------------------------- +// HostStatusAnalyzer +//----------------------------------------------------------------------------- + +/** + * @param host_address the address of the host it has to analyze. + * @param ping_fail_percentage_limit the percentage threshold of pings that can + * fail. + * @param notifier the object used to notify the status of the host. + */ +HostStatusAnalyzer::HostStatusAnalyzer( + const string &host_address, + const int ping_fail_percentage_limit, + shared_ptr notifier +) : + HostAddress( host_address ), + Notifier( notifier ), + PingFailPercentageLimit( ping_fail_percentage_limit ), + ResolvedIpCount( 0 ), + PingsPerformedCount( 0 ), + PingsFailedCount( 0 ), + ExceededPingFailedLimit( false ) +{ + BOOST_ASSERT( !HostAddress.empty() ); + BOOST_ASSERT( ( 0 <= PingFailPercentageLimit ) && ( PingFailPercentageLimit <= 100 ) ); +} + +HostStatusAnalyzer::~HostStatusAnalyzer() +{ +} + +/** + * @param resolved_ip_count the number of IPs resolved for the host. + */ +void HostStatusAnalyzer::set_resolved_ip_count( const int resolved_ip_count ) +{ + BOOST_ASSERT( 1 <= resolved_ip_count ); + + ResolvedIpCount = resolved_ip_count; +} + +/** + * @return true if the amount of failed pings given to the host exceeded the + * limit. + */ +bool HostStatusAnalyzer::exceeded_ping_failed_limit() const +{ + return ExceededPingFailedLimit; +} + +/** + * Adds a ping status (success or failure). + * @param ping_success + */ +void HostStatusAnalyzer::update_ping_statistics( bool ping_success ) +{ + BOOST_ASSERT( 1 <= ResolvedIpCount ); + BOOST_ASSERT( 0 <= PingsPerformedCount ); + BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount ); + + increase_ping_performed_count(); + + if ( !ping_success ) + { + increase_ping_failed_count(); + } + + // after we tried all IPs resolved for this host, we can analyze how many + // failed + if ( tried_all_resolved_ip() ) + { + analyze_ping_statistics(); + + reset_ping_counters(); + } + + BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount ); +} + +bool HostStatusAnalyzer::tried_all_resolved_ip() const +{ + BOOST_ASSERT( ( 0 <= PingsPerformedCount ) && ( PingsPerformedCount <= ResolvedIpCount ) ); + + return ( PingsPerformedCount == ResolvedIpCount ); +} + +void HostStatusAnalyzer::analyze_ping_statistics() +{ + BOOST_ASSERT( !HostAddress.empty() ); + BOOST_ASSERT( PingsPerformedCount == ResolvedIpCount ); + + // notify if the amount of pings that failed exceed the limit + if ( exceeded_ping_failed_limit() ) + { + Notifier->notify_host_down( HostAddress ); + } + else + { + Notifier->notify_host_up( HostAddress ); + } + +} + +void HostStatusAnalyzer::reset_ping_counters() +{ + PingsPerformedCount = 0; + PingsFailedCount = 0; +} + +void HostStatusAnalyzer::increase_ping_performed_count() +{ + ++PingsPerformedCount; + + BOOST_ASSERT( ( 0 <= PingsPerformedCount ) && ( PingsPerformedCount <= ResolvedIpCount ) ); +} + +void HostStatusAnalyzer::increase_ping_failed_count() +{ + ++PingsFailedCount; + + analyze_ping_failed_count(); + + BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) ); +} + +void HostStatusAnalyzer::analyze_ping_failed_count() +{ + BOOST_ASSERT( ( 0 <= PingFailPercentageLimit ) && ( PingFailPercentageLimit <= 100 ) ); + BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) ); + + int ping_fail_absolute_limit = PingFailPercentageLimit / 100; // TODO possible precision loss, check with care + + // keep a boolean variable because the PingsFailedCount can be reseted + ExceededPingFailedLimit = ( PingsFailedCount > ping_fail_absolute_limit ); +} diff --git a/src/ping/hoststatusanalyzer.h b/src/ping/hoststatusanalyzer.h new file mode 100644 index 0000000..c46cea6 --- /dev/null +++ b/src/ping/hoststatusanalyzer.h @@ -0,0 +1,52 @@ +#ifndef HOSTSTATUSANALYZER_H +#define HOSTSTATUSANALYZER_H + +#include + +#include + +class StatusNotifier; + +//----------------------------------------------------------------------------- +// HostStatusAnalyzer +//----------------------------------------------------------------------------- + +/** + * @brief This class analyze the host status (up or down) based on the pings + * responses (success or failure). And notifies its status. + * Scope: one object per host. + */ +class HostStatusAnalyzer +{ +public: + HostStatusAnalyzer( + const std::string &host_address, + const int ping_fail_percentage_limit, + boost::shared_ptr notifier + ); + virtual ~HostStatusAnalyzer(); + + void set_resolved_ip_count( const int resolved_ip_count ); + bool exceeded_ping_failed_limit() const; + void update_ping_statistics( bool ping_success ); + +private: + bool tried_all_resolved_ip() const; + void analyze_ping_statistics(); + void reset_ping_counters(); + void increase_ping_performed_count(); + void increase_ping_failed_count(); + void analyze_ping_failed_count(); + +private: + std::string HostAddress; + boost::shared_ptr Notifier; + int PingFailPercentageLimit; + int ResolvedIpCount; + int PingsPerformedCount; + int PingsFailedCount; + bool ExceededPingFailedLimit; + +}; + +#endif /* HOSTSTATUSANALYZER_H */ diff --git a/src/ping/pinganalyzer.cpp b/src/ping/pinganalyzer.cpp deleted file mode 100644 index b72d4c1..0000000 --- a/src/ping/pinganalyzer.cpp +++ /dev/null @@ -1,129 +0,0 @@ -#include "ping/pinganalyzer.h" - -#include - -#include - -#include "notify/statusnotifier.h" - -using namespace std; -using namespace boost; - -//----------------------------------------------------------------------------- -// PingAnalyzer -//----------------------------------------------------------------------------- - -PingAnalyzer::PingAnalyzer( - const string &host_address, - const int ping_fail_percentage_limit, - shared_ptr notifier -) : - HostAddress( host_address ), - Notifier( notifier ), - PingFailPercentageLimit( ping_fail_percentage_limit ), - ResolvedIpCount( 0 ), - PingsPerformedCount( 0 ), - PingsFailedCount( 0 ), - ExceededPingFailedLimit( false ) -{ - BOOST_ASSERT( !HostAddress.empty() ); - BOOST_ASSERT( ( 0 <= PingFailPercentageLimit ) && ( PingFailPercentageLimit <= 100 ) ); -} - -PingAnalyzer::~PingAnalyzer() -{ -} - -void PingAnalyzer::set_resolved_ip_count( const int resolved_ip_count ) -{ - BOOST_ASSERT( 1 <= resolved_ip_count ); - - ResolvedIpCount = resolved_ip_count; -} - -bool PingAnalyzer::exceeded_ping_failed_limit() const -{ - return ExceededPingFailedLimit; -} - -void PingAnalyzer::update_ping_statistics( bool ping_success ) -{ - BOOST_ASSERT( 1 <= ResolvedIpCount ); - BOOST_ASSERT( 0 <= PingsPerformedCount ); - BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount ); - - increase_ping_performed_count(); - - if ( !ping_success ) - { - increase_ping_failed_count(); - } - - // after we tried all IPs resolved for this host, we can analyze how many - // failed - if ( tried_all_resolved_ip() ) - { - analyze_ping_statistics(); - - reset_ping_counters(); - } - - BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount ); -} - -bool PingAnalyzer::tried_all_resolved_ip() const -{ - BOOST_ASSERT( ( 0 <= PingsPerformedCount ) && ( PingsPerformedCount <= ResolvedIpCount ) ); - - return ( PingsPerformedCount == ResolvedIpCount ); -} - -void PingAnalyzer::analyze_ping_statistics() -{ - BOOST_ASSERT( !HostAddress.empty() ); - BOOST_ASSERT( PingsPerformedCount == ResolvedIpCount ); - - // notify if the amount of pings that failed exceed the limit - if ( exceeded_ping_failed_limit() ) - { - Notifier->notify_host_down( HostAddress ); - } - else - { - Notifier->notify_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; - - analyze_ping_failed_count(); - - BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) ); -} - -void PingAnalyzer::analyze_ping_failed_count() -{ - BOOST_ASSERT( ( 0 <= PingFailPercentageLimit ) && ( PingFailPercentageLimit <= 100 ) ); - BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) ); - - int ping_fail_absolute_limit = PingFailPercentageLimit / 100; // TODO possible precision loss, check with care - - // keep a boolean variable because the PingsFailedCount can be reseted - ExceededPingFailedLimit = ( PingsFailedCount > ping_fail_absolute_limit ); -} diff --git a/src/ping/pinganalyzer.h b/src/ping/pinganalyzer.h deleted file mode 100644 index 6f89620..0000000 --- a/src/ping/pinganalyzer.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef PINGANALYZER_H -#define PINGANALYZER_H - -#include - -#include - -class StatusNotifier; - -//----------------------------------------------------------------------------- -// PingAnalyzer -//----------------------------------------------------------------------------- - -/** - * @brief This class analyze the ping status (success or failure) and decides - * what to do. - * Scope: one object per host. - */ -class PingAnalyzer -{ -public: - PingAnalyzer( - const std::string &host_address, - const int ping_fail_percentage_limit, - boost::shared_ptr notifier - ); - virtual ~PingAnalyzer(); - - void set_resolved_ip_count( const int resolved_ip_count ); - bool exceeded_ping_failed_limit() const; - void update_ping_statistics( bool ping_success ); - -private: - bool tried_all_resolved_ip() const; - void analyze_ping_statistics(); - void reset_ping_counters(); - void increase_ping_performed_count(); - void increase_ping_failed_count(); - void analyze_ping_failed_count(); - -private: - std::string HostAddress; - boost::shared_ptr Notifier; - int PingFailPercentageLimit; - int ResolvedIpCount; - int PingsPerformedCount; - int PingsFailedCount; - bool ExceededPingFailedLimit; - -}; - -#endif /* PINGANALYZER_H */ diff --git a/src/ping/pingscheduler.h b/src/ping/pingscheduler.h index a5b6683..bc0caed 100644 --- a/src/ping/pingscheduler.h +++ b/src/ping/pingscheduler.h @@ -7,7 +7,7 @@ #include #include "dns/dnsresolver.h" -#include "ping/pinganalyzer.h" +#include "ping/hoststatusanalyzer.h" #include "ping/pinginterval.h" class StatusNotifier; @@ -51,7 +51,7 @@ private: boost::posix_time::ptime TimeSentLastPing; PingInterval PingIntervalInSec; DnsResolver IpList; - PingAnalyzer Analyzer; + HostStatusAnalyzer Analyzer; }; -- 1.7.1