Changed ping protocol to a "per host" configuration, instead of one for all hosts
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sat, 3 Sep 2011 00:37:55 +0000 (21:37 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sat, 3 Sep 2011 00:37:55 +0000 (21:37 -0300)
src/config/configuration.cpp
src/config/configuration.h
src/config/configurationoptions.cpp
src/config/configurationoptions.h
src/config/host.cpp
src/config/host.h
src/main.cpp

index 4cf3747..e1844b0 100644 (file)
@@ -32,7 +32,6 @@ Configuration::Configuration() :
     ConfigFileName( "" ),
     SourceNetworkInterface( "" ),
     NameServer( "" ),
-    Protocol( PingProtocol_ICMP ),
     HostsDownLimit( 0 ),
     MinHostsDownLimit( 0 ),
     MaxHostsDownLimit( 50 ),
@@ -96,18 +95,6 @@ void Configuration::set_source_network_interface(
     SourceNetworkInterface = source_network_interface;
 }
 
-PingProtocol Configuration::get_ping_protocol() const
-{
-    return Protocol;
-}
-
-void Configuration::set_ping_protocol( const PingProtocol ping_protocol )
-{
-    BOOST_ASSERT( ( PingProtocol_First <= ping_protocol ) && ( ping_protocol <= PingProtocol_Last ) );
-
-    Protocol = ping_protocol;
-}
-
 int Configuration::get_hosts_down_limit() const
 {
     return HostsDownLimit;
index 54c3b3c..190fe53 100644 (file)
@@ -28,7 +28,6 @@ on this file might be covered by the GNU General Public License.
 #include <boost/shared_ptr.hpp>
 
 #include "config/host.h"
-#include "host/pingprotocol.h"
 
 //-----------------------------------------------------------------------------
 // Configuration
@@ -54,9 +53,6 @@ public:
             const std::string &source_network_interface
     );
 
-    PingProtocol get_ping_protocol() const;
-    void set_ping_protocol( const PingProtocol ping_protocol );
-
     int get_hosts_down_limit() const;
     void set_hosts_down_limit( const int hosts_down_limit );
 
@@ -80,7 +76,6 @@ private:
     std::string ConfigFileName;
     std::string SourceNetworkInterface;
     std::string NameServer;
-    PingProtocol Protocol;
     int HostsDownLimit;
     int MinHostsDownLimit;
     int MaxHostsDownLimit;
index 129abf4..65fcc89 100644 (file)
@@ -55,8 +55,6 @@ ConfigurationOptions::ConfigurationOptions() :
     DefaultNameServer( "127.0.0.1" ),
     NameServerCmdStr( "nameserver" ),
     NameServerCmdDesc( "The local address from where the DNS query will be made." ),
-    PingProtocolCmdStr( "ping-protocol" ),
-    PingProtocolCmdDesc( "Defines which protocol will be used to ping the destination." ),
     DefaultHostsDownLimit( 0 ), // no host down
     HostsDownLimitCmdStr( "hosts-down-limit" ),
     HostsDownLimitCmdDesc( "Limit of host that have to be down in order to notify." ),
@@ -76,6 +74,8 @@ ConfigurationOptions::ConfigurationOptions() :
     DefaultHostPort( 80 ), // HTTP port
     HostPortCmdStr( "host.port" ),
     HostPortCmdDesc( "Host port number" ),
+    HostPingProtocolCmdStr( "host.ping-protocol" ),
+    HostPingProtocolCmdDesc( "Defines which protocol will be used to ping the destination." ),
     DefaultHostIntervalInSec( 60 ), // 60 seconds
     HostIntervalCmdStr( "host.interval" ),
     HostIntervalCmdDesc( "Interval between each ping to the host" )
@@ -118,7 +118,6 @@ options_description ConfigurationOptions::get_configuration_options() const
     options.add_options()
         ( SourceNetworkInterfaceCmdStr.c_str(), value<string>(), SourceNetworkInterfaceCmdDesc.c_str() )
         ( NameServerCmdStr.c_str(), value<string>()->default_value( DefaultNameServer ), NameServerCmdDesc.c_str() )
-        ( PingProtocolCmdStr.c_str(), value<string>(), PingProtocolCmdDesc.c_str() )
         ( HostsDownLimitCmdStr.c_str(), value<int>()->default_value( DefaultHostsDownLimit ), HostsDownLimitCmdDesc.c_str() )
         ( PingFailLimitCmdStr.c_str(), value<int>()->default_value( DefaultPingFailLimit ), PingFailLimitCmdDesc.c_str() )
         ( StatusNotifierCmdCmdStr.c_str(), value<string>(), StatusNotifierCmdCmdDesc.c_str() )
@@ -126,6 +125,7 @@ options_description ConfigurationOptions::get_configuration_options() const
         ( LinkDownIntervalCmdStr.c_str(), value<int>()->default_value( DefaultLinkDownIntervalInMin ), LinkDownIntervalCmdDesc.c_str() )
         ( HostNameCmdStr.c_str(), value< vector<string> >(), HostNameCmdDesc.c_str() )
         ( HostPortCmdStr.c_str(), value< vector<int> >(), HostPortCmdDesc.c_str() )
+        ( HostPingProtocolCmdStr.c_str(), value< vector<string> >(), HostPingProtocolCmdDesc.c_str() )
         ( HostIntervalCmdStr.c_str(), value< vector<int> >(), HostIntervalCmdDesc.c_str() )
     ;
 
@@ -219,17 +219,6 @@ bool ConfigurationOptions::parse_configuration_options(
                 << nameserver << endl;
     }
 
-    // ping-protocol
-    if ( vm.count( PingProtocolCmdStr ) > 0 )
-    {
-        string protocol_string = vm[ PingProtocolCmdStr ].as<string> ();
-        PingProtocol protocol = get_ping_protocol_from_string( protocol_string );
-        configuration->set_ping_protocol( protocol );
-
-        GlobalLogger.info() << PingProtocolCmdStr << "="
-                << protocol_string << endl;
-    }
-
     // hosts-down-limit
     int host_down_limit = 0;
     if ( vm.count( HostsDownLimitCmdStr ) > 0 )
@@ -328,6 +317,30 @@ bool ConfigurationOptions::parse_configuration_options(
         host_port_count = hosts_ports.size();
     }
 
+    // [host] ping-protocol
+    size_t host_ping_protocol_count = 0;
+    if ( vm.count( HostPingProtocolCmdStr ) > 0 )
+    {
+        HostList hosts_list = configuration->get_hosts();
+        HostList::iterator hosts_it = hosts_list.begin();
+
+        vector<string> hosts_protocols = vm[ HostPingProtocolCmdStr ].as< vector<string> >();
+        BOOST_FOREACH( string protocol_string, hosts_protocols )
+        {
+            BOOST_ASSERT( !protocol_string.empty() );
+
+            HostItem host_item = *hosts_it;
+            PingProtocol host_protocol = get_ping_protocol_from_string( protocol_string );
+            host_item->set_ping_protocol( host_protocol );
+            ++hosts_it;
+
+            GlobalLogger.info() << HostPingProtocolCmdStr << "="
+                    << protocol_string << endl;
+        }
+
+        host_ping_protocol_count = hosts_protocols.size();
+    }
+
     // [host] interval
     size_t hosts_interval_count = 0;
     if ( vm.count( HostIntervalCmdStr ) > 0 )
@@ -360,6 +373,7 @@ bool ConfigurationOptions::parse_configuration_options(
     }
 
     BOOST_ASSERT( hosts_names_count == host_port_count );
+    BOOST_ASSERT( hosts_names_count == host_ping_protocol_count );
     BOOST_ASSERT( hosts_names_count == hosts_interval_count );
 
     return true;
index a8d014c..c68799b 100644 (file)
@@ -71,8 +71,6 @@ public: // TODO change to private
     const std::string DefaultNameServer;
     const std::string NameServerCmdStr;
     const std::string NameServerCmdDesc;
-    const std::string PingProtocolCmdStr;
-    const std::string PingProtocolCmdDesc;
     const int DefaultHostsDownLimit;
     const std::string HostsDownLimitCmdStr;
     const std::string HostsDownLimitCmdDesc;
@@ -92,6 +90,8 @@ public: // TODO change to private
     const int DefaultHostPort;
     const std::string HostPortCmdStr;
     const std::string HostPortCmdDesc;
+    const std::string HostPingProtocolCmdStr;
+    const std::string HostPingProtocolCmdDesc;
     const int DefaultHostIntervalInSec;
     const std::string HostIntervalCmdStr;
     const std::string HostIntervalCmdDesc;
index 07bd2ca..3f2e3f9 100644 (file)
@@ -32,6 +32,7 @@ using namespace std;
 Host::Host( string address ) :
     Address( address ),
     Port( 0 ),
+    Protocol( PingProtocol_ICMP ),
     IntervalInSec( 0 )
 {
 }
@@ -77,6 +78,24 @@ void Host::set_port( const uint16_t port )
 }
 
 /**
+ * @return The protocol used to ping this host.
+ */
+PingProtocol Host::get_ping_protocol() const
+{
+    return Protocol;
+}
+
+/**
+ * @param ping_protocol The protocol to be used to ping this host.
+ */
+void Host::set_ping_protocol( const PingProtocol ping_protocol )
+{
+    BOOST_ASSERT( ( PingProtocol_First <= ping_protocol ) && ( ping_protocol <= PingProtocol_Last ) );
+
+    this->Protocol = ping_protocol;
+}
+
+/**
  * @return the interval between each ping to the host.
  */
 int Host::get_interval_in_sec() const
