From fb469ffa0be4c250100a0c2ece78782c4c5453c9 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Fri, 25 Mar 2011 17:45:06 +0100 Subject: [PATCH] Renamed StatusNotifier to LinkStatusAnalyzer - the main purpose of this class is to decide whether the link is down or up, by keeping track the amount of hosts down. - renamed the variable Analyzer to HostAnalyzer to avoid confusion with the LinkAnalyzer --- src/CMakeLists.txt | 2 +- src/main.cpp | 8 ++-- src/notify/linkstatusanalyzer.cpp | 100 +++++++++++++++++++++++++++++++++++++ src/notify/linkstatusanalyzer.h | 46 +++++++++++++++++ src/notify/statusnotifier.cpp | 100 ------------------------------------- src/notify/statusnotifier.h | 46 ----------------- src/ping/hoststatusanalyzer.cpp | 12 ++--- src/ping/hoststatusanalyzer.h | 6 +- src/ping/pingscheduler.cpp | 12 ++-- src/ping/pingscheduler.h | 7 +-- 10 files changed, 168 insertions(+), 171 deletions(-) create mode 100644 src/notify/linkstatusanalyzer.cpp create mode 100644 src/notify/linkstatusanalyzer.h delete mode 100644 src/notify/statusnotifier.cpp delete mode 100644 src/notify/statusnotifier.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fa0d756..d7f1891 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -27,7 +27,7 @@ set(SOURCES icmp/icmpheader.cpp icmp/icmppacket.cpp icmp/ipv4header.cpp - notify/statusnotifier.cpp + notify/linkstatusanalyzer.cpp notify/statusnotifiercommand.cpp ping/boostpinger.cpp ping/hoststatusanalyzer.cpp diff --git a/src/main.cpp b/src/main.cpp index 6ffff97..e9de4bc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ #include "config/configurationreader.h" #include "config/host.h" -#include "notify/statusnotifier.h" +#include "notify/linkstatusanalyzer.h" #include "ping/pingscheduler.h" using namespace std; @@ -29,8 +29,8 @@ int main( int argc, char* argv[] ) // TODO init_notifier and get_notifier int hosts_down_limit = config.get_hosts_down_limit(); string status_notifier_cmd = config.get_status_notifier_cmd(); - shared_ptr notifier( - new StatusNotifier( hosts_down_limit, status_notifier_cmd ) + shared_ptr link_analyzer( + new LinkStatusAnalyzer( hosts_down_limit, status_notifier_cmd ) ); // TODO init_pingers() @@ -43,7 +43,7 @@ int main( int argc, char* argv[] ) PingSchedulerItem scheduler( new PingScheduler( io_service, ping_address, ping_interval, - ping_fail_limit, notifier + ping_fail_limit, link_analyzer ) ); scheduler_list.push_back( scheduler ); diff --git a/src/notify/linkstatusanalyzer.cpp b/src/notify/linkstatusanalyzer.cpp new file mode 100644 index 0000000..93f9023 --- /dev/null +++ b/src/notify/linkstatusanalyzer.cpp @@ -0,0 +1,100 @@ +#include "notify/linkstatusanalyzer.h" + +#include + +#include + +using namespace std; + +//----------------------------------------------------------------------------- +// LinkStatusAnalyzer +//----------------------------------------------------------------------------- + +LinkStatusAnalyzer::LinkStatusAnalyzer( + const int hosts_down_limit, + const string &status_notifier_cmd +) : + HostsDownLimit( hosts_down_limit ), + StatusNotifierCmd( status_notifier_cmd ), + HostsDownList() +{ + BOOST_ASSERT( 0 <= hosts_down_limit ); + BOOST_ASSERT( !status_notifier_cmd.empty() ); +} + +LinkStatusAnalyzer::~LinkStatusAnalyzer() +{ +} + +void LinkStatusAnalyzer::notify_host_up( const string &host_address ) +{ + BOOST_ASSERT( !host_address.empty() ); + + cout << "- Host up: " << host_address << endl; // TODO + + add_host_up( host_address ); + + if ( !exceeded_host_down_limit() ) + { + notify_link_up(); + } + + // removed from the list? + BOOST_ASSERT( HostsDownList.count( host_address ) == 0 ); +} + +void LinkStatusAnalyzer::notify_host_down( const string &host_address ) +{ + BOOST_ASSERT( !host_address.empty() ); + + cout << "- Host down: " << host_address << endl; // TODO + + add_host_down( host_address ); + + if ( exceeded_host_down_limit() ) + { + notify_link_down(); + } + + // inserted in the list? + BOOST_ASSERT( HostsDownList.count( host_address ) == 1 ); +} + +void LinkStatusAnalyzer::add_host_up( const string &host_address ) +{ + if ( HostsDownList.count( host_address ) > 0 ) + { + HostsDownList.erase( host_address ); + } +} + +void LinkStatusAnalyzer::add_host_down( const string &host_address ) +{ + HostsDownList.insert( host_address ); +} + +bool LinkStatusAnalyzer::exceeded_host_down_limit() const +{ + int host_down_count = HostsDownList.size(); + return ( host_down_count > HostsDownLimit ); +} + +void LinkStatusAnalyzer::notify_link_up() +{ + StatusNotifierCmd.set_token_value( + StatusNotifierCommand::StatusToken, + "up" + ); + + StatusNotifierCmd.execute(); +} + +void LinkStatusAnalyzer::notify_link_down() +{ + StatusNotifierCmd.set_token_value( + StatusNotifierCommand::StatusToken, + "down" + ); + + StatusNotifierCmd.execute(); +} diff --git a/src/notify/linkstatusanalyzer.h b/src/notify/linkstatusanalyzer.h new file mode 100644 index 0000000..dddc81a --- /dev/null +++ b/src/notify/linkstatusanalyzer.h @@ -0,0 +1,46 @@ +#ifndef LINKSTATUSANALYZER_H +#define LINKSTATUSANALYZER_H + +#include +#include + +#include "notify/statusnotifiercommand.h" + +//----------------------------------------------------------------------------- +// LinkStatusAnalyzer +//----------------------------------------------------------------------------- + +/** + * @brief This class analyzes and notifies the link status, through keeping + * track of the amount of hosts down. + * Scope: one object for many hosts. + */ +class LinkStatusAnalyzer +{ +public: + LinkStatusAnalyzer( + const int hosts_down_limit, + const std::string &status_notifier_cmd + ); + virtual ~LinkStatusAnalyzer(); + + void notify_host_up( const std::string &host_address ); + void notify_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_limit() const; + + void notify_link_up(); + void notify_link_down(); + +private: + const int HostsDownLimit; + StatusNotifierCommand StatusNotifierCmd; + std::set HostsDownList; + +}; + +#endif /* LINKSTATUSANALYZER_H */ diff --git a/src/notify/statusnotifier.cpp b/src/notify/statusnotifier.cpp deleted file mode 100644 index b1bca0e..0000000 --- a/src/notify/statusnotifier.cpp +++ /dev/null @@ -1,100 +0,0 @@ -#include "notify/statusnotifier.h" - -#include - -#include - -using namespace std; - -//----------------------------------------------------------------------------- -// StatusNotifier -//----------------------------------------------------------------------------- - -StatusNotifier::StatusNotifier( - const int hosts_down_limit, - const string &status_notifier_cmd -) : - HostsDownLimit( hosts_down_limit ), - StatusNotifierCmd( status_notifier_cmd ), - HostsDownList() -{ - BOOST_ASSERT( 0 <= hosts_down_limit ); - BOOST_ASSERT( !status_notifier_cmd.empty() ); -} - -StatusNotifier::~StatusNotifier() -{ -} - -void StatusNotifier::notify_host_up( const string &host_address ) -{ - BOOST_ASSERT( !host_address.empty() ); - - cout << "- Host up: " << host_address << endl; // TODO - - add_host_up( host_address ); - - if ( !exceeded_host_down_limit() ) - { - notify_system_up(); - } - - // removed from the list? - BOOST_ASSERT( HostsDownList.count( host_address ) == 0 ); -} - -void StatusNotifier::notify_host_down( const string &host_address ) -{ - BOOST_ASSERT( !host_address.empty() ); - - cout << "- Host down: " << host_address << endl; // TODO - - add_host_down( host_address ); - - if ( exceeded_host_down_limit() ) - { - notify_system_down(); - } - - // inserted in the list? - BOOST_ASSERT( HostsDownList.count( host_address ) == 1 ); -} - -void StatusNotifier::add_host_up( const string &host_address ) -{ - if ( HostsDownList.count( host_address ) > 0 ) - { - HostsDownList.erase( host_address ); - } -} - -void StatusNotifier::add_host_down( const string &host_address ) -{ - HostsDownList.insert( host_address ); -} - -bool StatusNotifier::exceeded_host_down_limit() const -{ - int host_down_count = HostsDownList.size(); - return ( host_down_count > HostsDownLimit ); -} - -void StatusNotifier::notify_system_up() -{ - StatusNotifierCmd.set_token_value( - StatusNotifierCommand::StatusToken, - "up" - ); - - StatusNotifierCmd.execute(); -} - -void StatusNotifier::notify_system_down() -{ - StatusNotifierCmd.set_token_value( - StatusNotifierCommand::StatusToken, - "down" - ); - - StatusNotifierCmd.execute(); -} diff --git a/src/notify/statusnotifier.h b/src/notify/statusnotifier.h deleted file mode 100644 index 6793c9f..0000000 --- a/src/notify/statusnotifier.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef STATUSNOTIFIER_H -#define STATUSNOTIFIER_H - -#include -#include - -#include "notify/statusnotifiercommand.h" - -//----------------------------------------------------------------------------- -// StatusNotifier -//----------------------------------------------------------------------------- - -/** - * @brief This class is responsible for notify when the system is down, by - * keeping track of the number of hosts down. - * Scope: one object for many hosts. - */ -class StatusNotifier -{ -public: - StatusNotifier( - const int hosts_down_limit, - const std::string &status_notifier_cmd - ); - virtual ~StatusNotifier(); - - void notify_host_up( const std::string &host_address ); - void notify_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_limit() const; - - void notify_system_up(); - void notify_system_down(); - -private: - const int HostsDownLimit; - StatusNotifierCommand StatusNotifierCmd; - std::set HostsDownList; - -}; - -#endif /* STATUSNOTIFIER_H */ diff --git a/src/ping/hoststatusanalyzer.cpp b/src/ping/hoststatusanalyzer.cpp index 47118ea..9d763af 100644 --- a/src/ping/hoststatusanalyzer.cpp +++ b/src/ping/hoststatusanalyzer.cpp @@ -4,8 +4,6 @@ #include -#include "notify/statusnotifier.h" - using namespace std; using namespace boost; @@ -17,15 +15,15 @@ using namespace boost; * @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. + * @param link_analyzer 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 + shared_ptr link_analyzer ) : HostAddress( host_address ), - Notifier( notifier ), + LinkAnalyzer( link_analyzer ), PingFailPercentageLimit( ping_fail_percentage_limit ), ResolvedIpCount( 0 ), PingsPerformedCount( 0 ), @@ -103,11 +101,11 @@ void HostStatusAnalyzer::analyze_ping_statistics() // notify if the amount of pings that failed exceed the limit if ( exceeded_ping_failed_limit() ) { - Notifier->notify_host_down( HostAddress ); + LinkAnalyzer->notify_host_down( HostAddress ); } else { - Notifier->notify_host_up( HostAddress ); + LinkAnalyzer->notify_host_up( HostAddress ); } } diff --git a/src/ping/hoststatusanalyzer.h b/src/ping/hoststatusanalyzer.h index c46cea6..d9e127f 100644 --- a/src/ping/hoststatusanalyzer.h +++ b/src/ping/hoststatusanalyzer.h @@ -5,7 +5,7 @@ #include -class StatusNotifier; +#include "notify/linkstatusanalyzer.h" //----------------------------------------------------------------------------- // HostStatusAnalyzer @@ -22,7 +22,7 @@ public: HostStatusAnalyzer( const std::string &host_address, const int ping_fail_percentage_limit, - boost::shared_ptr notifier + boost::shared_ptr link_analyzer ); virtual ~HostStatusAnalyzer(); @@ -40,7 +40,7 @@ private: private: std::string HostAddress; - boost::shared_ptr Notifier; + boost::shared_ptr LinkAnalyzer; int PingFailPercentageLimit; int ResolvedIpCount; int PingsPerformedCount; diff --git a/src/ping/pingscheduler.cpp b/src/ping/pingscheduler.cpp index 994a90f..47d916d 100644 --- a/src/ping/pingscheduler.cpp +++ b/src/ping/pingscheduler.cpp @@ -5,7 +5,7 @@ #include #include "dns/dnsresolver.h" -#include "notify/statusnotifier.h" +#include "notify/linkstatusanalyzer.h" #include "ping/boostpinger.h" using namespace std; @@ -22,7 +22,7 @@ PingScheduler::PingScheduler( const string &ping_address, const long ping_interval_in_sec, const int ping_fail_percentage_limit, - shared_ptr notifier + shared_ptr link_analyzer ) : IoService( io_service ), @@ -30,7 +30,7 @@ PingScheduler::PingScheduler( TimeSentLastPing( microsec_clock::universal_time() ), PingIntervalInSec( ping_interval_in_sec ), IpList( ping_address ), - Analyzer( ping_address, ping_fail_percentage_limit, notifier ) + HostAnalyzer( ping_address, ping_fail_percentage_limit, link_analyzer ) { } @@ -60,7 +60,7 @@ bool PingScheduler::resolve_ping_address() } int resolved_ip_count = IpList.get_resolved_ip_count(); - Analyzer.set_resolved_ip_count( resolved_ip_count ); + HostAnalyzer.set_resolved_ip_count( resolved_ip_count ); return true; } @@ -104,7 +104,7 @@ void PingScheduler::handle_next_ping() void PingScheduler::update_ping_statistics( const bool ping_success ) { - Analyzer.update_ping_statistics( ping_success ); + HostAnalyzer.update_ping_statistics( ping_success ); // TODO you must call the method bellow AFTER update_ping_statistics // Fix this method, once it has a semantic dependency with the @@ -116,7 +116,7 @@ void PingScheduler::update_ping_statistics( const bool ping_success ) void PingScheduler::update_ping_interval() { // must to ping more often? - if ( Analyzer.exceeded_ping_failed_limit() ) + if ( HostAnalyzer.exceeded_ping_failed_limit() ) { PingIntervalInSec.speed_up(); diff --git a/src/ping/pingscheduler.h b/src/ping/pingscheduler.h index bc0caed..915e819 100644 --- a/src/ping/pingscheduler.h +++ b/src/ping/pingscheduler.h @@ -7,11 +7,10 @@ #include #include "dns/dnsresolver.h" +#include "notify/linkstatusanalyzer.h" #include "ping/hoststatusanalyzer.h" #include "ping/pinginterval.h" -class StatusNotifier; - //----------------------------------------------------------------------------- // PingScheduler //----------------------------------------------------------------------------- @@ -29,7 +28,7 @@ public: const std::string &ping_address, const long ping_interval_in_sec, const int ping_fail_percentage_limit, - boost::shared_ptr notifier + boost::shared_ptr link_analyzer ); virtual ~PingScheduler(); @@ -51,7 +50,7 @@ private: boost::posix_time::ptime TimeSentLastPing; PingInterval PingIntervalInSec; DnsResolver IpList; - HostStatusAnalyzer Analyzer; + HostStatusAnalyzer HostAnalyzer; }; -- 1.7.1