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