From: Guilherme Maciel Ferreira Date: Fri, 4 Mar 2011 15:14:59 +0000 (+0100) Subject: The app reads a list of hosts instead of just one X-Git-Tag: v1.0~162 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=8739a651cda52f6118a0b1ec2c39634960ed19ca;p=pingcheck The app reads a list of hosts instead of just one Caveat: it pings the host sequentially, this is not the desirable behavior --- diff --git a/conf/pingcheck.conf b/conf/pingcheck.conf index 08edd6f..d9ed170 100644 --- a/conf/pingcheck.conf +++ b/conf/pingcheck.conf @@ -2,4 +2,12 @@ limit-to-notify=5 [host] name=www.intra2net.com -interval=2 \ No newline at end of file +interval=4 + +[host] +name=209.85.148.105 +interval=2 + +[host] +name=www.ufsc.br +interval=3 diff --git a/src/config/configuration.cpp b/src/config/configuration.cpp index be75d80..654a384 100644 --- a/src/config/configuration.cpp +++ b/src/config/configuration.cpp @@ -11,7 +11,7 @@ using namespace std; Configuration::Configuration() : ConfigFileName( "" ), LimitToNotify( 0 ), - Host_( "" ), + Hosts(), MinLimitToNotify( 0 ), MaxLimitToNotify( 50 ) { @@ -45,13 +45,13 @@ void Configuration::set_limit_to_notify( const std::size_t limit_to_notify ) this->LimitToNotify = limit_to_notify; } -Host Configuration::get_host() const +vector< HostItem > Configuration::get_hosts() const { - return Host_; + return Hosts; } -void Configuration::set_host( const Host& host ) +void Configuration::set_hosts( const vector< HostItem > &hosts_list ) { - this->Host_ = host; + this->Hosts = hosts_list; } diff --git a/src/config/configuration.h b/src/config/configuration.h index e56004c..229e747 100644 --- a/src/config/configuration.h +++ b/src/config/configuration.h @@ -3,6 +3,7 @@ #include #include +#include #include "host.h" @@ -22,13 +23,13 @@ public: std::size_t get_limit_to_notify() const; void set_limit_to_notify( const std::size_t limit_to_notify ); - Host get_host() const; - void set_host( const Host& host ); + std::vector get_hosts() const; + void set_hosts( const std::vector &hosts_list ); private: std::string ConfigFileName; std::size_t LimitToNotify; - Host Host_; + std::vector Hosts; const std::size_t MinLimitToNotify; const std::size_t MaxLimitToNotify; diff --git a/src/config/configurationreader.cpp b/src/config/configurationreader.cpp index adf9525..a60481a 100644 --- a/src/config/configurationreader.cpp +++ b/src/config/configurationreader.cpp @@ -1,11 +1,12 @@ #include +#include #include #include -#include #include "configurationreader.h" using namespace std; +using namespace boost; using namespace boost::program_options; //----------------------------------------------------------------------------- @@ -108,8 +109,8 @@ options_description ConfigurationReader::get_configuration_options() const options_description options( "Configuration" ); options.add_options() ( LimitToNotifyCmdStr.c_str(), value()->default_value( DefaultLimitToNotify ), LimitToNotifyCmdDesc.c_str() ) - ( HostNameCmdStr.c_str(), value< string >(), HostNameCmdDesc.c_str() ) - ( HostIntervalCmdStr.c_str(), value()->default_value( DefaultHostInterval ), HostIntervalCmdDesc.c_str() ) + ( HostNameCmdStr.c_str(), value< vector >(), HostNameCmdDesc.c_str() ) + ( HostIntervalCmdStr.c_str(), value< vector >(), HostIntervalCmdDesc.c_str() ) ; return options; @@ -133,26 +134,47 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm ) cout << LimitToNotifyCmdStr << "=" << limit_to_notify << endl; } + size_t hosts_names_total = 0; if ( vm.count( HostNameCmdStr ) ) { - string host_name = vm[ HostNameCmdStr ].as (); - Host host = Config.get_host(); - host.set_address( host_name ); - Config.set_host( host ); + vector hosts_list; - cout << HostNameCmdStr << "=" << host_name << endl; + vector hosts_names = vm[ HostNameCmdStr ].as< vector > (); + BOOST_FOREACH( string host_name, hosts_names ) + { + HostItem host_item( new Host( host_name ) ); + hosts_list.push_back( host_item ); + + cout << HostNameCmdStr << "=" << host_name << endl; + } + + Config.set_hosts( hosts_list ); + + hosts_names_total = hosts_names.size(); } + size_t hosts_interval_total = 0; if ( vm.count( HostIntervalCmdStr ) ) { - size_t host_interval = vm[ HostIntervalCmdStr ].as (); - Host host = Config.get_host(); - host.set_interval( host_interval ); - Config.set_host( host ); + vector hosts_list = Config.get_hosts(); + vector::iterator hosts_it = hosts_list.begin(); - cout << HostIntervalCmdStr << "=" << host_interval << endl; + vector hosts_intervals = vm[ HostIntervalCmdStr ].as< vector > (); + BOOST_FOREACH( size_t host_interval, hosts_intervals ) + { + HostItem host_item = *hosts_it; + host_item->set_interval( host_interval ); + hosts_it++; + + cout << HostIntervalCmdStr << "=" << host_interval << endl; + } + + hosts_interval_total = hosts_intervals.size(); } + // TODO deal when interval for a given host was not set. use DefaultHostInterval + BOOST_ASSERT(hosts_names_total == hosts_interval_total); + return true; } @@ -190,7 +212,7 @@ bool ConfigurationReader::process_command_line( } } - catch ( exception& e ) + catch ( std::exception &e ) { cout << e.what() << endl; return false; diff --git a/src/main.cpp b/src/main.cpp index 6dbf200..40bd258 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,10 @@ #include #include +#include #include #include #include +#include #include "configurationreader.h" #include "host.h" @@ -17,10 +19,12 @@ int main( int argc, char* argv[] ) if ( read_success ) { Configuration config = config_reader.get_configuration(); - Host host = config.get_host(); - - PingCheck check( host ); - check.start_pinging(); + vector hosts = config.get_hosts(); + BOOST_FOREACH( HostItem host, hosts ) + { + PingCheck check( *host ); + check.start_pinging(); + } } return 0; diff --git a/src/ping/host.h b/src/ping/host.h index d684c5e..08c1c56 100644 --- a/src/ping/host.h +++ b/src/ping/host.h @@ -1,6 +1,7 @@ #ifndef HOST_H #define HOST_H +#include #include #include #include @@ -35,4 +36,10 @@ private: }; +//----------------------------------------------------------------------------- +// HostItem +//----------------------------------------------------------------------------- + +typedef boost::shared_ptr HostItem; + #endif /* HOST_H */ diff --git a/src/ping/pingcheck.cpp b/src/ping/pingcheck.cpp index 6f1d0a8..18013f7 100644 --- a/src/ping/pingcheck.cpp +++ b/src/ping/pingcheck.cpp @@ -12,7 +12,7 @@ using namespace boost::asio; // PingCheck //----------------------------------------------------------------------------- -PingCheck::PingCheck( Host host ) : +PingCheck::PingCheck( const Host &host ) : Host_( host ) { } diff --git a/src/ping/pingcheck.h b/src/ping/pingcheck.h index 652af6e..6c940e2 100644 --- a/src/ping/pingcheck.h +++ b/src/ping/pingcheck.h @@ -12,7 +12,7 @@ class PingCheck { public: - PingCheck( Host host ); + PingCheck( const Host &host ); virtual ~PingCheck(); void start_pinging();