From: Guilherme Maciel Ferreira Date: Tue, 15 Mar 2011 10:48:05 +0000 (+0100) Subject: Bring aboard the ping analyze and notification classes. X-Git-Tag: v1.0~140 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=ddf41c8939f580c19b9bf3bf24809f670d968293;p=pingcheck Bring aboard the ping analyze and notification classes. - 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. --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 054c3bc..4ad5443 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 index 0000000..72692ca --- /dev/null +++ b/src/ping/pinganalyzer.cpp @@ -0,0 +1,84 @@ +#include "ping/pinganalyzer.h" + +#include + +#include + +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 index 0000000..b97adc4 --- /dev/null +++ b/src/ping/pinganalyzer.h @@ -0,0 +1,37 @@ +#ifndef PINGANALYZER_H +#define PINGANALYZER_H + +#include + +#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 index 0000000..c21f3a3 --- /dev/null +++ b/src/ping/pingstatusnotifier.cpp @@ -0,0 +1,30 @@ +#include "ping/pingstatusnotifier.h" + +#include + +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 index 0000000..5fa7d9e --- /dev/null +++ b/src/ping/pingstatusnotifier.h @@ -0,0 +1,24 @@ +#ifndef PINGSTATUSNOTIFIER_H +#define PINGSTATUSNOTIFIER_H + +#include + +//----------------------------------------------------------------------------- +// 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 */