]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable.cpp
Update Vagrant documentation link.
[icinga2] / lib / icinga / checkable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 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/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>
35
36 using namespace icinga;
37
38 REGISTER_TYPE(Checkable);
39
40 INITIALIZE_ONCE(&Checkable::StartDowntimesExpiredTimer);
41
42 boost::signals2::signal<void (const Checkable::Ptr&, const String&, const String&, AcknowledgementType, double, const MessageOrigin&)> Checkable::OnAcknowledgementSet;
43 boost::signals2::signal<void (const Checkable::Ptr&, const MessageOrigin&)> Checkable::OnAcknowledgementCleared;
44
45 Checkable::Checkable(void)
46         : m_CheckRunning(false)
47 { }
48
49 void Checkable::Start(void)
50 {
51         double now = Utility::GetTime();
52
53         if (GetNextCheck() < now + 300)
54                 UpdateNextCheck();
55
56         DynamicObject::Start();
57 }
58
59 void Checkable::OnConfigLoaded(void)
60 {
61         DynamicObject::OnConfigLoaded();
62
63         SetSchedulingOffset(Utility::Random());
64 }
65
66 void Checkable::OnStateLoaded(void)
67 {
68         AddDowntimesToCache();
69         AddCommentsToCache();
70
71         std::vector<String> ids;
72         Dictionary::Ptr downtimes = GetDowntimes();
73
74         {
75                 ObjectLock dlock(downtimes);
76                 BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
77                         Downtime::Ptr downtime = kv.second;
78
79                         if (downtime->GetScheduledBy().IsEmpty())
80                                 continue;
81
82                         ids.push_back(kv.first);
83                 }
84         }
85
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);
91         }
92 }
93
94 void Checkable::AddGroup(const String& name)
95 {
96         boost::mutex::scoped_lock lock(m_CheckableMutex);
97
98         Array::Ptr groups = GetGroups();
99
100         if (groups && groups->Contains(name))
101                 return;
102
103         if (!groups)
104                 groups = make_shared<Array>();
105
106         groups->Add(name);
107 }
108
109 AcknowledgementType Checkable::GetAcknowledgement(void)
110 {
111         ASSERT(OwnsLock());
112
113         AcknowledgementType avalue = static_cast<AcknowledgementType>(GetAcknowledgementRaw());
114
115         if (avalue != AcknowledgementNone) {
116                 double expiry = GetAcknowledgementExpiry();
117
118                 if (expiry != 0 && expiry < Utility::GetTime()) {
119                         avalue = AcknowledgementNone;
120                         ClearAcknowledgement();
121                 }
122         }
123
124         return avalue;
125 }
126
127 bool Checkable::IsAcknowledged(void)
128 {
129         return GetAcknowledgement() != AcknowledgementNone;
130 }
131
132 void Checkable::AcknowledgeProblem(const String& author, const String& comment, AcknowledgementType type, double expiry, const MessageOrigin& origin)
133 {
134         {
135                 ObjectLock olock(this);
136
137                 SetAcknowledgementRaw(type);
138                 SetAcknowledgementExpiry(expiry);
139         }
140
141         OnNotificationsRequested(GetSelf(), NotificationAcknowledgement, GetLastCheckResult(), author, comment);
142
143         OnAcknowledgementSet(GetSelf(), author, comment, type, expiry, origin);
144 }
145
146 void Checkable::ClearAcknowledgement(const MessageOrigin& origin)
147 {
148         ASSERT(OwnsLock());
149
150         SetAcknowledgementRaw(AcknowledgementNone);
151         SetAcknowledgementExpiry(0);
152
153         OnAcknowledgementCleared(GetSelf(), origin);
154 }
155
156 bool Checkable::GetEnablePerfdata(void) const
157 {
158         if (!GetOverrideEnablePerfdata().IsEmpty())
159                 return GetOverrideEnablePerfdata();
160         else
161                 return GetEnablePerfdataRaw();
162 }
163
164 void Checkable::SetEnablePerfdata(bool enabled, const MessageOrigin& origin)
165 {
166         SetOverrideEnablePerfdata(enabled);
167 }
168
169 int Checkable::GetModifiedAttributes(void) const
170 {
171         int attrs = 0;
172
173         if (!GetOverrideEnableNotifications().IsEmpty())
174                 attrs |= ModAttrNotificationsEnabled;
175
176         if (!GetOverrideEnableActiveChecks().IsEmpty())
177                 attrs |= ModAttrActiveChecksEnabled;
178
179         if (!GetOverrideEnablePassiveChecks().IsEmpty())
180                 attrs |= ModAttrPassiveChecksEnabled;
181
182         if (!GetOverrideEnableFlapping().IsEmpty())
183                 attrs |= ModAttrFlapDetectionEnabled;
184
185         if (!GetOverrideEnableEventHandler().IsEmpty())
186                 attrs |= ModAttrEventHandlerEnabled;
187
188         if (!GetOverrideEnablePerfdata().IsEmpty())
189                 attrs |= ModAttrPerformanceDataEnabled;
190
191         if (!GetOverrideCheckInterval().IsEmpty())
192                 attrs |= ModAttrNormalCheckInterval;
193
194         if (!GetOverrideRetryInterval().IsEmpty())
195                 attrs |= ModAttrRetryCheckInterval;
196
197         if (!GetOverrideEventCommand().IsEmpty())
198                 attrs |= ModAttrEventHandlerCommand;
199
200         if (!GetOverrideCheckCommand().IsEmpty())
201                 attrs |= ModAttrCheckCommand;
202
203         if (!GetOverrideMaxCheckAttempts().IsEmpty())
204                 attrs |= ModAttrMaxCheckAttempts;
205
206         if (!GetOverrideCheckPeriod().IsEmpty())
207                 attrs |= ModAttrCheckTimeperiod;
208
209         if (!GetOverrideVars().IsEmpty())
210                 attrs |= ModAttrCustomVariable;
211
212         // TODO: finish
213
214         return attrs;
215 }
216
217 void Checkable::SetModifiedAttributes(int flags)
218 {
219         if ((flags & ModAttrNotificationsEnabled) == 0)
220                 SetOverrideEnableNotifications(Empty);
221
222         if ((flags & ModAttrActiveChecksEnabled) == 0)
223                 SetOverrideEnableActiveChecks(Empty);
224
225         if ((flags & ModAttrPassiveChecksEnabled) == 0)
226                 SetOverrideEnablePassiveChecks(Empty);
227
228         if ((flags & ModAttrFlapDetectionEnabled) == 0)
229                 SetOverrideEnableFlapping(Empty);
230
231         if ((flags & ModAttrEventHandlerEnabled) == 0)
232                 SetOverrideEnableEventHandler(Empty);
233
234         if ((flags & ModAttrPerformanceDataEnabled) == 0)
235                 SetOverrideEnablePerfdata(Empty);
236
237         if ((flags & ModAttrNormalCheckInterval) == 0)
238                 SetOverrideCheckInterval(Empty);
239
240         if ((flags & ModAttrRetryCheckInterval) == 0)
241                 SetOverrideRetryInterval(Empty);
242
243         if ((flags & ModAttrEventHandlerCommand) == 0)
244                 SetOverrideEventCommand(Empty);
245
246         if ((flags & ModAttrCheckCommand) == 0)
247                 SetOverrideCheckCommand(Empty);
248
249         if ((flags & ModAttrMaxCheckAttempts) == 0)
250                 SetOverrideMaxCheckAttempts(Empty);
251
252         if ((flags & ModAttrCheckTimeperiod) == 0)
253                 SetOverrideCheckPeriod(Empty);
254
255         if ((flags & ModAttrCustomVariable) == 0) {
256                 SetOverrideVars(Empty);
257                 OnVarsChanged(GetSelf());
258         }
259 }