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 ),
67 RatioRandomHosts( 1.0f ),
68 PrintVersion( false ),
69 RandomNumberGenerator( boost::numeric_cast<unsigned int>(time(0)) )
73 Configuration::~Configuration()
77 bool Configuration::get_daemon() const
82 void Configuration::set_daemon( bool daemon )
87 LogLevel Configuration::get_log_level() const
92 void Configuration::set_log_level( const LogLevel &log_level )
94 BOOST_ASSERT( (LogLevel::Emergency <= log_level) && (log_level <= LogLevel::Debug) );
96 this->LoggingLevel = log_level;
99 LogOutput Configuration::get_log_output() const
101 return LoggingOutput;
104 void Configuration::set_log_output( const LogOutput &log_output )
106 BOOST_ASSERT( (LogOutput_First <= log_output) && (log_output <= LogOutput_Last) );
108 this->LoggingOutput = log_output;
111 string Configuration::get_log_file() const
116 void Configuration::set_log_file( const std::string &log_file )
118 BOOST_ASSERT( !log_file.empty() );
120 this->LogFileName = log_file;
123 string Configuration::get_config_file_name() const
125 return ConfigFileName;
128 void Configuration::set_config_file_name( const string &config_file_name )
130 BOOST_ASSERT( !config_file_name.empty() );
132 this->ConfigFileName = config_file_name;
135 string Configuration::get_nameserver() const
140 void Configuration::set_nameserver( const string &nameserver )
142 NameServer = nameserver;
145 string Configuration::get_source_network_interface() const
147 return SourceNetworkInterface;
150 void Configuration::set_source_network_interface(
151 const string &source_network_interface
154 SourceNetworkInterface = source_network_interface;
157 int Configuration::get_hosts_down_limit() const
159 return HostsDownLimit;
162 void Configuration::set_hosts_down_limit( const int hosts_down_limit )
164 BOOST_ASSERT( ( MinHostsDownLimit <= hosts_down_limit ) && ( hosts_down_limit <= MaxHostsDownLimit ) );
166 this->HostsDownLimit = hosts_down_limit;
169 int Configuration::get_ping_fail_limit() const
171 return PingFailLimit;
174 void Configuration::set_ping_fail_limit( const int ping_fail_limit )
176 BOOST_ASSERT( ( MinPingFailLimit <= ping_fail_limit ) && ( ping_fail_limit <= MaxPingFailLimit ) );
178 PingFailLimit = ping_fail_limit;
181 string Configuration::get_status_notifier_cmd() const
183 return StatusNotifierCmd;
186 void Configuration::set_status_notifier_cmd( const string &status_notifier_cmd )
188 BOOST_ASSERT( !status_notifier_cmd.empty() );
190 StatusNotifierCmd = status_notifier_cmd;
193 int Configuration::get_link_up_interval_in_min() const
195 return LinkUpIntervalInMin;
198 void Configuration::set_link_up_interval_in_min(
199 const int link_up_interval_in_min
202 BOOST_ASSERT( ( MinStableLinkIntervalInMin <= link_up_interval_in_min ) && ( link_up_interval_in_min <= MaxStableLinkIntervalInMin ) );
204 LinkUpIntervalInMin = link_up_interval_in_min;
207 int Configuration::get_link_down_interval_in_min() const
209 return LinkDownIntervalInMin;
212 void Configuration::set_link_down_interval_in_min(
213 const int link_down_interval_in_min
216 BOOST_ASSERT( ( MinStableLinkIntervalInMin <= link_down_interval_in_min ) && ( link_down_interval_in_min <= MaxStableLinkIntervalInMin ) );
218 LinkDownIntervalInMin = link_down_interval_in_min;
221 int Configuration::get_ping_reply_timeout() const
223 return PingReplyTimeout;
225 void Configuration::set_ping_reply_timeout( const int ping_reply_timeout )
227 BOOST_ASSERT(ping_reply_timeout > 0 );
228 PingReplyTimeout = ping_reply_timeout;
231 int Configuration::get_max_address_resolution_attempts() const
233 return MaxAddressResolutionAttempts;
235 void Configuration::set_max_address_resolution_attempts( const int max_address_resolution_attempts )
237 BOOST_ASSERT(max_address_resolution_attempts > 0 );
238 MaxAddressResolutionAttempts = max_address_resolution_attempts;
241 int Configuration::get_resolved_ip_ttl_threshold() const
243 return ResolvedIpTtlThreshold;
245 void Configuration::set_resolved_ip_ttl_threshold( const int resolved_ip_ttl_threshold )
247 BOOST_ASSERT(resolved_ip_ttl_threshold >= 0 );
248 ResolvedIpTtlThreshold = resolved_ip_ttl_threshold;
251 void Configuration::set_dns_cache_file( const std::string &dns_cache_file)
253 BOOST_ASSERT( !dns_cache_file.empty() );
254 DnsCacheFile = dns_cache_file;
257 std::string Configuration::get_dns_cache_file() const
262 float Configuration::get_ratio_random_hosts() const
264 return RatioRandomHosts;
267 void Configuration::set_ratio_random_hosts( const float ratio )
269 BOOST_ASSERT( (ratio > 0.0f) && (ratio <= 1.0f) );
270 RatioRandomHosts = ratio;
273 HostList Configuration::get_hosts() const
278 void Configuration::set_hosts( const HostList &hosts_list )
283 bool Configuration::get_print_version()
288 void Configuration::set_print_version( const bool do_print )
290 PrintVersion = do_print;
293 int Configuration::get_random_number(const int lowest, const int highest)
295 rand_dist_type random_distribution(lowest, highest);
296 rand_var_type random_variate(RandomNumberGenerator, random_distribution);
297 return random_variate();
301 * @brief select RatioRandomHosts of the hosts at random.
302 * changes internal list of hosts
303 * @returns false if could not randomize (e.g. only 1 host
304 * available or ratio=1)
306 bool Configuration::randomize_hosts()
308 // need this a few times
309 unsigned int n_hosts = Hosts.size();
310 float n_hosts_float = boost::numeric_cast<float>(n_hosts);
312 // check if RatioRandomHosts == 1
313 if ((1. - RatioRandomHosts) < 0.5/n_hosts_float)
316 // determine number of hosts to keep
317 unsigned int n_wanted = boost::math::iround(n_hosts_float * RatioRandomHosts);
319 n_wanted = 1; // want at least 1 host
320 if (n_wanted == n_hosts) // e.g. always the case if only 1 host given
323 GlobalLogger.info() << "randomizing hosts: keeping "
324 << n_wanted << " from overall " << n_hosts;
326 BOOST_ASSERT(n_wanted <= n_hosts); // just to be sure
328 // create new set of hosts (more efficient than removal from std::vector Hosts)
329 std::set<HostItem> new_hosts;
332 // add hosts at random until size is good
333 rand_dist_type random_distribution(0, n_hosts-1);
334 rand_var_type random_variate(RandomNumberGenerator, random_distribution);
335 while (new_hosts.size() < n_wanted)
337 take_idx = random_variate();
338 new_hosts.insert( Hosts[take_idx] ); // does nothing if host is already in there
341 // convert from set to list
343 BOOST_FOREACH(HostItem host, new_hosts)
344 new_list.push_back(host);
346 this->Hosts = new_list;