Added intelligence to know how many hosts are down
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Wed, 23 Mar 2011 10:37:30 +0000 (11:37 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Wed, 23 Mar 2011 10:39:12 +0000 (11:39 +0100)
src/ping/pingstatusnotifier.cpp
src/ping/pingstatusnotifier.h

index e5da632..02da7e7 100644 (file)
@@ -15,47 +15,69 @@ PingStatusNotifier::PingStatusNotifier(
         const std::string &status_notifier_cmd
 ) :
     LimitHostsDown( limit_hosts_down ),
-    StatusNotifierCmd( status_notifier_cmd )
+    StatusNotifierCmd( status_notifier_cmd ),
+    HostsDownList()
 {
+    BOOST_ASSERT( 0 <= limit_hosts_down );
+    BOOST_ASSERT( !status_notifier_cmd.empty() );
 }
 
 PingStatusNotifier::~PingStatusNotifier()
 {
 }
 
-void PingStatusNotifier::alert_host_up( const string &host_address ) const
+void PingStatusNotifier::alert_host_up( const string &host_address )
 {
     BOOST_ASSERT( !host_address.empty() );
 
-    // TODO call StatusNotifierCmd
-    cerr << "- " << host_address << " - LINK IS UP /\\ " << endl;
+    cerr << "- " << host_address << " - LINK IS UP /\\ " << endl; // TODO
 
+    add_host_up( host_address );
+
+    if ( !exceeded_host_down_count_limit() )
+    {
+        //            alert the system is up
+        cerr << "SYSTEM IS UP /\\" << endl;
+        //            execute the script
+    }
+
+    BOOST_ASSERT( HostsDownList.count( host_address ) == 0 );
 }
 
-void PingStatusNotifier::alert_host_down( const string &host_address ) const
+void PingStatusNotifier::alert_host_down( const string &host_address )
 {
     BOOST_ASSERT( !host_address.empty() );
 
-    // TODO call StatusNotifierCmd
-    cerr << "- " << host_address << " - LINK IS DOWN \\/" << endl;
-
-    /*
+    cerr << "- " << host_address << " - LINK IS DOWN \\/" << endl; // TODO
 
-    Knows how many hosts we have
+    add_host_down( host_address );
 
-    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
+    if ( exceeded_host_down_count_limit() )
+    {
+        //        alert the system is down.
+        cerr << "****** DANGER WILL ROBINSON! THE SYSTEM IS DOWN \\/ *******"
+             << endl; // TODO
+        //        execute the script
+    }
 
-    Must have a central system we can alert this lint is down, this central system
-    knows the number of hosts.
+    BOOST_ASSERT( HostsDownList.count( host_address ) == 1 );
+}
 
-    After N systems have alert this central one, if this N is greater than
-    limit-hosts-down, then we call the script
+void PingStatusNotifier::add_host_up( const string &host_address )
+{
+    if ( HostsDownList.count( host_address ) > 0 )
+    {
+        HostsDownList.erase( host_address );
+    }
+}
 
-    Must pass which hosts are down
+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 );
 }
index fe0d52f..59dda2c 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef PINGSTATUSNOTIFIER_H
 #define PINGSTATUSNOTIFIER_H
 
+#include <set>
 #include <string>
 
 //-----------------------------------------------------------------------------
@@ -8,7 +9,8 @@
 //-----------------------------------------------------------------------------
 
 /**
- * @brief This class is responsible for notify when a host is down or up.
+ * @brief This class is responsible for notify when a host is down or up. And
+ * to keep track of which hosts are down.
  * Scope: one object for many hosts.
  */
 class PingStatusNotifier
@@ -20,12 +22,18 @@ public:
     );
     virtual ~PingStatusNotifier();
 
-    void alert_host_up( const std::string &host_address ) const;
-    void alert_host_down( const std::string &host_address ) const;
+    void alert_host_up( const std::string &host_address );
+    void alert_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;
 
 private:
     int LimitHostsDown;
     std::string StatusNotifierCmd;
+    std::set<std::string> HostsDownList;
 
 };