f6a0257084fdd863a8c14e296c4321fc7730dbe1
[pingcheck] / src / host / hoststatus.cpp
1 /*
2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
4
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
7
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.
13
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.
16
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.
19 */
20 #include "host/hoststatus.h"
21
22 #include <iostream>
23
24 #include "boost_assert_handler.h"
25
26 using namespace std;
27
28 //-----------------------------------------------------------------------------
29 // HostStatus
30 //-----------------------------------------------------------------------------
31
32 /**
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
35  * fail.
36  * @param link_analyzer The object used to notify the status of the host.
37  */
38 HostStatus::HostStatus(
39         const string &host_address,
40         const int ping_fail_limit_percentage,
41         const LinkStatusItem link_analyzer
42 ) :
43     HostAddress( host_address ),
44     LinkAnalyzer( link_analyzer ),
45     PingFailLimitPercentage( ping_fail_limit_percentage ),
46     ResolvedIpCount( 0 ),
47     PingsPerformedCount( 0 ),
48     PingsFailedCount( 0 ),
49     ExceededPingFailedLimit( false )
50 {
51     BOOST_ASSERT( !HostAddress.empty() );
52     BOOST_ASSERT( ( 0 <= PingFailLimitPercentage ) && ( PingFailLimitPercentage <= 100 ) );
53 }
54
55 HostStatus::~HostStatus()
56 {
57 }
58
59 /**
60  * @param resolved_ip_count The number of IPs resolved for the host.
61  */
62 void HostStatus::set_resolved_ip_count( const int resolved_ip_count )
63 {
64     BOOST_ASSERT( 1 <= resolved_ip_count );
65
66     ResolvedIpCount = resolved_ip_count;
67 }
68
69 /**
70  * @return true if the amount of failed pings given to the host exceeded the
71  * limit.
72  */
73 bool HostStatus::exceeded_ping_failed_limit() const
74 {
75     return ExceededPingFailedLimit;
76 }
77
78 /**
79  * Adds a ping status (success or failure).
80  * @param ping_success
81  */
82 void HostStatus::update_ping_statistics( bool ping_success )
83 {
84     BOOST_ASSERT( 1 <= ResolvedIpCount );
85     BOOST_ASSERT( 0 <= PingsPerformedCount );
86     BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount );
87
88     increase_ping_performed_count();
89
90     if ( !ping_success )
91     {
92         increase_ping_failed_count();
93     }
94
95     analyze_ping_failed_count();
96
97     // after we tried all IPs resolved for this host, we can analyze how many
98     // failed
99     if ( tried_all_resolved_ip() )
100     {
101         analyze_ping_statistics();
102
103         reset_ping_counters();
104     }
105
106     BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount );
107 }
108
109 void HostStatus::report_dns_resolution_failure()
110 {
111     LinkAnalyzer->notify_host_down( HostAddress );
112 }
113
114
115 bool HostStatus::tried_all_resolved_ip() const
116 {
117     BOOST_ASSERT( ( 0 < PingsPerformedCount ) && ( PingsPerformedCount <= ResolvedIpCount ) );
118
119     return ( PingsPerformedCount == ResolvedIpCount );
120 }
121
122 void HostStatus::analyze_ping_statistics()
123 {
124     BOOST_ASSERT( !HostAddress.empty() );
125     BOOST_ASSERT( PingsPerformedCount == ResolvedIpCount );
126
127     // notify if the amount of pings that failed exceed the limit
128     if ( exceeded_ping_failed_limit() )
129     {
130         LinkAnalyzer->notify_host_down( HostAddress );
131     }
132     else
133     {
134         LinkAnalyzer->notify_host_up( HostAddress );
135     }
136 } //lint !e1762
137
138 void HostStatus::reset_ping_counters()
139 {
140     PingsPerformedCount = 0;
141     PingsFailedCount = 0;
142 }
143
144 void HostStatus::increase_ping_performed_count()
145 {
146     ++PingsPerformedCount;
147
148     BOOST_ASSERT( ( 0 <= PingsPerformedCount ) && ( PingsPerformedCount <= ResolvedIpCount ) );
149 }
150
151 void HostStatus::increase_ping_failed_count()
152 {
153     ++PingsFailedCount;
154
155     BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) );
156 }
157
158 void HostStatus::analyze_ping_failed_count()
159 {
160     BOOST_ASSERT( ( 0 <= PingFailLimitPercentage ) && ( PingFailLimitPercentage <= 100 ) );
161     BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) );
162
163     int ping_fail_limit_count = ( ResolvedIpCount * PingFailLimitPercentage ) / 100;
164
165     // keep a boolean variable because the PingsFailedCount can be reseted
166     if ( PingsFailedCount > ping_fail_limit_count )
167     {
168         ExceededPingFailedLimit = true;
169     }
170     else
171     {
172         ExceededPingFailedLimit = false;
173     }
174 }