1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
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"
25 DependencyCheckExecution,
26 DependencyNotification
47 SeverityFlagDowntime = 1,
48 SeverityFlagAcknowledgement = 2,
49 SeverityFlagHostDown = 4,
50 SeverityFlagUnhandled = 8,
51 SeverityFlagPending = 16,
52 SeverityFlagWarning = 32,
53 SeverityFlagUnknown = 64,
54 SeverityFlagCritical = 128,
66 class Checkable : public ObjectImpl<Checkable>
69 DECLARE_OBJECT(Checkable);
70 DECLARE_OBJECTNAME(Checkable);
72 static void StaticInitialize();
76 std::set<Checkable::Ptr> GetParents() const;
77 std::set<Checkable::Ptr> GetChildren() const;
78 std::set<Checkable::Ptr> GetAllChildren() const;
80 void AddGroup(const String& name);
82 bool IsReachable(DependencyType dt = DependencyState, intrusive_ptr<Dependency> *failedDependency = nullptr, int rstack = 0) const;
84 AcknowledgementType GetAcknowledgement();
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);
89 int GetSeverity() const override;
90 bool GetProblem() const override;
91 bool GetHandled() const override;
94 intrusive_ptr<CheckCommand> GetCheckCommand() const;
95 TimePeriod::Ptr GetCheckPeriod() const;
97 long GetSchedulingOffset();
98 void SetSchedulingOffset(long offset);
100 void UpdateNextCheck(const MessageOrigin::Ptr& origin = nullptr);
102 bool HasBeenChecked() const;
103 virtual bool IsStateOK(ServiceState state) const = 0;
105 double GetLastCheck() const final;
107 virtual void SaveLastState(ServiceState state, double timestamp) = 0;
109 static void UpdateStatistics(const CheckResult::Ptr& cr, CheckableType type);
111 void ExecuteRemoteCheck(const Dictionary::Ptr& resolvedMacros = nullptr);
113 void ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrigin::Ptr& origin = nullptr);
115 Endpoint::Ptr GetCommandEndpoint() const;
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;
135 int GetDowntimeDepth() const final;
137 void RemoveAllDowntimes();
138 void TriggerDowntimes();
139 bool IsInDowntime() const;
140 bool IsAcknowledged() const;
142 std::set<Downtime::Ptr> GetDowntimes() const;
143 void RegisterDowntime(const Downtime::Ptr& downtime);
144 void UnregisterDowntime(const Downtime::Ptr& downtime);
147 void RemoveAllComments();
148 void RemoveCommentsByType(int type);
150 std::set<Comment::Ptr> GetComments() const;
151 void RegisterComment(const Comment::Ptr& comment);
152 void UnregisterComment(const Comment::Ptr& comment);
155 void SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author = "", const String& text = "");
157 std::set<Notification::Ptr> GetNotifications() const;
158 void RegisterNotification(const Notification::Ptr& notification);
159 void UnregisterNotification(const Notification::Ptr& notification);
161 void ResetNotificationNumbers();
164 void ExecuteEventHandler(const Dictionary::Ptr& resolvedMacros = nullptr,
165 bool useResolvedMacros = false);
167 intrusive_ptr<EventCommand> GetEventCommand() const;
169 /* Flapping Detection */
170 bool IsFlapping() const;
173 void AddDependency(const intrusive_ptr<Dependency>& dep);
174 void RemoveDependency(const intrusive_ptr<Dependency>& dep);
175 std::vector<intrusive_ptr<Dependency> > GetDependencies() const;
177 void AddReverseDependency(const intrusive_ptr<Dependency>& dep);
178 void RemoveReverseDependency(const intrusive_ptr<Dependency>& dep);
179 std::vector<intrusive_ptr<Dependency> > GetReverseDependencies() const;
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;
185 static void IncreasePendingChecks();
186 static void DecreasePendingChecks();
187 static int GetPendingChecks();
188 static void AquirePendingCheckSlot(int maxPendingChecks);
190 static Object::Ptr GetPrototype();
193 void Start(bool runtimeCreated) override;
194 void OnAllConfigLoaded() override;
197 mutable boost::mutex m_CheckableMutex;
198 bool m_CheckRunning{false};
199 long m_SchedulingOffset;
201 static boost::mutex m_StatsMutex;
202 static int m_PendingChecks;
203 static boost::condition_variable m_PendingChecksCV;
206 std::set<Downtime::Ptr> m_Downtimes;
207 mutable boost::mutex m_DowntimeMutex;
209 static void NotifyFixedDowntimeStart(const Downtime::Ptr& downtime);
210 static void NotifyFlexibleDowntimeStart(const Downtime::Ptr& downtime);
211 static void NotifyDowntimeInternal(const Downtime::Ptr& downtime);
213 static void NotifyDowntimeEnd(const Downtime::Ptr& downtime);
215 static void FireSuppressedNotifications(const Timer * const&);
218 std::set<Comment::Ptr> m_Comments;
219 mutable boost::mutex m_CommentMutex;
222 std::set<Notification::Ptr> m_Notifications;
223 mutable boost::mutex m_NotificationMutex;
226 mutable boost::mutex m_DependencyMutex;
227 std::set<intrusive_ptr<Dependency> > m_Dependencies;
228 std::set<intrusive_ptr<Dependency> > m_ReverseDependencies;
230 void GetAllChildrenInternal(std::set<Checkable::Ptr>& children, int level = 0) const;
233 void UpdateFlappingStatus(bool stateChange);
238 #endif /* CHECKABLE_H */
240 #include "icinga/dependency.hpp"