1 /******************************************************************************
3 * Copyright (C) 2012-2014 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/service.h"
21 #include "icinga/servicegroup.h"
22 #include "icinga/checkcommand.h"
23 #include "icinga/icingaapplication.h"
24 #include "icinga/macroprocessor.h"
25 #include "icinga/pluginutility.h"
26 #include "icinga/dependency.h"
27 #include "config/configitembuilder.h"
28 #include "base/dynamictype.h"
29 #include "base/objectlock.h"
30 #include "base/convert.h"
31 #include "base/utility.h"
32 #include "base/initialize.h"
33 #include <boost/foreach.hpp>
34 #include <boost/bind/apply.hpp>
36 using namespace icinga;
38 REGISTER_TYPE(Checkable);
40 INITIALIZE_ONCE(&Checkable::StartDowntimesExpiredTimer);
42 boost::signals2::signal<void (const Checkable::Ptr&, const String&, const String&, AcknowledgementType, double, const String&)> Checkable::OnAcknowledgementSet;
43 boost::signals2::signal<void (const Checkable::Ptr&, const String&)> Checkable::OnAcknowledgementCleared;
45 Checkable::Checkable(void)
46 : m_CheckRunning(false)
49 void Checkable::Start(void)
51 double now = Utility::GetTime();
53 if (GetNextCheck() < now + 300)
56 DynamicObject::Start();
59 void Checkable::OnConfigLoaded(void)
61 DynamicObject::OnConfigLoaded();
63 SetSchedulingOffset(Utility::Random());
66 void Checkable::OnStateLoaded(void)
68 AddDowntimesToCache();
71 std::vector<String> ids;
72 Dictionary::Ptr downtimes = GetDowntimes();
75 ObjectLock dlock(downtimes);
76 BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
77 Downtime::Ptr downtime = kv.second;
79 if (downtime->GetScheduledBy().IsEmpty())
82 ids.push_back(kv.first);
86 BOOST_FOREACH(const String& id, ids) {
87 /* override config owner to clear downtimes once */
88 Downtime::Ptr downtime = GetDowntimeByID(id);
89 downtime->SetConfigOwner(Empty);
90 RemoveDowntime(id, true);
94 void Checkable::AddGroup(const String& name)
96 boost::mutex::scoped_lock lock(m_CheckableMutex);
98 Array::Ptr groups = GetGroups();
100 if (groups && groups->Contains(name))
104 groups = make_shared<Array>();
109 AcknowledgementType Checkable::GetAcknowledgement(void)
113 AcknowledgementType avalue = static_cast<AcknowledgementType>(GetAcknowledgementRaw());
115 if (avalue != AcknowledgementNone) {
116 double expiry = GetAcknowledgementExpiry();
118 if (expiry != 0 && expiry < Utility::GetTime()) {
119 avalue = AcknowledgementNone;
120 ClearAcknowledgement();
127 bool Checkable::IsAcknowledged(void)
129 return GetAcknowledgement() != AcknowledgementNone;
132 void Checkable::AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, double expiry, const String& authority)
135 ObjectLock olock(this);
137 SetAcknowledgementRaw(type);
138 SetAcknowledgementExpiry(expiry);
141 OnNotificationsRequested(GetSelf(), NotificationAcknowledgement, GetLastCheckResult(), author, comment);
143 OnAcknowledgementSet(GetSelf(), author, comment, type, expiry, authority);
146 void Checkable::ClearAcknowledgement(const String& authority)
150 SetAcknowledgementRaw(AcknowledgementNone);
151 SetAcknowledgementExpiry(0);
153 OnAcknowledgementCleared(GetSelf(), authority);
156 bool Checkable::GetEnablePerfdata(void) const
158 if (!GetOverrideEnablePerfdata().IsEmpty())
159 return GetOverrideEnablePerfdata();
161 return GetEnablePerfdataRaw();
164 void Checkable::SetEnablePerfdata(bool enabled, const String& authority)
166 SetOverrideEnablePerfdata(enabled);
169 int Checkable::GetModifiedAttributes(void) const
173 if (!GetOverrideEnableNotifications().IsEmpty())
174 attrs |= ModAttrNotificationsEnabled;
176 if (!GetOverrideEnableActiveChecks().IsEmpty())
177 attrs |= ModAttrActiveChecksEnabled;
179 if (!GetOverrideEnablePassiveChecks().IsEmpty())
180 attrs |= ModAttrPassiveChecksEnabled;
182 if (!GetOverrideEnableFlapping().IsEmpty())
183 attrs |= ModAttrFlapDetectionEnabled;
185 if (!GetOverrideEnableEventHandler().IsEmpty())
186 attrs |= ModAttrEventHandlerEnabled;
188 if (!GetOverrideEnablePerfdata().IsEmpty())
189 attrs |= ModAttrPerformanceDataEnabled;
191 if (!GetOverrideCheckInterval().IsEmpty())
192 attrs |= ModAttrNormalCheckInterval;
194 if (!GetOverrideRetryInterval().IsEmpty())
195 attrs |= ModAttrRetryCheckInterval;
197 if (!GetOverrideEventCommand().IsEmpty())
198 attrs |= ModAttrEventHandlerCommand;
200 if (!GetOverrideCheckCommand().IsEmpty())
201 attrs |= ModAttrCheckCommand;
203 if (!GetOverrideMaxCheckAttempts().IsEmpty())
204 attrs |= ModAttrMaxCheckAttempts;
206 if (!GetOverrideCheckPeriod().IsEmpty())
207 attrs |= ModAttrCheckTimeperiod;
209 if (!GetOverrideVars().IsEmpty())
210 attrs |= ModAttrCustomVariable;
217 void Checkable::SetModifiedAttributes(int flags)
219 if ((flags & ModAttrNotificationsEnabled) == 0)
220 SetOverrideEnableNotifications(Empty);
222 if ((flags & ModAttrActiveChecksEnabled) == 0)
223 SetOverrideEnableActiveChecks(Empty);
225 if ((flags & ModAttrPassiveChecksEnabled) == 0)
226 SetOverrideEnablePassiveChecks(Empty);
228 if ((flags & ModAttrFlapDetectionEnabled) == 0)
229 SetOverrideEnableFlapping(Empty);
231 if ((flags & ModAttrEventHandlerEnabled) == 0)
232 SetOverrideEnableEventHandler(Empty);
234 if ((flags & ModAttrPerformanceDataEnabled) == 0)
235 SetOverrideEnablePerfdata(Empty);
237 if ((flags & ModAttrNormalCheckInterval) == 0)
238 SetOverrideCheckInterval(Empty);
240 if ((flags & ModAttrRetryCheckInterval) == 0)
241 SetOverrideRetryInterval(Empty);
243 if ((flags & ModAttrEventHandlerCommand) == 0)
244 SetOverrideEventCommand(Empty);
246 if ((flags & ModAttrCheckCommand) == 0)
247 SetOverrideCheckCommand(Empty);
249 if ((flags & ModAttrMaxCheckAttempts) == 0)
250 SetOverrideMaxCheckAttempts(Empty);
252 if ((flags & ModAttrCheckTimeperiod) == 0)
253 SetOverrideCheckPeriod(Empty);
255 if ((flags & ModAttrCustomVariable) == 0) {
256 SetOverrideVars(Empty);
257 OnVarsChanged(GetSelf());