removed the HostStatus::report_dns_resolution_failure (not used any more)
[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>
23
780b0bca 24#include "boost_assert_handler.h"
ddf41c89
GMF
25
26using namespace std;
27
28//-----------------------------------------------------------------------------
6c14bbee 29// HostStatus
ddf41c89
GMF
30//-----------------------------------------------------------------------------
31
c01a6023 32/**
6c14bbee
GMF
33 * @param host_address The address of the host it has to analyze.
34 * @param ping_fail_percentage_limit The percentage threshold of pings that can
c01a6023 35 * fail.
6c14bbee 36 * @param link_analyzer The object used to notify the status of the host.
c01a6023 37 */
6c14bbee 38HostStatus::HostStatus(
ddf41c89 39 const string &host_address,
cd4048df 40 const int ping_fail_limit_percentage,
c6c54dfb 41 const LinkStatusItem link_analyzer
ddf41c89 42) :
c1fff16a 43 HostAddress( host_address ),
fb469ffa 44 LinkAnalyzer( link_analyzer ),
cd4048df 45 PingFailLimitPercentage( ping_fail_limit_percentage ),
ddf41c89
GMF
46 ResolvedIpCount( 0 ),
47 PingsPerformedCount( 0 ),
d8a91bd6 48 PingsFailedCount( 0 ),
a341119a 49 ExceededPingFailedLimit( false )
ddf41c89 50{
d4793cc9 51 BOOST_ASSERT( !HostAddress.empty() );
cd4048df 52 BOOST_ASSERT( ( 0 <= PingFailLimitPercentage ) && ( PingFailLimitPercentage <= 100 ) );
ddf41c89
GMF
53}
54
6c14bbee 55HostStatus::~HostStatus()
ddf41c89
GMF
56{
57}
58
c01a6023 59/**
6c14bbee 60 * @param resolved_ip_count The number of IPs resolved for the host.
c01a6023 61 */
6c14bbee 62void HostStatus::set_resolved_ip_count( const int resolved_ip_count )
ddf41c89
GMF
63{
64 BOOST_ASSERT( 1 <= resolved_ip_count );
65
66 ResolvedIpCount = resolved_ip_count;
67}
68
c01a6023
GMF
69/**
70 * @return true if the amount of failed pings given to the host exceeded the
71 * limit.
72 */
6c14bbee 73bool HostStatus::exceeded_ping_failed_limit() const
d8a91bd6 74{
a341119a 75 return ExceededPingFailedLimit;
d8a91bd6
GMF
76}
77
c01a6023
GMF
78/**
79 * Adds a ping status (success or failure).
80 * @param ping_success
81 */
6c14bbee 82void HostStatus::update_ping_statistics( bool ping_success )
ddf41c89
GMF
83{
84 BOOST_ASSERT( 1 <= ResolvedIpCount );
85 BOOST_ASSERT( 0 <= PingsPerformedCount );
86 BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount );
87
c5e4bfa1 88 increase_ping_performed_count();
ddf41c89
GMF
89
90 if ( !ping_success )
91 {
c5e4bfa1 92 increase_ping_failed_count();
ddf41c89
GMF
93 }
94
2c10f87b
GMF
95 analyze_ping_failed_count();
96
c5e4bfa1
GMF
97 // after we tried all IPs resolved for this host, we can analyze how many
98 // failed
99 if ( tried_all_resolved_ip() )
ddf41c89 100 {
d8a91bd6 101 analyze_ping_statistics();
ddf41c89 102
c5e4bfa1 103 reset_ping_counters();
ddf41c89
GMF
104 }
105
106 BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount );
107}
108
c1d776ba 109
6c14bbee 110bool HostStatus::tried_all_resolved_ip() const
d8a91bd6 111{
2c10f87b 112 BOOST_ASSERT( ( 0 < PingsPerformedCount ) && ( PingsPerformedCount <= ResolvedIpCount ) );
d4793cc9
GMF
113
114 return ( PingsPerformedCount == ResolvedIpCount );
d8a91bd6
GMF
115}
116
6c14bbee 117void HostStatus::analyze_ping_statistics()
ddf41c89 118{
c1fff16a 119 BOOST_ASSERT( !HostAddress.empty() );
d4793cc9 120 BOOST_ASSERT( PingsPerformedCount == ResolvedIpCount );
ddf41c89 121
c1fff16a 122 // notify if the amount of pings that failed exceed the limit
a341119a 123 if ( exceeded_ping_failed_limit() )
ddf41c89 124 {
fb469ffa 125 LinkAnalyzer->notify_host_down( HostAddress );
ddf41c89
GMF
126 }
127 else
128 {
fb469ffa 129 LinkAnalyzer->notify_host_up( HostAddress );
ddf41c89 130 }
6fd0993e 131} //lint !e1762
ddf41c89 132
6c14bbee 133void HostStatus::reset_ping_counters()
c1fff16a
GMF
134{
135 PingsPerformedCount = 0;
136 PingsFailedCount = 0;
137}
138
6c14bbee 139void HostStatus::increase_ping_performed_count()
c5e4bfa1
GMF
140{
141 ++PingsPerformedCount;
c1fff16a
GMF
142
143 BOOST_ASSERT( ( 0 <= PingsPerformedCount ) && ( PingsPerformedCount <= ResolvedIpCount ) );
c5e4bfa1
GMF
144}
145
6c14bbee 146void HostStatus::increase_ping_failed_count()
c5e4bfa1
GMF
147{
148 ++PingsFailedCount;
c1fff16a
GMF
149
150 BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) );
c5e4bfa1
GMF
151}
152
6c14bbee 153void HostStatus::analyze_ping_failed_count()
ddf41c89 154{
cd4048df 155 BOOST_ASSERT( ( 0 <= PingFailLimitPercentage ) && ( PingFailLimitPercentage <= 100 ) );
c1fff16a
GMF
156 BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) );
157
cd4048df 158 int ping_fail_limit_count = ( ResolvedIpCount * PingFailLimitPercentage ) / 100;
c1fff16a 159
6827496c 160 // keep a boolean variable because the PingsFailedCount can be reseted
cd4048df 161 if ( PingsFailedCount > ping_fail_limit_count )
1d7d7cb2
GMF
162 {
163 ExceededPingFailedLimit = true;
164 }
165 else
166 {
167 ExceededPingFailedLimit = false;
168 }
c5e4bfa1 169}