Reading the nameserver form configuration file
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Fri, 15 Apr 2011 13:19:35 +0000 (15:19 +0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@intra2net.com>
Fri, 15 Apr 2011 13:19:35 +0000 (15:19 +0200)
Readme
conf/pingcheck.conf
src/config/configuration.cpp
src/config/configuration.h
src/config/configurationreader.cpp
src/config/configurationreader.h
src/dns/dnsresolver.cpp
src/dns/dnsresolver.h
src/host/pingscheduler.cpp
src/host/pingscheduler.h
src/main.cpp

diff --git a/Readme b/Readme
index 947ee02..4487cc1 100644 (file)
--- a/Readme
+++ b/Readme
@@ -167,6 +167,11 @@ configuration block.
 4.1. General
 ---------------------------------------
 This configurations are shared among and affect all the hosts.
+- source-network-interface: the local network interface from where the ping
+  packages with originate.
+- nameserver: the server which the hosts names will be resolved. It is the
+  lookup server which the application will query first. If left blank or omited,
+  it will use the /etc/resolv.conf.
 - hosts-down-limit: an absolute number, which ranges from 0 to the number of
   hosts available. This value represents the minimum number of hosts that have
   to fail (i.e. do not reply to the ping) in order to alert any external system.
index 82468a3..be79e61 100644 (file)
@@ -1,4 +1,5 @@
 source-network-interface=eth1
+nameserver=127.0.0.1
 hosts-down-limit=4
 ping-fail-limit=40
 status-notifier-cmd=./system_status_alert.sh ${status}
index 69b89b6..29937e7 100644 (file)
@@ -11,6 +11,7 @@ using namespace std;
 Configuration::Configuration() :
     ConfigFileName( "" ),
     SourceNetworkInterface( "" ),
+    NameServer( "" ),
     HostsDownLimit( 0 ),
     MinHostsDownLimit( 0 ),
     MaxHostsDownLimit( 50 ),
@@ -41,6 +42,16 @@ void Configuration::set_config_file_name( const std::string &config_file_name )
     this->ConfigFileName = config_file_name;
 }
 
+std::string Configuration::get_nameserver() const
+{
+    return NameServer;
+}
+
+void Configuration::set_nameserver( const std::string &nameserver )
+{
+    NameServer = nameserver;
+}
+
 string Configuration::get_source_network_interface() const
 {
     return SourceNetworkInterface;
index b6de740..4255a0f 100644 (file)
@@ -21,6 +21,9 @@ public:
     std::string get_config_file_name() const;
     void set_config_file_name( const std::string &config_file_name );
 
+    std::string get_nameserver() const;
+    void set_nameserver( const std::string &nameserver );
+
     std::string get_source_network_interface() const;
     void set_source_network_interface(
             const std::string &source_network_interface
@@ -44,6 +47,7 @@ public:
 private:
     std::string ConfigFileName;
     std::string SourceNetworkInterface;
+    std::string NameServer;
     int HostsDownLimit;
     const int MinHostsDownLimit;
     const int MaxHostsDownLimit;
index df32be9..d59d530 100644 (file)
@@ -29,6 +29,8 @@ ConfigurationReader::ConfigurationReader() :
     ConfigFileCmdDesc( "Name of the configuration file." ),
     SourceNetworkInterfaceCmdStr( "source-network-interface" ),
     SourceNetworkInterfaceCmdDesc( "The network interface from where the packets will be received and originated" ),
+    NameServerCmdStr( "nameserver" ),
+    NameServerCmdDesc( "The local address from where the DNS query will be made." ),
     DefaultHostsDownLimit( 1 ), // 1 host down at most
     HostsDownLimitCmdStr( "hosts-down-limit" ),
     HostsDownLimitCmdDesc( "Limit of host that have to be down in order to notify." ),
@@ -124,6 +126,7 @@ options_description ConfigurationReader::get_configuration_options() const
     options_description options( "Configuration" );
     options.add_options()
         ( SourceNetworkInterfaceCmdStr.c_str(), value<string>(), SourceNetworkInterfaceCmdDesc.c_str() )
+        ( NameServerCmdStr.c_str(), value<string>(), NameServerCmdDesc.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() )
@@ -157,6 +160,15 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm )
              << endl;
     }
 
+    // nameserver
+    if ( vm.count( NameServerCmdStr ) )
+    {
+        string nameserver = vm[ NameServerCmdStr ].as<string> ();
+        Config.set_nameserver( nameserver );
+
+        cout << NameServerCmdStr << "=" << nameserver << endl;
+    }
+
     // hosts-down-limit
     int host_down_limit = 0;
     if ( vm.count( HostsDownLimitCmdStr ) )
