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 "host/hoststatus.h"
23 #include <logfunc.hpp>
25 #include "boost_assert_handler.h"
28 using I2n::Logger::GlobalLogger;
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
35 * @param host_address The address of the host it has to analyze.
36 * @param ping_fail_percentage_limit The percentage threshold of pings that can
38 * @param link_analyzer The object used to notify the status of the host.
40 HostStatus::HostStatus(
41 const string &host_address,
42 const int ping_fail_limit_percentage,
43 const LinkStatusItem link_analyzer
45 HostAddress( host_address ),
46 LinkAnalyzer( link_analyzer ),
47 PingFailLimitPercentage( ping_fail_limit_percentage ),
49 PingsPerformedCount( 0 ),
50 PingsFailedCount( 0 ),
51 ExceededPingFailedLimit( false )
53 BOOST_ASSERT( !HostAddress.empty() );
54 BOOST_ASSERT( ( 0 <= PingFailLimitPercentage ) && ( PingFailLimitPercentage <= 100 ) );
57 HostStatus::~HostStatus()
62 * @param resolved_ip_count The number of IPs resolved for the host.
64 void HostStatus::set_resolved_ip_count( const int resolved_ip_count )
66 BOOST_ASSERT( 1 <= resolved_ip_count );
68 if (resolved_ip_count != ResolvedIpCount)
69 { // assume that the target has changed --> reset counters
70 reset_ping_counters();
72 ResolvedIpCount = resolved_ip_count;
74 GlobalLogger.notice() << "Stat(" << HostAddress << "): "
75 << PingsFailedCount << " fail/" << PingsPerformedCount << " pings/"
76 << ResolvedIpCount << " IPs: #IPs set";
80 * @return true if the amount of failed pings given to the host exceeded the
83 bool HostStatus::exceeded_ping_failed_limit() const
85 return ExceededPingFailedLimit;
89 * Adds a ping status (success or failure).
92 void HostStatus::update_ping_statistics( bool ping_success )
94 GlobalLogger.notice() << "Stat(" << HostAddress << "): "
95 << PingsFailedCount << " fail/" << PingsPerformedCount << " pings/"
96 << ResolvedIpCount << " IPs: add ping with success=" << ping_success;
98 BOOST_ASSERT( 1 <= ResolvedIpCount );
99 BOOST_ASSERT( 0 <= PingsPerformedCount );
100 BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount );
102 increase_ping_performed_count();
106 increase_ping_failed_count();
109 analyze_ping_failed_count();
111 // after we tried all IPs resolved for this host, we can analyze how many
113 if ( tried_all_resolved_ip() )
115 analyze_ping_statistics();
117 reset_ping_counters();
120 BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount );
124 bool HostStatus::tried_all_resolved_ip() const
126 BOOST_ASSERT( ( 0 < PingsPerformedCount ) && ( PingsPerformedCount <= ResolvedIpCount ) );
128 return ( PingsPerformedCount == ResolvedIpCount );
131 void HostStatus::analyze_ping_statistics()
133 BOOST_ASSERT( !HostAddress.empty() );
134 BOOST_ASSERT( PingsPerformedCount == ResolvedIpCount );
136 // notify if the amount of pings that failed exceed the limit
137 if ( exceeded_ping_failed_limit() )
139 GlobalLogger.notice() << "Stat(" << HostAddress << "): "
140 << PingsFailedCount << " fail/" << PingsPerformedCount << " pings/"
141 << ResolvedIpCount << " IPs: notify down";
142 LinkAnalyzer->notify_host_down( HostAddress );
146 GlobalLogger.notice() << "Stat(" << HostAddress << "): "
147 << PingsFailedCount << " fail/" << PingsPerformedCount << " pings/"
148 << ResolvedIpCount << " IPs: notify up";
149 LinkAnalyzer->notify_host_up( HostAddress );
153 void HostStatus::reset_ping_counters()
155 PingsPerformedCount = 0;
156 PingsFailedCount = 0;
159 void HostStatus::increase_ping_performed_count()
161 ++PingsPerformedCount;
163 BOOST_ASSERT( ( 0 < PingsPerformedCount ) && ( PingsPerformedCount <= ResolvedIpCount ) );
166 void HostStatus::increase_ping_failed_count()
170 BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) );
173 void HostStatus::analyze_ping_failed_count()
175 BOOST_ASSERT( ( 0 <= PingFailLimitPercentage ) && ( PingFailLimitPercentage <= 100 ) );
176 BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) );
178 int ping_fail_limit_count = ( ResolvedIpCount * PingFailLimitPercentage ) / 100;
180 // keep a boolean variable because the PingsFailedCount can be reseted
181 if ( PingsFailedCount > ping_fail_limit_count )
183 ExceededPingFailedLimit = true;
185 GlobalLogger.notice() << "Stat(" << HostAddress << "): "
186 << PingsFailedCount << " fail/" << PingsPerformedCount << " pings/"
187 << ResolvedIpCount << " IPs: exceed limit=" << ping_fail_limit_count;
191 ExceededPingFailedLimit = false;
193 GlobalLogger.notice() << "Stat(" << HostAddress << "): "
194 << PingsFailedCount << " fail/" << PingsPerformedCount << " pings/"
195 << ResolvedIpCount << " IPs: below limit=" << ping_fail_limit_count;