From: Guilherme Maciel Ferreira Date: Wed, 23 Mar 2011 16:11:04 +0000 (+0100) Subject: Added the requirement "If XX servers don't reply, it has to notify another system... X-Git-Tag: v1.0~119 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=b279ae0950da1f8eb1dd7375433ba2855541b65b;p=pingcheck Added the requirement "If XX servers don't reply, it has to notify another system (e.g. execute a script)" - moved the class PingStatusNotifier from ping to notify directory. - included a new class that encapsulate the process of system notification --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index aadb2a3..1701940 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 ) diff --git a/src/main.cpp b/src/main.cpp index 3e128ec..e8256d5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; diff --git a/src/ping/pingstatusnotifier.cpp b/src/notify/pingstatusnotifier.cpp similarity index 69% rename from src/ping/pingstatusnotifier.cpp rename to src/notify/pingstatusnotifier.cpp index b4e49c7..d0dcc20 100644 --- a/src/ping/pingstatusnotifier.cpp +++ b/src/notify/pingstatusnotifier.cpp @@ -1,4 +1,4 @@ -#include "ping/pingstatusnotifier.h" +#include "notify/pingstatusnotifier.h" #include @@ -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(); +} diff --git a/src/ping/pingstatusnotifier.h b/src/notify/pingstatusnotifier.h similarity index 74% rename from src/ping/pingstatusnotifier.h rename to src/notify/pingstatusnotifier.h index 45e5e4f..7aca573 100644 --- a/src/ping/pingstatusnotifier.h +++ b/src/notify/pingstatusnotifier.h @@ -4,13 +4,15 @@ #include #include +#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 HostsDownList; }; diff --git a/src/notify/statusnotifiercommand.cpp b/src/notify/statusnotifiercommand.cpp new file mode 100644 index 0000000..bd4a75d --- /dev/null +++ b/src/notify/statusnotifiercommand.cpp @@ -0,0 +1,99 @@ +#include "notify/statusnotifiercommand.h" + +#include + +#include + +#include + +#include + +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 index 0000000..11c2f37 --- /dev/null +++ b/src/notify/statusnotifiercommand.h @@ -0,0 +1,39 @@ +#ifndef STATUSNOTIFIERCOMMAND_H +#define STATUSNOTIFIERCOMMAND_H + +#include + +//----------------------------------------------------------------------------- +// 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 */ diff --git a/src/ping/pinganalyzer.cpp b/src/ping/pinganalyzer.cpp index 6a0e510..d4af2da 100644 --- a/src/ping/pinganalyzer.cpp +++ b/src/ping/pinganalyzer.cpp @@ -4,7 +4,7 @@ #include -#include "ping/pingstatusnotifier.h" +#include "notify/pingstatusnotifier.h" using namespace std; using namespace boost; diff --git a/src/ping/pingscheduler.cpp b/src/ping/pingscheduler.cpp index 34ee5dd..5d020d6 100644 --- a/src/ping/pingscheduler.cpp +++ b/src/ping/pingscheduler.cpp @@ -5,8 +5,8 @@ #include #include "dns/dnsresolver.h" +#include "notify/pingstatusnotifier.h" #include "ping/boostpinger.h" -#include "ping/pingstatusnotifier.h" using namespace std; using namespace boost;