Clearly rearranged update algorithm.
authorBjoern Sikora <bjoern.sikora@intra2net.com>
Tue, 22 Sep 2009 15:14:40 +0000 (17:14 +0200)
committerBjoern Sikora <bjoern.sikora@intra2net.com>
Tue, 22 Sep 2009 15:14:40 +0000 (17:14 +0200)
Improved logging.

src/logger.cpp
src/logger.h
src/updater.cpp

index 5c7d8c7..59113e0 100644 (file)
@@ -1062,23 +1062,6 @@ void Logger::print_recheck_dns_entry(const string& hostname, const int lastupdat
 
 
 /**
- * Found following cached DNS record
- * @param hostname Hostname
- * @param cached_dns_entry IP
- */
-void Logger::print_cached_dns_entry(const string& hostname, const string& cached_dns_entry, const string& lastupdated_ip) const
-{
-    int level = 1;
-    if ( level <= Loglevel )
-    {
-        ostringstream msg;
-        msg << "Cached DNS record for host <" << hostname << "> : " << cached_dns_entry << " Last updated IP: " << lastupdated_ip << endl;
-        log_notice(msg.str(),level);
-    }
-}
-
-
-/**
  * Missing proxy option on command line.
  */
 void Logger::print_missing_cmd_proxy_option() const
@@ -1253,7 +1236,7 @@ void Logger::print_service_not_authorized(const string& service, const string& u
  * @param url Url
  * @param output HTTP status code
  */
-void Logger::print_http_status_code(const std::string& url, const long http_code) const
+void Logger::print_http_status_code(const string& url, const long http_code) const
 {
     int level = 1;
     if ( level <= Loglevel )
@@ -1520,3 +1503,141 @@ void Logger::print_webcheck_exceed_interval( const int last_webcheck, const int
         log_notice(msg.str(),level);
     }
 }
+
+
+/**
+ * Checking if hosts needs update.
+ * @param hostname Hostname
+ * @param current_time Current time
+ * @param lastupdated Last updated
+ */
+void Logger::print_check_service_update(const string& hostname, const int current_time, const int lastupdated) const
+{
+    int level = 1;
+    if ( level <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Checking if host: " << hostname << " needs update. Current time: " << current_time << " Last updated: " << lastupdated << endl;
+        log_notice(msg.str(),level);
+    }
+}
+
+
+/**
+ * Cached DNS entry
+ * @param hostname Hostname
+ * @param ip_dns_recheck DNS recheck IP
+ * @param ip_last_update IP set in last update
+ * @param ip_host Hosts' IP
+ */
+void Logger::print_cached_dns_entry(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host) const
+{
+    int level = 1;
+    if ( level <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Cached DNS record for host <" << hostname << "> : " << ip_dns_recheck << " Last updated IP: " << ip_last_update << " Hosts IP: " << ip_host << endl;
+        log_notice(msg.str(),level);
+    }
+}
+
+
+/**
+ * Updating service for the first time.
+ * @param hostname Hostname
+ * @param ip_dns_recheck Cached DNS entry
+ * @param ip_host Hosts IP
+ */
+void Logger::print_update_service_firttime(const string& hostname, const string& ip_dns_recheck, const string& ip_host) const
+{
+    int level = 1;
+    if ( level <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Updating service for the first time. Hostname: " << hostname << " DNS-Record: " << ip_dns_recheck << " Hosts IP: " << ip_host << endl;
+        log_notice(msg.str(),level);
+    }
+}
+
+
+/**
+ * Updating service
+ * @param hostname Hostname
+ * @param ip_dns_recheck Cached DNS entry
+ * @param ip_last_update IP set in last update
+ * @param ip_host Hosts IP
+ * @param lastupdated Lastupdated
+ */
+void Logger::print_update_service(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host, const int lastupdated) const{
+    int level = 1;
+    if ( level <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Updating service. Hostname: " << hostname << " DNS-Record: " << ip_dns_recheck << " IP set in last update: " << ip_last_update << " Lastupdated: " << lastupdated << " Hosts IP: " << ip_host << endl;
+        log_notice(msg.str(),level);
+    }
+}
+
+
+/**
+ * TTL expired
+ * @param hostname Hostname
+ * @param ip_dns_recheck Cached DNS entry
+ * @param ip_last_update IP set in last update
+ * @param ip_host Hosts IP
+ * @param lastupdated Last updated
+ * @param dns_cache_ttl DNS cache ttl
+ * @param current_time Current time
+ */
+void Logger::print_update_service_ttl_expired(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host, const int lastupdated, const int dns_cache_ttl, const int current_time) const
+{
+    int level = 1;
+    if ( level <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "TTL for service expired and still pointing to old IP. Hostname: " << hostname << " DNS-Record: " << ip_dns_recheck << " IP set in last update: " << ip_last_update << " Lastupdated: " << lastupdated << "DNS Cache TTL: " << dns_cache_ttl << " Current Time: " << current_time << " Hosts IP: " << ip_host << endl;
+        log_notice(msg.str(),level);
+    }
+}
+
+
+/**
+ * TTL expired
+ * @param hostname Hostname
+ * @param ip_dns_recheck Cached DNS entry
+ * @param ip_last_update IP set in last update
+ * @param ip_host Hosts IP
+ * @param lastupdated Last updated
+ * @param dns_cache_ttl DNS cache ttl
+ * @param current_time Current time
+ */
+void Logger::print_update_service_ttl_not_expired(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host, const int lastupdated, const int dns_cache_ttl, const int current_time) const
+{
+    int level = 1;
+    if ( level <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "Waiting for DNS cache TTL to expire. Hostname: " << hostname << " DNS-Record: " << ip_dns_recheck << " IP set in last update: " << ip_last_update << " Lastupdated: " << lastupdated << "DNS Cache TTL: " << dns_cache_ttl << " Current Time: " << current_time << " Hosts IP: " << ip_host << endl;
+        log_notice(msg.str(),level);
+    }
+}
+
+
+/**
+ * No update needed
+ * @param hostname Hostname
+ * @param ip_dns_recheck Cached DNS entry
+ * @param ip_last_update IP set in last update
+ * @param ip_host Hosts IP
+ * @param lastupdated Last updated
+ */
+void Logger::print_no_update_needed(const string& hostname, const string& ip_dns_recheck, const string& ip_last_update, const string& ip_host, const int lastupdated) const
+{
+    int level = 1;
+    if ( level <= Loglevel )
+    {
+        ostringstream msg;
+        msg << "No update needed for host: " << hostname << " Cached DNS record: " << ip_dns_recheck << " IP set in last update: " << ip_last_update << " Hosts IP: " << ip_host << " Last updated: " << lastupdated << endl;
+        log_notice(msg.str(),level);
+    }
+}
index e326906..a5e5d73 100644 (file)
@@ -170,8 +170,6 @@ public:
 
     void print_recheck_dns_entry(const std::string& hostname, const int lastupdated, const int dns_cache_ttl, const int current_time) const;
 
-    void print_cached_dns_entry(const std::string& hostname, const std::string& cached_dns_entry, const std::string& lastupdated_ip) const;
-
     void print_missing_cmd_proxy_option() const;
 
     void print_missing_conf_proxy_option(const std::string& main_conf_filename) const;
@@ -219,6 +217,20 @@ public:
     void print_error_parsing_cmd(const std::string& error) const;
 
     void print_webcheck_exceed_interval( const int last_webcheck, const int webcheck_interval, const int current_time ) const;
+
+    void print_check_service_update(const std::string& hostname, const int current_time, const int lastupdated) const;
+
+    void print_cached_dns_entry(const std::string& hostname, const std::string& ip_dns_recheck, const std::string& ip_last_update, const std::string& ip_host) const;
+
+    void print_update_service_firttime(const std::string& hostname, const std::string& ip_dns_recheck, const std::string& ip_host) const;
+
+    void print_update_service(const std::string& hostname, const std::string& ip_dns_recheck, const std::string& ip_last_update, const std::string& ip_host, const int lastupdated) const;
+
+    void print_update_service_ttl_expired(const std::string& hostname, const std::string& ip_dns_recheck, const std::string& ip_last_update, const std::string& ip_host, const int lastupdated, const int dns_cache_ttl, const int current_time) const;
+
+    void print_update_service_ttl_not_expired(const std::string& hostname, const std::string& ip_dns_recheck, const std::string& ip_last_update, const std::string& ip_host, const int lastupdated, const int dns_cache_ttl, const int current_time) const;
+
+    void print_no_update_needed(const std::string& hostname, const std::string& ip_dns_recheck, const std::string& ip_last_update, const std::string& ip_host, const int lastupdated) const;
 };
 
 #endif
index 3f92a48..3961d77 100644 (file)
@@ -220,39 +220,73 @@ void Updater::init_log_facility()
  */
 void Updater::update_services()
 {
+    // Get all services from the ServiceHolder.
     list<Service::Ptr> services = ServiceHolder->get_services();
 
-    string ip = IPAddrHelp->get_actual_ip();
+    // Get the actual IP of this host.
+    string ip_host = IPAddrHelp->get_actual_ip();
 
-    if ( !ip.empty() )
+    if ( !ip_host.empty() )
     {
         BOOST_FOREACH(Service::Ptr &service, services )
         {
-            string dns_recheck_ip = ip;
+            string ip_last_update = service->get_actual_ip();
+            string hostname = service->get_hostname();
+            int lastupdated = 0;
             int current_time = time(NULL);
 
-            int lastupdated = 0;
+            // Try to get the lastupdated time of the actual service if there is one.
             if ( service->get_last_updates().size() > 0 )
                 lastupdated = service->get_last_updates().front();
 
-            // If the dns cache ttl is expired or the service is updated for the first time, then get the actual ip of the dns record (this should be the IP in the last update)
-            if ( ((lastupdated != 0) && ((lastupdated + service->get_dns_cache_ttl()) < current_time)) || (lastupdated == 0) )
+            Log->print_check_service_update(hostname, current_time, lastupdated);
+
+            // Do a DNS Query for the dynamic hostname.
+            string ip_dns_recheck = IPAddrHelp->dns_query(hostname);
+
+            Log->print_cached_dns_entry(hostname, ip_dns_recheck, ip_last_update, ip_host);
+
+            // Test if the actual DNS-Record differs from the host's IP.
+            if ( ip_host != ip_dns_recheck )
             {
-                Log->print_recheck_dns_entry(service->get_hostname(),lastupdated,service->get_dns_cache_ttl(),current_time);
-                string _dns_recheck_ip = IPAddrHelp->dns_query(service->get_hostname());
-                Log->print_cached_dns_entry(service->get_hostname(), _dns_recheck_ip, service->get_actual_ip());
-                if (!_dns_recheck_ip.empty())
-                    dns_recheck_ip = _dns_recheck_ip;
+                // Check if the service will be updated for the first time.
+                if ( lastupdated == 0 )
+                {
+                    // Update for the firt time.
+                    Log->print_update_service_firttime(hostname, ip_dns_recheck, ip_host);
+                    service->update(ip_host,current_time);
+                }
+                else
+                {
+                    // We already have updated, check if the IP set in last update differs from the actual host's ip.
+                    if ( ip_last_update != ip_host )
+                    {
+                        // Update
+                        Log->print_update_service(hostname, ip_dns_recheck, ip_last_update, ip_host, lastupdated);
+                        service->update(ip_host,current_time);
+                    }
+                    else
+                    {
+                        int dns_cache_ttl = service->get_dns_cache_ttl();
+                        // Check if DNS Cache TTL is expired, if so, then update the same IP again.
+                        if ( (lastupdated + dns_cache_ttl) < current_time )
+                        {
+                            // Update
+                            Log->print_update_service_ttl_expired(hostname, ip_dns_recheck, ip_last_update, ip_host, lastupdated, dns_cache_ttl, current_time);
+                            service->update(ip_host,current_time);
+                        }
+                        else
+                        {
+                            // DNS cache TTL isn't expired
+                            Log->print_update_service_ttl_not_expired(hostname, ip_dns_recheck, ip_last_update, ip_host, lastupdated, dns_cache_ttl, current_time);
+                        }
+                    }
+                }
+            }
+            else
+            {
+                Log->print_no_update_needed(hostname, ip_dns_recheck, ip_last_update, ip_host, lastupdated);
             }
-
-            // In case the local hosts IP (ip) differ from the IP set in the last update (actual_ip) or
-            // the IP of the dns record (dns_recheck_ip) differs from the IP of the local host (ip)
-            // then perform an update. This implies that the update is not performed if actual_ip == dns_recheck_ip.
-            // Special case when latupdated == 0 then only perform the update if dns_rechek_ip differs from ip
-            if ( ((service->get_actual_ip() != ip) && (lastupdated != 0)) || (dns_recheck_ip != ip)  )
-                service->update(ip,current_time);
-            else if ( (service->get_actual_ip() != ip) && (lastupdated == 0)  )
-                service->set_actual_ip(ip);
         }
     }
 }