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(Checkable);
34 boost::signals2::signal<void (const Checkable::Ptr&, const String&, const String&, AcknowledgementType, bool, double, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementSet;
35 boost::signals2::signal<void (const Checkable::Ptr&, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementCleared;
37 Checkable::Checkable(void)
38 : m_CheckRunning(false)
40 SetSchedulingOffset(Utility::Random());
43 void Checkable::Start(bool runtimeCreated)
45 double now = Utility::GetTime();
47 if (GetNextCheck() < now + 300)
50 ObjectImpl<Checkable>::Start(runtimeCreated);
53 void Checkable::AddGroup(const String& name)
55 boost::mutex::scoped_lock lock(m_CheckableMutex);
58 Host *host = dynamic_cast<Host *>(this);
61 groups = host->GetGroups();
63 groups = static_cast<Service *>(this)->GetGroups();
65 if (groups && groups->Contains(name))
74 AcknowledgementType Checkable::GetAcknowledgement(void)
76 AcknowledgementType avalue = static_cast<AcknowledgementType>(GetAcknowledgementRaw());
78 if (avalue != AcknowledgementNone) {
79 double expiry = GetAcknowledgementExpiry();
81 if (expiry != 0 && expiry < Utility::GetTime()) {
82 avalue = AcknowledgementNone;
83 ClearAcknowledgement();
90 bool Checkable::IsAcknowledged(void)
92 return GetAcknowledgement() != AcknowledgementNone;
95 void Checkable::AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, bool notify, double expiry, const MessageOrigin::Ptr& origin)
97 SetAcknowledgementRaw(type);
98 SetAcknowledgementExpiry(expiry);
101 OnNotificationsRequested(this, NotificationAcknowledgement, GetLastCheckResult(), author, comment);
103 OnAcknowledgementSet(this, author, comment, type, notify, expiry, origin);
106 void Checkable::ClearAcknowledgement(const MessageOrigin::Ptr& origin)
108 SetAcknowledgementRaw(AcknowledgementNone);
109 SetAcknowledgementExpiry(0);
111 OnAcknowledgementCleared(this, origin);
114 Endpoint::Ptr Checkable::GetCommandEndpoint(void) const
116 return Endpoint::GetByName(GetCommandEndpointRaw());
119 void Checkable::ValidateCheckInterval(double value, const ValidationUtils& utils)
121 ObjectImpl<Checkable>::ValidateCheckInterval(value, utils);
124 BOOST_THROW_EXCEPTION(ValidationError(this, boost::assign::list_of("check_interval"), "Interval must be greater than 0."));