]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable.hpp
Merge pull request #7116 from Icinga/feature/no-reachable
[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         bool GetProblem() const override;
89         bool GetHandled() const override;
90
91         /* Checks */
92         intrusive_ptr<CheckCommand> GetCheckCommand() const;
93         TimePeriod::Ptr GetCheckPeriod() const;
94
95         long GetSchedulingOffset();
96         void SetSchedulingOffset(long offset);
97
98         void UpdateNextCheck(const MessageOrigin::Ptr& origin = nullptr);
99
100         bool HasBeenChecked() const;
101         virtual bool IsStateOK(ServiceState state) const = 0;
102
103         double GetLastCheck() const final;
104
105         virtual void SaveLastState(ServiceState state, double timestamp) = 0;
106
107         static void UpdateStatistics(const CheckResult::Ptr& cr, CheckableType type);
108
109         void ExecuteRemoteCheck(const Dictionary::Ptr& resolvedMacros = nullptr);
110         void ExecuteCheck();
111         void ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrigin::Ptr& origin = nullptr);
112
113         Endpoint::Ptr GetCommandEndpoint() const;
114
115         static boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, const MessageOrigin::Ptr&)> OnNewCheckResult;
116         static boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, StateType, const MessageOrigin::Ptr&)> OnStateChange;
117         static boost::signals2::signal<void (const Checkable::Ptr&, const CheckResult::Ptr&, std::set<Checkable::Ptr>, const MessageOrigin::Ptr&)> OnReachabilityChanged;
118         static boost::signals2::signal<void (const Checkable::Ptr&, NotificationType, const CheckResult::Ptr&,
119                 const String&, const String&, const MessageOrigin::Ptr&)> OnNotificationsRequested;
120         static boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const User::Ptr&,
121                 const NotificationType&, const CheckResult::Ptr&, const String&, const String&, const String&,
122                 const MessageOrigin::Ptr&)> OnNotificationSentToUser;
123         static boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const std::set<User::Ptr>&,
124                 const NotificationType&, const CheckResult::Ptr&, const String&,
125                 const String&, const MessageOrigin::Ptr&)> OnNotificationSentToAllUsers;
126         static boost::signals2::signal<void (const Checkable::Ptr&, const String&, const String&, AcknowledgementType,
127                 bool, bool, double, const MessageOrigin::Ptr&)> OnAcknowledgementSet;
128         static boost::signals2::signal<void (const Checkable::Ptr&, const MessageOrigin::Ptr&)> OnAcknowledgementCleared;
129         static boost::signals2::signal<void (const Checkable::Ptr&)> OnNextCheckUpdated;
130         static boost::signals2::signal<void (const Checkable::Ptr&)> OnEventCommandExecuted;
131
132         /* Downtimes */
133         int GetDowntimeDepth() const final;
134
135         void RemoveAllDowntimes();
136         void TriggerDowntimes();
137         bool IsInDowntime() const;
138         bool IsAcknowledged() const;
139
140         std::set<Downtime::Ptr> GetDowntimes() const;
141         void RegisterDowntime(const Downtime::Ptr& downtime);
142         void UnregisterDowntime(const Downtime::Ptr& downtime);
143
144         /* Comments */
145         void RemoveAllComments();
146         void RemoveCommentsByType(int type);
147
148         std::set<Comment::Ptr> GetComments() const;
149         void RegisterComment(const Comment::Ptr& comment);
150         void UnregisterComment(const Comment::Ptr& comment);
151
152         /* Notifications */
153         void SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author = "", const String& text = "");
154
155         std::set<Notification::Ptr> GetNotifications() const;
156         void RegisterNotification(const Notification::Ptr& notification);
157         void UnregisterNotification(const Notification::Ptr& notification);
158
159         void ResetNotificationNumbers();
160
161         /* Event Handler */
162         void ExecuteEventHandler(const Dictionary::Ptr& resolvedMacros = nullptr,
163                 bool useResolvedMacros = false);
164
165         intrusive_ptr<EventCommand> GetEventCommand() const;
166
167         /* Flapping Detection */
168         bool IsFlapping() const;
169
170         /* Dependencies */
171         void AddDependency(const intrusive_ptr<Dependency>& dep);
172         void RemoveDependency(const intrusive_ptr<Dependency>& dep);
173         std::vector<intrusive_ptr<Dependency> > GetDependencies() const;
174
175         void AddReverseDependency(const intrusive_ptr<Dependency>& dep);
176         void RemoveReverseDependency(const intrusive_ptr<Dependency>& dep);
177         std::vector<intrusive_ptr<Dependency> > GetReverseDependencies() const;
178
179         void ValidateCheckInterval(const Lazy<double>& lvalue, const ValidationUtils& value) final;
180         void ValidateRetryInterval(const Lazy<double>& lvalue, const ValidationUtils& value) final;
181         void ValidateMaxCheckAttempts(const Lazy<int>& lvalue, const ValidationUtils& value) final;
182
183         static void IncreasePendingChecks();
184         static void DecreasePendingChecks();
185         static int GetPendingChecks();
186         static void AquirePendingCheckSlot(int maxPendingChecks);
187
188         static Object::Ptr GetPrototype();
189
190 protected:
191         void Start(bool runtimeCreated) override;
192         void OnAllConfigLoaded() override;
193
194 private:
195         mutable boost::mutex m_CheckableMutex;
196         bool m_CheckRunning{false};
197         long m_SchedulingOffset;
198
199         static boost::mutex m_StatsMutex;
200         static int m_PendingChecks;
201         static boost::condition_variable m_PendingChecksCV;
202
203         /* Downtimes */
204         std::set<Downtime::Ptr> m_Downtimes;
205         mutable boost::mutex m_DowntimeMutex;
206
207         static void NotifyFixedDowntimeStart(const Downtime::Ptr& downtime);
208         static void NotifyFlexibleDowntimeStart(const Downtime::Ptr& downtime);
209         static void NotifyDowntimeInternal(const Downtime::Ptr& downtime);
210
211         static void NotifyDowntimeEnd(const Downtime::Ptr& downtime);
212
213         /* Comments */
214         std::set<Comment::Ptr> m_Comments;
215         mutable boost::mutex m_CommentMutex;
216
217         /* Notifications */
218         std::set<Notification::Ptr> m_Notifications;
219         mutable boost::mutex m_NotificationMutex;
220
221         /* Dependencies */
222         mutable boost::mutex m_DependencyMutex;
223         std::set<intrusive_ptr<Dependency> > m_Dependencies;
224         std::set<intrusive_ptr<Dependency> > m_ReverseDependencies;
225
226         void GetAllChildrenInternal(std::set<Checkable::Ptr>& children, int level = 0) const;
227
228         /* Flapping */
229         void UpdateFlappingStatus(bool stateChange);
230 };
231
232 }
233
234 #endif /* CHECKABLE_H */
235
236 #include "icinga/dependency.hpp"