changed value of link_up/down_interval from minutes to seconds
[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     //
56     // Types
57     //
58
59     enum Status
60     {
61         Status_Up,
62         Status_Down
63     };
64
65     enum NotificationStatus
66     {
67         NotificationStatus_NotReported,
68         NotificationStatus_Reported
69     };
70
71 #ifdef UNDER_TEST
72 public:
73 #endif
74     //
75     // Methods
76     //
77
78     bool add_host_up( const std::string &host_address );
79     void add_host_down( const std::string &host_address );
80
81     bool exceeded_host_down_limit() const;
82
83     void notify_link_up();
84     void notify_link_down();
85
86     bool is_link_up_enough_time() const;
87     bool is_link_down_enough_time() const;
88
89     bool can_report_link_status() const;
90     void set_link_status(
91             const LinkStatus::Status new_link_status
92     );
93
94     //
95     // Attributes
96     //
97
98     /// The maximum amount of hosts which can be down before sound the alarm
99     const int HostsDownLimit;
100     /// List of hosts down (obvious isn't it?)
101     std::set<std::string> HostsDownList;
102     /// Interval the link have to be stable in order to consider it is functional
103     const int LinkUpIntervalInSec;
104     /// Interval the link have to be down in order to consider it is non-functional
105     const int LinkDownIntervalInSec;
106     /// Keep track of the actual link status
107     LinkStatus::Status CurrentLinkStatus;
108     /// Indicates if the last link status change was notified
109     LinkStatus::NotificationStatus CurrentNotificationStatus;
110     /// When was the last time the status changed
111     boost::posix_time::ptime TimeLinkStatusChanged;
112     /// Command used to notify the status of the link
113     StatusNotifierCommandItem StatusNotifierCmd;
114
115 };
116
117 //-----------------------------------------------------------------------------
118 // LinkStatusItem
119 //-----------------------------------------------------------------------------
120
121 typedef boost::shared_ptr<LinkStatus> LinkStatusItem;
122
123 #endif // LINK_STATUS_H