- 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
#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;
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;
PingSchedulerItem scheduler(
new PingScheduler(
io_service, ping_address, ping_interval,
- limit_ping_fail
+ limit_ping_fail, notifier
)
);
scheduler_list.push_back( scheduler );
#include <boost/assert.hpp>
+#include "ping/pingstatusnotifier.h"
+
using namespace std;
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 ),
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
#include <boost/shared_ptr.hpp>
-#include "ping/pingstatusnotifier.h"
+class PingStatusNotifier;
//-----------------------------------------------------------------------------
// 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();
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;
#include "dns/dnsresolver.h"
#include "ping/boostpinger.h"
+#include "ping/pingstatusnotifier.h"
using namespace std;
using namespace boost;
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 ),
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 )
{
}
#include "dns/dnsresolver.h"
#include "ping/pinganalyzer.h"
+class PingStatusNotifier;
+
//-----------------------------------------------------------------------------
// PingScheduler
//-----------------------------------------------------------------------------
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();
#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 )
{
}
{
}
-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
+
+ */
}
// 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;
};