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