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"
10 #include "base/timer.hpp"
11 #include <boost/thread/once.hpp>
13 using namespace icinga;
15 REGISTER_TYPE_WITH_PROTOTYPE(Checkable, Checkable::GetPrototype());
16 INITIALIZE_ONCE(&Checkable::StaticInitialize);
18 boost::signals2::signal<void (const Checkable::Ptr&, const String&, const String&, AcknowledgementType, bool, bool, double, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementSet;
19 boost::signals2::signal<void (const Checkable::Ptr&, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementCleared;
21 static Timer::Ptr l_CheckablesFireSuppressedNotifications;
23 void Checkable::StaticInitialize()
25 /* fixed downtime start */
26 Downtime::OnDowntimeStarted.connect(std::bind(&Checkable::NotifyFixedDowntimeStart, _1));
27 /* flexible downtime start */
28 Downtime::OnDowntimeTriggered.connect(std::bind(&Checkable::NotifyFlexibleDowntimeStart, _1));
29 /* fixed/flexible downtime end */
30 Downtime::OnDowntimeRemoved.connect(std::bind(&Checkable::NotifyDowntimeEnd, _1));
33 Checkable::Checkable()
35 SetSchedulingOffset(Utility::Random());
38 void Checkable::OnAllConfigLoaded()
40 ObjectImpl<Checkable>::OnAllConfigLoaded();
42 Endpoint::Ptr endpoint = GetCommandEndpoint();
45 Zone::Ptr checkableZone = static_pointer_cast<Zone>(GetZone());
48 Zone::Ptr cmdZone = endpoint->GetZone();
50 if (cmdZone != checkableZone && cmdZone->GetParent() != checkableZone) {
51 BOOST_THROW_EXCEPTION(ValidationError(this, { "command_endpoint" },
52 "Command endpoint must be in zone '" + checkableZone->GetName() + "' or in a direct child zone thereof."));
55 BOOST_THROW_EXCEPTION(ValidationError(this, { "command_endpoint" },
56 "Command endpoint must not be set."));
61 void Checkable::Start(bool runtimeCreated)
63 double now = Utility::GetTime();
65 if (GetNextCheck() < now + 60) {
66 double delta = std::min(GetCheckInterval(), 60.0);
67 delta *= (double)std::rand() / RAND_MAX;
68 SetNextCheck(now + delta);
71 ObjectImpl<Checkable>::Start(runtimeCreated);
73 static boost::once_flag once = BOOST_ONCE_INIT;
75 boost::call_once(once, []() {
76 l_CheckablesFireSuppressedNotifications = new Timer();
77 l_CheckablesFireSuppressedNotifications->SetInterval(5);
78 l_CheckablesFireSuppressedNotifications->OnTimerExpired.connect(&Checkable::FireSuppressedNotifications);
79 l_CheckablesFireSuppressedNotifications->Start();
83 void Checkable::AddGroup(const String& name)
85 boost::mutex::scoped_lock lock(m_CheckableMutex);
88 auto *host = dynamic_cast<Host *>(this);
91 groups = host->GetGroups();
93 groups = static_cast<Service *>(this)->GetGroups();
95 if (groups && groups->Contains(name))
104 AcknowledgementType Checkable::GetAcknowledgement()
106 auto avalue = static_cast<AcknowledgementType>(GetAcknowledgementRaw());
108 if (avalue != AcknowledgementNone) {
109 double expiry = GetAcknowledgementExpiry();
111 if (expiry != 0 && expiry < Utility::GetTime()) {
112 avalue = AcknowledgementNone;
113 ClearAcknowledgement();
120 bool Checkable::IsAcknowledged() const
122 return const_cast<Checkable *>(this)->GetAcknowledgement() != AcknowledgementNone;
125 void Checkable::AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, bool notify, bool persistent, double expiry, const MessageOrigin::Ptr& origin)
127 SetAcknowledgementRaw(type);
128 SetAcknowledgementExpiry(expiry);
130 if (notify && !IsPaused())
131 OnNotificationsRequested(this, NotificationAcknowledgement, GetLastCheckResult(), author, comment, nullptr);
133 OnAcknowledgementSet(this, author, comment, type, notify, persistent, expiry, origin);
136 void Checkable::ClearAcknowledgement(const MessageOrigin::Ptr& origin)
138 SetAcknowledgementRaw(AcknowledgementNone);
139 SetAcknowledgementExpiry(0);
141 OnAcknowledgementCleared(this, origin);
144 Endpoint::Ptr Checkable::GetCommandEndpoint() const
146 return Endpoint::GetByName(GetCommandEndpointRaw());
149 int Checkable::GetSeverity() const
151 /* overridden in Host/Service class. */
155 bool Checkable::GetProblem() const
157 return !IsStateOK(GetStateRaw());
160 bool Checkable::GetHandled() const
162 return GetProblem() && (IsInDowntime() || IsAcknowledged());
165 void Checkable::NotifyFixedDowntimeStart(const Downtime::Ptr& downtime)
167 if (!downtime->GetFixed())
170 NotifyDowntimeInternal(downtime);
173 void Checkable::NotifyFlexibleDowntimeStart(const Downtime::Ptr& downtime)
175 if (downtime->GetFixed())
178 NotifyDowntimeInternal(downtime);
181 void Checkable::NotifyDowntimeInternal(const Downtime::Ptr& downtime)
183 Checkable::Ptr checkable = downtime->GetCheckable();
185 if (!checkable->IsPaused())
186 OnNotificationsRequested(checkable, NotificationDowntimeStart, checkable->GetLastCheckResult(), downtime->GetAuthor(), downtime->GetComment(), nullptr);
189 void Checkable::NotifyDowntimeEnd(const Downtime::Ptr& downtime)
191 /* don't send notifications for flexible downtimes which never triggered */
192 if (!downtime->GetFixed() && !downtime->IsTriggered())
195 Checkable::Ptr checkable = downtime->GetCheckable();
197 if (!checkable->IsPaused())
198 OnNotificationsRequested(checkable, NotificationDowntimeEnd, checkable->GetLastCheckResult(), downtime->GetAuthor(), downtime->GetComment(), nullptr);
201 void Checkable::ValidateCheckInterval(const Lazy<double>& lvalue, const ValidationUtils& utils)
203 ObjectImpl<Checkable>::ValidateCheckInterval(lvalue, utils);
206 BOOST_THROW_EXCEPTION(ValidationError(this, { "check_interval" }, "Interval must be greater than 0."));
209 void Checkable::ValidateRetryInterval(const Lazy<double>& lvalue, const ValidationUtils& utils)
211 ObjectImpl<Checkable>::ValidateRetryInterval(lvalue, utils);
214 BOOST_THROW_EXCEPTION(ValidationError(this, { "retry_interval" }, "Interval must be greater than 0."));
217 void Checkable::ValidateMaxCheckAttempts(const Lazy<int>& lvalue, const ValidationUtils& utils)
219 ObjectImpl<Checkable>::ValidateMaxCheckAttempts(lvalue, utils);
222 BOOST_THROW_EXCEPTION(ValidationError(this, { "max_check_attempts" }, "Value must be greater than 0."));