1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
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"
24 DependencyCheckExecution,
25 DependencyNotification
46 SeverityFlagDowntime = 1,
47 SeverityFlagAcknowledgement = 2,
48 SeverityFlagUnhandled = 8,
49 SeverityFlagPending = 16,
50 SeverityFlagWarning = 32,
51 SeverityFlagUnknown = 64,
52 SeverityFlagCritical = 128,
64 class Checkable : public ObjectImpl<Checkable>
67 DECLARE_OBJECT(Checkable);
68 DECLARE_OBJECTNAME(Checkable);
70 static void StaticInitialize();
74 std::set<Checkable::Ptr> GetParents() const;
75 std::set<Checkable::Ptr> GetChildren() const;
76 std::set<Checkable::Ptr> GetAllChildren() const;
78 void AddGroup(const String& name);
80 bool IsReachable(DependencyType dt = DependencyState, intrusive_ptr<Dependency> *failedDependency = nullptr, int rstack = 0) const;
82 AcknowledgementType GetAcknowledgement();
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);
87 int GetSeverity() const override;
90 intrusive_ptr<CheckCommand> GetCheckCommand() const;
91 TimePeriod::Ptr GetCheckPeriod() const;
93 long GetSchedulingOffset();
94 void SetSchedulingOffset(long offset);
96 void UpdateNextCheck(const MessageOrigin::Ptr& origin = nullptr);
98 bool HasBeenChecked() const;
99 virtual bool IsStateOK(ServiceState state) = 0;
101 double GetLastCheck() const final;
103 virtual void SaveLastState(ServiceState state, double timestamp) = 0;
105 static void UpdateStatistics(const CheckResult::Ptr& cr, CheckableType type);
107 void ExecuteRemoteCheck(const Dictionary::Ptr& resolvedMacros = nullptr);
109 void ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrigin::Ptr& origin = nullptr);
111 Endpoint::Ptr GetCommandEndpoint() const;
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;
131 int GetDowntimeDepth() const final;
133 void RemoveAllDowntimes();
134 void TriggerDowntimes();
135 bool IsInDowntime() const;
136 bool IsAcknowledged() const;
138 std::set<Downtime::Ptr> GetDowntimes() const;
139 void RegisterDowntime(const Downtime::Ptr& downtime);
140 void UnregisterDowntime(const Downtime::Ptr& downtime);
143 void RemoveAllComments();
144 void RemoveCommentsByType(int type);
146 std::set<Comment::Ptr> GetComments() const;
147 void RegisterComment(const Comment::Ptr& comment);
148 void UnregisterComment(const Comment::Ptr& comment);
151 void SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author = "", const String& text = "");
153 std::set<Notification::Ptr> GetNotifications() const;
154 void RegisterNotification(const Notification::Ptr& notification);
155 void UnregisterNotification(const Notification::Ptr& notification);
157 void ResetNotificationNumbers();
160 void ExecuteEventHandler(const Dictionary::Ptr& resolvedMacros = nullptr,
161 bool useResolvedMacros = false);
163 intrusive_ptr<EventCommand> GetEventCommand() const;
165 /* Flapping Detection */
166 bool IsFlapping() const;
169 void AddDependency(const intrusive_ptr<Dependency>& dep);
170 void RemoveDependency(const intrusive_ptr<Dependency>& dep);
171 std::vector<intrusive_ptr<Dependency> > GetDependencies() const;
173 void AddReverseDependency(const intrusive_ptr<Dependency>& dep);
174 void RemoveReverseDependency(const intrusive_ptr<Dependency>& dep);
175 std::vector<intrusive_ptr<Dependency> > GetReverseDependencies() const;
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;
181 static void IncreasePendingChecks();
182 static void DecreasePendingChecks();
183 static int GetPendingChecks();
184 static void AquirePendingCheckSlot(int maxPendingChecks);
186 static Object::Ptr GetPrototype();
189 void Start(bool runtimeCreated) override;
190 void OnAllConfigLoaded() override;
193 mutable boost::mutex m_CheckableMutex;
194 bool m_CheckRunning{false};
195 long m_SchedulingOffset;
197 static boost::mutex m_StatsMutex;
198 static int m_PendingChecks;
199 static boost::condition_variable m_PendingChecksCV;
202 std::set<Downtime::Ptr> m_Downtimes;
203 mutable boost::mutex m_DowntimeMutex;
205 static void NotifyFixedDowntimeStart(const Downtime::Ptr& downtime);
206 static void NotifyFlexibleDowntimeStart(const Downtime::Ptr& downtime);
207 static void NotifyDowntimeInternal(const Downtime::Ptr& downtime);
209 static void NotifyDowntimeEnd(const Downtime::Ptr& downtime);
212 std::set<Comment::Ptr> m_Comments;
213 mutable boost::mutex m_CommentMutex;
216 std::set<Notification::Ptr> m_Notifications;
217 mutable boost::mutex m_NotificationMutex;
220 mutable boost::mutex m_DependencyMutex;
221 std::set<intrusive_ptr<Dependency> > m_Dependencies;
222 std::set<intrusive_ptr<Dependency> > m_ReverseDependencies;
224 void GetAllChildrenInternal(std::set<Checkable::Ptr>& children, int level = 0) const;
227 void UpdateFlappingStatus(bool stateChange);
232 #endif /* CHECKABLE_H */
234 #include "icinga/dependency.hpp"