2 * @brief The abstract service class. This class represents all services.
6 * @copyright Intra2net AG
10 #include "service.hpp"
11 #include <boost/foreach.hpp>
15 /// Number of update errors until a service will be blocked
16 const int MaxErrorCount = 3;
17 /// Number of seconds a service will be blocked if MaxErrorCount is reached
18 const int ErrorBlockServiceSeconds = 15 * 60;
24 : Login("NOT SERIALIZED")
25 , Password("NOT SERIALIZED")
27 , ActualIPIsBurnt(false)
29 , MaxUpdatesWithinInterval(3)
30 , MaxEqualUpdatesInSuccession(2)
33 , ErrorServiceBlockedUntil(0)
40 * Default Destructor needed for deserialization.
48 * Setter for member Protocol.
49 * @param _protocol Value to set Protocol to.
51 void Service::set_protocol(const string& _protocol)
58 * Getter for memeber Protocol.
59 * @return Value of member Protocol.
61 string Service::get_protocol() const
68 * Setter for member Hostname.
69 * @param _hostname Value to set Hostname to.
71 void Service::set_hostname(const string& _hostname)
78 * Getter for member Hostname.
79 * @return Value of member Hostname.
81 string Service::get_hostname() const
88 * Setter for member Login.
89 * @param _login Value to set Login to.
91 void Service::set_login(const string& _login)
98 * Getter for member Login.
99 * @return Value of member Login.
101 string Service::get_login() const
108 * Setter for member Password.
109 * @param _password Value to set Password to.
111 void Service::set_password(const string& _password)
113 Password = _password;
118 * Getter for member Password.
119 * @return Value of member Password.
121 string Service::get_password() const
128 void Service::set_logger(const Logger::Ptr& _log)
135 * Getter for member Log.
136 * @return Shared pointer to Logger object.
138 Logger::Ptr Service::get_logger() const
145 * Setter for member LastUpdates.
146 * @param _last_updates Value to set LastUpdates to.
148 void Service::set_last_updates(std::map<time_t,std::string> _last_updates)
150 std::map<time_t,std::string> temp = _last_updates;
151 LastUpdates.swap(temp);
156 * Getter for member LastUpdates.
157 * @return Value of member LastUpdates.
159 const std::map<time_t,std::string> Service::get_last_updates() const
166 * Setter for member ActualIP.
167 * @param _actual_ip Value to set ActualIP to.
169 void Service::set_actual_ip(const std::string& _actual_ip)
171 ActualIP = _actual_ip;
176 * Getter for member ActualIP.
177 * @return Value of member ActualIP.
179 std::string Service::get_actual_ip() const
186 * Overloading of comparison operator.
187 * @param other Reference to other Service object.
188 * @return True if they equal, false if not.
190 bool Service::operator== (const Service& other) const
192 if ( ( this->Protocol == other.Protocol ) && ( this->Hostname == other.Hostname ) )
199 * Overloading of disparate operator.
200 * @param other Reference to other Service object.
201 * @return True if they differ, false if they are equal.
203 bool Service::operator!= (const Service& other) const
205 return !(*this == other);
210 * Checks if update will exceed max update interval or if the IP address is burnt.
211 * @param current_time Current time.
212 * @param changed_to_online True if we just changed to online, false if we were already online
213 * @param ip_host The new ip to set for the hostname.
214 * @return True if update is allowed, false if update would exceed max update interval.
216 bool Service::update_allowed(const time_t current_time, bool changed_to_online, const std::string& ip_host)
218 Log->print_last_updates(ip_host,current_time,UpdateInterval,MaxUpdatesWithinInterval,MaxEqualUpdatesInSuccession,LastUpdates,get_service_name());
220 // Check for update interval overstepping.
222 for ( std::map<time_t,std::string>::reverse_iterator r_iter = LastUpdates.rbegin(); (r_iter != LastUpdates.rend()) && ( i < MaxUpdatesWithinInterval ); r_iter++)
224 if ( (i == (MaxUpdatesWithinInterval-1)) && ( (r_iter->first + ((time_t)UpdateInterval*60)) >= current_time ) )
226 Log->print_update_not_allowed(changed_to_online,current_time,r_iter->first,MaxUpdatesWithinInterval,get_service_name());
234 // Changed IP address removes the "burnt state"
235 if (ip_host != ActualIP)
236 ActualIPIsBurnt = false;
239 // IP is burnt and didn't change -> Update not allowed
244 // Check for burnt IP.
245 // Only check for burnt IP address if there are at least max_equal_updates_in_succession entries in the last_updates map.
246 if ( (MaxEqualUpdatesInSuccession != 0) && ((int)LastUpdates.size() >= MaxEqualUpdatesInSuccession) )
248 bool ip_burnt = true;
250 // Reverse iterate "last_updates" list so the latest update
251 // will be the first entry (map key is the unix timestamp)
252 for ( std::map<time_t,std::string>::reverse_iterator r_iter = LastUpdates.rbegin();
253 ( r_iter != LastUpdates.rend() ) && ( i < MaxEqualUpdatesInSuccession ); r_iter++ )
255 if ( ip_host != r_iter->second )
265 // IP Address is burnt. Too many updates in succession with the same IP. Log once.
266 Log->print_ip_burnt(ip_host,get_service_name());
267 ActualIPIsBurnt = true;
277 * Service update method, common to each service.
278 * @param ip The new ip to set for the hostname.
279 * @param current_time Current time
280 * @param changed_to_online True if we just changed to online, false if we were already online
282 void Service::update(const string& ip, const time_t current_time, bool changed_to_online)
284 const std::string service_name = get_service_name();
286 // Check if service is blocked for a short period of time (because of update errors)
287 if (ErrorServiceBlockedUntil && current_time < ErrorServiceBlockedUntil)
289 Log->print_update_service_is_blocked(service_name, ErrorServiceBlockedUntil - current_time);
293 // test if update is permitted by UpdateInterval Status
294 if ( update_allowed(current_time, changed_to_online, ip) )
296 Log->print_update_service(service_name);
298 UpdateErrorCode update_res = perform_update(ip);
300 if (update_res == UpdateOk)
302 // if update was successful, we need to set the Lastupdated and ActualIP base member.
303 set_last_update(current_time,ip);
305 Log->print_update_service_successful(service_name);
308 ErrorServiceBlockedUntil = 0;
312 if (update_res == NoChange || update_res == Blocked)
314 // Log update for burnt IP logic and update "ActualIP" of this service
315 set_last_update(current_time,ip);
319 // problem while trying to update service
320 Log->print_update_service_failure(service_name);
323 if (ErrorCount >= MaxErrorCount)
325 Log->print_block_service(service_name, ErrorBlockServiceSeconds);
327 ErrorServiceBlockedUntil = time(NULL) + ErrorBlockServiceSeconds;
336 * Sets the given time into the LastUpdates member and deletes expired entries.
337 * @param _timeout Value to set into LastUpdates.
339 void Service::set_last_update(const time_t current_time, const string& ip)
341 // Insert value into the list.
342 LastUpdates.insert(make_pair(current_time,ip));
344 // Get the maximum of MaxUpdatesWithinInterval and MaxEqualUpdatesInSuccession
345 int maximum = max(MaxUpdatesWithinInterval,MaxEqualUpdatesInSuccession);
347 // Check for expired entries:
349 // MaxUpdatesWithinInterval given in service config, then use this to check for expired entries.
352 // Delete the oldest entry if there are more than max(MaxUpdatesWithinInterval,MaxEqualUpdatesInSuccession)+1 entries in the list.
353 if ( LastUpdates.size() > (size_t)(maximum+1) )
354 LastUpdates.erase(LastUpdates.begin());
357 // UpdateInterval given in service config, then use this to check for expired entries.
358 else if ( UpdateInterval > 0 )
360 // Delete the oldest entry if it's older than current_time - UpdateInterval(minutes) + 1.
361 if ( (current_time - ((time_t)UpdateInterval*60) + 1) > LastUpdates.begin()->first )
362 LastUpdates.erase(LastUpdates.begin());
365 // Neither MaxUpdatesWithinInterval nor UpdateInterval are given, so keep fix number of 10 entries.
368 if ( LastUpdates.size() > 10 )
369 LastUpdates.erase(LastUpdates.begin());
376 * Getter the last updated time.
377 * @return Value of the last update as time_t.
379 time_t Service::get_last_update_time( )
381 time_t last_update = 0;
382 if ( !LastUpdates.empty() )
384 std::map<time_t,std::string>::reverse_iterator r_iter = LastUpdates.rbegin();
385 if ( r_iter != LastUpdates.rend() )
386 last_update = r_iter->first;
393 * Setter for member Timeout.
394 * @param _timeout Value to set Timeout to.
396 void Service::set_update_interval(const int _update_interval)
398 UpdateInterval = _update_interval;
403 * Getter for member Timeout.
404 * @return Value of Timeout.
406 int Service::get_update_interval() const
408 return UpdateInterval;
413 * Setter for member Max_updates_per_timeout.
414 * @param _max_updates_per_timeout Value to set Max_updates_per_timeout to.
416 void Service::set_max_updates_within_interval(const int _max_updates_within_interval)
418 MaxUpdatesWithinInterval = _max_updates_within_interval;
423 * Getter for member Max_updates_per_timeout.
424 * @return Value of Max_updates_per_timeout.
426 int Service::get_max_updates_within_interval() const
428 return MaxUpdatesWithinInterval;
433 * Setter for member MaxEqualUpdatesInSuccession.
434 * @param _max_equal_updates_in_succession Value to set MaxEqualUpdatesInSuccession to.
436 void Service::set_max_equal_updates_in_succession(const int _max_equal_updates_in_succession)
438 MaxEqualUpdatesInSuccession = _max_equal_updates_in_succession;
443 * Getter for member MaxEqualUpdatesInSuccession.
444 * @return Value of MaxEqualUpdatesInSuccession.
446 int Service::get_max_equal_updates_in_succession() const
448 return MaxEqualUpdatesInSuccession;
453 * Get a unique service identify string
454 * @return A unique service identify string
456 string Service::get_service_name() const
460 service_name.append(Protocol);
461 service_name.append(" ");
462 service_name.append(Hostname);
469 * Get member DNSCacheTTL
470 * @return DNSCacheTTL
472 int Service::get_dns_cache_ttl() const
479 * Set member DNSCacheTTL
480 * @param _dns_cache_ttl DNSCacheTTL
482 void Service::set_dns_cache_ttl(const int _dns_cache_ttl)
484 DNSCacheTTL = _dns_cache_ttl;