remove debug output or more precisely: changed its log level from notice to debug...
[pingcheck] / src / host / hoststatus.cpp
CommitLineData
91fcc471
TJ
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on this file might be covered by the GNU General Public License.
19*/
6c14bbee 20#include "host/hoststatus.h"
ddf41c89
GMF
21
22#include <iostream>
3f7c921f 23#include <logfunc.hpp>
ddf41c89 24
780b0bca 25#include "boost_assert_handler.h"
ddf41c89
GMF
26
27using namespace std;
3f7c921f 28using I2n::Logger::GlobalLogger;
ddf41c89
GMF
29
30//-----------------------------------------------------------------------------
6c14bbee 31// HostStatus
ddf41c89
GMF
32//-----------------------------------------------------------------------------
33
c01a6023 34/**
6c14bbee
GMF
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
c01a6023 37 * fail.
6c14bbee 38 * @param link_analyzer The object used to notify the status of the host.
c01a6023 39 */
6c14bbee 40HostStatus::HostStatus(
ddf41c89 41 const string &host_address,
cd4048df 42 const int ping_fail_limit_percentage,
c6c54dfb 43 const LinkStatusItem link_analyzer
ddf41c89 44) :
c1fff16a 45 HostAddress( host_address ),
fb469ffa 46 LinkAnalyzer( link_analyzer ),
cd4048df 47 PingFailLimitPercentage( ping_fail_limit_percentage ),
ddf41c89
GMF
48 ResolvedIpCount( 0 ),
49 PingsPerformedCount( 0 ),
d8a91bd6 50 PingsFailedCount( 0 ),
a341119a 51 ExceededPingFailedLimit( false )
ddf41c89 52{
d4793cc9 53 BOOST_ASSERT( !HostAddress.empty() );
cd4048df 54 BOOST_ASSERT( ( 0 <= PingFailLimitPercentage ) && ( PingFailLimitPercentage <= 100 ) );
ddf41c89
GMF
55}
56
6c14bbee 57HostStatus::~HostStatus()
ddf41c89
GMF
58{
59}
60
c01a6023 61/**
6c14bbee 62 * @param resolved_ip_count The number of IPs resolved for the host.
c01a6023 63 */
6c14bbee 64void HostStatus::set_resolved_ip_count( const int resolved_ip_count )
ddf41c89
GMF
65{
66 BOOST_ASSERT( 1 <= resolved_ip_count );
67
db625177
CH
68 if (resolved_ip_count != ResolvedIpCount)
69 { // assume that the target has changed --> reset counters
70 reset_ping_counters();
71 }
ddf41c89 72 ResolvedIpCount = resolved_ip_count;
3f7c921f 73
03c2a2c3 74 GlobalLogger.debug() << "Stat(" << HostAddress << "): "
3f7c921f
CH
75 << PingsFailedCount << " fail/" << PingsPerformedCount << " pings/"
76 << ResolvedIpCount << " IPs: #IPs set";
ddf41c89
GMF
77}
78
c01a6023
GMF
79/**
80 * @return true if the amount of failed pings given to the host exceeded the
81 * limit.
82 */
6c14bbee 83bool HostStatus::exceeded_ping_failed_limit() const
d8a91bd6 84{
a341119a 85 return ExceededPingFailedLimit;
d8a91bd6
GMF
86}
87
c01a6023
GMF
88/**
89 * Adds a ping status (success or failure).
90 * @param ping_success
91 */
6c14bbee 92void HostStatus::update_ping_statistics( bool ping_success )
ddf41c89 93{
03c2a2c3 94 GlobalLogger.debug() << "Stat(" << HostAddress << "): "
3f7c921f
CH
95 << PingsFailedCount << " fail/" << PingsPerformedCount << " pings/"
96 << ResolvedIpCount << " IPs: add ping with success=" << ping_success;
97
ddf41c89
GMF
98 BOOST_ASSERT( 1 <= ResolvedIpCount );
99 BOOST_ASSERT( 0 <= PingsPerformedCount );
100 BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount );
101
c5e4bfa1 102 increase_ping_performed_count();
ddf41c89
GMF
103
104 if ( !ping_success )
105 {
c5e4bfa1 106 increase_ping_failed_count();
ddf41c89
GMF
107 }
108
2c10f87b
GMF
109 analyze_ping_failed_count();
110
c5e4bfa1
GMF
111 // after we tried all IPs resolved for this host, we can analyze how many
112 // failed
113 if ( tried_all_resolved_ip() )
ddf41c89 114 {
d8a91bd6 115 analyze_ping_statistics();
ddf41c89 116
c5e4bfa1 117 reset_ping_counters();
ddf41c89
GMF
118 }
119
120 BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount );
121}
122
c1d776ba 123
6c14bbee 124bool HostStatus::tried_all_resolved_ip() const
d8a91bd6 125{
2c10f87b 126 BOOST_ASSERT( ( 0 < PingsPerformedCount ) && ( PingsPerformedCount <= ResolvedIpCount ) );
d4793cc9
GMF
127
128 return ( PingsPerformedCount == ResolvedIpCount );
d8a91bd6
GMF
129}
130
6c14bbee 131void HostStatus::analyze_ping_statistics()
ddf41c89 132{
c1fff16a 133 BOOST_ASSERT( !HostAddress.empty() );
d4793cc9 134 BOOST_ASSERT( PingsPerformedCount == ResolvedIpCount );
ddf41c89 135
c1fff16a 136 // notify if the amount of pings that failed exceed the limit
a341119a 137 if ( exceeded_ping_failed_limit() )
ddf41c89 138 {
03c2a2c3 139 GlobalLogger.debug() << "Stat(" << HostAddress << "): "
3f7c921f
CH
140 << PingsFailedCount << " fail/" << PingsPerformedCount << " pings/"
141 << ResolvedIpCount << " IPs: notify down";
fb469ffa 142 LinkAnalyzer->notify_host_down( HostAddress );
ddf41c89
GMF
143 }
144 else
145 {
03c2a2c3 146 GlobalLogger.debug() << "Stat(" << HostAddress << "): "
3f7c921f
CH
147 << PingsFailedCount << " fail/" << PingsPerformedCount << " pings/"
148 << ResolvedIpCount << " IPs: notify up";
fb469ffa 149 LinkAnalyzer->notify_host_up( HostAddress );
ddf41c89 150 }
6fd0993e 151} //lint !e1762
ddf41c89 152
6c14bbee 153void HostStatus::reset_ping_counters()
c1fff16a
GMF
154{
155 PingsPerformedCount = 0;
156 PingsFailedCount = 0;
157}
158
6c14bbee 159void HostStatus::increase_ping_performed_count()
c5e4bfa1
GMF
160{
161 ++PingsPerformedCount;
c1fff16a 162
3f7c921f 163 BOOST_ASSERT( ( 0 < PingsPerformedCount ) && ( PingsPerformedCount <= ResolvedIpCount ) );
c5e4bfa1
GMF
164}
165
6c14bbee 166void HostStatus::increase_ping_failed_count()
c5e4bfa1
GMF
167{
168 ++PingsFailedCount;
c1fff16a
GMF
169
170 BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) );
c5e4bfa1
GMF
171}
172
6c14bbee 173void HostStatus::analyze_ping_failed_count()
ddf41c89 174{
cd4048df 175 BOOST_ASSERT( ( 0 <= PingFailLimitPercentage ) && ( PingFailLimitPercentage <= 100 ) );
c1fff16a
GMF
176 BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) );
177
cd4048df 178 int ping_fail_limit_count = ( ResolvedIpCount * PingFailLimitPercentage ) / 100;
c1fff16a 179
6827496c 180 // keep a boolean variable because the PingsFailedCount can be reseted
cd4048df 181 if ( PingsFailedCount > ping_fail_limit_count )
1d7d7cb2
GMF
182 {
183 ExceededPingFailedLimit = true;
3f7c921f 184
03c2a2c3 185 GlobalLogger.debug() << "Stat(" << HostAddress << "): "
3f7c921f
CH
186 << PingsFailedCount << " fail/" << PingsPerformedCount << " pings/"
187 << ResolvedIpCount << " IPs: exceed limit=" << ping_fail_limit_count;
1d7d7cb2
GMF
188 }
189 else
190 {
191 ExceededPingFailedLimit = false;
3f7c921f 192
03c2a2c3 193 GlobalLogger.debug() << "Stat(" << HostAddress << "): "
3f7c921f
CH
194 << PingsFailedCount << " fail/" << PingsPerformedCount << " pings/"
195 << ResolvedIpCount << " IPs: below limit=" << ping_fail_limit_count;
1d7d7cb2 196 }
c5e4bfa1 197}