2 * @brief The updater class implementation. This class implements the updater logic.
6 * @copyright Intra2net AG
12 #include "serviceholder.h"
16 #include <boost/foreach.hpp>
19 // Following boost macros are needed for serialization of derived classes through a base class pointer (Service *).
20 BOOST_CLASS_EXPORT_GUID(ODS, "ODS")
21 BOOST_CLASS_EXPORT_GUID(DHS, "DHS")
23 namespace fs = boost::filesystem;
28 * Default constructor which initializes the member Conf.
31 : IPHelp(new IPHelper)
33 // Initialize program wide Logger, at this point we don't know where to log and with which loglevel.
34 Logger::Ptr _log(new Logger);
38 // initialize Serviceholder
39 Serviceholder::Ptr _serviceholder(new Serviceholder(Log));
40 ServiceHolder = _serviceholder;
41 _serviceholder.reset();
44 Config::Ptr _config(new Config(Log,ServiceHolder));
59 * Parse the command line arguments and initialize corresponding options.
60 * @param argc Number command line arguments.
61 * @param argv[] Array with arguments.
62 * @return 0 if cmd options successfully parsed, 1 if usage or version.
64 int Updater::init_config_from_cmd(int argc, char *argv[])
66 // Load the command line parameters
67 if( Conf->parse_cmd_line( argc, argv) != 0)
70 // If we have loaded the cmd options we need to init the log facility immediately in case debugging is enabled from cmd.
74 Log->print_cmd_parsed();
80 * Load the main config and the service definition files in config path.
81 * @return 0 if all is fine,
83 int Updater::init_config_from_files()
85 // Load the main and service config files in config path
86 if ( Conf->load_config_from_files() != 0 )
89 // Re-init log facility, perhaps new config file options for logger are set.
90 // These config file options will only overwrite the cmd options if the SIGHUP (reload config) is caught.
93 // Here we are. All Service Objects should be initialized, so it is time to deserialize the old service objects and compare them.
94 if ( ServiceHolder->deserialize_services() != 0 )
98 Log->print_conf_files_parsed();
104 * Getter for member Config.
105 * @return Member Config.
107 Config::Ptr Updater::get_config() const
114 * Getter for member Logger.
115 * @return Member Logger.
117 Logger::Ptr Updater::get_logger() const
124 * Init the IPHelp member with needed values.
125 * @return 0 if all is fine, -1 otherwise.
127 int Updater::init_ip_helper()
129 // initialize IPHelper
130 IPHelper::Ptr _iphelp(new IPHelper(Log,Conf->get_webcheck_ip_url(),Conf->get_webcheck_ip_url_alt(),Conf->get_enable_ipv6()));
139 * Reloading the config. Delete all Service objects and then init new Service objects from config files.
141 int Updater::reload_config()
143 // serialize all service objects
144 if ( ServiceHolder->serialize_services() != 0 )
147 // delete all service objects
148 ServiceHolder->delete_services();
150 // delete the actual Variables_map, perhaps with old cmd options which would overwrite new config file options.
151 Conf->delete_variables_map();
153 // load only config files
154 if ( init_config_from_files() != 0 )
162 * Initialize the logging facility with loglevel and syslog.
164 void Updater::init_log_facility()
166 Log->set_log_facility(Conf->get_loglevel(),Conf->get_syslog());
167 Log->print_init_log_facility();
172 * Update all configured services.
174 void Updater::update_services()
176 list<Service::Ptr> services = ServiceHolder->get_services();
178 string ip = IPHelp->get_actual_ip();
182 BOOST_FOREACH(Service::Ptr &service, services )
184 string dns_recheck_ip = ip;
185 int current_time = time(NULL);
188 if ( service->get_last_updates()->size() > 0 )
189 lastupdated = service->get_last_updates()->front();
191 if ( (lastupdated != 0) && ((lastupdated + service->get_dns_cache_ttl()) < current_time) )
192 dns_recheck_ip = IPHelp->dns_query(service->get_hostname());
194 if ( (service->get_actual_ip() != ip) || (dns_recheck_ip != ip) )
195 service->update(ip,current_time);
202 * Getter for member ServiceHolder.
203 * @return ServiceHolder
205 Serviceholder::Ptr Updater::get_service_holder() const
207 return ServiceHolder;