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 SeverityFlagHostDown = 4,
49 SeverityFlagUnhandled = 8,
50 SeverityFlagPending = 16,
51 SeverityFlagWarning = 32,
52 SeverityFlagUnknown = 64,
53 SeverityFlagCritical = 128,
65 class Checkable : public ObjectImpl<Checkable>
68 DECLARE_OBJECT(Checkable);
69 DECLARE_OBJECTNAME(Checkable);
71 static void StaticInitialize();
75 std::set<Checkable::Ptr> GetParents() const;
76 std::set<Checkable::Ptr> GetChildren() const;
77 std::set<Checkable::Ptr> GetAllChildren() const;
79 void AddGroup(const String& name);
81 bool IsReachable(DependencyType dt = DependencyState, intrusive_ptr<Dependency> *failedDependency = nullptr, int rstack = 0) const;
83 AcknowledgementType GetAcknowledgement();
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);
88 int GetSeverity() const override;
89 bool GetProblem() const override;
90 bool GetHandled() const override;
93 intrusive_ptr<CheckCommand> GetCheckCommand() const;
94 TimePeriod::Ptr GetCheckPeriod() const;
96 long GetSchedulingOffset();
97 void SetSchedulingOffset(long offset);
99 void UpdateNextCheck(const MessageOrigin::Ptr& origin = nullptr);
101 bool HasBeenChecked() const;
102 virtual bool IsStateOK(ServiceState state) const = 0;
104 double GetLastCheck() const final;
106 virtual void SaveLastState(ServiceState state, double timestamp) = 0;
108 static void UpdateStatistics(const CheckResult::Ptr& cr, CheckableType type);
110 void ExecuteRemoteCheck(const Dictionary::Ptr& resolvedMacros = nullptr);
112 void ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrigin::Ptr& origin = nullptr);
114 Endpoint::Ptr GetCommandEndpoint() const;
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;
134 int GetDowntimeDepth() const final;
136 void RemoveAllDowntimes();
137 void TriggerDowntimes();
138 bool IsInDowntime() const;
139 bool IsAcknowledged() const;
141 std::set<Downtime::Ptr> GetDowntimes() const;
142 void RegisterDowntime(const Downtime::Ptr& downtime);
143 void UnregisterDowntime(const Downtime::Ptr& downtime);
146 void RemoveAllComments();
147 void RemoveCommentsByType(int type);
149 std::set<Comment::Ptr> GetComments() const;
150 void RegisterComment(const Comment::Ptr& comment);
151 void UnregisterComment(const Comment::Ptr& comment);
154 void SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author = "", const String& text = "");
156 std::set<Notification::Ptr> GetNotifications() const;
157 void RegisterNotification(const Notification::Ptr& notification);
158 void UnregisterNotification(const Notification::Ptr& notification);
160 void ResetNotificationNumbers();
163 void ExecuteEventHandler(const Dictionary::Ptr& resolvedMacros = nullptr,
164 bool useResolvedMacros = false);
166 intrusive_ptr<EventCommand> GetEventCommand() const;
168 /* Flapping Detection */
169 bool IsFlapping() const;
172 void AddDependency(const intrusive_ptr<Dependency>& dep);
173 void RemoveDependency(const intrusive_ptr<Dependency>& dep);
174 std::vector<intrusive_ptr<Dependency> > GetDependencies() const;
176 void AddReverseDependency(const intrusive_ptr<Dependency>& dep);
177 void RemoveReverseDependency(const intrusive_ptr<Dependency>& dep);
178 std::vector<intrusive_ptr<Dependency> > GetReverseDependencies() const;
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;
184 static void IncreasePendingChecks();
185 static void DecreasePendingChecks();
186 static int GetPendingChecks();
187 static void AquirePendingCheckSlot(int maxPendingChecks);
189 static Object::Ptr GetPrototype();
192 void Start(bool runtimeCreated) override;
193 void OnAllConfigLoaded() override;
196 mutable boost::mutex m_CheckableMutex;
197 bool m_CheckRunning{false};
198 long m_SchedulingOffset;
200 static boost::mutex m_StatsMutex;
201 static int m_PendingChecks;
202 static boost::condition_variable m_PendingChecksCV;
205 std::set<Downtime::Ptr> m_Downtimes;
206 mutable boost::mutex m_DowntimeMutex;
208 static void NotifyFixedDowntimeStart(const Downtime::Ptr& downtime);
209 static void NotifyFlexibleDowntimeStart(const Downtime::Ptr& downtime);
210 static void NotifyDowntimeInternal(const Downtime::Ptr& downtime);
212 static void NotifyDowntimeEnd(const Downtime::Ptr& downtime);
215 std::set<Comment::Ptr> m_Comments;
216 mutable boost::mutex m_CommentMutex;
219 std::set<Notification::Ptr> m_Notifications;
220 mutable boost::mutex m_NotificationMutex;
223 mutable boost::mutex m_DependencyMutex;
224 std::set<intrusive_ptr<Dependency> > m_Dependencies;
225 std::set<intrusive_ptr<Dependency> > m_ReverseDependencies;
227 void GetAllChildrenInternal(std::set<Checkable::Ptr>& children, int level = 0) const;
230 void UpdateFlappingStatus(bool stateChange);
235 #endif /* CHECKABLE_H */
237 #include "icinga/dependency.hpp"