]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable.hpp
Re-send suppressed notifications
[icinga2] / lib / icinga / checkable.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef CHECKABLE_H
4 #define CHECKABLE_H
5
6 #include "base/timer.hpp"
7 #include "icinga/i2-icinga.hpp"
8 #include "icinga/checkable-ti.hpp"
9 #include "icinga/timeperiod.hpp"
10 #include "icinga/notification.hpp"
11 #include "icinga/comment.hpp"
12 #include "icinga/downtime.hpp"
13 #include "remote/endpoint.hpp"
14 #include "remote/messageorigin.hpp"
15
16 namespace icinga
17 {
18
19 /**
20  * @ingroup icinga
21  */
22 enum DependencyType
23 {
24         DependencyState,
25         DependencyCheckExecution,
26         DependencyNotification
27 };
28
29 /**
30  * Checkable Types
31  *
32  * @ingroup icinga
33  */
34 enum CheckableType
35 {
36         CheckableHost,
37         CheckableService
38 };
39
40 /**
41  * Severity Flags
42  *
43  * @ingroup icinga
44  */
45 enum SeverityFlag
46 {
47         SeverityFlagDowntime = 1,
48         SeverityFlagAcknowledgement = 2,
49         SeverityFlagHostDown = 4,
50         SeverityFlagUnhandled = 8,
51         SeverityFlagPending = 16,
52         SeverityFlagWarning = 32,
53         SeverityFlagUnknown = 64,
54         SeverityFlagCritical = 128,
55 };
56
57 class CheckCommand;
58 class EventCommand;
59 class Dependency;
60
61 /**
62  * An Icinga service.
63  *
64  * @ingroup icinga
65  */
66 class Checkable : public ObjectImpl<Checkable>
67 {
68 public:
69         DECLARE_OBJECT(Checkable);
70         DECLARE_OBJECTNAME(Checkable);
71
72         static void StaticInitialize();
73
74         Checkable();
75
76         std::set<Checkable::Ptr> GetParents() const;
77         std::set<Checkable::Ptr> GetChildren() const;
78         std::set<Checkable::Ptr> GetAllChildren() const;
79
80         void AddGroup(const String& name);
81
82         bool IsReachable(DependencyType dt = DependencyState, intrusive_ptr<Dependency> *failedDependency = nullptr, int rstack = 0) const;
83
84         AcknowledgementType GetAcknowledgement();
85
86         void AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, bool notify = true, bool persistent = false, double expiry = 0, const MessageOrigin::Ptr& origin = nullptr);
87         void ClearAcknowledgement(const MessageOrigin::Ptr& origin = nullptr);
88
89         int GetSeverity() const override;
90         bool GetProblem() const override;
91         bool GetHandled() const override;
92
93         /* Checks */
94         intrusive_ptr<CheckCommand> GetCheckCommand() const;
95         TimePeriod::Ptr GetCheckPeriod() const;
96
97         long GetSchedulingOffset();
98         void SetSchedulingOffset(long offset);
99
100         void UpdateNextCheck(const MessageOrigin::Ptr& origin = nullptr);
101
102         bool HasBeenChecked() const;
103         virtual bool IsStateOK(ServiceState state) const = 0;
104
105         double GetLastCheck() const final;
106
107         virtual void SaveLastState(ServiceState state, double timestamp) = 0;
108
109         static void UpdateStatistics(const CheckResult::Ptr& cr, CheckableType type);
110
111         void ExecuteRemoteCheck(const Dictionary::Ptr& resolvedMacros = nullptr);
112         void ExecuteCheck();
113         void ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrigin::Ptr& origin = nullptr);
114
115         Endpoint::Ptr GetCommandEndpoint() const;
116
117         static boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, const MessageOrigin::Ptr&)> OnNewCheckResult;
118         static boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, StateType, const MessageOrigin::Ptr&)> OnStateChange;
119         static boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, std::set<Checkable::Ptr>, const MessageOrigin::Ptr&)> OnReachabilityChanged;
120         static boost::signals2::signal<void (const Checkable::Ptr&, NotificationType, const CheckResult::Ptr&,
121                 const String&, const String&, const MessageOrigin::Ptr&)> OnNotificationsRequested;
122         static boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const User::Ptr&,
123                 const NotificationType&, const CheckResult::Ptr&, const NotificationResult::Ptr&, const String&,
124                 const String&, const String&, const MessageOrigin::Ptr&)> OnNotificationSentToUser;
125         static boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const std::set<User::Ptr>&,
126                 const NotificationType&, const CheckResult::Ptr&, const String&,
127                 const String&, const MessageOrigin::Ptr&)> OnNotificationSentToAllUsers;
128         static boost::signals2::signal<void (const Checkable::Ptr&, const String&, const String&, AcknowledgementType,
129                 bool, bool, double, const MessageOrigin::Ptr&)> OnAcknowledgementSet;
130         static boost::signals2::signal<void (const Checkable::Ptr&, const MessageOrigin::Ptr&)> OnAcknowledgementCleared;
131         static boost::signals2::signal<void (const Checkable::Ptr&)> OnNextCheckUpdated;
132         static boost::signals2::signal<void (const Checkable::Ptr&)> OnEventCommandExecuted;
133
134         /* Downtimes */
135         int GetDowntimeDepth() const final;
136
137         void RemoveAllDowntimes();
138         void TriggerDowntimes();
139         bool IsInDowntime() const;
140         bool IsAcknowledged() const;
141
142         std::set<Downtime::Ptr> GetDowntimes() const;
143         void RegisterDowntime(const Downtime::Ptr& downtime);
144         void UnregisterDowntime(const Downtime::Ptr& downtime);
145
146         /* Comments */
147         void RemoveAllComments();
148         void RemoveCommentsByType(int type);
149
150         std::set<Comment::Ptr> GetComments() const;
151         void RegisterComment(const Comment::Ptr& comment);
152         void UnregisterComment(const Comment::Ptr& comment);
153
154         /* Notifications */
155         void SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author = "", const String& text = "");
156
157         std::set<Notification::Ptr> GetNotifications() const;
158         void RegisterNotification(const Notification::Ptr& notification);
159         void UnregisterNotification(const Notification::Ptr& notification);
160
161         void ResetNotificationNumbers();
162
163         /* Event Handler */
164         void ExecuteEventHandler(const Dictionary::Ptr& resolvedMacros = nullptr,
165                 bool useResolvedMacros = false);
166
167         intrusive_ptr<EventCommand> GetEventCommand() const;
168
169         /* Flapping Detection */
170         bool IsFlapping() const;
171
172         /* Dependencies */
173         void AddDependency(const intrusive_ptr<Dependency>& dep);
174         void RemoveDependency(const intrusive_ptr<Dependency>& dep);
175         std::vector<intrusive_ptr<Dependency> > GetDependencies() const;
176
177         void AddReverseDependency(const intrusive_ptr<Dependency>& dep);
178         void RemoveReverseDependency(const intrusive_ptr<Dependency>& dep);
179         std::vector<intrusive_ptr<Dependency> > GetReverseDependencies() const;
180
181         void ValidateCheckInterval(const Lazy<double>& lvalue, const ValidationUtils& value) final;
182         void ValidateRetryInterval(const Lazy<double>& lvalue, const ValidationUtils& value) final;
183         void ValidateMaxCheckAttempts(const Lazy<int>& lvalue, const ValidationUtils& value) final;
184
185         static void IncreasePendingChecks();
186         static void DecreasePendingChecks();
187         static int GetPendingChecks();
188         static void AquirePendingCheckSlot(int maxPendingChecks);
189
190         static Object::Ptr GetPrototype();
191
192 protected:
193         void Start(bool runtimeCreated) override;
194         void OnAllConfigLoaded() override;
195
196 private:
197         mutable boost::mutex m_CheckableMutex;
198         bool m_CheckRunning{false};
199         long m_SchedulingOffset;
200
201         static boost::mutex m_StatsMutex;
202         static int m_PendingChecks;
203         static boost::condition_variable m_PendingChecksCV;
204
205         /* Downtimes */
206         std::set<Downtime::Ptr> m_Downtimes;
207         mutable boost::mutex m_DowntimeMutex;
208
209         static void NotifyFixedDowntimeStart(const Downtime::Ptr& downtime);
210         static void NotifyFlexibleDowntimeStart(const Downtime::Ptr& downtime);
211         static void NotifyDowntimeInternal(const Downtime::Ptr& downtime);
212
213         static void NotifyDowntimeEnd(const Downtime::Ptr& downtime);
214
215         static void FireSuppressedNotifications(const Timer * const&);
216
217         /* Comments */
218         std::set<Comment::Ptr> m_Comments;
219         mutable boost::mutex m_CommentMutex;
220
221         /* Notifications */
222         std::set<Notification::Ptr> m_Notifications;
223         mutable boost::mutex m_NotificationMutex;
224
225         /* Dependencies */
226         mutable boost::mutex m_DependencyMutex;
227         std::set<intrusive_ptr<Dependency> > m_Dependencies;
228         std::set<intrusive_ptr<Dependency> > m_ReverseDependencies;
229
230         void GetAllChildrenInternal(std::set<Checkable::Ptr>& children, int level = 0) const;
231
232         /* Flapping */
233         void UpdateFlappingStatus(bool stateChange);
234 };
235
236 }
237
238 #endif /* CHECKABLE_H */
239
240 #include "icinga/dependency.hpp"