prevent pingcheck from repeating report of same status; show notification status...
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Fri, 12 Jun 2015 08:20:11 +0000 (10:20 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Fri, 12 Jun 2015 08:20:11 +0000 (10:20 +0200)
src/link/linkstatus.cpp
src/link/linkstatus.h

index aeebb5c..fb0dc16 100644 (file)
@@ -72,6 +72,38 @@ LinkStatus::~LinkStatus()
 {
 }
 
+std::string LinkStatus::log_prefix() const
+{
+    stringstream temp;
+    temp << "Status (" << HostsDownList.size() << " down, "
+         << "limit=" << HostsDownLimit << ", ";
+    if ( can_report_link_status() )
+    {
+        ptime now = microsec_clock::universal_time();
+        long seconds_missing;
+        if ( CurrentLinkStatus == Status_Down )
+        {
+            seconds_missing = LinkDownIntervalInSec
+                              - (now - TimeLinkStatusChanged).total_seconds();
+            temp << "notify down ";
+        }
+        else // (assume CurrentLinkStatus == Status_Up)
+        {
+            seconds_missing = LinkUpIntervalInSec
+                              - (now - TimeLinkStatusChanged).total_seconds();
+            temp << "notify up ";
+        }
+        if (seconds_missing < 0)
+            temp << "now";
+        else
+            temp << "in " << seconds_missing << "s";
+    }
+    else
+        temp << "no notify";
+    temp << "): ";
+    return temp.str();
+}
+
 /**
  * @brief Notify the system that a given host is up. The object takes an
  * appropriated action to deal with that.
@@ -86,20 +118,18 @@ void LinkStatus::notify_host_up( const string &host_address )
 
     bool has_changed = add_host_up( host_address );
 
-    if (has_changed)
-        GlobalLogger.notice() << "Status (" << HostsDownList.size()
-            << " down, limit=" << HostsDownLimit << "): now up again is "
-            << DnsMaster::get_cname_chain_str(host_address) << endl;
-    else   // less important so log only at info level
-        GlobalLogger.info() << "Status (" << HostsDownList.size()
-            << " down, limit=" << HostsDownLimit << "): still up is "
-            << DnsMaster::get_cname_chain_str(host_address) << endl;
-
     if ( !exceeded_host_down_limit() )
     {
         notify_link_up();
     }
 
+    if (has_changed)
+        GlobalLogger.notice() << log_prefix() << "now up again is "
+            << DnsMaster::get_cname_chain_str(host_address) << endl;
+    else   // less important so log only at info level
+        GlobalLogger.info() << log_prefix() << "still up is "
+            << DnsMaster::get_cname_chain_str(host_address) << endl;
+
     // removed from the list?
     BOOST_ASSERT( HostsDownList.count( host_address ) == 0 );
 } //lint !e1788
@@ -118,16 +148,15 @@ void LinkStatus::notify_host_down( const string &host_address )
 
     add_host_down( host_address );
 
-    // report this always at notice level
-    GlobalLogger.notice() << "Status (" << HostsDownList.size()
-        << " down, limit=" << HostsDownLimit << "): down is "
-        << DnsMaster::get_cname_chain_str(host_address) << endl;
-
     if ( exceeded_host_down_limit() )
     {
         notify_link_down();
     }
 
+    // report this always at notice level
+    GlobalLogger.notice() << log_prefix() << "down is "
+        << DnsMaster::get_cname_chain_str(host_address) << endl;
+
     // inserted in the list?
     BOOST_ASSERT( HostsDownList.count( host_address ) == 1 );
 } //lint !e1788
@@ -176,13 +205,13 @@ void LinkStatus::notify_link_up()
                 "up"
         );                                                                          //lint !e534
 
-        GlobalLogger.notice() << "Status (" << HostsDownList.size()
-            << " down, limit=" << HostsDownLimit << "): report link up" << endl;
+        GlobalLogger.notice() << log_prefix() << "report link up" << endl;
         bool executed = StatusNotifierCmd->execute();
 
         if ( executed )
         {
             CurrentNotificationStatus = NotificationStatus_Reported;
+            LastReportedStatus = CurrentLinkStatus;
         }
     }
 }
@@ -198,9 +227,7 @@ void LinkStatus::notify_link_down()
         BOOST_ASSERT( CurrentLinkStatus == Status_Down );
         BOOST_ASSERT( CurrentNotificationStatus == NotificationStatus_NotReported );
 
-        GlobalLogger.notice() << "Status (" << HostsDownList.size()
-            << " down, limit=" << HostsDownLimit << "): report link down"
-            << endl;
+        GlobalLogger.notice() << log_prefix() << "report link down" << endl;
         StatusNotifierCmd->set_token_value(
                 StatusNotifierCommand::StatusToken,
                 "down"
@@ -211,6 +238,7 @@ void LinkStatus::notify_link_down()
         if ( executed )
         {
             CurrentNotificationStatus = NotificationStatus_Reported;
+            LastReportedStatus = CurrentLinkStatus;
         }
     }
 }
@@ -247,9 +275,18 @@ bool LinkStatus::is_link_down_enough_time() const
     return false;
 }
 
+/**
+ * @brief determine if we should report the current link status
+ *
+ * checks if the current status has already been reported; does not take
+ * LinkUp/DownInterval into account
+ *
+ * @returns true if status should be reported
+ */
 bool LinkStatus::can_report_link_status() const
 {
-    return ( CurrentNotificationStatus == NotificationStatus_NotReported );
+    return ( CurrentNotificationStatus == NotificationStatus_NotReported
+                 && LastReportedStatus != CurrentLinkStatus );
 }
 
 void LinkStatus::set_link_status(
index 19118f1..ccc2b95 100644 (file)
@@ -52,6 +52,8 @@ public:
     void notify_host_down( const std::string &host_address );
 
 private:
+    std::string log_prefix() const;
+
     //
     // Types
     //
@@ -111,6 +113,8 @@ public:
     boost::posix_time::ptime TimeLinkStatusChanged;
     /// Command used to notify the status of the link
     StatusNotifierCommandItem StatusNotifierCmd;
+    /// last reported status to prevent multiple calls of same status
+    LinkStatus::Status LastReportedStatus;
 
 };