Added the requirement "If XX servers don't reply, it has to notify another system...
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Wed, 23 Mar 2011 16:11:04 +0000 (17:11 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Thu, 24 Mar 2011 09:05:59 +0000 (10:05 +0100)
- moved the class PingStatusNotifier from ping to notify directory.
- included a new class that encapsulate the process of system notification

src/CMakeLists.txt
src/main.cpp
src/notify/pingstatusnotifier.cpp [moved from src/ping/pingstatusnotifier.cpp with 69% similarity]
src/notify/pingstatusnotifier.h [moved from src/ping/pingstatusnotifier.h with 74% similarity]
src/notify/statusnotifiercommand.cpp [new file with mode: 0644]
src/notify/statusnotifiercommand.h [new file with mode: 0644]
src/ping/pinganalyzer.cpp
src/ping/pingscheduler.cpp

index aadb2a3..1701940 100644 (file)
@@ -27,10 +27,11 @@ set(SOURCES
     icmp/icmpheader.cpp
     icmp/icmppacket.cpp
     icmp/ipv4header.cpp
+    notify/pingstatusnotifier.cpp
+    notify/statusnotifiercommand.cpp
     ping/boostpinger.cpp
     ping/pinganalyzer.cpp
     ping/pingscheduler.cpp
-    ping/pingstatusnotifier.cpp
     main.cpp
 )
 
index 3e128ec..e8256d5 100644 (file)
@@ -6,8 +6,8 @@
 
 #include "config/configurationreader.h"
 #include "config/host.h"
+#include "notify/pingstatusnotifier.h"
 #include "ping/pingscheduler.h"
-#include "ping/pingstatusnotifier.h"
 
 using namespace std;
 using namespace boost;
similarity index 69%
rename from src/ping/pingstatusnotifier.cpp
rename to src/notify/pingstatusnotifier.cpp
index b4e49c7..d0dcc20 100644 (file)
@@ -1,4 +1,4 @@
-#include "ping/pingstatusnotifier.h"
+#include "notify/pingstatusnotifier.h"
 
 #include <iostream>
 
@@ -12,7 +12,7 @@ using namespace std;
 
 PingStatusNotifier::PingStatusNotifier(
         const int limit_hosts_down,
-        const std::string &status_notifier_cmd
+        const string &status_notifier_cmd
 ) :
     LimitHostsDown( limit_hosts_down ),
     StatusNotifierCmd( status_notifier_cmd ),
@@ -30,17 +30,16 @@ void PingStatusNotifier::notify_host_up( const string &host_address )
 {
     BOOST_ASSERT( !host_address.empty() );
 
-    cerr << "- " << host_address << " - LINK IS UP /\\ " << endl; // TODO
+    cout << "- Link up: " << host_address << 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
+        notify_system_up();
     }
 
+    // removed from the list?
     BOOST_ASSERT( HostsDownList.count( host_address ) == 0 );
 }
 
@@ -48,18 +47,16 @@ void PingStatusNotifier::notify_host_down( const string &host_address )
 {
     BOOST_ASSERT( !host_address.empty() );
 
-    cerr << "- " << host_address << " - LINK IS DOWN \\/" << endl; // TODO
+    cout << "- Link down: " << host_address << endl; // TODO
 
     add_host_down( host_address );
 
     if ( exceeded_host_down_count_limit() )
     {
-        //        alert the system is down.
-        cerr << "****** DANGER WILL ROBINSON! THE SYSTEM IS DOWN \\/ *******"
-             << endl; // TODO
-        //        execute the script
+        notify_system_down();
     }
 
+    // inserted in the list?
     BOOST_ASSERT( HostsDownList.count( host_address ) == 1 );
 }
 
@@ -81,3 +78,23 @@ 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();
+}
similarity index 74%
rename from src/ping/pingstatusnotifier.h
rename to src/notify/pingstatusnotifier.h
index 45e5e4f..7aca573 100644 (file)
@@ -4,13 +4,15 @@
 #include <set>
 #include <string>
 
