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