Changed PingStatusNotifier interface, it is now an object shared among N hosts
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Tue, 22 Mar 2011 09:31:58 +0000 (10:31 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Tue, 22 Mar 2011 09:32:48 +0000 (10:32 +0100)
- 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

src/main.cpp
src/ping/pinganalyzer.cpp
src/ping/pinganalyzer.h
src/ping/pingscheduler.cpp
src/ping/pingscheduler.h
src/ping/pingstatusnotifier.cpp
src/ping/pingstatusnotifier.h

index 1608ffd..3e128ec 100644 (file)
@@ -2,10 +2,12 @@
 
 #include <boost/asio.hpp>
 #include <boost/foreach.hpp>
+#include <boost/shared_ptr.hpp>
 
 #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<PingStatusNotifier> 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 );
index b7f4acd..b51f40e 100644 (file)
@@ -4,6 +4,8 @@
 
 #include <boost/assert.hpp>
 
+#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<PingStatusNotifier> 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
index e7033cc..6a97f56 100644 (file)
@@ -5,7 +5,7 @@
 
 #include <boost/shared_ptr.hpp>
 
-#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<PingStatusNotifier> 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<PingStatusNotifier> Notifier;
     int LimitPingFailPercentage;
     int ResolvedIpCount;
     int PingsPerformedCount;
index 38a7ee3..34ee5dd 100644 (file)
@@ -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<PingStatusNotifier> 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 )
 {
 }
 
index d9b9d5a..01e1b3a 100644 (file)
@@ -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<PingStatusNotifier> notifier
     );
     virtual ~PingScheduler();
 
index c21f3a3..e5da632 100644 (file)
@@ -2,14 +2,20 @@
 
 #include <iostream>
 
+#include <boost/assert.hpp>
+
 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
+
+     */
 }
index 5fa7d9e..fe0d52f 100644 (file)
@@ -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;
 
 };