1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "icinga/checkable.hpp"
4 #include "icinga/checkable-ti.cpp"
5 #include "icinga/host.hpp"
6 #include "icinga/service.hpp"
7 #include "base/objectlock.hpp"
8 #include "base/utility.hpp"
9 #include "base/exception.hpp"
11 using namespace icinga;
13 REGISTER_TYPE_WITH_PROTOTYPE(Checkable, Checkable::GetPrototype());
14 INITIALIZE_ONCE(&Checkable::StaticInitialize);
16 boost::signals2::signal<void (const Checkable::Ptr&, const String&, const String&, AcknowledgementType, bool, bool, double, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementSet;
17 boost::signals2::signal<void (const Checkable::Ptr&, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementCleared;
19 void Checkable::StaticInitialize()
21 /* fixed downtime start */
22 Downtime::OnDowntimeStarted.connect(std::bind(&Checkable::NotifyFixedDowntimeStart, _1));
23 /* flexible downtime start */
24 Downtime::OnDowntimeTriggered.connect(std::bind(&Checkable::NotifyFlexibleDowntimeStart, _1));
25 /* fixed/flexible downtime end */
26 Downtime::OnDowntimeRemoved.connect(std::bind(&Checkable::NotifyDowntimeEnd, _1));
29 Checkable::Checkable()
31 SetSchedulingOffset(Utility::Random());
34 void Checkable::OnAllConfigLoaded()
36 ObjectImpl<Checkable>::OnAllConfigLoaded();
38 Endpoint::Ptr endpoint = GetCommandEndpoint();
41 Zone::Ptr checkableZone = static_pointer_cast<Zone>(GetZone());
44 Zone::Ptr cmdZone = endpoint->GetZone();
46 if (cmdZone != checkableZone && cmdZone->GetParent() != checkableZone) {
47 BOOST_THROW_EXCEPTION(ValidationError(this, { "command_endpoint" },
48 "Command endpoint must be in zone '" + checkableZone->GetName() + "' or in a direct child zone thereof."));
51 BOOST_THROW_EXCEPTION(ValidationError(this, { "command_endpoint" },
52 "Command endpoint must not be set."));
57 void Checkable::Start(bool runtimeCreated)
59 double now = Utility::GetTime();
61 if (GetNextCheck() < now + 60) {
62 double delta = std::min(GetCheckInterval(), 60.0);
63 delta *= (double)std::rand() / RAND_MAX;
64 SetNextCheck(now + delta);
67 ObjectImpl<Checkable>::Start(runtimeCreated);
70 void Checkable::AddGroup(const String& name)
72 boost::mutex::scoped_lock lock(m_CheckableMutex);
75 auto *host = dynamic_cast<Host *>(this);
78 groups = host->GetGroups();
80 groups = static_cast<Service *>(this)->GetGroups();
82 if (groups && groups->Contains(name))
91 AcknowledgementType Checkable::GetAcknowledgement()
93 auto avalue = static_cast<AcknowledgementType>(GetAcknowledgementRaw());
95 if (avalue != AcknowledgementNone) {
96 double expiry = GetAcknowledgementExpiry();
98 if (expiry != 0 && expiry < Utility::GetTime()) {
99 avalue = AcknowledgementNone;
100 ClearAcknowledgement();
107 bool Checkable::IsAcknowledged() const
109 return const_cast<Checkable *>(this)->GetAcknowledgement() != AcknowledgementNone;
112 void Checkable::AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, bool notify, bool persistent, double expiry, const MessageOrigin::Ptr& origin)
114 SetAcknowledgementRaw(type);
115 SetAcknowledgementExpiry(expiry);
117 if (notify && !IsPaused())
118 OnNotificationsRequested(this, NotificationAcknowledgement, GetLastCheckResult(), author, comment, nullptr);
120 OnAcknowledgementSet(this, author, comment, type, notify, persistent, expiry, origin);
123 void Checkable::ClearAcknowledgement(const MessageOrigin::Ptr& origin)
125 SetAcknowledgementRaw(AcknowledgementNone);
126 SetAcknowledgementExpiry(0);
128 OnAcknowledgementCleared(this, origin);
131 Endpoint::Ptr Checkable::GetCommandEndpoint() const
133 return Endpoint::GetByName(GetCommandEndpointRaw());
136 int Checkable::GetSeverity() const
138 /* overridden in Host/Service class. */
142 bool Checkable::GetProblem() const
144 return !IsStateOK(GetStateRaw());
147 bool Checkable::GetHandled() const
149 return GetProblem() && (IsInDowntime() || IsAcknowledged());
152 void Checkable::NotifyFixedDowntimeStart(const Downtime::Ptr& downtime)
154 if (!downtime->GetFixed())
157 NotifyDowntimeInternal(downtime);
160 void Checkable::NotifyFlexibleDowntimeStart(const Downtime::Ptr& downtime)
162 if (downtime->GetFixed())
165 NotifyDowntimeInternal(downtime);
168 void Checkable::NotifyDowntimeInternal(const Downtime::Ptr& downtime)
170 Checkable::Ptr checkable = downtime->GetCheckable();
172 if (!checkable->IsPaused())
173 OnNotificationsRequested(checkable, NotificationDowntimeStart, checkable->GetLastCheckResult(), downtime->GetAuthor(), downtime->GetComment(), nullptr);
176 void Checkable::NotifyDowntimeEnd(const Downtime::Ptr& downtime)
178 /* don't send notifications for flexible downtimes which never triggered */
179 if (!downtime->GetFixed() && !downtime->IsTriggered())
182 Checkable::Ptr checkable = downtime->GetCheckable();
184 if (!checkable->IsPaused())
185 OnNotificationsRequested(checkable, NotificationDowntimeEnd, checkable->GetLastCheckResult(), downtime->GetAuthor(), downtime->GetComment(), nullptr);
188 void Checkable::ValidateCheckInterval(const Lazy<double>& lvalue, const ValidationUtils& utils)
190 ObjectImpl<Checkable>::ValidateCheckInterval(lvalue, utils);
193 BOOST_THROW_EXCEPTION(ValidationError(this, { "check_interval" }, "Interval must be greater than 0."));
196 void Checkable::ValidateRetryInterval(const Lazy<double>& lvalue, const ValidationUtils& utils)
198 ObjectImpl<Checkable>::ValidateRetryInterval(lvalue, utils);
201 BOOST_THROW_EXCEPTION(ValidationError(this, { "retry_interval" }, "Interval must be greater than 0."));
204 void Checkable::ValidateMaxCheckAttempts(const Lazy<int>& lvalue, const ValidationUtils& utils)
206 ObjectImpl<Checkable>::ValidateMaxCheckAttempts(lvalue, utils);
209 BOOST_THROW_EXCEPTION(ValidationError(this, { "max_check_attempts" }, "Value must be greater than 0."));