1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
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.tcpp"
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"
27 #include <boost/foreach.hpp>
28 #include <boost/bind/apply.hpp>
30 using namespace icinga;
32 REGISTER_TYPE_WITH_PROTOTYPE(Checkable, Checkable::GetPrototype());
33 INITIALIZE_ONCE(&Checkable::StaticInitialize);
35 boost::signals2::signal<void (const Checkable::Ptr&, const String&, const String&, AcknowledgementType, bool, double, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementSet;
36 boost::signals2::signal<void (const Checkable::Ptr&, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementCleared;
38 void Checkable::StaticInitialize(void)
40 /* fixed downtime start */
41 Downtime::OnDowntimeAdded.connect(boost::bind(&Checkable::NotifyFixedDowntimeStart, _1));
42 /* flexible downtime start */
43 Downtime::OnDowntimeTriggered.connect(boost::bind(&Checkable::NotifyFlexibleDowntimeStart, _1));
44 /* fixed/flexible downtime end */
45 Downtime::OnDowntimeRemoved.connect(boost::bind(&Checkable::NotifyDowntimeEnd, _1));
48 Checkable::Checkable(void)
49 : m_CheckRunning(false)
51 SetSchedulingOffset(Utility::Random());
54 void Checkable::Start(bool runtimeCreated)
56 double now = Utility::GetTime();
58 if (GetNextCheck() < now + 300)
61 ObjectImpl<Checkable>::Start(runtimeCreated);
64 void Checkable::AddGroup(const String& name)
66 boost::mutex::scoped_lock lock(m_CheckableMutex);
69 Host *host = dynamic_cast<Host *>(this);
72 groups = host->GetGroups();
74 groups = static_cast<Service *>(this)->GetGroups();
76 if (groups && groups->Contains(name))
85 AcknowledgementType Checkable::GetAcknowledgement(void)
87 AcknowledgementType avalue = static_cast<AcknowledgementType>(GetAcknowledgementRaw());
89 if (avalue != AcknowledgementNone) {
90 double expiry = GetAcknowledgementExpiry();
92 if (expiry != 0 && expiry < Utility::GetTime()) {
93 avalue = AcknowledgementNone;
94 ClearAcknowledgement();
101 bool Checkable::IsAcknowledged(void)
103 return GetAcknowledgement() != AcknowledgementNone;
106 void Checkable::AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, bool notify, double expiry, const MessageOrigin::Ptr& origin)
108 SetAcknowledgementRaw(type);
109 SetAcknowledgementExpiry(expiry);
111 if (notify && !IsPaused())
112 OnNotificationsRequested(this, NotificationAcknowledgement, GetLastCheckResult(), author, comment, MessageOrigin::Ptr());
114 OnAcknowledgementSet(this, author, comment, type, notify, expiry, origin);
117 void Checkable::ClearAcknowledgement(const MessageOrigin::Ptr& origin)
119 SetAcknowledgementRaw(AcknowledgementNone);
120 SetAcknowledgementExpiry(0);
122 OnAcknowledgementCleared(this, origin);
125 Endpoint::Ptr Checkable::GetCommandEndpoint(void) const
127 return Endpoint::GetByName(GetCommandEndpointRaw());
130 void Checkable::NotifyFixedDowntimeStart(const Downtime::Ptr& downtime)
132 if (!downtime->GetFixed())
135 NotifyDowntimeInternal(downtime);
138 void Checkable::NotifyFlexibleDowntimeStart(const Downtime::Ptr& downtime)
140 if (downtime->GetFixed())
143 NotifyDowntimeInternal(downtime);
146 void Checkable::NotifyDowntimeInternal(const Downtime::Ptr& downtime)
148 Checkable::Ptr checkable = downtime->GetCheckable();
150 if (!checkable->IsPaused())
151 OnNotificationsRequested(checkable, NotificationDowntimeStart, checkable->GetLastCheckResult(), downtime->GetAuthor(), downtime->GetComment(), MessageOrigin::Ptr());
154 void Checkable::NotifyDowntimeEnd(const Downtime::Ptr& downtime)
156 /* don't send notifications for flexible downtimes which never triggered */
157 if (!downtime->GetFixed() && !downtime->IsTriggered())
160 Checkable::Ptr checkable = downtime->GetCheckable();
162 if (!checkable->IsPaused())
163 OnNotificationsRequested(checkable, NotificationDowntimeEnd, checkable->GetLastCheckResult(), downtime->GetAuthor(), downtime->GetComment(), MessageOrigin::Ptr());
166 void Checkable::ValidateCheckInterval(double value, const ValidationUtils& utils)
168 ObjectImpl<Checkable>::ValidateCheckInterval(value, utils);
171 BOOST_THROW_EXCEPTION(ValidationError(this, boost::assign::list_of("check_interval"), "Interval must be greater than 0."));