1 /******************************************************************************
3 * Copyright (C) 2012-2015 Icinga Development Team (http://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(void)
45 double now = Utility::GetTime();
47 if (GetNextCheck() < now + 300)
50 ObjectImpl<Checkable>::Start();
53 void Checkable::OnStateLoaded(void)
55 AddDowntimesToCache();
58 std::vector<String> ids;
59 Dictionary::Ptr downtimes = GetDowntimes();
62 ObjectLock dlock(downtimes);
63 BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
64 Downtime::Ptr downtime = kv.second;
66 if (downtime->GetScheduledBy().IsEmpty())
69 ids.push_back(kv.first);
73 BOOST_FOREACH(const String& id, ids) {
74 /* override config owner to clear downtimes once */
75 Downtime::Ptr downtime = GetDowntimeByID(id);
76 downtime->SetConfigOwner(Empty);
77 RemoveDowntime(id, true);
81 void Checkable::AddGroup(const String& name)
83 boost::mutex::scoped_lock lock(m_CheckableMutex);
86 Host *host = dynamic_cast<Host *>(this);
89 groups = host->GetGroups();
91 groups = static_cast<Service *>(this)->GetGroups();
93 if (groups && groups->Contains(name))
102 AcknowledgementType Checkable::GetAcknowledgement(void)
104 AcknowledgementType avalue = static_cast<AcknowledgementType>(GetAcknowledgementRaw());
106 if (avalue != AcknowledgementNone) {
107 double expiry = GetAcknowledgementExpiry();
109 if (expiry != 0 && expiry < Utility::GetTime()) {
110 avalue = AcknowledgementNone;
111 ClearAcknowledgement();
118 bool Checkable::IsAcknowledged(void)
120 return GetAcknowledgement() != AcknowledgementNone;
123 void Checkable::AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, bool notify, double expiry, const MessageOrigin::Ptr& origin)
125 SetAcknowledgementRaw(type);
126 SetAcknowledgementExpiry(expiry);
129 OnNotificationsRequested(this, NotificationAcknowledgement, GetLastCheckResult(), author, comment);
131 OnAcknowledgementSet(this, author, comment, type, notify, expiry, origin);
134 void Checkable::ClearAcknowledgement(const MessageOrigin::Ptr& origin)
136 SetAcknowledgementRaw(AcknowledgementNone);
137 SetAcknowledgementExpiry(0);
139 OnAcknowledgementCleared(this, origin);
142 Endpoint::Ptr Checkable::GetCommandEndpoint(void) const
144 return Endpoint::GetByName(GetCommandEndpointRaw());
147 void Checkable::ValidateCheckInterval(double value, const ValidationUtils& utils)
149 ObjectImpl<Checkable>::ValidateCheckInterval(value, utils);
152 BOOST_THROW_EXCEPTION(ValidationError(this, boost::assign::list_of("check_interval"), "Interval must be greater than 0."));