added log output target UNDEFINED; return if no hosts defined
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 5 May 2015 13:44:10 +0000 (15:44 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Tue, 5 May 2015 13:44:10 +0000 (15:44 +0200)
* added the target so can warn user in case of unrecognized command line option
* also updated connd_state (there is a new connd subsys)
* avoid warning from cmake by not adding test subdir in test_dns cmake file

src/host/logoutput.cpp
src/host/logoutput.h
src/main.cpp
test/CMakeLists.test_dns.txt
test/connd_state.py
test/long_term_test.py
test/test_logoutput.cpp

index a26392e..cb85d72 100644 (file)
@@ -54,6 +54,7 @@ LogOutput get_log_output_from_string( const string &log_output_string )
     log_output_string_map[ "SYSLOG" ] = LogOutput_SYSLOG;
     log_output_string_map[ "TERMINAL" ] = LogOutput_TERMINAL;
     log_output_string_map[ "CONSOLE" ] = LogOutput_TERMINAL;
+    log_output_string_map[ "STDERR" ] = LogOutput_TERMINAL;
     log_output_string_map[ "FILE" ] = LogOutput_FILE;
 
     LogOutput protocol = log_output_string_map[ log_output_uppercase_string ];
index 4fe2209..49a7417 100644 (file)
@@ -30,7 +30,8 @@
 enum LogOutput
 {
     LogOutput_First = 0,
-    LogOutput_SYSLOG = LogOutput_First,
+    LogOutput_UNDEFINED = LogOutput_First,
+    LogOutput_SYSLOG,
     LogOutput_TERMINAL,
     LogOutput_FILE,
     LogOutput_Last = LogOutput_FILE
index d080334..6afc047 100644 (file)
@@ -66,7 +66,7 @@ LinkStatusItem get_status_notifier(const ConfigurationItem&);
 void init_logger();
 void set_log_output(const ConfigurationItem &);
 DelayMap calc_pinger_delays(const HostList &hosts);
-void init_pingers(const IoServiceItem, const ConfigurationItem&,
+bool init_pingers(const IoServiceItem, const ConfigurationItem&,
                   const LinkStatusItem&, PingSchedulerList*);
 void start_pingers(const PingSchedulerList&);
 void stop_pingers(const PingSchedulerList&);
@@ -166,6 +166,8 @@ void set_log_output(
     string log_file_name = configuration->get_log_file();
     switch (log_output)
     {
+    case LogOutput_UNDEFINED:
+        GlobalLogger.warning() << "Unknown output target -- use syslog";
     case LogOutput_SYSLOG:
         GlobalLogger.info() << "Setting log output target to syslog" << endl;
         I2n::Logger::enable_syslog(true);
@@ -236,7 +238,7 @@ DelayMap calc_pinger_delays(const HostList &hosts)
     return delay_shifts;
 }
 
-void init_pingers(
+bool init_pingers(
         const IoServiceItem io_service,
         const ConfigurationItem &configuration,
         const LinkStatusItem &status_notifier,
@@ -259,6 +261,10 @@ void init_pingers(
         delays[interval_and_delay.first] = 0.0f;
 
     HostList hosts = configuration->get_hosts();
+
+    if (hosts.empty())
+        return false;
+
     BOOST_FOREACH( const HostItem &host, hosts )
     {
         string destination_address = host->get_address();
@@ -290,6 +296,8 @@ void init_pingers(
         );
         scheduler_list->push_back( scheduler );
     }
+
+    return true;
 }
 
 void start_pingers(
@@ -475,9 +483,14 @@ int main( int argc, const char *argv[] )
 
         if ( configuration->get_print_version() )     // do this even if parsing of config failed
         {
-            GlobalLogger.debug() << "Printing version info (" << VERSION_STRING << ") and exit" << endl;
-            cout << PROJECT_NAME << " version " << VERSION_STRING << "."
-                 << VERSION_REVISION_STRING << " build " << __DATE__ << endl;
+            GlobalLogger.debug() << "Printing version info ("
+                 << VERSION_STRING << "." << VERSION_REVISION_STRING
+                 << " build " << __DATE__
+                 << ") and exit" << endl;
+            cout << PROJECT_NAME << " version "
+                 << VERSION_STRING << "." << VERSION_REVISION_STRING
+                 << " build " << __DATE__
+                 << endl;
             return 0;
         }
 
@@ -494,7 +507,10 @@ int main( int argc, const char *argv[] )
         GlobalLogger.info() << "Set LogLevel to " << I2n::Logger::get_log_level_string() << endl;
 
         set_log_output( configuration );
-        GlobalLogger.notice() << "started";
+        GlobalLogger.notice() << "started pingcheck version "
+                 << VERSION_STRING << "." << VERSION_REVISION_STRING
+                 << " build " << __DATE__
+                 << endl;
 
         bool daemon_mode = configuration->get_daemon();
         if ( daemon_mode )
@@ -512,8 +528,7 @@ int main( int argc, const char *argv[] )
         boost::asio::ip::address name_server_ip =
                                     boost::asio::ip::address::from_string(
                                             configuration->get_nameserver() );
-
-        int max_recursion_count = 10;
+        int max_recursion_count = 10;   // could make a config var some time
         DnsMaster::create_master(
                            io_service,
                            name_server_ip,
@@ -523,7 +538,13 @@ int main( int argc, const char *argv[] )
                            max_recursion_count,
                            configuration->get_dns_cache_file() );
 
-        init_pingers( io_service, configuration, status_notifier, &scheduler_list );
+        if ( !init_pingers(io_service, configuration, status_notifier,
+                           &scheduler_list) )
+        {
+            GlobalLogger.error() << "Could not initialize pingers or no hosts "
+                << "given to ping --> exit";
+            return 2;
+        }
 
         install_signal_handlers( io_service, log_level );
 
@@ -532,12 +553,12 @@ int main( int argc, const char *argv[] )
     catch ( const std::exception &ex )
     {
         GlobalLogger.error() << "Uncaught exception. " << ex.what() << endl;
-        ret_code = 2;
+        ret_code = 3;
         ++n_exceptions;
     }
     catch (...) {
         GlobalLogger.error() << "Caught unknown exception!" << endl;
-        ret_code = 3;
+        ret_code = 4;
         ++n_exceptions;
     }
 
@@ -591,11 +612,11 @@ int main( int argc, const char *argv[] )
     catch ( const std::exception &ex )
     {
         GlobalLogger.error() << "Uncaught exception while cleaning up: " << ex.what() << endl;
-        ret_code += 4;
+        ret_code += 16;
     }
     catch (...) {
         GlobalLogger.error() << "Caught unknown exception while cleaning up!" << endl;
-        ret_code += 8;
+        ret_code += 32;
     }
 
     GlobalLogger.notice() << "Pingcheck done " << endl;
index 43cf380..c93c79a 100644 (file)
@@ -36,4 +36,6 @@ target_link_libraries(
 add_test(test_dns test_dns)
 
 # add data subdir for test data
-add_subdirectory(data)
+# not necessary since is already added by test_icmppacket
+# and if both add it, we get warnings from cmake
+#add_subdirectory(data)
index ca96011..5c387ae 100755 (executable)
@@ -30,8 +30,10 @@ SUBSYS_SOCKS     = 'socks'
 SUBSYS_VPN       = 'vpn'
 SUBSYS_WEBPROXY  = 'webproxy'
 SUBSYS_PINGCHECK = 'pingcheck'
+SUBSYS_IPONLINE  = 'iponline'
 ALL_SUBSYS = (SUBSYS_DNS, SUBSYS_DYNDNS, SUBSYS_MAIL, SUBSYS_NTP, \
-              SUBSYS_SOCKS, SUBSYS_VPN, SUBSYS_WEBPROXY, SUBSYS_PINGCHECK)
+              SUBSYS_SOCKS, SUBSYS_VPN, SUBSYS_WEBPROXY, SUBSYS_PINGCHECK, \
+              SUBSYS_IPONLINE)
 
 class ConndState:
     """ representation of connd's status as returned by tell-connd --status """
index 03559a6..01b1d92 100755 (executable)
@@ -339,7 +339,7 @@ class Tester:
 
 
     def _handle_new_ip(self, message):
-        # "Have IP 172.16.1.1 [61963s] for aqua.m.i2n"
+        # "Have IP 11.22.33.44 [61963s] for host.domain.com"
         matches = regexp('Have IP (\d+\.\d+\.\d+\.\d+) \[\d+s\] for (.+)', message)
         if matches:
             ip,host_name = matches.groups()
index ddedbd1..b2c9d41 100644 (file)
@@ -34,6 +34,7 @@ BOOST_AUTO_TEST_CASE( lowercase )
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "syslog" )), static_cast<int>(LogOutput_SYSLOG) );
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "terminal" )), static_cast<int>(LogOutput_TERMINAL) );
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "console" )), static_cast<int>(LogOutput_TERMINAL) );
+    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "stderr" )), static_cast<int>(LogOutput_TERMINAL) );
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "file" )), static_cast<int>(LogOutput_FILE) );
 }
 
