* @param last_updates Map with the last updates in it.
* @param hostname The service hostname.
*/
-void Logger::print_last_updates( const std::string& ip_host, const int max_equal_updates_in_succession, const std::map<time_t,std::string>& last_updates, const std::string& hostname ) const
+void Logger::print_last_updates( const std::string& ip_host, const time_t current_time, const int update_interval, const int max_updates_within_interval, const int max_equal_updates_in_succession, const std::map<time_t,std::string>& last_updates, const std::string& servicename ) const
{
int level = 1;
if ( level <= Loglevel )
{
ostringstream msg;
- msg << "Last updates for " << hostname << " : ";
+ msg << "Last updates for " << servicename << " : ";
int i = 0;
+
for ( std::map<time_t,std::string>::reverse_iterator r_iter; (r_iter != last_updates.rend()) && (i < max_equal_updates_in_succession); r_iter++ )
{
msg << r_iter->first << "->" << r_iter->second;
i++;
}
- msg << ". Actual internet IP: " << ip_host << endl;
+
+ msg << ". Actual internet IP: " << ip_host
+ << " Current time: " << current_time
+ << " Update interval: " << update_interval
+ << " Max updates within interval: " << max_updates_within_interval
+ << " Max equal updates in succession: " << max_equal_updates_in_succession << endl;
log_notice(msg.str());
}
void print_msg( const std::string& msg ) const;
- void print_last_updates( const std::string& ip_host, const int max_equal_updates_in_succession, const std::map<time_t,std::string>& last_updates, const std::string& hostname ) const;
+ void print_last_updates( const std::string& ip_host, const time_t current_time, const int update_interval, const int max_updates_within_interval, const int max_equal_updates_in_succession, const std::map<time_t,std::string>& last_updates, const std::string& servicename ) const;
void print_ip_burnt( const std::string& ip_host, const std::string& hostname ) const;
};
/**
- * Checks if update will exceed max update interval.
+ * Checks if update will exceed max update interval or if the IP address is burnt.
* @param current_time Current time.
* @param changed_to_online True if we just changed to online, false if we were already online
* @return True if update is allowed, false if update would exceed max update interval.
*/
-bool Service::update_allowed(const time_t current_time, bool changed_to_online)
+bool Service::update_allowed(const time_t current_time, bool changed_to_online, const std::string& ip_host)
{
+ Log->print_last_updates(ip_host,current_time,UpdateInterval,MaxUpdatesWithinInterval,MaxEqualUpdatesInSuccession,LastUpdates,get_service_name());
+
+ // Check for update interval overstepping.
int i = 0;
for ( std::map<time_t,std::string>::reverse_iterator r_iter = LastUpdates.rbegin(); (r_iter != LastUpdates.rend()) && ( i < MaxUpdatesWithinInterval ); r_iter++)
{
}
i++;
}
+
+ // Check for burnt IP.
+ // Only check for burnt IP address if there are at least max_equal_updates_in_succession entries in the last_updates map.
+ if ( (MaxEqualUpdatesInSuccession != 0) && ((int)LastUpdates.size() >= MaxEqualUpdatesInSuccession) )
+ {
+ bool ip_burnt = true;
+ i = 0;
+ // Reverse iterate "last_updates" list so the latest update
+ // will be the first entry (map key is the unix timestamp)
+ for ( std::map<time_t,std::string>::reverse_iterator r_iter = LastUpdates.rbegin();
+ ( r_iter != LastUpdates.rend() ) && ( i < MaxEqualUpdatesInSuccession ); r_iter++ )
+ {
+ if ( ip_host != r_iter->second )
+ {
+ ip_burnt = false;
+ break;
+ }
+ i++;
+ }
+
+ if ( ip_burnt )
+ {
+ // IP Address is burnt. Too many updates in succession with the same IP.
+ Log->print_ip_burnt(ip_host,get_service_name());
+ return false;
+ }
+ }
+
return true;
}
}
// test if update is permitted by UpdateInterval Status
- if ( update_allowed(current_time, changed_to_online) )
+ if ( update_allowed(current_time, changed_to_online, ip) )
{
Log->print_update_service(service_name);
void update(const std::string& ip, const time_t current_time, bool changed_to_online);
- bool update_allowed(const time_t current_time, bool changed_to_online);
+ bool update_allowed(const time_t current_time, bool changed_to_online, const std::string& ip_host);
void set_last_update(const time_t current_time, const std::string& ip);
string hostname = service->get_hostname();
time_t current_time = time(NULL);
- // Try to get the last update time of the service if there is one.
- // And check for burnt IP, too.
- std::map<time_t,std::string> last_updates = service->get_last_updates(); /*lint !e1793 */
- time_t lastupdated = 0;
-
- int last_updates_size = last_updates.size();
- int max_equal_updates_in_succession = service->get_max_equal_updates_in_succession();
-
- Log->print_last_updates(ip_host,max_equal_updates_in_succession,last_updates,service->get_hostname());
-
- // Only check for burnt IP address if there are at least max_equal_updates_in_succession entries in the last_updates map.
- if ( (max_equal_updates_in_succession != 0) && (last_updates_size >= max_equal_updates_in_succession) )
- {
- bool ip_burnt = true;
- int i = 0;
- // Reverse iterate "last_updates" list so the latest update
- // will be the first entry (map key is the unix timestamp)
- for ( std::map<time_t,std::string>::reverse_iterator r_iter = last_updates.rbegin();
- (r_iter != last_updates.rend()) && (i < max_equal_updates_in_succession); r_iter++ )
- {
- if ( i == 0 )
- lastupdated = r_iter->first;
-
- if ( ip_host != r_iter->second )
- {
- ip_burnt = false;
- break;
- }
-
- i++;
- }
-
- if ( ip_burnt )
- {
- // IP Address is burnt. Too many updates in succession with the same IP.
- Log->print_ip_burnt(ip_host,service->get_hostname());
- continue;
- }
- }
+ // Get the last update time of the service.
+ time_t lastupdated = service->get_last_update_time();
Log->print_check_service_update(hostname, current_time, lastupdated);