prevent pingcheck from repeating report of same status; show notification status...
[pingcheck] / src / link / linkstatus.h
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 #ifndef LINK_STATUS_H
21 #define LINK_STATUS_H
22
23 #include <set>
24 #include <string>
25
26 #include <boost/asio.hpp>
27 #include <boost/shared_ptr.hpp>
28
29 #include "link/statusnotifiercommand.h"
30
31 //-----------------------------------------------------------------------------
32 // LinkStatus
33 //-----------------------------------------------------------------------------
34
35 /**
36  * @brief This class analyzes and notifies the link status, through keeping
37  * track of the amount of hosts down.
38  * Scope: one object for many hosts.
39  */
40 class LinkStatus
41 {
42 public:
43     LinkStatus(
44             const int hosts_down_limit,
45             const int link_up_interval_in_sec,
46             const int link_down_interval_in_sec,
47             const std::string &status_notifier_cmd
48     );
49     ~LinkStatus();
50
51     void notify_host_up( const std::string &host_address );
52     void notify_host_down( const std::string &host_address );
53
54 private:
55     std::string log_prefix() const;
56
57     //
58     // Types
59     //
60
61     enum Status
62     {
63         Status_Up,
64         Status_Down
65     };
66
67     enum NotificationStatus
68     {
69         NotificationStatus_NotReported,
70         NotificationStatus_Reported
71     };
72
73 #ifdef UNDER_TEST
74 public:
75 #endif
76     //
77     // Methods
78     //
79
80     bool add_host_up( const std::string &host_address );
81     void add_host_down( const std::string &host_address );
82
83     bool exceeded_host_down_limit() const;
84
85     void notify_link_up();
86     void notify_link_down();
87
88     bool is_link_up_enough_time() const;
89     bool is_link_down_enough_time() const;
90
91     bool can_report_link_status() const;
92     void set_link_status(
93             const LinkStatus::Status new_link_status
94     );
95
96     //
97     // Attributes
98     //
99
100     /// The maximum amount of hosts which can be down before sound the alarm
101     const int HostsDownLimit;
102     /// List of hosts down (obvious isn't it?)
103     std::set<std::string> HostsDownList;
104     /// Interval the link have to be stable in order to consider it is functional
105     const int LinkUpIntervalInSec;
106     /// Interval the link have to be down in order to consider it is non-functional
107     const int LinkDownIntervalInSec;
108     /// Keep track of the actual link status
109     LinkStatus::Status CurrentLinkStatus;
110     /// Indicates if the last link status change was notified
111     LinkStatus::NotificationStatus CurrentNotificationStatus;
112     /// When was the last time the status changed
113     boost::posix_time::ptime TimeLinkStatusChanged;
114     /// Command used to notify the status of the link
115     StatusNotifierCommandItem StatusNotifierCmd;
116     /// last reported status to prevent multiple calls of same status
117     LinkStatus::Status LastReportedStatus;
118
119 };
120
121 //-----------------------------------------------------------------------------
122 // LinkStatusItem
123 //-----------------------------------------------------------------------------
124
125 typedef boost::shared_ptr<LinkStatus> LinkStatusItem;
126
127 #endif // LINK_STATUS_H