@@ -42,6 +43,7 @@ BOOST_AUTO_TEST_CASE( uppercase )
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "SYSLOG" )), static_cast<int>(LogOutput_SYSLOG) );
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "TERMINAL" )), static_cast<int>(LogOutput_TERMINAL) );
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "CONSOLE" )), static_cast<int>(LogOutput_TERMINAL) );
+    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "STDERR" )), static_cast<int>(LogOutput_TERMINAL) );
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "FILE" )), static_cast<int>(LogOutput_FILE) );
 }
 
@@ -50,19 +52,20 @@ BOOST_AUTO_TEST_CASE( mixed_case )
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "SysLoG" )), static_cast<int>(LogOutput_SYSLOG) );
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "teRmINAl" )), static_cast<int>(LogOutput_TERMINAL) );
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "ConSOlE" )), static_cast<int>(LogOutput_TERMINAL) );
+    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "StdErr" )), static_cast<int>(LogOutput_TERMINAL) );
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "File" )), static_cast<int>(LogOutput_FILE) );
 }
 
 BOOST_AUTO_TEST_CASE( misspelled )
 {
-    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "when" )), static_cast<int>(LogOutput_SYSLOG) );
-    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "the" )), static_cast<int>(LogOutput_SYSLOG) );
-    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "word" )), static_cast<int>(LogOutput_SYSLOG) );
-    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "is" )), static_cast<int>(LogOutput_SYSLOG) );
-    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "wrong" )), static_cast<int>(LogOutput_SYSLOG) );
-    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "stick" )), static_cast<int>(LogOutput_SYSLOG) );
-    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "to" )), static_cast<int>(LogOutput_SYSLOG) );
-    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "SYSLOG" )), static_cast<int>(LogOutput_SYSLOG) );
+    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "when" )), static_cast<int>(LogOutput_UNDEFINED) );
+    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "the" )), static_cast<int>(LogOutput_UNDEFINED) );
+    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "word" )), static_cast<int>(LogOutput_UNDEFINED) );
+    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "is" )), static_cast<int>(LogOutput_UNDEFINED) );
+    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "wrong" )), static_cast<int>(LogOutput_UNDEFINED) );
+    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "stick" )), static_cast<int>(LogOutput_UNDEFINED) );
+    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "to" )), static_cast<int>(LogOutput_UNDEFINED) );
+    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "UNDEFINED" )), static_cast<int>(LogOutput_UNDEFINED) );
 }
 
 BOOST_AUTO_TEST_SUITE_END()