Bring aboard the ping analyze and notification classes.
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Tue, 15 Mar 2011 10:48:05 +0000 (11:48 +0100)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Tue, 15 Mar 2011 10:48:05 +0000 (11:48 +0100)
- PingAnalyzer implements the requirement to analyze if there are multiple dns entries for a host, only call this host "failed" if can't reach any of these multiple entries.
- PingStatusNotifier implements the requirement to notify an external system that a link is down, also notify it if the link is back up.But needs improvements.

src/CMakeLists.txt
src/ping/pinganalyzer.cpp [new file with mode: 0644]
src/ping/pinganalyzer.h [new file with mode: 0644]
src/ping/pingstatusnotifier.cpp [new file with mode: 0644]
src/ping/pingstatusnotifier.h [new file with mode: 0644]

index 054c3bc..4ad5443 100644 (file)
@@ -15,8 +15,10 @@ set( SOURCES
     icmp/icmppacket.cpp
     icmp/ipv4header.cpp
     ping/boostpinger.cpp
+    ping/pinganalyzer.cpp
     ping/pinger.cpp
     ping/pingscheduler.cpp
+    ping/pingstatusnotifier.cpp
     main.cpp
 )
 
diff --git a/src/ping/pinganalyzer.cpp b/src/ping/pinganalyzer.cpp
new file mode 100644 (file)
index 0000000..72692ca
--- /dev/null
@@ -0,0 +1,84 @@
+#include "ping/pinganalyzer.h"
+
+#include <iostream>
+
+#include <boost/assert.hpp>
+
+using namespace std;
+
+//-----------------------------------------------------------------------------
+// PingAnalyzer
+//-----------------------------------------------------------------------------
+
+PingAnalyzer::PingAnalyzer(
+        const string &host_address,
+        const int ping_fail_threshold_percentage
+) :
+    Notifier( host_address ),
+    PingFailThresholdPercentage( ping_fail_threshold_percentage ),
+    ResolvedIpCount( 0 ),
+    PingsPerformedCount( 0 ),
+    PingsFailedCount( 0 )
+{
+    BOOST_ASSERT( !host_address.empty() );
+    BOOST_ASSERT( ( 0 <= ping_fail_threshold_percentage ) && ( ping_fail_threshold_percentage <= 100 ) );
+}
+
+PingAnalyzer::~PingAnalyzer()
+{
+}
+
+void PingAnalyzer::set_resolved_ip_count( const int resolved_ip_count )
+{
+    BOOST_ASSERT( 1 <= resolved_ip_count );
+
+    ResolvedIpCount = resolved_ip_count;
+}
+
+void PingAnalyzer::update_ping_statistics( bool ping_success )
+{
+    BOOST_ASSERT( 1 <= ResolvedIpCount );
+    BOOST_ASSERT( 0 <= PingsPerformedCount );
+    BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount );
+
+    ++PingsPerformedCount;
+
+    if ( !ping_success )
+    {
+        ++PingsFailedCount;
+    }
+
+    if ( PingsPerformedCount >= ResolvedIpCount )
+    {
+        analyze();
+
+        reset_ping_counter();
+    }
+
+    BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount );
+}
+
+void PingAnalyzer::analyze()
+{
+    BOOST_ASSERT( ( 0 <= PingFailThresholdPercentage ) && ( PingFailThresholdPercentage <= 100 ) );
+    BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) );
+    BOOST_ASSERT( PingsPerformedCount == ResolvedIpCount );
+
+    int ping_fail_threshold_absolute = PingFailThresholdPercentage / 100; // TODO possible number loss, check with care
+
+    if ( PingsFailedCount > ping_fail_threshold_absolute )
+    {
+        Notifier.alert_host_down();
+    }
+    else
+    {
+        Notifier.alert_host_up();
+    }
+
+}
+
+void PingAnalyzer::reset_ping_counter()
+{
+    PingsPerformedCount = 0;
+    PingsFailedCount = 0;
+}
diff --git a/src/ping/pinganalyzer.h b/src/ping/pinganalyzer.h
new file mode 100644 (file)
index 0000000..b97adc4
--- /dev/null
@@ -0,0 +1,37 @@
+#ifndef PINGANALYZER_H
+#define PINGANALYZER_H
+
+#include <string>
+
+#include "ping/pingstatusnotifier.h"
+
+//-----------------------------------------------------------------------------
+// PingAnalyzer
+//-----------------------------------------------------------------------------
+
+class PingAnalyzer
+{
+public:
+    PingAnalyzer(
+            const std::string &host_address,
+            const int ping_fail_threshold_percentage
+    );
+    virtual ~PingAnalyzer();
+
+    void set_resolved_ip_count( const int resolved_ip_count );
+    void update_ping_statistics( bool ping_success );
+
+private:
+    void analyze();
+    void reset_ping_counter();
+
+private:
+    PingStatusNotifier Notifier;
+    int PingFailThresholdPercentage;
+    int ResolvedIpCount;
+    int PingsPerformedCount;
+    int PingsFailedCount;
+
+};
+
+#endif /* PINGANALYZER_H */
diff --git a/src/ping/pingstatusnotifier.cpp b/src/ping/pingstatusnotifier.cpp
new file mode 100644 (file)
index 0000000..c21f3a3
--- /dev/null
@@ -0,0 +1,30 @@
+#include "ping/pingstatusnotifier.h"
+
+#include <iostream>
+
+using namespace std;
+
+//-----------------------------------------------------------------------------
+// PingStatusNotifier
+//-----------------------------------------------------------------------------
+
+PingStatusNotifier::PingStatusNotifier( const string &host_address ) :
+    HostAddress( host_address )
+{
+}
+
+PingStatusNotifier::~PingStatusNotifier()
+{
+}
+
+void PingStatusNotifier::alert_host_up() const
+{
+    // TODO call script
+    cerr << "- " << HostAddress << " - LINK IS UP /\\ " << endl;
+}
+
+void PingStatusNotifier::alert_host_down() const
+{
+    // TODO call script
+    cerr << "- " << HostAddress << " - LINK IS DOWN \\/" << endl;
+}
diff --git a/src/ping/pingstatusnotifier.h b/src/ping/pingstatusnotifier.h
new file mode 100644 (file)
index 0000000..5fa7d9e
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef PINGSTATUSNOTIFIER_H
+#define PINGSTATUSNOTIFIER_H
+
+#include <string>
+
+//-----------------------------------------------------------------------------
+// PingStatusNotifier
+//-----------------------------------------------------------------------------
+
+class PingStatusNotifier
+{
+public:
+    PingStatusNotifier( const std::string &host_address );
+    virtual ~PingStatusNotifier();
+
+    void alert_host_up() const;
+    void alert_host_down() const;
+
+private:
+    const std::string HostAddress;
+
+};
+
+#endif /* PINGSTATUSNOTIFIER_H */