]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable.hpp
Merge pull request #6718 from Icinga/bugfix/ssl-shutdown
[icinga2] / lib / icinga / checkable.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/)      *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #ifndef CHECKABLE_H
21 #define CHECKABLE_H
22
23 #include "icinga/i2-icinga.hpp"
24 #include "icinga/checkable-ti.hpp"
25 #include "icinga/timeperiod.hpp"
26 #include "icinga/notification.hpp"
27 #include "icinga/comment.hpp"
28 #include "icinga/downtime.hpp"
29 #include "remote/endpoint.hpp"
30 #include "remote/messageorigin.hpp"
31
32 namespace icinga
33 {
34
35 /**
36  * @ingroup icinga
37  */
38 enum DependencyType
39 {
40         DependencyState,
41         DependencyCheckExecution,
42         DependencyNotification
43 };
44
45 /**
46  * Checkable Types
47  *
48  * @ingroup icinga
49  */
50 enum CheckableType
51 {
52         CheckableHost,
53         CheckableService
54 };
55
56 /**
57  * Severity Flags
58  *
59  * @ingroup icinga
60  */
61 enum SeverityFlag
62 {
63         SeverityFlagDowntime = 1,
64         SeverityFlagAcknowledgement = 2,
65         SeverityFlagUnhandled = 8,
66         SeverityFlagPending = 16,
67         SeverityFlagWarning = 32,
68         SeverityFlagUnknown = 64,
69         SeverityFlagCritical = 128,
70 };
71
72 class CheckCommand;
73 class EventCommand;
74 class Dependency;
75
76 /**
77  * An Icinga service.
78  *
79  * @ingroup icinga
80  */
81 class Checkable : public ObjectImpl<Checkable>
82 {
83 public:
84         DECLARE_OBJECT(Checkable);
85         DECLARE_OBJECTNAME(Checkable);
86
87         static void StaticInitialize();
88
89         Checkable();
90
91         std::set<Checkable::Ptr> GetParents() const;
92         std::set<Checkable::Ptr> GetChildren() const;
93         std::set<Checkable::Ptr> GetAllChildren() const;
94
95         void AddGroup(const String& name);
96
97         bool IsReachable(DependencyType dt = DependencyState, intrusive_ptr<Dependency> *failedDependency = nullptr, int rstack = 0) const;
98
99         AcknowledgementType GetAcknowledgement();
100
101         void AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, bool notify = true, bool persistent = false, double expiry = 0, const MessageOrigin::Ptr& origin = nullptr);
102         void ClearAcknowledgement(const MessageOrigin::Ptr& origin = nullptr);
103
104         int GetSeverity() const override;
105
106         /* Checks */
107         intrusive_ptr<CheckCommand> GetCheckCommand() const;
108         TimePeriod::Ptr GetCheckPeriod() const;
109
110         long GetSchedulingOffset();
111         void SetSchedulingOffset(long offset);
112
113         void UpdateNextCheck(const MessageOrigin::Ptr& origin = nullptr);
114
115         bool HasBeenChecked() const;
116         virtual bool IsStateOK(ServiceState state) = 0;
117
118         double GetLastCheck() const final;
119
120         virtual void SaveLastState(ServiceState state, double timestamp) = 0;
121
122         static void UpdateStatistics(const CheckResult::Ptr& cr, CheckableType type);
123
124         void ExecuteRemoteCheck(const Dictionary::Ptr& resolvedMacros = nullptr);
125         void ExecuteCheck();
126         void ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrigin::Ptr& origin = nullptr);
127
128         Endpoint::Ptr GetCommandEndpoint() const;
129
130         static boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, const MessageOrigin::Ptr&)> OnNewCheckResult;
131         static boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, StateType, const MessageOrigin::Ptr&)> OnStateChange;
132         static boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, std::set<Checkable::Ptr>, const MessageOrigin::Ptr&)> OnReachabilityChanged;
133         static boost::signals2::signal<void (const Checkable::Ptr&, NotificationType, const CheckResult::Ptr&,
134                 const String&, const String&, const MessageOrigin::Ptr&)> OnNotificationsRequested;
135         static boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const User::Ptr&,
136                 const NotificationType&, const CheckResult::Ptr&, const String&, const String&, const String&,
137                 const MessageOrigin::Ptr&)> OnNotificationSentToUser;
138         static boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const std::set<User::Ptr>&,
139                 const NotificationType&, const CheckResult::Ptr&, const String&,
140                 const String&, const MessageOrigin::Ptr&)> OnNotificationSentToAllUsers;
141         static boost::signals2::signal<void (const Checkable::Ptr&, const String&, const String&, AcknowledgementType,
142                 bool, bool, double, const MessageOrigin::Ptr&)> OnAcknowledgementSet;
143         static boost::signals2::signal<void (const Checkable::Ptr&, const MessageOrigin::Ptr&)> OnAcknowledgementCleared;
144         static boost::signals2::signal<void (const Checkable::Ptr&)> OnNextCheckUpdated;
145         static boost::signals2::signal<void (const Checkable::Ptr&)> OnEventCommandExecuted;
146
147         /* Downtimes */
148         int GetDowntimeDepth() const final;
149
150         void RemoveAllDowntimes();
151         void TriggerDowntimes();
152         bool IsInDowntime() const;
153         bool IsAcknowledged() const;
154
155         std::set<Downtime::Ptr> GetDowntimes() const;
156         void RegisterDowntime(const Downtime::Ptr& downtime);
157         void UnregisterDowntime(const Downtime::Ptr& downtime);
158
159         /* Comments */
160         void RemoveAllComments();
161         void RemoveCommentsByType(int type);
162
163         std::set<Comment::Ptr> GetComments() const;
164         void RegisterComment(const Comment::Ptr& comment);
165         void UnregisterComment(const Comment::Ptr& comment);
166
167         /* Notifications */
168         void SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author = "", const String& text = "");
169
170         std::set<Notification::Ptr> GetNotifications() const;
171         void RegisterNotification(const Notification::Ptr& notification);
172         void UnregisterNotification(const Notification::Ptr& notification);
173
174         void ResetNotificationNumbers();
175
176         /* Event Handler */
177         void ExecuteEventHandler(const Dictionary::Ptr& resolvedMacros = nullptr,
178                 bool useResolvedMacros = false);
179
180         intrusive_ptr<EventCommand> GetEventCommand() const;
181
182         /* Flapping Detection */
183         bool IsFlapping() const;
184
185         /* Dependencies */
186         void AddDependency(const intrusive_ptr<Dependency>& dep);
187         void RemoveDependency(const intrusive_ptr<Dependency>& dep);
188         std::vector<intrusive_ptr<Dependency> > GetDependencies() const;
189
190         void AddReverseDependency(const intrusive_ptr<Dependency>& dep);
191         void RemoveReverseDependency(const intrusive_ptr<Dependency>& dep);
192         std::vector<intrusive_ptr<Dependency> > GetReverseDependencies() const;
193
194         void ValidateCheckInterval(const Lazy<double>& lvalue, const ValidationUtils& value) final;
195         void ValidateMaxCheckAttempts(const Lazy<int>& lvalue, const ValidationUtils& value) final;
196
197         static void IncreasePendingChecks();
198         static void DecreasePendingChecks();
199         static int GetPendingChecks();
200         static void AquirePendingCheckSlot(int maxPendingChecks);
201
202         static Object::Ptr GetPrototype();
203
204 protected:
205         void Start(bool runtimeCreated) override;
206         void OnAllConfigLoaded() override;
207
208 private:
209         mutable boost::mutex m_CheckableMutex;
210         bool m_CheckRunning{false};
211         long m_SchedulingOffset;
212
213         static boost::mutex m_StatsMutex;
214         static int m_PendingChecks;
215         static boost::condition_variable m_PendingChecksCV;
216
217         /* Downtimes */
218         std::set<Downtime::Ptr> m_Downtimes;
219         mutable boost::mutex m_DowntimeMutex;
220
221         static void NotifyFixedDowntimeStart(const Downtime::Ptr& downtime);
222         static void NotifyFlexibleDowntimeStart(const Downtime::Ptr& downtime);
223         static void NotifyDowntimeInternal(const Downtime::Ptr& downtime);
224
225         static void NotifyDowntimeEnd(const Downtime::Ptr& downtime);
226
227         /* Comments */
228         std::set<Comment::Ptr> m_Comments;
229         mutable boost::mutex m_CommentMutex;
230
231         /* Notifications */
232         std::set<Notification::Ptr> m_Notifications;
233         mutable boost::mutex m_NotificationMutex;
234
235         /* Dependencies */
236         mutable boost::mutex m_DependencyMutex;
237         std::set<intrusive_ptr<Dependency> > m_Dependencies;
238         std::set<intrusive_ptr<Dependency> > m_ReverseDependencies;
239
240         void GetAllChildrenInternal(std::set<Checkable::Ptr>& children, int level = 0) const;
241
242         /* Flapping */
243         void UpdateFlappingStatus(bool stateChange);
244 };
245
246 }
247
248 #endif /* CHECKABLE_H */
249
250 #include "icinga/dependency.hpp"