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 Log = Logger::Ptr(new Logger);
31 // initialize Serviceholder
32 ServiceHolder = Serviceholder::Ptr(new Serviceholder(Log));
35 Conf = Config::Ptr(new Config(Log,ServiceHolder));
49 * Load config and initialize helper classes.
50 * @param argc Number of arguments in array
51 * @param argv Array with cmd options
53 int Updater::load_config(int argc, char *argv[])
55 // load the cmd options
56 if ( init_config_from_cmd(argc,argv) != 0 )
59 // load the config and service files
60 if ( init_config_from_files() != 0 )
63 // init all helper classes
64 if ( init_helper_classes() != 0 )
72 * Reloading the config. Delete all Service objects and then init new Service objects from config files.
74 int Updater::reload_config()
76 // serialize all service objects
77 if ( ServiceHolder->serialize_services() != 0 )
80 // delete all service objects
81 ServiceHolder->delete_services();
83 // delete the actual Variables_map, perhaps with old cmd options which would overwrite new config file options.
84 Conf->delete_variables_map();
86 // load only config files
87 if ( init_config_from_files() != 0 )
90 // init all helper classes
91 if ( init_helper_classes() != 0 )
99 * Parse the command line arguments and initialize corresponding options.
100 * @param argc Number command line arguments.
101 * @param argv[] Array with arguments.
102 * @return 0 if cmd options successfully parsed, 1 if usage or version.
104 int Updater::init_config_from_cmd(int argc, char *argv[]) const
106 // Load the command line parameters
107 if( Conf->parse_cmd_line( argc, argv) != 0)
110 // If we have loaded the cmd options we need to init the log facility immediately in case debugging is enabled from cmd.
114 Log->print_cmd_parsed();
120 * Load the main config and the service definition files in config path.
121 * @return 0 if all is fine,
123 int Updater::init_config_from_files() const
125 // Load the main and service config files in config path
126 if ( Conf->load_config_from_files() != 0 )
129 // Re-init log facility, perhaps new config file options for logger are set.
130 // These config file options will only overwrite the cmd options if the SIGHUP (reload config) is caught.
133 // Here we are. All Service Objects should be initialized, so it is time to deserialize the old service objects and compare them.
134 if ( ServiceHolder->deserialize_services() != 0 )
138 Log->print_conf_files_parsed();
144 * Init all Helper classes
147 int Updater::init_helper_classes()
149 // Initialize IPHelper
150 if ( init_ip_helper() != 0 )
158 * Init the IPHelp member with needed values.
159 * @return 0 if all is fine, -1 otherwise.
161 int Updater::init_ip_helper()
163 // Try to get deserialized IPAddrHelper from ServiceHolder
164 IPAddrHelper::Ptr _ip_addr_help = ServiceHolder->get_ip_addr_helper();
165 if ( _ip_addr_help.use_count() != 0 )
167 // Initialize IPHelper
168 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() ) );
172 // IPAddrHelper from ServiceHolder was not declared, so init oen with LastWebcheck 0
173 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() ) );
176 // Put the IPAddrHelper into ServiceHolder, so the LastWebcheck state will be serialized too.
177 ServiceHolder->set_ip_addr_helper(IPAddrHelp);
184 * Getter for member Config.
185 * @return Member Config.
187 Config::Ptr Updater::get_config() const
194 * Getter for member Logger.
195 * @return Member Logger.
197 Logger::Ptr Updater::get_logger() const
204 * Initialize the logging facility with loglevel and syslog.
206 void Updater::init_log_facility() const
208 Log->set_log_facility(Conf->get_loglevel(),Conf->get_syslog(),Conf->get_external_warning_log(),Conf->get_external_warning_level());
209 Log->print_init_log_facility();
214 * Update all configured services.
216 void Updater::update_services() const
218 // Get all services from the ServiceHolder.
219 list<Service::Ptr> services = ServiceHolder->get_services();
221 // Get the actual IP of this host.
222 string ip_host = IPAddrHelp->get_actual_ip();
224 if ( !ip_host.empty() )
226 BOOST_FOREACH(Service::Ptr &service, services )
228 string ip_last_update = service->get_actual_ip();
229 string hostname = service->get_hostname();
230 time_t lastupdated = 0;
231 time_t current_time = time(NULL);
233 // Try to get the lastupdated time of the actual service if there is one.
234 if ( service->get_last_updates().size() > 0 )
235 lastupdated = service->get_last_updates().front(); /*lint !e1793 */
237 Log->print_check_service_update(hostname, current_time, lastupdated);
239 // Do a DNS Query for the dynamic hostname.
240 string ip_dns_recheck = IPAddrHelp->dns_query(hostname);
242 Log->print_cached_dns_entry(hostname, ip_dns_recheck, ip_last_update, ip_host);
244 // Test if the DNS-Record could not be found.
245 if ( ip_dns_recheck.empty() )
247 // Next in BOOST_FOREACH
249 // Test if the actual DNS-Record differs from the host's IP.
250 else if ( ip_host != ip_dns_recheck )
252 // Check if the service will be updated for the first time.
253 if ( lastupdated == 0 )
255 // Update for the firt time.
256 Log->print_update_service_firttime(hostname, ip_dns_recheck, ip_host);
257 service->update(ip_host,current_time);
261 // We already have updated, check if the IP set in last update differs from the actual host's ip.
262 if ( ip_last_update != ip_host )
265 Log->print_update_service(hostname, ip_dns_recheck, ip_last_update, ip_host, lastupdated);
266 service->update(ip_host,current_time);
270 int dns_cache_ttl = service->get_dns_cache_ttl();
271 // Check if DNS Cache TTL is expired, if so, then update the same IP again.
272 if ( (lastupdated + dns_cache_ttl) < current_time )
275 Log->print_update_service_ttl_expired(hostname, ip_dns_recheck, ip_last_update, ip_host, lastupdated, dns_cache_ttl, current_time);
276 service->update(ip_host,current_time);
280 // DNS cache TTL isn't expired
281 Log->print_update_service_ttl_not_expired(hostname, ip_dns_recheck, ip_last_update, ip_host, lastupdated, dns_cache_ttl, current_time);
288 Log->print_no_update_needed(hostname, ip_dns_recheck, ip_last_update, ip_host, lastupdated);
294 Log->print_no_wan_ip();
300 * Getter for member ServiceHolder.
301 * @return ServiceHolder
303 Serviceholder::Ptr Updater::get_service_holder() const
305 return ServiceHolder;