1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "icinga/checkable.hpp"
21 #include "icinga/checkable-ti.cpp"
22 #include "icinga/host.hpp"
23 #include "icinga/service.hpp"
24 #include "base/objectlock.hpp"
25 #include "base/utility.hpp"
26 #include "base/exception.hpp"
28 using namespace icinga;
30 REGISTER_TYPE_WITH_PROTOTYPE(Checkable, Checkable::GetPrototype());
31 INITIALIZE_ONCE(&Checkable::StaticInitialize);
33 boost::signals2::signal<void (const Checkable::Ptr&, const String&, const String&, AcknowledgementType, bool, bool, double, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementSet;
34 boost::signals2::signal<void (const Checkable::Ptr&, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementCleared;
36 void Checkable::StaticInitialize()
38 /* fixed downtime start */
39 Downtime::OnDowntimeStarted.connect(std::bind(&Checkable::NotifyFixedDowntimeStart, _1));
40 /* flexible downtime start */
41 Downtime::OnDowntimeTriggered.connect(std::bind(&Checkable::NotifyFlexibleDowntimeStart, _1));
42 /* fixed/flexible downtime end */
43 Downtime::OnDowntimeRemoved.connect(std::bind(&Checkable::NotifyDowntimeEnd, _1));
46 Checkable::Checkable()
48 SetSchedulingOffset(Utility::Random());
51 void Checkable::OnAllConfigLoaded()
53 ObjectImpl<Checkable>::OnAllConfigLoaded();
55 Endpoint::Ptr endpoint = GetCommandEndpoint();
58 Zone::Ptr checkableZone = static_pointer_cast<Zone>(GetZone());
61 Zone::Ptr cmdZone = endpoint->GetZone();
63 if (cmdZone != checkableZone && cmdZone->GetParent() != checkableZone) {
64 BOOST_THROW_EXCEPTION(ValidationError(this, { "command_endpoint" },
65 "Command endpoint must be in zone '" + checkableZone->GetName() + "' or in a direct child zone thereof."));
68 BOOST_THROW_EXCEPTION(ValidationError(this, { "command_endpoint" },
69 "Command endpoint must not be set."));
74 void Checkable::Start(bool runtimeCreated)
76 double now = Utility::GetTime();
78 if (GetNextCheck() < now + 60) {
79 double delta = std::min(GetCheckInterval(), 60.0);
80 delta *= (double)std::rand() / RAND_MAX;
81 SetNextCheck(now + delta);
84 ObjectImpl<Checkable>::Start(runtimeCreated);
87 void Checkable::AddGroup(const String& name)
89 boost::mutex::scoped_lock lock(m_CheckableMutex);
92 auto *host = dynamic_cast<Host *>(this);
95 groups = host->GetGroups();
97 groups = static_cast<Service *>(this)->GetGroups();
99 if (groups && groups->Contains(name))
103 groups = new Array();
108 AcknowledgementType Checkable::GetAcknowledgement()
110 auto avalue = static_cast<AcknowledgementType>(GetAcknowledgementRaw());
112 if (avalue != AcknowledgementNone) {
113 double expiry = GetAcknowledgementExpiry();
115 if (expiry != 0 && expiry < Utility::GetTime()) {
116 avalue = AcknowledgementNone;
117 ClearAcknowledgement();
124 bool Checkable::IsAcknowledged() const
126 return const_cast<Checkable *>(this)->GetAcknowledgement() != AcknowledgementNone;
129 void Checkable::AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, bool notify, bool persistent, double expiry, const MessageOrigin::Ptr& origin)
131 SetAcknowledgementRaw(type);
132 SetAcknowledgementExpiry(expiry);
134 if (notify && !IsPaused())
135 OnNotificationsRequested(this, NotificationAcknowledgement, GetLastCheckResult(), author, comment, nullptr);
137 OnAcknowledgementSet(this, author, comment, type, notify, persistent, expiry, origin);
140 void Checkable::ClearAcknowledgement(const MessageOrigin::Ptr& origin)
142 SetAcknowledgementRaw(AcknowledgementNone);
143 SetAcknowledgementExpiry(0);
145 OnAcknowledgementCleared(this, origin);
148 Endpoint::Ptr Checkable::GetCommandEndpoint() const
150 return Endpoint::GetByName(GetCommandEndpointRaw());
153 int Checkable::GetSeverity() const
155 /* overridden in Host/Service class. */
159 void Checkable::NotifyFixedDowntimeStart(const Downtime::Ptr& downtime)
161 if (!downtime->GetFixed())
164 NotifyDowntimeInternal(downtime);
167 void Checkable::NotifyFlexibleDowntimeStart(const Downtime::Ptr& downtime)
169 if (downtime->GetFixed())
172 NotifyDowntimeInternal(downtime);
175 void Checkable::NotifyDowntimeInternal(const Downtime::Ptr& downtime)
177 Checkable::Ptr checkable = downtime->GetCheckable();
179 if (!checkable->IsPaused())
180 OnNotificationsRequested(checkable, NotificationDowntimeStart, checkable->GetLastCheckResult(), downtime->GetAuthor(), downtime->GetComment(), nullptr);
183 void Checkable::NotifyDowntimeEnd(const Downtime::Ptr& downtime)
185 /* don't send notifications for flexible downtimes which never triggered */
186 if (!downtime->GetFixed() && !downtime->IsTriggered())
189 Checkable::Ptr checkable = downtime->GetCheckable();
191 if (!checkable->IsPaused())
192 OnNotificationsRequested(checkable, NotificationDowntimeEnd, checkable->GetLastCheckResult(), downtime->GetAuthor(), downtime->GetComment(), nullptr);
195 void Checkable::ValidateCheckInterval(const Lazy<double>& lvalue, const ValidationUtils& utils)
197 ObjectImpl<Checkable>::ValidateCheckInterval(lvalue, utils);
200 BOOST_THROW_EXCEPTION(ValidationError(this, { "check_interval" }, "Interval must be greater than 0."));
203 void Checkable::ValidateRetryInterval(const Lazy<double>& lvalue, const ValidationUtils& utils)
205 ObjectImpl<Checkable>::ValidateRetryInterval(lvalue, utils);
208 BOOST_THROW_EXCEPTION(ValidationError(this, { "retry_interval" }, "Interval must be greater than 0."));
211 void Checkable::ValidateMaxCheckAttempts(const Lazy<int>& lvalue, const ValidationUtils& utils)
213 ObjectImpl<Checkable>::ValidateMaxCheckAttempts(lvalue, utils);
216 BOOST_THROW_EXCEPTION(ValidationError(this, { "max_check_attempts" }, "Value must be greater than 0."));