2 * @brief The updater class implementation. This class implements the updater logic.
6 * @copyright Intra2net AG
12 #include "serviceholder.h"
15 #include <boost/foreach.hpp>
16 #include <boost/serialization/export.hpp>
23 * Default constructor which initializes the member Conf.
26 : IPAddrHelp(new IPAddrHelper)
28 // Initialize program wide Logger, at this point we don't know where to log and with which loglevel.
29 Logger::Ptr _log(new Logger);
32 // initialize Serviceholder
33 Serviceholder::Ptr _serviceholder(new Serviceholder(Log));
34 ServiceHolder.swap(_serviceholder);
37 Config::Ptr _config(new Config(Log,ServiceHolder));
52 * Load config and initialize helper classes.
53 * @param argc Number of arguments in array
54 * @param argv Array with cmd options
56 int Updater::load_config(int argc, char *argv[])
58 // load the cmd options
59 if ( init_config_from_cmd(argc,argv) != 0 )
62 // load the config and service files
63 if ( init_config_from_files() != 0 )
66 // init all helper classes
67 if ( init_helper_classes() != 0 )
75 * Reloading the config. Delete all Service objects and then init new Service objects from config files.
77 int Updater::reload_config()
79 // serialize all service objects
80 if ( ServiceHolder->serialize_services() != 0 )
83 // delete all service objects
84 ServiceHolder->delete_services();
86 // delete the actual Variables_map, perhaps with old cmd options which would overwrite new config file options.
87 Conf->delete_variables_map();
89 // load only config files
90 if ( init_config_from_files() != 0 )
93 // init all helper classes
94 if ( init_helper_classes() != 0 )
102 * Parse the command line arguments and initialize corresponding options.
103 * @param argc Number command line arguments.
104 * @param argv[] Array with arguments.
105 * @return 0 if cmd options successfully parsed, 1 if usage or version.
107 int Updater::init_config_from_cmd(int argc, char *argv[])
109 // Load the command line parameters
110 if( Conf->parse_cmd_line( argc, argv) != 0)
113 // If we have loaded the cmd options we need to init the log facility immediately in case debugging is enabled from cmd.
117 Log->print_cmd_parsed();
123 * Load the main config and the service definition files in config path.
124 * @return 0 if all is fine,
126 int Updater::init_config_from_files()
128 // Load the main and service config files in config path
129 if ( Conf->load_config_from_files() != 0 )
132 // Re-init log facility, perhaps new config file options for logger are set.
133 // These config file options will only overwrite the cmd options if the SIGHUP (reload config) is caught.
136 // Here we are. All Service Objects should be initialized, so it is time to deserialize the old service objects and compare them.
137 if ( ServiceHolder->deserialize_services() != 0 )
141 Log->print_conf_files_parsed();
147 * Init all Helper classes
150 int Updater::init_helper_classes()
152 // Initialize IPHelper
153 if ( init_ip_helper() != 0 )
161 * Init the IPHelp member with needed values.
162 * @return 0 if all is fine, -1 otherwise.
164 int Updater::init_ip_helper()
166 // initialize IPHelper
167 IPAddrHelper::Ptr _ipaddrhelp(new IPAddrHelper(Log,Conf->get_webcheck_ip_url(),Conf->get_webcheck_ip_url_alt(),Conf->get_enable_ipv6(),Conf->get_proxy(),Conf->get_proxy_port()));
168 IPAddrHelp.swap(_ipaddrhelp);
175 * Getter for member Config.
176 * @return Member Config.
178 Config::Ptr Updater::get_config() const
185 * Getter for member Logger.
186 * @return Member Logger.
188 Logger::Ptr Updater::get_logger() const
195 * Initialize the logging facility with loglevel and syslog.
197 void Updater::init_log_facility()
199 Log->set_log_facility(Conf->get_loglevel(),Conf->get_syslog(),Conf->get_external_warning_log(),Conf->get_external_warning_level());
200 Log->print_init_log_facility();
205 * Update all configured services.
207 void Updater::update_services()
209 list<Service::Ptr> services = ServiceHolder->get_services();
211 string ip = IPAddrHelp->get_actual_ip();
215 BOOST_FOREACH(Service::Ptr &service, services )
217 string dns_recheck_ip = ip;
218 int current_time = time(NULL);
221 if ( service->get_last_updates().size() > 0 )
222 lastupdated = service->get_last_updates().front();
224 // 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)
225 if ( ((lastupdated != 0) && ((lastupdated + service->get_dns_cache_ttl()) < current_time)) || (lastupdated == 0) )
227 Log->print_recheck_dns_entry(service->get_hostname(),lastupdated,service->get_dns_cache_ttl(),current_time);
228 string _dns_recheck_ip = IPAddrHelp->dns_query(service->get_hostname());
229 Log->print_cached_dns_entry(service->get_hostname(), _dns_recheck_ip, service->get_actual_ip());
230 if (!_dns_recheck_ip.empty())
231 dns_recheck_ip = _dns_recheck_ip;
234 // In case the local hosts IP (ip) differ from the IP set in the last update (actual_ip) or
235 // the IP of the dns record (dns_recheck_ip) differs from the IP of the local host (ip)
236 // then perform an update. This implies that the update is not performed if actual_ip == dns_recheck_ip.
237 // Special case when latupdated == 0 then only perform the update if dns_rechek_ip differs from ip
238 if ( ((service->get_actual_ip() != ip) && (lastupdated != 0)) || (dns_recheck_ip != ip) )
239 service->update(ip,current_time);
240 else if ( (service->get_actual_ip() != ip) && (lastupdated == 0) )
241 service->set_actual_ip(ip);
248 * Getter for member ServiceHolder.
249 * @return ServiceHolder
251 Serviceholder::Ptr Updater::get_service_holder() const
253 return ServiceHolder;