2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
20 #include "config/configuration.h"
23 #include <ctime> // for seeding random number generator
24 #include <boost/math/special_functions/round.hpp>
25 #include <boost/numeric/conversion/cast.hpp>
26 #include <boost/foreach.hpp>
27 #include <boost/random/uniform_int.hpp>
28 #include <boost/random/variate_generator.hpp>
30 #include "boost_assert_handler.h"
33 using I2n::Logger::LogLevel;
34 using I2n::Logger::GlobalLogger;
36 typedef boost::uniform_int<> rand_dist_type;
37 typedef boost::variate_generator<rand_gen_type&, rand_dist_type> rand_var_type;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 Configuration::Configuration() :
45 LoggingLevel( LogLevel::Error ),
46 LoggingOutput( LogOutput_SYSLOG ),
49 SourceNetworkInterface( "" ),
52 MinHostsDownLimit( 0 ),
53 MaxHostsDownLimit( 50 ),
55 MinPingFailLimit( 0 ),
56 MaxPingFailLimit( 100 ),
57 StatusNotifierCmd( "" ),
58 LinkUpIntervalInMin( 0 ),
59 LinkDownIntervalInMin( 0 ),
60 MinStableLinkIntervalInMin( 0 ),
61 MaxStableLinkIntervalInMin( 60 ),
62 PingReplyTimeout( 30 ),
63 MaxAddressResolutionAttempts( 10 ),
64 ResolvedIpTtlThreshold( 10 ),
65 MinTimeBetweenResolves( 10 ),
68 RatioRandomHosts( 1.0f ),
69 PrintVersion( false ),
70 RandomNumberGenerator( boost::numeric_cast<unsigned int>(time(0)) )
74 Configuration::~Configuration()
78 bool Configuration::get_daemon() const
83 void Configuration::set_daemon( bool daemon )
88 LogLevel Configuration::get_log_level() const
93 void Configuration::set_log_level( const LogLevel &log_level )
95 BOOST_ASSERT( (LogLevel::Emergency <= log_level) && (log_level <= LogLevel::Debug) );
97 this->LoggingLevel = log_level;
100 LogOutput Configuration::get_log_output() const
102 return LoggingOutput;
105 void Configuration::set_log_output( const LogOutput &log_output )
107 BOOST_ASSERT( (LogOutput_First <= log_output) && (log_output <= LogOutput_Last) );
109 this->LoggingOutput = log_output;
112 string Configuration::get_log_file() const
117 void Configuration::set_log_file( const std::string &log_file )
119 BOOST_ASSERT( !log_file.empty() );
121 this->LogFileName = log_file;
124 string Configuration::get_config_file_name() const
126 return ConfigFileName;
129 void Configuration::set_config_file_name( const string &config_file_name )
131 BOOST_ASSERT( !config_file_name.empty() );
133 this->ConfigFileName = config_file_name;
136 string Configuration::get_nameserver() const
141 void Configuration::set_nameserver( const string &nameserver )
143 NameServer = nameserver;
146 string Configuration::get_source_network_interface() const
148 return SourceNetworkInterface;
151 void Configuration::set_source_network_interface(
152 const string &source_network_interface
155 SourceNetworkInterface = source_network_interface;
158 int Configuration::get_hosts_down_limit() const
160 return HostsDownLimit;
163 void Configuration::set_hosts_down_limit( const int hosts_down_limit )
165 BOOST_ASSERT( ( MinHostsDownLimit <= hosts_down_limit ) && ( hosts_down_limit <= MaxHostsDownLimit ) );
167 this->HostsDownLimit = hosts_down_limit;
170 int Configuration::get_ping_fail_limit() const
172 return PingFailLimit;
175 void Configuration::set_ping_fail_limit( const int ping_fail_limit )
177 BOOST_ASSERT( ( MinPingFailLimit <= ping_fail_limit ) && ( ping_fail_limit <= MaxPingFailLimit ) );
179 PingFailLimit = ping_fail_limit;
182 string Configuration::get_status_notifier_cmd() const
184 return StatusNotifierCmd;
187 void Configuration::set_status_notifier_cmd( const string &status_notifier_cmd )
189 BOOST_ASSERT( !status_notifier_cmd.empty() );
191 StatusNotifierCmd = status_notifier_cmd;
194 int Configuration::get_link_up_interval_in_min() const
196 return LinkUpIntervalInMin;
199 void Configuration::set_link_up_interval_in_min(
200 const int link_up_interval_in_min
203 BOOST_ASSERT( ( MinStableLinkIntervalInMin <= link_up_interval_in_min ) && ( link_up_interval_in_min <= MaxStableLinkIntervalInMin ) );
205 LinkUpIntervalInMin = link_up_interval_in_min;
208 int Configuration::get_link_down_interval_in_min() const
210 return LinkDownIntervalInMin;
213 void Configuration::set_link_down_interval_in_min(
214 const int link_down_interval_in_min
217 BOOST_ASSERT( ( MinStableLinkIntervalInMin <= link_down_interval_in_min ) && ( link_down_interval_in_min <= MaxStableLinkIntervalInMin ) );
219 LinkDownIntervalInMin = link_down_interval_in_min;
222 int Configuration::get_ping_reply_timeout() const
224 return PingReplyTimeout;
226 void Configuration::set_ping_reply_timeout( const int ping_reply_timeout )
228 BOOST_ASSERT(ping_reply_timeout > 0 );
229 PingReplyTimeout = ping_reply_timeout;
232 int Configuration::get_max_address_resolution_attempts() const
234 return MaxAddressResolutionAttempts;
236 void Configuration::set_max_address_resolution_attempts( const int max_address_resolution_attempts )
238 BOOST_ASSERT(max_address_resolution_attempts > 0 );
239 MaxAddressResolutionAttempts = max_address_resolution_attempts;
242 int Configuration::get_resolved_ip_ttl_threshold() const
244 return ResolvedIpTtlThreshold;
246 void Configuration::set_resolved_ip_ttl_threshold( const int resolved_ip_ttl_threshold )
248 BOOST_ASSERT(resolved_ip_ttl_threshold >= 0 );
249 ResolvedIpTtlThreshold = resolved_ip_ttl_threshold;
252 int Configuration::get_min_time_between_resolves() const
254 return MinTimeBetweenResolves;
256 void Configuration::set_min_time_between_resolves( const int min_time_between_resolves )
258 BOOST_ASSERT(min_time_between_resolves >= 0 );
259 MinTimeBetweenResolves = min_time_between_resolves;
262 void Configuration::set_dns_cache_file( const std::string &dns_cache_file)
264 BOOST_ASSERT( !dns_cache_file.empty() );
265 DnsCacheFile = dns_cache_file;
268 std::string Configuration::get_dns_cache_file() const
273 float Configuration::get_ratio_random_hosts() const
275 return RatioRandomHosts;
278 void Configuration::set_ratio_random_hosts( const float ratio )
280 BOOST_ASSERT( (ratio > 0.0f) && (ratio <= 1.0f) );
281 RatioRandomHosts = ratio;
284 HostList Configuration::get_hosts() const
289 void Configuration::set_hosts( const HostList &hosts_list )
294 bool Configuration::get_print_version()
299 void Configuration::set_print_version( const bool do_print )
301 PrintVersion = do_print;
304 int Configuration::get_random_number(const int lowest, const int highest)
306 rand_dist_type random_distribution(lowest, highest);
307 rand_var_type random_variate(RandomNumberGenerator, random_distribution);
308 return random_variate();
312 * @brief select RatioRandomHosts of the hosts at random.
313 * changes internal list of hosts
314 * @returns false if could not randomize (e.g. only 1 host
315 * available or ratio=1)
317 bool Configuration::randomize_hosts()
319 // need this a few times
320 unsigned int n_hosts = Hosts.size();
321 float n_hosts_float = boost::numeric_cast<float>(n_hosts);
323 // check if RatioRandomHosts == 1
324 if ((1. - RatioRandomHosts) < 0.5/n_hosts_float)
327 // determine number of hosts to keep
328 unsigned int n_wanted = boost::math::iround(n_hosts_float * RatioRandomHosts);
330 n_wanted = 1; // want at least 1 host
331 if (n_wanted == n_hosts) // e.g. always the case if only 1 host given
334 GlobalLogger.info() << "randomizing hosts: keeping "
335 << n_wanted << " from overall " << n_hosts;
337 BOOST_ASSERT(n_wanted <= n_hosts); // just to be sure
339 // create new set of hosts (more efficient than removal from std::vector Hosts)
340 std::set<HostItem> new_hosts;
343 // add hosts at random until size is good
344 rand_dist_type random_distribution(0, n_hosts-1);
345 rand_var_type random_variate(RandomNumberGenerator, random_distribution);
346 while (new_hosts.size() < n_wanted)
348 take_idx = random_variate();
349 new_hosts.insert( Hosts[take_idx] ); // does nothing if host is already in there
352 // convert from set to list
354 BOOST_FOREACH(HostItem host, new_hosts)
355 new_list.push_back(host);
357 this->Hosts = new_list;