]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable.cpp
Update copyright year
[icinga2] / lib / icinga / checkable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #include "icinga/checkable.hpp"
21 #include "base/objectlock.hpp"
22 #include "base/utility.hpp"
23 #include "base/function.hpp"
24 #include "base/exception.hpp"
25 #include <boost/foreach.hpp>
26 #include <boost/bind/apply.hpp>
27
28 using namespace icinga;
29
30 REGISTER_TYPE(Checkable);
31 REGISTER_SCRIPTFUNCTION(ValidateCheckableCheckInterval, &Checkable::ValidateCheckInterval);
32
33 boost::signals2::signal<void (const Checkable::Ptr&, bool, const MessageOrigin&)> Checkable::OnEnablePerfdataChanged;
34 boost::signals2::signal<void (const Checkable::Ptr&, const String&, const String&, AcknowledgementType, double, const MessageOrigin&)> Checkable::OnAcknowledgementSet;
35 boost::signals2::signal<void (const Checkable::Ptr&, const MessageOrigin&)> Checkable::OnAcknowledgementCleared;
36
37 Checkable::Checkable(void)
38         : m_CheckRunning(false)
39 { }
40
41 void Checkable::Start(void)
42 {
43         double now = Utility::GetTime();
44
45         if (GetNextCheck() < now + 300)
46                 UpdateNextCheck();
47
48         DynamicObject::Start();
49 }
50
51 void Checkable::OnConfigLoaded(void)
52 {
53         DynamicObject::OnConfigLoaded();
54
55         SetSchedulingOffset(Utility::Random());
56 }
57
58 void Checkable::OnStateLoaded(void)
59 {
60         AddDowntimesToCache();
61         AddCommentsToCache();
62
63         std::vector<String> ids;
64         Dictionary::Ptr downtimes = GetDowntimes();
65
66         {
67                 ObjectLock dlock(downtimes);
68                 BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
69                         Downtime::Ptr downtime = kv.second;
70
71                         if (downtime->GetScheduledBy().IsEmpty())
72                                 continue;
73
74                         ids.push_back(kv.first);
75                 }
76         }
77
78         BOOST_FOREACH(const String& id, ids) {
79                 /* override config owner to clear downtimes once */
80                 Downtime::Ptr downtime = GetDowntimeByID(id);
81                 downtime->SetConfigOwner(Empty);
82                 RemoveDowntime(id, true);
83         }
84 }
85
86 void Checkable::AddGroup(const String& name)
87 {
88         boost::mutex::scoped_lock lock(m_CheckableMutex);
89
90         Array::Ptr groups = GetGroups();
91
92         if (groups && groups->Contains(name))
93                 return;
94
95         if (!groups)
96                 groups = new Array();
97
98         groups->Add(name);
99 }
100
101 AcknowledgementType Checkable::GetAcknowledgement(void)
102 {
103         ASSERT(OwnsLock());
104
105         AcknowledgementType avalue = static_cast<AcknowledgementType>(GetAcknowledgementRaw());
106
107         if (avalue != AcknowledgementNone) {
108                 double expiry = GetAcknowledgementExpiry();
109
110                 if (expiry != 0 && expiry < Utility::GetTime()) {
111                         avalue = AcknowledgementNone;
112                         ClearAcknowledgement();
113                 }
114         }
115
116         return avalue;
117 }
118
119 bool Checkable::IsAcknowledged(void)
120 {
121         return GetAcknowledgement() != AcknowledgementNone;
122 }
123
124 void Checkable::AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, double expiry, const MessageOrigin& origin)
125 {
126         {
127                 ObjectLock olock(this);
128
129                 SetAcknowledgementRaw(type);
130                 SetAcknowledgementExpiry(expiry);
131         }
132
133         OnNotificationsRequested(this, NotificationAcknowledgement, GetLastCheckResult(), author, comment);
134
135         OnAcknowledgementSet(this, author, comment, type, expiry, origin);
136 }
137
138 void Checkable::ClearAcknowledgement(const MessageOrigin& origin)
139 {
140         ASSERT(OwnsLock());
141
142         SetAcknowledgementRaw(AcknowledgementNone);
143         SetAcknowledgementExpiry(0);
144
145         OnAcknowledgementCleared(this, origin);
146 }
147
148 bool Checkable::GetEnablePerfdata(void) const
149 {
150         if (!GetOverrideEnablePerfdata().IsEmpty())
151                 return GetOverrideEnablePerfdata();
152         else
153                 return GetEnablePerfdataRaw();
154 }
155
156 void Checkable::SetEnablePerfdata(bool enabled, const MessageOrigin& origin)
157 {
158         SetOverrideEnablePerfdata(enabled);
159
160         OnEnablePerfdataChanged(this, enabled, origin);
161 }
162
163 int Checkable::GetModifiedAttributes(void) const
164 {
165         int attrs = 0;
166
167         if (!GetOverrideEnableNotifications().IsEmpty())
168                 attrs |= ModAttrNotificationsEnabled;
169
170         if (!GetOverrideEnableActiveChecks().IsEmpty())
171                 attrs |= ModAttrActiveChecksEnabled;
172
173         if (!GetOverrideEnablePassiveChecks().IsEmpty())
174                 attrs |= ModAttrPassiveChecksEnabled;
175
176         if (!GetOverrideEnableFlapping().IsEmpty())
177                 attrs |= ModAttrFlapDetectionEnabled;
178
179         if (!GetOverrideEnableEventHandler().IsEmpty())
180                 attrs |= ModAttrEventHandlerEnabled;
181
182         if (!GetOverrideEnablePerfdata().IsEmpty())
183                 attrs |= ModAttrPerformanceDataEnabled;
184
185         if (!GetOverrideCheckInterval().IsEmpty())
186                 attrs |= ModAttrNormalCheckInterval;
187
188         if (!GetOverrideRetryInterval().IsEmpty())
189                 attrs |= ModAttrRetryCheckInterval;
190
191         if (!GetOverrideEventCommand().IsEmpty())
192                 attrs |= ModAttrEventHandlerCommand;
193
194         if (!GetOverrideCheckCommand().IsEmpty())
195                 attrs |= ModAttrCheckCommand;
196
197         if (!GetOverrideMaxCheckAttempts().IsEmpty())
198                 attrs |= ModAttrMaxCheckAttempts;
199
200         if (!GetOverrideCheckPeriod().IsEmpty())
201                 attrs |= ModAttrCheckTimeperiod;
202
203         if (GetOverrideVars())
204                 attrs |= ModAttrCustomVariable;
205
206         // TODO: finish
207
208         return attrs;
209 }
210
211 void Checkable::SetModifiedAttributes(int flags, const MessageOrigin& origin)
212 {
213         if ((flags & ModAttrNotificationsEnabled) == 0) {
214                 SetOverrideEnableNotifications(Empty);
215                 OnEnableNotificationsChanged(this, GetEnableNotifications(), origin);
216         }
217
218         if ((flags & ModAttrActiveChecksEnabled) == 0) {
219                 SetOverrideEnableActiveChecks(Empty);
220                 OnEnableActiveChecksChanged(this, GetEnableActiveChecks(), origin);
221         }
222
223         if ((flags & ModAttrPassiveChecksEnabled) == 0) {
224                 SetOverrideEnablePassiveChecks(Empty);
225                 OnEnablePassiveChecksChanged(this, GetEnablePassiveChecks(), origin);
226         }
227
228         if ((flags & ModAttrFlapDetectionEnabled) == 0) {
229                 SetOverrideEnableFlapping(Empty);
230                 OnEnableFlappingChanged(this, GetEnableFlapping(), origin);
231         }
232
233         if ((flags & ModAttrEventHandlerEnabled) == 0)
234                 SetOverrideEnableEventHandler(Empty);
235
236         if ((flags & ModAttrPerformanceDataEnabled) == 0) {
237                 SetOverrideEnablePerfdata(Empty);
238                 OnEnablePerfdataChanged(this, GetEnablePerfdata(), origin);
239         }
240
241         if ((flags & ModAttrNormalCheckInterval) == 0)
242                 SetOverrideCheckInterval(Empty);
243
244         if ((flags & ModAttrRetryCheckInterval) == 0)
245                 SetOverrideRetryInterval(Empty);
246
247         if ((flags & ModAttrEventHandlerCommand) == 0)
248                 SetOverrideEventCommand(Empty);
249
250         if ((flags & ModAttrCheckCommand) == 0)
251                 SetOverrideCheckCommand(Empty);
252
253         if ((flags & ModAttrMaxCheckAttempts) == 0)
254                 SetOverrideMaxCheckAttempts(Empty);
255
256         if ((flags & ModAttrCheckTimeperiod) == 0)
257                 SetOverrideCheckPeriod(Empty);
258
259         if ((flags & ModAttrCustomVariable) == 0) {
260                 SetOverrideVars(Empty);
261                 OnVarsChanged(this, GetVars(), origin);
262         }
263 }
264
265 Endpoint::Ptr Checkable::GetCommandEndpoint(void) const
266 {
267         return Endpoint::GetByName(GetCommandEndpointRaw());
268 }
269
270 void Checkable::ValidateCheckInterval(const String& location, const Checkable::Ptr& object)
271 {
272         if (object->GetCheckInterval() <= 0) {
273                 BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
274                     location + ": check_interval must be greater than 0.", object->GetDebugInfo()));
275         }
276 }