- moved the class PingStatusNotifier from ping to notify directory.
- included a new class that encapsulate the process of system notification
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
)
#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;
-#include "ping/pingstatusnotifier.h"
+#include "notify/pingstatusnotifier.h"
#include <iostream>
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 ),
{
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 );
}
{
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 );
}
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();
+}
#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
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;
};
--- /dev/null
+#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;
+}
--- /dev/null
+#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 */
#include <boost/assert.hpp>
-#include "ping/pingstatusnotifier.h"
+#include "notify/pingstatusnotifier.h"
using namespace std;
using namespace boost;
#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;