From c15a722df0e84b9970d408a134677118676ddb81 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Thu, 24 Mar 2011 11:35:15 +0100 Subject: [PATCH] Renamed PingStatusNotifier to StatusNotifier - the class scope is not restricted to ping status notification, it is responsible to notify the system when certain amount of hosts are down, independently of the number of pings given to that host. --- src/CMakeLists.txt | 2 +- src/main.cpp | 6 +- src/notify/pingstatusnotifier.cpp | 100 ------------------------------------- src/notify/pingstatusnotifier.h | 46 ----------------- src/notify/statusnotifier.cpp | 100 +++++++++++++++++++++++++++++++++++++ src/notify/statusnotifier.h | 46 +++++++++++++++++ src/ping/pinganalyzer.cpp | 4 +- src/ping/pinganalyzer.h | 8 ++-- src/ping/pingscheduler.cpp | 6 +- src/ping/pingscheduler.h | 8 ++-- 10 files changed, 163 insertions(+), 163 deletions(-) delete mode 100644 src/notify/pingstatusnotifier.cpp delete mode 100644 src/notify/pingstatusnotifier.h create mode 100644 src/notify/statusnotifier.cpp create mode 100644 src/notify/statusnotifier.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1701940..d63f58f 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/pingstatusnotifier.cpp + notify/statusnotifier.cpp notify/statusnotifiercommand.cpp ping/boostpinger.cpp ping/pinganalyzer.cpp diff --git a/src/main.cpp b/src/main.cpp index e8256d5..dc4d4ff 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ #include "config/configurationreader.h" #include "config/host.h" -#include "notify/pingstatusnotifier.h" +#include "notify/statusnotifier.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 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 ) + shared_ptr notifier( + new StatusNotifier( limit_hosts_down, status_notifier_cmd ) ); // TODO init_pingers() diff --git a/src/notify/pingstatusnotifier.cpp b/src/notify/pingstatusnotifier.cpp deleted file mode 100644 index d0dcc20..0000000 --- a/src/notify/pingstatusnotifier.cpp +++ /dev/null @@ -1,100 +0,0 @@ -#include "notify/pingstatusnotifier.h" - -#include - -#include - -using namespace std; - -//----------------------------------------------------------------------------- -// PingStatusNotifier -//----------------------------------------------------------------------------- - -PingStatusNotifier::PingStatusNotifier( - const int limit_hosts_down, - const string &status_notifier_cmd -) : - LimitHostsDown( limit_hosts_down ), - StatusNotifierCmd( status_notifier_cmd ), - HostsDownList() -{ - BOOST_ASSERT( 0 <= limit_hosts_down ); - BOOST_ASSERT( !status_notifier_cmd.empty() ); -} - -PingStatusNotifier::~PingStatusNotifier() -{ -} - -void PingStatusNotifier::notify_host_up( const string &host_address ) -{ - BOOST_ASSERT( !host_address.empty() ); - - cout << "- Link up: " << host_address << endl; // TODO - - add_host_up( host_address ); - - if ( !exceeded_host_down_count_limit() ) - { - notify_system_up(); - } - - // removed from the list? - BOOST_ASSERT( HostsDownList.count( host_address ) == 0 ); -} - -void PingStatusNotifier::notify_host_down( const string &host_address ) -{ - BOOST_ASSERT( !host_address.empty() ); - - cout << "- Link down: " << host_address << endl; // TODO - - add_host_down( host_address ); - - if ( exceeded_host_down_count_limit() ) - { - notify_system_down(); - } - - // inserted in the list? - BOOST_ASSERT( HostsDownList.count( host_address ) == 1 ); -} - -void PingStatusNotifier::add_host_up( const string &host_address ) -{ - if ( HostsDownList.count( host_address ) > 0 ) - { - HostsDownList.erase( host_address ); - } -} - -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 ); -} - -void PingStatusNotifier::notify_system_up() -{ - StatusNotifierCmd.set_token_value( - StatusNotifierCommand::StatusToken, - "up" - ); - - StatusNotifierCmd.execute(); -} - -void PingStatusNotifier::notify_system_down() -{ - StatusNotifierCmd.set_token_value( - StatusNotifierCommand::StatusToken, - "down" - ); - - StatusNotifierCmd.execute(); -} diff --git a/src/notify/pingstatusnotifier.h b/src/notify/pingstatusnotifier.h deleted file mode 100644 index 7aca573..0000000 --- a/src/notify/pingstatusnotifier.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef PINGSTATUSNOTIFIER_H -#define PINGSTATUSNOTIFIER_H - -#include -#include - -#include "notify/statusnotifiercommand.h" - -//----------------------------------------------------------------------------- -// PingStatusNotifier -//----------------------------------------------------------------------------- - -/** - * @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 PingStatusNotifier -{ -public: - PingStatusNotifier( - const int limit_hosts_down, - const std::string &status_notifier_cmd - ); - virtual ~PingStatusNotifier(); - - 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_count_limit() const; - - void notify_system_up(); - void notify_system_down(); - -private: - const int LimitHostsDown; - StatusNotifierCommand StatusNotifierCmd; - std::set HostsDownList; - -}; - -#endif /* PINGSTATUSNOTIFIER_H */ diff --git a/src/notify/statusnotifier.cpp b/src/notify/statusnotifier.cpp new file mode 100644 index 0000000..7493a3f --- /dev/null +++ b/src/notify/statusnotifier.cpp @@ -0,0 +1,100 @@ +#include "notify/statusnotifier.h" + +#include + +#include + +using namespace std; + +//----------------------------------------------------------------------------- +// StatusNotifier +//----------------------------------------------------------------------------- + +StatusNotifier::StatusNotifier( + const int limit_hosts_down, + const string &status_notifier_cmd +) : + LimitHostsDown( limit_hosts_down ), + StatusNotifierCmd( status_notifier_cmd ), + HostsDownList() +{ + BOOST_ASSERT( 0 <= limit_hosts_down ); + BOOST_ASSERT( !status_notifier_cmd.empty() ); +} + +StatusNotifier::~StatusNotifier() +{ +} + +void StatusNotifier::notify_host_up( const string &host_address ) +{ + BOOST_ASSERT( !host_address.empty() ); + + cout << "- Link up: " << host_address << endl; // TODO + + add_host_up( host_address ); + + if ( !exceeded_host_down_count_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 << "- Link down: " << host_address << endl; // TODO + + add_host_down( host_address ); + + if ( exceeded_host_down_count_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_count_limit() const +{ + int host_down_count = HostsDownList.size(); + return ( host_down_count >= LimitHostsDown ); +} + +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 new file mode 100644 index 0000000..18829cc --- /dev/null +++ b/src/notify/statusnotifier.h @@ -0,0 +1,46 @@ +#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 limit_hosts_down, + 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_count_limit() const; + + void notify_system_up(); + void notify_system_down(); + +private: + const int LimitHostsDown; + StatusNotifierCommand StatusNotifierCmd; + std::set HostsDownList; + +}; + +#endif /* STATUSNOTIFIER_H */ diff --git a/src/ping/pinganalyzer.cpp b/src/ping/pinganalyzer.cpp index d4af2da..ff2326e 100644 --- a/src/ping/pinganalyzer.cpp +++ b/src/ping/pinganalyzer.cpp @@ -4,7 +4,7 @@ #include -#include "notify/pingstatusnotifier.h" +#include "notify/statusnotifier.h" using namespace std; using namespace boost; @@ -16,7 +16,7 @@ using namespace boost; PingAnalyzer::PingAnalyzer( const string &host_address, const int limit_ping_fail_percentage, - shared_ptr notifier + shared_ptr notifier ) : HostAddress( host_address ), Notifier( notifier ), diff --git a/src/ping/pinganalyzer.h b/src/ping/pinganalyzer.h index 964141e..b9b8c45 100644 --- a/src/ping/pinganalyzer.h +++ b/src/ping/pinganalyzer.h @@ -5,7 +5,7 @@ #include -class PingStatusNotifier; +class StatusNotifier; //----------------------------------------------------------------------------- // PingAnalyzer @@ -21,8 +21,8 @@ class PingAnalyzer public: PingAnalyzer( const std::string &host_address, - const int ping_fail_threshold_percentage, - boost::shared_ptr notifier + const int limit_ping_fail_percentage, + boost::shared_ptr notifier ); virtual ~PingAnalyzer(); @@ -39,7 +39,7 @@ private: private: std::string HostAddress; - boost::shared_ptr Notifier; + boost::shared_ptr Notifier; int LimitPingFailPercentage; int ResolvedIpCount; int PingsPerformedCount; diff --git a/src/ping/pingscheduler.cpp b/src/ping/pingscheduler.cpp index 5d020d6..0cc0631 100644 --- a/src/ping/pingscheduler.cpp +++ b/src/ping/pingscheduler.cpp @@ -5,7 +5,7 @@ #include #include "dns/dnsresolver.h" -#include "notify/pingstatusnotifier.h" +#include "notify/statusnotifier.h" #include "ping/boostpinger.h" using namespace std; @@ -20,9 +20,9 @@ using namespace boost::posix_time; PingScheduler::PingScheduler( boost::asio::io_service &io_service, const string &ping_address, - const int ping_interval_in_sec, + const long ping_interval_in_sec, const int limit_ping_fail_percentage, - shared_ptr notifier + shared_ptr notifier ) : IoService( io_service ), diff --git a/src/ping/pingscheduler.h b/src/ping/pingscheduler.h index 01e1b3a..24cea1c 100644 --- a/src/ping/pingscheduler.h +++ b/src/ping/pingscheduler.h @@ -9,7 +9,7 @@ #include "dns/dnsresolver.h" #include "ping/pinganalyzer.h" -class PingStatusNotifier; +class StatusNotifier; //----------------------------------------------------------------------------- // PingScheduler @@ -26,9 +26,9 @@ public: PingScheduler( boost::asio::io_service &io_service, const std::string &ping_address, - const int ping_interval_in_sec, + const long ping_interval_in_sec, const int limit_ping_fail_percentage, - boost::shared_ptr notifier + boost::shared_ptr notifier ); virtual ~PingScheduler(); @@ -47,7 +47,7 @@ private: boost::asio::io_service &IoService; boost::asio::deadline_timer Timer; boost::posix_time::ptime TimeSentLastPing; - const int PingIntervalInSec; + const long PingIntervalInSec; DnsResolver IpList; PingAnalyzer Analyzer; -- 1.7.1