From: Guilherme Maciel Ferreira Date: Wed, 4 May 2011 13:29:20 +0000 (+0200) Subject: Implemented the feature: X-Git-Tag: v1.0~24 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=1634f2a1c2bc1b5e5833b53eb91381b98fbe4f37;p=pingcheck Implemented the feature: - Make the link down interval also configurable. Today it is possible just to configure the amount of time the link must to be up to notify. Do another configuration to allow the time of link down. --- diff --git a/Readme b/Readme index e0950ac..2910e20 100644 --- a/Readme +++ b/Readme @@ -65,8 +65,10 @@ This configurations are shared among and affect all the hosts. - status-notifier-cmd: the command line that is called when a host is down, or up. Accepted variables are: ${status} - down or up -- link-up-interval: how long (in minutes) the pings don't have to fail - in order to consider the link up, or stable. +- link-up-interval: how long (in minutes) the pings must be returned with + success in order to consider the link up, or stable. +- link-down-interval: how long (in minutes) the pings must fail, in order to the + application consider the link down. 2.2. Host diff --git a/TODO b/TODO index 5eceba4..6d586e7 100644 --- a/TODO +++ b/TODO @@ -29,10 +29,6 @@ Guilherme: - Documentation of the functions (doxygen) -- Make the link down interval also configurable. Today it is possible just to - configure the amount of time the link must to be up to notify. Do another - configuration to allow the time of link down. - - The interval between each ping to the same host is 1 second later than in the configuration file. For example, a ping configured to be performed each 5 seconds takes 6. diff --git a/src/config/configuration.cpp b/src/config/configuration.cpp index 353a73d..552109f 100644 --- a/src/config/configuration.cpp +++ b/src/config/configuration.cpp @@ -21,6 +21,7 @@ Configuration::Configuration() : MaxPingFailLimit( 100 ), StatusNotifierCmd( "" ), LinkUpIntervalInMin( 0 ), + LinkDownIntervalInMin( 0 ), MinStableLinkIntervalInMin( 0 ), MaxStableLinkIntervalInMin( 60 ), Hosts() @@ -123,6 +124,18 @@ void Configuration::set_link_up_interval_in_min( const int link_up_interval_in_m LinkUpIntervalInMin = link_up_interval_in_min; } +int Configuration::get_link_down_interval_in_min() const +{ + return LinkDownIntervalInMin; +} + +void Configuration::set_link_down_interval_in_min( const int link_down_interval_in_min ) +{ + BOOST_ASSERT( ( MinStableLinkIntervalInMin <= link_down_interval_in_min ) && ( link_down_interval_in_min <= MaxStableLinkIntervalInMin ) ); + + LinkDownIntervalInMin = link_down_interval_in_min; +} + HostList Configuration::get_hosts() const { return Hosts; diff --git a/src/config/configuration.h b/src/config/configuration.h index b152aa2..0d24dfc 100644 --- a/src/config/configuration.h +++ b/src/config/configuration.h @@ -46,6 +46,9 @@ public: int get_link_up_interval_in_min() const; void set_link_up_interval_in_min( const int link_up_interval_in_min ); + int get_link_down_interval_in_min() const; + void set_link_down_interval_in_min( const int link_down_interval_in_min ); + HostList get_hosts() const; void set_hosts( const HostList &hosts_list ); @@ -62,6 +65,7 @@ private: int MaxPingFailLimit; std::string StatusNotifierCmd; int LinkUpIntervalInMin; + int LinkDownIntervalInMin; int MinStableLinkIntervalInMin; int MaxStableLinkIntervalInMin; HostList Hosts; diff --git a/src/config/configurationreader.cpp b/src/config/configurationreader.cpp index 0f11c1d..5ec85c8 100644 --- a/src/config/configurationreader.cpp +++ b/src/config/configurationreader.cpp @@ -48,6 +48,9 @@ ConfigurationReader::ConfigurationReader() : DefaultLinkUpIntervalInMin( 5 ), // 5 minutes LinkUpIntervalCmdStr( "link-up-interval" ), LinkUpIntervalCmdDesc( "How long the link must be responsive in order to consider it stable." ), + DefaultLinkDownIntervalInMin( 2 ), // 2 minutes + LinkDownIntervalCmdStr( "link-down-interval" ), + LinkDownIntervalCmdDesc( "How long the link must be offline in order to consider it down." ), HostNameCmdStr( "host.name" ), HostNameCmdDesc( "Host address" ), DefaultHostIntervalInSec( 60 ), // 60 seconds @@ -175,6 +178,7 @@ options_description ConfigurationReader::get_configuration_options() const ( PingFailLimitCmdStr.c_str(), value()->default_value( DefaultPingFailLimit ), PingFailLimitCmdDesc.c_str() ) ( StatusNotifierCmdCmdStr.c_str(), value(), StatusNotifierCmdCmdDesc.c_str() ) ( LinkUpIntervalCmdStr.c_str(), value()->default_value( DefaultLinkUpIntervalInMin ), LinkUpIntervalCmdDesc.c_str() ) + ( LinkDownIntervalCmdStr.c_str(), value()->default_value( DefaultLinkDownIntervalInMin ), LinkDownIntervalCmdDesc.c_str() ) ( HostNameCmdStr.c_str(), value< vector >(), HostNameCmdDesc.c_str() ) ( HostIntervalCmdStr.c_str(), value< vector >(), HostIntervalCmdDesc.c_str() ) ; @@ -246,6 +250,16 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm ) << link_up_interval_in_min << endl; } + // link-down-interval + if ( vm.count( LinkDownIntervalCmdStr ) > 0 ) + { + int link_down_interval_in_min = vm[ LinkDownIntervalCmdStr ].as(); + Config.set_link_down_interval_in_min( link_down_interval_in_min ); + + GlobalLogger.info() << LinkDownIntervalCmdStr << "=" + << link_down_interval_in_min << endl; + } + // [host] name size_t hosts_names_count = 0; if ( vm.count( HostNameCmdStr ) > 0 ) diff --git a/src/config/configurationreader.h b/src/config/configurationreader.h index 68f7f43..084ee46 100644 --- a/src/config/configurationreader.h +++ b/src/config/configurationreader.h @@ -78,6 +78,9 @@ private: const int DefaultLinkUpIntervalInMin; const std::string LinkUpIntervalCmdStr; const std::string LinkUpIntervalCmdDesc; + const int DefaultLinkDownIntervalInMin; + const std::string LinkDownIntervalCmdStr; + const std::string LinkDownIntervalCmdDesc; const std::string HostNameCmdStr; const std::string HostNameCmdDesc; const int DefaultHostIntervalInSec; diff --git a/src/link/linkstatusanalyzer.cpp b/src/link/linkstatusanalyzer.cpp index f01b6bf..cf7a637 100644 --- a/src/link/linkstatusanalyzer.cpp +++ b/src/link/linkstatusanalyzer.cpp @@ -22,11 +22,13 @@ typedef lock_guard mutex_lock_guard; LinkStatusAnalyzer::LinkStatusAnalyzer( const int hosts_down_limit, const int link_up_interval_in_min, + const int link_down_interval_in_min, const string &status_notifier_cmd ) : HostsDownLimit( hosts_down_limit ), HostsDownList(), LinkUpIntervalInMin( link_up_interval_in_min ), + LinkDownIntervalInMin( link_down_interval_in_min ), CurrentLinkStatus( LinkStatus_Down ), CurrentNotificationStatus( NotificationStatus_NotReported ), TimeLinkStatusChanged( microsec_clock::universal_time() ), @@ -35,6 +37,7 @@ LinkStatusAnalyzer::LinkStatusAnalyzer( { BOOST_ASSERT( 0 <= hosts_down_limit ); BOOST_ASSERT( 0 <= link_up_interval_in_min ); + BOOST_ASSERT( 0 <= link_down_interval_in_min ); } LinkStatusAnalyzer::~LinkStatusAnalyzer() @@ -105,8 +108,8 @@ void LinkStatusAnalyzer::notify_link_up() { set_link_status( LinkStatus_Up ); - // report the link status only if it is up longer than a configured amount - // of time and if we haven't reported the new status yet + // report the link status only if: it is up longer than a configured amount + // of time, and if we haven't reported the new status yet if ( is_link_up_enough_time() && can_report_link_status() ) { BOOST_ASSERT( CurrentLinkStatus == LinkStatus_Up ); @@ -130,7 +133,9 @@ void LinkStatusAnalyzer::notify_link_down() { set_link_status( LinkStatus_Down ); - if ( can_report_link_status() ) + // report the link status only if: it is down longer than a configured amount + // of time, and if we haven't reported the new status yet + if ( is_link_down_enough_time() && can_report_link_status() ) { BOOST_ASSERT( CurrentLinkStatus == LinkStatus_Down ); BOOST_ASSERT( CurrentNotificationStatus == NotificationStatus_NotReported ); @@ -166,6 +171,23 @@ bool LinkStatusAnalyzer::is_link_up_enough_time() const return false; } +bool LinkStatusAnalyzer::is_link_down_enough_time() const +{ + if ( CurrentLinkStatus == LinkStatus_Down ) + { + ptime now = microsec_clock::universal_time(); + long amount_time_link_is_down = (now - TimeLinkStatusChanged).total_seconds(); + long link_down_interval_in_sec = LinkDownIntervalInMin * 60; + + if ( amount_time_link_is_down >= link_down_interval_in_sec ) + { + return true; + } + } + + return false; +} + bool LinkStatusAnalyzer::can_report_link_status() const { return ( CurrentNotificationStatus == NotificationStatus_NotReported ); diff --git a/src/link/linkstatusanalyzer.h b/src/link/linkstatusanalyzer.h index 68aeb3f..b872bd9 100644 --- a/src/link/linkstatusanalyzer.h +++ b/src/link/linkstatusanalyzer.h @@ -25,6 +25,7 @@ public: LinkStatusAnalyzer( const int hosts_down_limit, const int link_up_interval_in_min, + const int link_down_interval_in_min, const std::string &status_notifier_cmd ); ~LinkStatusAnalyzer(); @@ -55,6 +56,7 @@ private: void notify_link_down(); bool is_link_up_enough_time() const; + bool is_link_down_enough_time() const; bool can_report_link_status() const; void set_link_status( @@ -68,6 +70,8 @@ private: std::set HostsDownList; /// interval the link have to be stable in order to consider it is functional const int LinkUpIntervalInMin; + /// interval the link have to be down in order to consider it is non-functional + const int LinkDownIntervalInMin; /// keep track of the actual link status LinkStatusAnalyzer::LinkStatus CurrentLinkStatus; /// indicates if the last link status change was notified diff --git a/src/main.cpp b/src/main.cpp index ea440d9..d817b5a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,11 +47,13 @@ LinkStatusAnalyzerItem get_status_notifier( { int hosts_down_limit = configuration->get_hosts_down_limit(); int link_up_interval_in_min = configuration->get_link_up_interval_in_min(); + int link_down_interval_in_min = configuration->get_link_down_interval_in_min(); string status_notifier_cmd = configuration->get_status_notifier_cmd(); LinkStatusAnalyzerItem link_analyzer( new LinkStatusAnalyzer( hosts_down_limit, link_up_interval_in_min, + link_down_interval_in_min, status_notifier_cmd ) );