2 * @brief The updater class implementation. This class implements the updater logic.
6 * @copyright Intra2net AG
10 #include "updater.hpp"
12 #include "serviceholder.hpp"
15 #include <boost/foreach.hpp>
16 #include <boost/serialization/export.hpp>
22 /// Server error threshold: When the host IP is not current, the DNS TTL expired
23 /// and we already sent an update.
24 const int ServerErrorTTLExpiredThreshold = 15 * 60;
28 * Default constructor which initializes the member Conf.
31 : IPAddrHelp(new IPAddrHelper)
33 // Initialize program wide Logger, at this point we don't know where to log and with which loglevel.
34 Log = Logger::Ptr(new Logger);
36 // initialize Serviceholder
37 ServiceHolder = Serviceholder::Ptr(new Serviceholder(Log));
40 Conf = Config::Ptr(new Config(Log,ServiceHolder));
54 * Load config and initialize helper classes.
55 * @param argc Number of arguments in array
56 * @param argv Array with cmd options
58 int Updater::load_config(int argc, char *argv[])
60 // load the cmd options
61 if ( init_config_from_cmd(argc,argv) != 0 )
64 // load the config and service files
65 if ( init_config_from_files() != 0 )
68 // init all helper classes
69 if ( init_helper_classes() != 0 )
77 * Reloading the config. Delete all Service objects and then init new Service objects from config files.
79 int Updater::reload_config()
81 // serialize all service objects
82 if ( ServiceHolder->serialize_services() != 0 )
85 // delete all service objects
86 ServiceHolder->delete_services();
88 // delete the actual Variables_map, perhaps with old cmd options which would overwrite new config file options.
89 Conf->delete_variables_map();
91 // clear the external send messages.
92 Log->clear_external_send_messages();
94 // load only config files
95 if ( init_config_from_files() != 0 )
98 // init all helper classes
99 if ( init_helper_classes() != 0 )
107 * Parse the command line arguments and initialize corresponding options.
108 * @param argc Number command line arguments.
109 * @param argv[] Array with arguments.
110 * @return 0 if cmd options successfully parsed, 1 if usage or version.
112 int Updater::init_config_from_cmd(int argc, char *argv[]) const
114 // Load the command line parameters
115 if( Conf->parse_cmd_line( argc, argv) != 0)
118 // If we have loaded the cmd options we need to init the log facility immediately in case debugging is enabled from cmd.
122 Log->print_cmd_parsed();
128 * Load the main config and the service definition files in config path.
129 * @return 0 if all is fine,
131 int Updater::init_config_from_files() const
133 // Load the main and service config files in config path
134 if ( Conf->load_config_from_files() != 0 )
137 // Re-init log facility, perhaps new config file options for logger are set.
138 // These config file options will only overwrite the cmd options if the SIGHUP (reload config) is caught.
141 // Here we are. All Service Objects should be initialized, so it is time to deserialize the old service objects and compare them.
142 if ( ServiceHolder->deserialize_services() != 0 )
146 Log->print_conf_files_parsed();
152 * Init all Helper classes
155 int Updater::init_helper_classes()
157 // Initialize IPHelper
158 if ( init_ip_helper() != 0 )
166 * Init the IPHelp member with needed values.
167 * @return 0 if all is fine, -1 otherwise.
169 int Updater::init_ip_helper()
171 // Try to get deserialized IPAddrHelper from ServiceHolder
172 IPAddrHelper::Ptr _ip_addr_help = ServiceHolder->get_ip_addr_helper();
173 if ( _ip_addr_help.use_count() != 0 )
175 // Initialize IPHelper
176 IPAddrHelp = IPAddrHelper::Ptr( new IPAddrHelper( Log, Conf->get_webcheck_ip_url(), Conf->get_webcheck_ip_url_alt(), Conf->get_webcheck_interval(), _ip_addr_help->get_last_webcheck(), Conf->get_enable_ipv6(), Conf->get_proxy(), Conf->get_proxy_port() ) );
180 // IPAddrHelper from ServiceHolder was not declared, so init oen with LastWebcheck 0
181 IPAddrHelp = IPAddrHelper::Ptr( new IPAddrHelper( Log, Conf->get_webcheck_ip_url(), Conf->get_webcheck_ip_url_alt(), Conf->get_webcheck_interval(), (size_t)0, Conf->get_enable_ipv6(), Conf->get_proxy(), Conf->get_proxy_port() ) );
184 // Put the IPAddrHelper into ServiceHolder, so the LastWebcheck state will be serialized too.
185 ServiceHolder->set_ip_addr_helper(IPAddrHelp);
192 * Getter for member Config.
193 * @return Member Config.
195 Config::Ptr Updater::get_config() const
202 * Getter for member Logger.
203 * @return Member Logger.
205 Logger::Ptr Updater::get_logger() const
212 * Initialize the logging facility with loglevel and syslog.
214 void Updater::init_log_facility() const
216 Log->set_log_facility(Conf->get_loglevel(),Conf->get_syslog(),Conf->get_external_warning_log(),Conf->get_external_warning_level(),Conf->get_external_log_only_once());
217 Log->print_init_log_facility();
222 * Update all configured services.
223 * @param changed_to_online True if we just changed to online, false if we were already online
225 void Updater::update_services(bool changed_to_online) const
227 // Get all services from the ServiceHolder.
228 list<Service::Ptr> services = ServiceHolder->get_services();
230 // Get the actual IP of this host.
231 string ip_host = IPAddrHelp->get_actual_ip(Conf->get_webcheck_enabled(), changed_to_online, Conf->get_wan_ip_override());
232 if ( ip_host.empty() )
234 Log->print_no_wan_ip(changed_to_online);
238 Log->print_external_wan_ip(changed_to_online, ip_host);
241 BOOST_FOREACH(Service::Ptr &service, services )
243 string ip_last_update = service->get_actual_ip();
244 string hostname = service->get_hostname();
245 time_t lastupdated = 0;
246 time_t current_time = time(NULL);
248 // Try to get the lastupdated time of the actual service if there is one.
249 if ( !service->get_last_updates().empty() )
250 lastupdated = service->get_last_updates().front(); /*lint !e1793 */
252 Log->print_check_service_update(hostname, current_time, lastupdated);
254 // Do a DNS Query for the dynamic hostname.
255 string ip_dns_recheck = IPAddrHelp->dns_query(hostname);
257 Log->print_cached_dns_entry(hostname, ip_dns_recheck, ip_last_update, ip_host);
259 // Test if the DNS-Record could not be found.
260 if ( ip_dns_recheck.empty() )
262 Log->print_dns_lookup_failed(changed_to_online, hostname);
266 // Test if the actual DNS-Record differs from the host's IP.
267 if (ip_host == ip_dns_recheck )
269 Log->print_no_update_needed(changed_to_online, hostname, ip_dns_recheck, ip_last_update, ip_host, lastupdated);
274 // Check if the IP set in last update differs from the actual host's ip.
275 if ( ip_last_update != ip_host )
278 Log->print_update_service(hostname, ip_dns_recheck, ip_last_update, ip_host, lastupdated);
279 service->update(ip_host, current_time, changed_to_online);
283 int dns_cache_ttl = service->get_dns_cache_ttl();
285 // Add server error threshold so we don't jam the server with updates for the same IP
286 // in case the server doesn't hand out the already sent IP via DNS
287 // (might indicate a server error).
288 dns_cache_ttl += ServerErrorTTLExpiredThreshold;
290 // Check if DNS Cache TTL is expired, if so, then update the same IP again.
291 if ( (lastupdated + dns_cache_ttl) < current_time )
294 Log->print_update_service_ttl_expired(hostname, ip_dns_recheck, ip_last_update, ip_host, lastupdated, dns_cache_ttl, current_time);
295 service->update(ip_host, current_time, changed_to_online);
299 // DNS cache TTL isn't expired
300 Log->print_update_service_ttl_not_expired(changed_to_online, hostname, ip_dns_recheck, ip_last_update, ip_host, lastupdated, dns_cache_ttl, current_time);
308 * Getter for member ServiceHolder.
309 * @return ServiceHolder
311 Serviceholder::Ptr Updater::get_service_holder() const
313 return ServiceHolder;