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;
88 bool GetProblem() const override;
89 bool GetHandled() const override;
92 intrusive_ptr<CheckCommand> GetCheckCommand() const;
93 TimePeriod::Ptr GetCheckPeriod() const;
95 long GetSchedulingOffset();
96 void SetSchedulingOffset(long offset);
98 void UpdateNextCheck(const MessageOrigin::Ptr& origin = nullptr);
100 bool HasBeenChecked() const;
101 virtual bool IsStateOK(ServiceState state) const = 0;
103 double GetLastCheck() const final;
105 virtual void SaveLastState(ServiceState state, double timestamp) = 0;
107 static void UpdateStatistics(const CheckResult::Ptr& cr, CheckableType type);
109 void ExecuteRemoteCheck(const Dictionary::Ptr& resolvedMacros = nullptr);
111 void ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrigin::Ptr& origin = nullptr);
113 Endpoint::Ptr GetCommandEndpoint() const;
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;
133 int GetDowntimeDepth() const final;
135 void RemoveAllDowntimes();
136 void TriggerDowntimes();
137 bool IsInDowntime() const;
138 bool IsAcknowledged() const;
140 std::set<Downtime::Ptr> GetDowntimes() const;
141 void RegisterDowntime(const Downtime::Ptr& downtime);
142 void UnregisterDowntime(const Downtime::Ptr& downtime);
145 void RemoveAllComments();
146 void RemoveCommentsByType(int type);
148 std::set<Comment::Ptr> GetComments() const;
149 void RegisterComment(const Comment::Ptr& comment);
150 void UnregisterComment(const Comment::Ptr& comment);
153 void SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author = "", const String& text = "");
155 std::set<Notification::Ptr> GetNotifications() const;
156 void RegisterNotification(const Notification::Ptr& notification);
157 void UnregisterNotification(const Notification::Ptr& notification);
159 void ResetNotificationNumbers();
162 void ExecuteEventHandler(const Dictionary::Ptr& resolvedMacros = nullptr,
163 bool useResolvedMacros = false);
165 intrusive_ptr<EventCommand> GetEventCommand() const;
167 /* Flapping Detection */
168 bool IsFlapping() const;
171 void AddDependency(const intrusive_ptr<Dependency>& dep);
172 void RemoveDependency(const intrusive_ptr<Dependency>& dep);
173 std::vector<intrusive_ptr<Dependency> > GetDependencies() const;
175 void AddReverseDependency(const intrusive_ptr<Dependency>& dep);
176 void RemoveReverseDependency(const intrusive_ptr<Dependency>& dep);
177 std::vector<intrusive_ptr<Dependency> > GetReverseDependencies() const;
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;
183 static void IncreasePendingChecks();
184 static void DecreasePendingChecks();
185 static int GetPendingChecks();
186 static void AquirePendingCheckSlot(int maxPendingChecks);
188 static Object::Ptr GetPrototype();
191 void Start(bool runtimeCreated) override;
192 void OnAllConfigLoaded() override;
195 mutable boost::mutex m_CheckableMutex;
196 bool m_CheckRunning{false};
197 long m_SchedulingOffset;
199 static boost::mutex m_StatsMutex;
200 static int m_PendingChecks;
201 static boost::condition_variable m_PendingChecksCV;
204 std::set<Downtime::Ptr> m_Downtimes;
205 mutable boost::mutex m_DowntimeMutex;
207 static void NotifyFixedDowntimeStart(const Downtime::Ptr& downtime);
208 static void NotifyFlexibleDowntimeStart(const Downtime::Ptr& downtime);
209 static void NotifyDowntimeInternal(const Downtime::Ptr& downtime);
211 static void NotifyDowntimeEnd(const Downtime::Ptr& downtime);
214 std::set<Comment::Ptr> m_Comments;
215 mutable boost::mutex m_CommentMutex;
218 std::set<Notification::Ptr> m_Notifications;
219 mutable boost::mutex m_NotificationMutex;
222 mutable boost::mutex m_DependencyMutex;
223 std::set<intrusive_ptr<Dependency> > m_Dependencies;
224 std::set<intrusive_ptr<Dependency> > m_ReverseDependencies;
226 void GetAllChildrenInternal(std::set<Checkable::Ptr>& children, int level = 0) const;
229 void UpdateFlappingStatus(bool stateChange);
234 #endif /* CHECKABLE_H */
236 #include "icinga/dependency.hpp"