index 313e23a..4aa5b9c 100644 (file)
@@ -63,6 +63,8 @@ private:
     const std::string ConfigFileCmdDesc;
     const std::string SourceNetworkInterfaceCmdStr;
     const std::string SourceNetworkInterfaceCmdDesc;
+    const std::string NameServerCmdStr;
+    const std::string NameServerCmdDesc;
     const int DefaultHostsDownLimit;
     const std::string HostsDownLimitCmdStr;
     const std::string HostsDownLimitCmdDesc;
index 4277ee3..bd33585 100644 (file)
@@ -19,9 +19,13 @@ using boost::net::dns::rr_list_t;
 // DnsResolver
 //-----------------------------------------------------------------------------
 
-DnsResolver::DnsResolver( const string &dns_address ) :
+DnsResolver::DnsResolver(
+        const string &dns_address,
+        const string &nameserver
+) :
     ResolvedHostAddressList(),
-    HostDnsAddress( dns_address )
+    HostDnsAddress( dns_address ),
+    NameServer( nameserver )
 {
 }
 
@@ -37,12 +41,13 @@ DnsResolver::~DnsResolver()
 bool DnsResolver::resolve()
 {
     BOOST_ASSERT( !HostDnsAddress.empty() );
+    BOOST_ASSERT( !NameServer.empty() );
 
     cout << "Resolved IP(s) for host : " << HostDnsAddress << endl;
 
     try
     {
-        address nameServer( address::from_string( "127.0.0.1" ) );
+        address nameServer( address::from_string( NameServer ) );
         boost::net::dns::resolve resolver;
         resolver.addServer( nameServer );
         message dns_message( HostDnsAddress, boost::net::dns::type_a );
index 30764b0..0ef5e7c 100644 (file)
@@ -13,7 +13,10 @@ class HostAddress;
 class DnsResolver
 {
 public:
-    explicit DnsResolver( const std::string &dns_address );
+    DnsResolver(
+            const std::string &dns_address,
+            const std::string &nameserver
+    );
     ~DnsResolver();
 
     bool resolve();
@@ -23,6 +26,7 @@ public:
 private:
     std::list<HostAddress> ResolvedHostAddressList;
     const std::string HostDnsAddress;
+    const std::string NameServer;
 
 };
 
index 4405097..aa0c302 100644 (file)
@@ -26,6 +26,7 @@ PingScheduler::PingScheduler(
         const string &destination_address,
         const long ping_interval_in_sec,
         const int ping_fail_percentage_limit,
+        const string &nameserver,
         shared_ptr<LinkStatusAnalyzer> link_analyzer
 
 ) :
@@ -34,7 +35,7 @@ PingScheduler::PingScheduler(
     NextPingTimer( io_serv ),
     TimeSentLastPing( microsec_clock::universal_time() ),
     PingIntervalInSec( ping_interval_in_sec ),
-    IpList( destination_address ),
+    IpList( destination_address, nameserver ),
     HostAnalyzer( destination_address, ping_fail_percentage_limit, link_analyzer )
 {
 }
index 14a549d..209fad1 100644 (file)
@@ -29,6 +29,7 @@ public:
             const std::string &destination_address,
             const long ping_interval_in_sec,
             const int ping_fail_percentage_limit,
+            const std::string &nameserver,
             boost::shared_ptr<LinkStatusAnalyzer> link_analyzer
     );
     ~PingScheduler();
index 40fc89b..b0d7c75 100644 (file)
@@ -39,22 +39,24 @@ int main( int argc, char* argv[] )
                 )
         );
 
-        string ping_interface = config.get_source_network_interface();
+        string local_interface = config.get_source_network_interface();
+        string nameserver = config.get_nameserver();
 
         // TODO init_pingers()
         vector< HostItem > hosts = config.get_hosts();
         vector< PingSchedulerItem > scheduler_list;
         BOOST_FOREACH( HostItem host, hosts )
         {
-            string ping_address = (*host).get_address();
+            string destination_address = (*host).get_address();
             int ping_interval_in_sec = (*host).get_interval_in_sec();
             PingSchedulerItem scheduler(
                     new PingScheduler(
                             io_serv,
-                            ping_interface,
-                            ping_address,
+                            local_interface,
+                            destination_address,
                             ping_interval_in_sec,
                             ping_fail_limit,
+                            nameserver,
                             link_analyzer
                     )
             );