]> granicus.if.org Git - icinga2/blob - lib/checker/checkercomponent.cpp
Move PerfdataValue() class into base library
[icinga2] / lib / checker / checkercomponent.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
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 "checker/checkercomponent.hpp"
21 #include "checker/checkercomponent.tcpp"
22 #include "icinga/icingaapplication.hpp"
23 #include "icinga/cib.hpp"
24 #include "remote/apilistener.hpp"
25 #include "base/configtype.hpp"
26 #include "base/objectlock.hpp"
27 #include "base/utility.hpp"
28 #include "base/perfdatavalue.hpp"
29 #include "base/logger.hpp"
30 #include "base/exception.hpp"
31 #include "base/convert.hpp"
32 #include "base/statsfunction.hpp"
33
34 using namespace icinga;
35
36 REGISTER_TYPE(CheckerComponent);
37
38 REGISTER_STATSFUNCTION(CheckerComponent, &CheckerComponent::StatsFunc);
39
40 void CheckerComponent::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata)
41 {
42         Dictionary::Ptr nodes = new Dictionary();
43
44         for (const CheckerComponent::Ptr& checker : ConfigType::GetObjectsByType<CheckerComponent>()) {
45                 unsigned long idle = checker->GetIdleCheckables();
46                 unsigned long pending = checker->GetPendingCheckables();
47
48                 Dictionary::Ptr stats = new Dictionary();
49                 stats->Set("idle", idle);
50                 stats->Set("pending", pending);
51
52                 nodes->Set(checker->GetName(), stats);
53
54                 String perfdata_prefix = "checkercomponent_" + checker->GetName() + "_";
55                 perfdata->Add(new PerfdataValue(perfdata_prefix + "idle", Convert::ToDouble(idle)));
56                 perfdata->Add(new PerfdataValue(perfdata_prefix + "pending", Convert::ToDouble(pending)));
57         }
58
59         status->Set("checkercomponent", nodes);
60 }
61
62 CheckerComponent::CheckerComponent(void)
63     : m_Stopped(false)
64 { }
65
66 void CheckerComponent::OnConfigLoaded(void)
67 {
68         ConfigObject::OnActiveChanged.connect(bind(&CheckerComponent::ObjectHandler, this, _1));
69         ConfigObject::OnPausedChanged.connect(bind(&CheckerComponent::ObjectHandler, this, _1));
70
71         Checkable::OnNextCheckChanged.connect(bind(&CheckerComponent::NextCheckChangedHandler, this, _1));
72 }
73
74 void CheckerComponent::Start(bool runtimeCreated)
75 {
76         ObjectImpl<CheckerComponent>::Start(runtimeCreated);
77
78         Log(LogInformation, "CheckerComponent")
79             << "'" << GetName() << "' started.";
80
81
82         m_Thread = boost::thread(boost::bind(&CheckerComponent::CheckThreadProc, this));
83
84         m_ResultTimer = new Timer();
85         m_ResultTimer->SetInterval(5);
86         m_ResultTimer->OnTimerExpired.connect(boost::bind(&CheckerComponent::ResultTimerHandler, this));
87         m_ResultTimer->Start();
88 }
89
90 void CheckerComponent::Stop(bool runtimeRemoved)
91 {
92         Log(LogInformation, "CheckerComponent")
93             << "'" << GetName() << "' stopped.";
94
95         {
96                 boost::mutex::scoped_lock lock(m_Mutex);
97                 m_Stopped = true;
98                 m_CV.notify_all();
99         }
100
101         m_ResultTimer->Stop();
102         m_Thread.join();
103
104         ObjectImpl<CheckerComponent>::Stop(runtimeRemoved);
105 }
106
107 void CheckerComponent::CheckThreadProc(void)
108 {
109         Utility::SetThreadName("Check Scheduler");
110
111         boost::mutex::scoped_lock lock(m_Mutex);
112
113         for (;;) {
114                 typedef boost::multi_index::nth_index<CheckableSet, 1>::type CheckTimeView;
115                 CheckTimeView& idx = boost::get<1>(m_IdleCheckables);
116
117                 while (idx.begin() == idx.end() && !m_Stopped)
118                         m_CV.wait(lock);
119
120                 if (m_Stopped)
121                         break;
122
123                 auto it = idx.begin();
124                 CheckableScheduleInfo csi = *it;
125
126                 double wait = csi.NextCheck - Utility::GetTime();
127
128                 if (Checkable::GetPendingChecks() >= GetConcurrentChecks())
129                         wait = 0.5;
130
131                 if (wait > 0) {
132                         /* Wait for the next check. */
133                         m_CV.timed_wait(lock, boost::posix_time::milliseconds(wait * 1000));
134
135                         continue;
136                 }
137
138                 Checkable::Ptr checkable = csi.Object;
139
140                 m_IdleCheckables.erase(checkable);
141
142                 bool forced = checkable->GetForceNextCheck();
143                 bool check = true;
144
145                 if (!forced) {
146                         if (!checkable->IsReachable(DependencyCheckExecution)) {
147                                 Log(LogNotice, "CheckerComponent")
148                                     << "Skipping check for object '" << checkable->GetName() << "': Dependency failed.";
149                                 check = false;
150                         }
151
152                         Host::Ptr host;
153                         Service::Ptr service;
154                         tie(host, service) = GetHostService(checkable);
155
156                         if (host && !service && (!checkable->GetEnableActiveChecks() || !IcingaApplication::GetInstance()->GetEnableHostChecks())) {
157                                 Log(LogNotice, "CheckerComponent")
158                                     << "Skipping check for host '" << host->GetName() << "': active host checks are disabled";
159                                 check = false;
160                         }
161                         if (host && service && (!checkable->GetEnableActiveChecks() || !IcingaApplication::GetInstance()->GetEnableServiceChecks())) {
162                                 Log(LogNotice, "CheckerComponent")
163                                     << "Skipping check for service '" << service->GetName() << "': active service checks are disabled";
164                                 check = false;
165                         }
166
167                         TimePeriod::Ptr tp = checkable->GetCheckPeriod();
168
169                         if (tp && !tp->IsInside(Utility::GetTime())) {
170                                 Log(LogNotice, "CheckerComponent")
171                                     << "Skipping check for object '" << checkable->GetName()
172                                     << "': not in check period '" << tp->GetName() << "'";
173                                 check = false;
174                         }
175                 }
176
177                 /* reschedule the checkable if checks are disabled */
178                 if (!check) {
179                         m_IdleCheckables.insert(GetCheckableScheduleInfo(checkable));
180                         lock.unlock();
181
182                         checkable->UpdateNextCheck();
183
184                         lock.lock();
185
186                         continue;
187                 }
188
189                 m_PendingCheckables.insert(GetCheckableScheduleInfo(checkable));
190
191                 lock.unlock();
192
193                 if (forced) {
194                         ObjectLock olock(checkable);
195                         checkable->SetForceNextCheck(false);
196                 }
197
198                 Log(LogDebug, "CheckerComponent")
199                     << "Executing check for '" << checkable->GetName() << "'";
200
201                 Checkable::IncreasePendingChecks();
202
203                 Utility::QueueAsyncCallback(boost::bind(&CheckerComponent::ExecuteCheckHelper, CheckerComponent::Ptr(this), checkable));
204
205                 lock.lock();
206         }
207 }
208
209 void CheckerComponent::ExecuteCheckHelper(const Checkable::Ptr& checkable)
210 {
211         try {
212                 checkable->ExecuteCheck();
213         } catch (const std::exception& ex) {
214                 CheckResult::Ptr cr = new CheckResult();
215                 cr->SetState(ServiceUnknown);
216
217                 String output = "Exception occured while checking '" + checkable->GetName() + "': " + DiagnosticInformation(ex);
218                 cr->SetOutput(output);
219
220                 double now = Utility::GetTime();
221                 cr->SetScheduleStart(now);
222                 cr->SetScheduleEnd(now);
223                 cr->SetExecutionStart(now);
224                 cr->SetExecutionEnd(now);
225
226                 checkable->ProcessCheckResult(cr);
227
228                 Log(LogCritical, "checker", output);
229         }
230
231         Checkable::DecreasePendingChecks();
232
233         {
234                 boost::mutex::scoped_lock lock(m_Mutex);
235
236                 /* remove the object from the list of pending objects; if it's not in the
237                  * list this was a manual (i.e. forced) check and we must not re-add the
238                  * object to the list because it's already there. */
239                 auto it = m_PendingCheckables.find(checkable);
240
241                 if (it != m_PendingCheckables.end()) {
242                         m_PendingCheckables.erase(it);
243
244                         if (checkable->IsActive())
245                                 m_IdleCheckables.insert(GetCheckableScheduleInfo(checkable));
246
247                         m_CV.notify_all();
248                 }
249         }
250
251         Log(LogDebug, "CheckerComponent")
252             << "Check finished for object '" << checkable->GetName() << "'";
253 }
254
255 void CheckerComponent::ResultTimerHandler(void)
256 {
257         std::ostringstream msgbuf;
258
259         {
260                 boost::mutex::scoped_lock lock(m_Mutex);
261
262                 msgbuf << "Pending checkables: " << m_PendingCheckables.size() << "; Idle checkables: " << m_IdleCheckables.size() << "; Checks/s: "
263                     << (CIB::GetActiveHostChecksStatistics(60) + CIB::GetActiveServiceChecksStatistics(60)) / 60.0;
264         }
265
266         Log(LogNotice, "CheckerComponent", msgbuf.str());
267 }
268
269 void CheckerComponent::ObjectHandler(const ConfigObject::Ptr& object)
270 {
271         Checkable::Ptr checkable = dynamic_pointer_cast<Checkable>(object);
272
273         if (!checkable)
274                 return;
275
276         Zone::Ptr zone = Zone::GetByName(checkable->GetZoneName());
277         bool same_zone = (!zone || Zone::GetLocalZone() == zone);
278
279         {
280                 boost::mutex::scoped_lock lock(m_Mutex);
281
282                 if (object->IsActive() && !object->IsPaused() && same_zone) {
283                         if (m_PendingCheckables.find(checkable) != m_PendingCheckables.end())
284                                 return;
285
286                         m_IdleCheckables.insert(GetCheckableScheduleInfo(checkable));
287                 } else {
288                         m_IdleCheckables.erase(checkable);
289                         m_PendingCheckables.erase(checkable);
290                 }
291
292                 m_CV.notify_all();
293         }
294 }
295
296 CheckableScheduleInfo CheckerComponent::GetCheckableScheduleInfo(const Checkable::Ptr& checkable)
297 {
298         CheckableScheduleInfo csi;
299         csi.Object = checkable;
300         csi.NextCheck = checkable->GetNextCheck();
301         return csi;
302 }
303
304 void CheckerComponent::NextCheckChangedHandler(const Checkable::Ptr& checkable)
305 {
306         boost::mutex::scoped_lock lock(m_Mutex);
307
308         /* remove and re-insert the object from the set in order to force an index update */
309         typedef boost::multi_index::nth_index<CheckableSet, 0>::type CheckableView;
310         CheckableView& idx = boost::get<0>(m_IdleCheckables);
311
312         auto it = idx.find(checkable);
313
314         if (it == idx.end())
315                 return;
316
317         idx.erase(checkable);
318
319         CheckableScheduleInfo csi = GetCheckableScheduleInfo(checkable);
320         idx.insert(csi);
321
322         m_CV.notify_all();
323 }
324
325 unsigned long CheckerComponent::GetIdleCheckables(void)
326 {
327         boost::mutex::scoped_lock lock(m_Mutex);
328
329         return m_IdleCheckables.size();
330 }
331
332 unsigned long CheckerComponent::GetPendingCheckables(void)
333 {
334         boost::mutex::scoped_lock lock(m_Mutex);
335
336         return m_PendingCheckables.size();
337 }