1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.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 checkableZone = Zone::GetLocalZone();
63 Zone::Ptr cmdZone = endpoint->GetZone();
65 if (checkableZone && cmdZone != checkableZone && cmdZone->GetParent() != checkableZone) {
66 BOOST_THROW_EXCEPTION(ValidationError(this, { "command_endpoint" },
67 "Command endpoint must be in zone '" + checkableZone->GetName() + "' or in a direct child zone thereof."));
72 void Checkable::Start(bool runtimeCreated)
74 double now = Utility::GetTime();
76 if (GetNextCheck() < now + 300)
79 ObjectImpl<Checkable>::Start(runtimeCreated);
82 void Checkable::AddGroup(const String& name)
84 boost::mutex::scoped_lock lock(m_CheckableMutex);
87 auto *host = dynamic_cast<Host *>(this);
90 groups = host->GetGroups();
92 groups = static_cast<Service *>(this)->GetGroups();
94 if (groups && groups->Contains(name))
103 AcknowledgementType Checkable::GetAcknowledgement()
105 auto avalue = static_cast<AcknowledgementType>(GetAcknowledgementRaw());
107 if (avalue != AcknowledgementNone) {
108 double expiry = GetAcknowledgementExpiry();
110 if (expiry != 0 && expiry < Utility::GetTime()) {
111 avalue = AcknowledgementNone;
112 ClearAcknowledgement();
119 bool Checkable::IsAcknowledged() const
121 return const_cast<Checkable *>(this)->GetAcknowledgement() != AcknowledgementNone;
124 void Checkable::AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, bool notify, bool persistent, double expiry, const MessageOrigin::Ptr& origin)
126 SetAcknowledgementRaw(type);
127 SetAcknowledgementExpiry(expiry);
129 if (notify && !IsPaused())
130 OnNotificationsRequested(this, NotificationAcknowledgement, GetLastCheckResult(), author, comment, nullptr);
132 OnAcknowledgementSet(this, author, comment, type, notify, persistent, expiry, origin);
135 void Checkable::ClearAcknowledgement(const MessageOrigin::Ptr& origin)
137 SetAcknowledgementRaw(AcknowledgementNone);
138 SetAcknowledgementExpiry(0);
140 OnAcknowledgementCleared(this, origin);
143 Endpoint::Ptr Checkable::GetCommandEndpoint() const
145 return Endpoint::GetByName(GetCommandEndpointRaw());
148 int Checkable::GetSeverity() const
150 /* overridden in Host/Service class. */
154 void Checkable::NotifyFixedDowntimeStart(const Downtime::Ptr& downtime)
156 if (!downtime->GetFixed())
159 NotifyDowntimeInternal(downtime);
162 void Checkable::NotifyFlexibleDowntimeStart(const Downtime::Ptr& downtime)
164 if (downtime->GetFixed())
167 NotifyDowntimeInternal(downtime);
170 void Checkable::NotifyDowntimeInternal(const Downtime::Ptr& downtime)
172 Checkable::Ptr checkable = downtime->GetCheckable();
174 if (!checkable->IsPaused())
175 OnNotificationsRequested(checkable, NotificationDowntimeStart, checkable->GetLastCheckResult(), downtime->GetAuthor(), downtime->GetComment(), nullptr);
178 void Checkable::NotifyDowntimeEnd(const Downtime::Ptr& downtime)
180 /* don't send notifications for flexible downtimes which never triggered */
181 if (!downtime->GetFixed() && !downtime->IsTriggered())
184 Checkable::Ptr checkable = downtime->GetCheckable();
186 if (!checkable->IsPaused())
187 OnNotificationsRequested(checkable, NotificationDowntimeEnd, checkable->GetLastCheckResult(), downtime->GetAuthor(), downtime->GetComment(), nullptr);
190 void Checkable::ValidateCheckInterval(const Lazy<double>& lvalue, const ValidationUtils& utils)
192 ObjectImpl<Checkable>::ValidateCheckInterval(lvalue, utils);
195 BOOST_THROW_EXCEPTION(ValidationError(this, { "check_interval" }, "Interval must be greater than 0."));
198 void Checkable::ValidateMaxCheckAttempts(const Lazy<int>& lvalue, const ValidationUtils& utils)
200 ObjectImpl<Checkable>::ValidateMaxCheckAttempts(lvalue, utils);
203 BOOST_THROW_EXCEPTION(ValidationError(this, { "max_check_attempts" }, "Value must be greater than 0."));