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