index cb73c56..8573ed6 100644 (file)
@@ -27,6 +27,8 @@ on this file might be covered by the GNU General Public License.
 
 #include <boost/shared_ptr.hpp>
 
+#include "host/pingprotocol.h"
+
 //-----------------------------------------------------------------------------
 // Host
 //-----------------------------------------------------------------------------
@@ -43,6 +45,9 @@ public:
     uint16_t get_port() const;
     void set_port( const uint16_t port );
 
+    PingProtocol get_ping_protocol() const;
+    void set_ping_protocol( const PingProtocol ping_protocol );
+
     int get_interval_in_sec() const;
     void set_interval_in_sec( const int interval_in_sec );
 
@@ -51,6 +56,8 @@ private:
     std::string Address;
     /// the port of the host
     uint16_t Port;
+    /// The protocol to ping
+    PingProtocol Protocol;
     /// the interval between each ping to the host
     int IntervalInSec;
 
index 0040f57..70749e9 100644 (file)
@@ -94,7 +94,6 @@ void init_pingers(
         PingSchedulerList *scheduler_list
 )
 {
-    PingProtocol protocol = configuration->get_ping_protocol();
     string local_interface = configuration->get_source_network_interface();
     string nameserver = configuration->get_nameserver();
     int ping_fail_limit = configuration->get_ping_fail_limit();
@@ -104,6 +103,7 @@ void init_pingers(
     {
         string destination_address = host->get_address();
         int destination_port = host->get_port();
+        PingProtocol protocol = host->get_ping_protocol();
         int ping_interval_in_sec = host->get_interval_in_sec();
         PingSchedulerItem scheduler(
                 new PingScheduler(