+#include "notify/statusnotifiercommand.h"
+
 //-----------------------------------------------------------------------------
 // PingStatusNotifier
 //-----------------------------------------------------------------------------
 
 /**
- * @brief This class is responsible for notify when a host is down or up. And
- * to keep track of which hosts are down.
+ * @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
@@ -28,11 +30,15 @@ public:
 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:
-    int LimitHostsDown;
-    std::string StatusNotifierCmd;
+    const int LimitHostsDown;
+    StatusNotifierCommand StatusNotifierCmd;
     std::set<std::string> HostsDownList;
 
 };
diff --git a/src/notify/statusnotifiercommand.cpp b/src/notify/statusnotifiercommand.cpp
new file mode 100644 (file)
index 0000000..bd4a75d
--- /dev/null
@@ -0,0 +1,99 @@
+#include "notify/statusnotifiercommand.h"
+
+#include <cstdlib>
+
+#include <iostream>
+
+#include <boost/assert.hpp>
+
+#include <stringfunc.hxx>
+
+using namespace std;
+
+//-----------------------------------------------------------------------------
+// StatusNotifierCommand
+//-----------------------------------------------------------------------------
+
+const string StatusNotifierCommand::StatusToken = "${status}";
+
+//-----------------------------------------------------------------------------
+
+StatusNotifierCommand::StatusNotifierCommand(
+        const string &status_notifier_cmd
+) :
+    CommandStr( status_notifier_cmd )
+{
+}
+
+StatusNotifierCommand::~StatusNotifierCommand()
+{
+}
+
+bool StatusNotifierCommand::set_token_value(
+        const string &token,
+        const string &value
+)
+{
+    BOOST_ASSERT( !token.empty() );
+    BOOST_ASSERT( !value.empty() );
+    BOOST_ASSERT( !CommandStr.empty() );
+
+    size_t token_begin_pos = CommandStr.find( token );
+    if ( token_begin_pos == string::npos )
+    {
+        return false; // token string not found!
+    }
+
+    const size_t token_size = token.length();
+    CommandStr.replace( token_begin_pos, token_size, value );
+
+    // assert the token is no longer within the command string
+    BOOST_ASSERT( CommandStr.find( token ) == string::npos );
+
+    return true;
+}
+
+bool StatusNotifierCommand::execute()
+{
+    BOOST_ASSERT( !CommandStr.empty() );
+
+    string app = get_application_string();
+    string args = get_arguments_string();
+    string secure_command = app + " " + args;
+
+    cout << "- Command: " << secure_command << endl; // TODO
+
+    int ret = system( secure_command.c_str() );
+    if ( ret != 0 )
+        return false;
+
+    return true;
+}
+
+string StatusNotifierCommand::get_application_string() const
+{
+    // retrieve the application name from within the command string
+    const size_t app_begin_pos = 0;
+    size_t app_end_pos = CommandStr.find( " " );
+    if ( app_end_pos == string::npos )
+    {
+        app_end_pos = CommandStr.find( "\n" );
+        if ( app_end_pos == string::npos )
+        {
+            return false;
+        }
+    }
+
+    return CommandStr.substr( app_begin_pos, app_end_pos );;
+}
+
+string StatusNotifierCommand::get_arguments_string() const
+{
+    // retrieve the arguments string from within the command string
+    const size_t args_begin_pos = CommandStr.find( " " ) + 1;
+    const size_t args_end_pos = CommandStr.length();
+    string args = CommandStr.substr( args_begin_pos, args_end_pos );
+    args = escape_shellarg( args );
+
+    return args;
+}
diff --git a/src/notify/statusnotifiercommand.h b/src/notify/statusnotifiercommand.h
new file mode 100644 (file)
index 0000000..11c2f37
--- /dev/null
@@ -0,0 +1,39 @@
+#ifndef STATUSNOTIFIERCOMMAND_H
+#define STATUSNOTIFIERCOMMAND_H
+
+#include <string>
+
+//-----------------------------------------------------------------------------
+// StatusNotifierCommand
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief This class provides methods to handle status-notifier-cmd
+ * configuration.
+ */
+class StatusNotifierCommand
+{
+public:
+    static const std::string StatusToken;
+
+public:
+    explicit StatusNotifierCommand( const std::string &status_notifier_cmd );
+    virtual ~StatusNotifierCommand();
+
+    bool set_token_value(
+            const std::string &token,
+            const std::string &value
+    );
+
+    bool execute();
+
+private:
+    std::string get_application_string() const;
+    std::string get_arguments_string() const;
+
+private:
+    std::string CommandStr;
+
+};
+
+#endif /* STATUSNOTIFIERCOMMAND_H */
index 6a0e510..d4af2da 100644 (file)
@@ -4,7 +4,7 @@
 
 #include <boost/assert.hpp>
 
-#include "ping/pingstatusnotifier.h"
+#include "notify/pingstatusnotifier.h"
 
 using namespace std;
 using namespace boost;
index 34ee5dd..5d020d6 100644 (file)
@@ -5,8 +5,8 @@
 #include <boost/bind.hpp>
 
 #include "dns/dnsresolver.h"
+#include "notify/pingstatusnotifier.h"
 #include "ping/boostpinger.h"
-#include "ping/pingstatusnotifier.h"
 
 using namespace std;
 using namespace boost;