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 ),
48 SourceNetworkInterface( "" ),
51 MinHostsDownLimit( 0 ),
52 MaxHostsDownLimit( 50 ),
54 MinPingFailLimit( 0 ),
55 MaxPingFailLimit( 100 ),
56 StatusNotifierCmd( "" ),
57 LinkUpIntervalInMin( 0 ),
58 LinkDownIntervalInMin( 0 ),
59 MinStableLinkIntervalInMin( 0 ),
60 MaxStableLinkIntervalInMin( 60 ),
61 PingReplyTimeout( 30 ),
62 MaxAddressResolutionAttempts( 10 ),
63 ResolvedIpTtlThreshold( 10 ),
66 RatioRandomHosts( 1.0f ),
67 PrintVersion( false ),
68 RandomNumberGenerator( boost::numeric_cast<unsigned int>(time(0)) )
72 Configuration::~Configuration()
76 bool Configuration::get_daemon() const
81 void Configuration::set_daemon( bool daemon )
86 LogLevel Configuration::get_log_level() const
91 void Configuration::set_log_level( const LogLevel &log_level )
93 BOOST_ASSERT( (LogLevel::Emergency <= log_level) && (log_level <= LogLevel::Debug) );
95 this->LoggingLevel = log_level;
98 LogOutput Configuration::get_log_output() const
100 return LoggingOutput;
103 void Configuration::set_log_output( const LogOutput &log_output )
105 BOOST_ASSERT( (LogOutput_First <= log_output) && (log_output <= LogOutput_Last) );
107 this->LoggingOutput = log_output;
110 string Configuration::get_config_file_name() const
112 return ConfigFileName;
115 void Configuration::set_config_file_name( const string &config_file_name )
117 BOOST_ASSERT( !config_file_name.empty() );
119 this->ConfigFileName = config_file_name;
122 string Configuration::get_nameserver() const
127 void Configuration::set_nameserver( const string &nameserver )
129 NameServer = nameserver;
132 string Configuration::get_source_network_interface() const
134 return SourceNetworkInterface;
137 void Configuration::set_source_network_interface(
138 const string &source_network_interface
141 SourceNetworkInterface = source_network_interface;
144 int Configuration::get_hosts_down_limit() const
146 return HostsDownLimit;
149 void Configuration::set_hosts_down_limit( const int hosts_down_limit )
151 BOOST_ASSERT( ( MinHostsDownLimit <= hosts_down_limit ) && ( hosts_down_limit <= MaxHostsDownLimit ) );
153 this->HostsDownLimit = hosts_down_limit;
156 int Configuration::get_ping_fail_limit() const
158 return PingFailLimit;
161 void Configuration::set_ping_fail_limit( const int ping_fail_limit )
163 BOOST_ASSERT( ( MinPingFailLimit <= ping_fail_limit ) && ( ping_fail_limit <= MaxPingFailLimit ) );
165 PingFailLimit = ping_fail_limit;
168 string Configuration::get_status_notifier_cmd() const
170 return StatusNotifierCmd;
173 void Configuration::set_status_notifier_cmd( const string &status_notifier_cmd )
175 BOOST_ASSERT( !status_notifier_cmd.empty() );
177 StatusNotifierCmd = status_notifier_cmd;
180 int Configuration::get_link_up_interval_in_min() const
182 return LinkUpIntervalInMin;
185 void Configuration::set_link_up_interval_in_min(
186 const int link_up_interval_in_min
189 BOOST_ASSERT( ( MinStableLinkIntervalInMin <= link_up_interval_in_min ) && ( link_up_interval_in_min <= MaxStableLinkIntervalInMin ) );
191 LinkUpIntervalInMin = link_up_interval_in_min;
194 int Configuration::get_link_down_interval_in_min() const
196 return LinkDownIntervalInMin;
199 void Configuration::set_link_down_interval_in_min(
200 const int link_down_interval_in_min
203 BOOST_ASSERT( ( MinStableLinkIntervalInMin <= link_down_interval_in_min ) && ( link_down_interval_in_min <= MaxStableLinkIntervalInMin ) );
205 LinkDownIntervalInMin = link_down_interval_in_min;
208 int Configuration::get_ping_reply_timeout() const
210 return PingReplyTimeout;
212 void Configuration::set_ping_reply_timeout( const int ping_reply_timeout )
214 BOOST_ASSERT(ping_reply_timeout > 0 );
215 PingReplyTimeout = ping_reply_timeout;
218 int Configuration::get_max_address_resolution_attempts() const
220 return MaxAddressResolutionAttempts;
222 void Configuration::set_max_address_resolution_attempts( const int max_address_resolution_attempts )
224 BOOST_ASSERT(max_address_resolution_attempts > 0 );
225 MaxAddressResolutionAttempts = max_address_resolution_attempts;
228 int Configuration::get_resolved_ip_ttl_threshold() const
230 return ResolvedIpTtlThreshold;
232 void Configuration::set_resolved_ip_ttl_threshold( const int resolved_ip_ttl_threshold )
234 BOOST_ASSERT(resolved_ip_ttl_threshold >= 0 );
235 ResolvedIpTtlThreshold = resolved_ip_ttl_threshold;
238 void Configuration::set_dns_cache_file( const std::string &dns_cache_file)
240 BOOST_ASSERT( !dns_cache_file.empty() );
241 DnsCacheFile = dns_cache_file;
244 std::string Configuration::get_dns_cache_file() const
249 float Configuration::get_ratio_random_hosts() const
251 return RatioRandomHosts;
254 void Configuration::set_ratio_random_hosts( const float ratio )
256 BOOST_ASSERT( (ratio > 0.0f) && (ratio <= 1.0f) );
257 RatioRandomHosts = ratio;
260 HostList Configuration::get_hosts() const
265 void Configuration::set_hosts( const HostList &hosts_list )
270 bool Configuration::get_print_version()
275 void Configuration::set_print_version( const bool do_print )
277 PrintVersion = do_print;
280 int Configuration::get_random_number(const int lowest, const int highest)
282 rand_dist_type random_distribution(lowest, highest);
283 rand_var_type random_variate(RandomNumberGenerator, random_distribution);
284 return random_variate();
288 * @brief select RatioRandomHosts of the hosts at random.
289 * changes internal list of hosts
290 * @returns false if could not randomize (e.g. only 1 host
291 * available or ratio=1)
293 bool Configuration::randomize_hosts()
295 // need this a few times
296 unsigned int n_hosts = Hosts.size();
297 float n_hosts_float = boost::numeric_cast<float>(n_hosts);
299 // check if RatioRandomHosts == 1
300 if ((1. - RatioRandomHosts) < 0.5/n_hosts_float)
303 // determine number of hosts to keep
304 unsigned int n_wanted = boost::math::iround(n_hosts_float * RatioRandomHosts);
306 n_wanted = 1; // want at least 1 host
307 if (n_wanted == n_hosts) // e.g. always the case if only 1 host given
310 GlobalLogger.info() << "randomizing hosts: keeping "
311 << n_wanted << " from overall " << n_hosts;
313 BOOST_ASSERT(n_wanted <= n_hosts); // just to be sure
315 // create new set of hosts (more efficient than removal from std::vector Hosts)
316 std::set<HostItem> new_hosts;
319 // add hosts at random until size is good
320 rand_dist_type random_distribution(0, n_hosts-1);
321 rand_var_type random_variate(RandomNumberGenerator, random_distribution);
322 while (new_hosts.size() < n_wanted)
324 take_idx = random_variate();
325 new_hosts.insert( Hosts[take_idx] ); // does nothing if host is already in there
328 // convert from set to list
330 BOOST_FOREACH(HostItem host, new_hosts)
331 new_list.push_back(host);
333 this->Hosts = new_list;