1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
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 "checker/checkercomponent.hpp"
21 #include "checker/checkercomponent-ti.cpp"
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"
34 using namespace icinga;
36 REGISTER_TYPE(CheckerComponent);
38 REGISTER_STATSFUNCTION(CheckerComponent, &CheckerComponent::StatsFunc);
40 void CheckerComponent::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata)
44 for (const CheckerComponent::Ptr& checker : ConfigType::GetObjectsByType<CheckerComponent>()) {
45 unsigned long idle = checker->GetIdleCheckables();
46 unsigned long pending = checker->GetPendingCheckables();
48 nodes.emplace_back(checker->GetName(), new Dictionary({
50 { "pending", pending }
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)));
58 status->Set("checkercomponent", new Dictionary(std::move(nodes)));
61 void CheckerComponent::OnConfigLoaded()
63 ConfigObject::OnActiveChanged.connect(std::bind(&CheckerComponent::ObjectHandler, this, _1));
64 ConfigObject::OnPausedChanged.connect(std::bind(&CheckerComponent::ObjectHandler, this, _1));
66 Checkable::OnNextCheckChanged.connect(std::bind(&CheckerComponent::NextCheckChangedHandler, this, _1));
69 void CheckerComponent::Start(bool runtimeCreated)
71 ObjectImpl<CheckerComponent>::Start(runtimeCreated);
73 Log(LogInformation, "CheckerComponent")
74 << "'" << GetName() << "' started.";
77 m_Thread = std::thread(std::bind(&CheckerComponent::CheckThreadProc, this));
79 m_ResultTimer = new Timer();
80 m_ResultTimer->SetInterval(5);
81 m_ResultTimer->OnTimerExpired.connect(std::bind(&CheckerComponent::ResultTimerHandler, this));
82 m_ResultTimer->Start();
85 void CheckerComponent::Stop(bool runtimeRemoved)
87 Log(LogInformation, "CheckerComponent")
88 << "'" << GetName() << "' stopped.";
91 boost::mutex::scoped_lock lock(m_Mutex);
96 m_ResultTimer->Stop();
99 ObjectImpl<CheckerComponent>::Stop(runtimeRemoved);
102 void CheckerComponent::CheckThreadProc()
104 Utility::SetThreadName("Check Scheduler");
106 boost::mutex::scoped_lock lock(m_Mutex);
109 typedef boost::multi_index::nth_index<CheckableSet, 1>::type CheckTimeView;
110 CheckTimeView& idx = boost::get<1>(m_IdleCheckables);
112 while (idx.begin() == idx.end() && !m_Stopped)
118 auto it = idx.begin();
119 CheckableScheduleInfo csi = *it;
121 double wait = csi.NextCheck - Utility::GetTime();
123 if (Checkable::GetPendingChecks() >= GetConcurrentChecks())
127 /* Wait for the next check. */
128 m_CV.timed_wait(lock, boost::posix_time::milliseconds(long(wait * 1000)));
133 Checkable::Ptr checkable = csi.Object;
135 m_IdleCheckables.erase(checkable);
137 bool forced = checkable->GetForceNextCheck();
141 if (!checkable->IsReachable(DependencyCheckExecution)) {
142 Log(LogNotice, "CheckerComponent")
143 << "Skipping check for object '" << checkable->GetName() << "': Dependency failed.";
148 Service::Ptr service;
149 tie(host, service) = GetHostService(checkable);
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";
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";
162 TimePeriod::Ptr tp = checkable->GetCheckPeriod();
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() << "'";
172 /* reschedule the checkable if checks are disabled */
174 m_IdleCheckables.insert(GetCheckableScheduleInfo(checkable));
177 Log(LogDebug, "CheckerComponent")
178 << "Checks for checkable '" << checkable->GetName() << "' are disabled. Rescheduling check.";
180 checkable->UpdateNextCheck();
188 csi = GetCheckableScheduleInfo(checkable);
190 Log(LogDebug, "CheckerComponent")
191 << "Scheduling info for checkable '" << checkable->GetName() << "' ("
192 << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", checkable->GetNextCheck()) << "): Object '"
193 << csi.Object->GetName() << "', Next Check: "
194 << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", csi.NextCheck) << "(" << csi.NextCheck << ").";
196 m_PendingCheckables.insert(csi);
201 ObjectLock olock(checkable);
202 checkable->SetForceNextCheck(false);
205 Log(LogDebug, "CheckerComponent")
206 << "Executing check for '" << checkable->GetName() << "'";
208 Checkable::IncreasePendingChecks();
210 Utility::QueueAsyncCallback(std::bind(&CheckerComponent::ExecuteCheckHelper, CheckerComponent::Ptr(this), checkable));
216 void CheckerComponent::ExecuteCheckHelper(const Checkable::Ptr& checkable)
219 checkable->ExecuteCheck();
220 } catch (const std::exception& ex) {
221 CheckResult::Ptr cr = new CheckResult();
222 cr->SetState(ServiceUnknown);
224 String output = "Exception occurred while checking '" + checkable->GetName() + "': " + DiagnosticInformation(ex);
225 cr->SetOutput(output);
227 double now = Utility::GetTime();
228 cr->SetScheduleStart(now);
229 cr->SetScheduleEnd(now);
230 cr->SetExecutionStart(now);
231 cr->SetExecutionEnd(now);
233 checkable->ProcessCheckResult(cr);
235 Log(LogCritical, "checker", output);
238 Checkable::DecreasePendingChecks();
241 boost::mutex::scoped_lock lock(m_Mutex);
243 /* remove the object from the list of pending objects; if it's not in the
244 * list this was a manual (i.e. forced) check and we must not re-add the
245 * object to the list because it's already there. */
246 auto it = m_PendingCheckables.find(checkable);
248 if (it != m_PendingCheckables.end()) {
249 m_PendingCheckables.erase(it);
251 if (checkable->IsActive())
252 m_IdleCheckables.insert(GetCheckableScheduleInfo(checkable));
258 Log(LogDebug, "CheckerComponent")
259 << "Check finished for object '" << checkable->GetName() << "'";
262 void CheckerComponent::ResultTimerHandler()
264 std::ostringstream msgbuf;
267 boost::mutex::scoped_lock lock(m_Mutex);
269 msgbuf << "Pending checkables: " << m_PendingCheckables.size() << "; Idle checkables: " << m_IdleCheckables.size() << "; Checks/s: "
270 << (CIB::GetActiveHostChecksStatistics(60) + CIB::GetActiveServiceChecksStatistics(60)) / 60.0;
273 Log(LogNotice, "CheckerComponent", msgbuf.str());
276 void CheckerComponent::ObjectHandler(const ConfigObject::Ptr& object)
278 Checkable::Ptr checkable = dynamic_pointer_cast<Checkable>(object);
283 Zone::Ptr zone = Zone::GetByName(checkable->GetZoneName());
284 bool same_zone = (!zone || Zone::GetLocalZone() == zone);
287 boost::mutex::scoped_lock lock(m_Mutex);
289 if (object->IsActive() && !object->IsPaused() && same_zone) {
290 if (m_PendingCheckables.find(checkable) != m_PendingCheckables.end())
293 m_IdleCheckables.insert(GetCheckableScheduleInfo(checkable));
295 m_IdleCheckables.erase(checkable);
296 m_PendingCheckables.erase(checkable);
303 CheckableScheduleInfo CheckerComponent::GetCheckableScheduleInfo(const Checkable::Ptr& checkable)
305 CheckableScheduleInfo csi;
306 csi.Object = checkable;
307 csi.NextCheck = checkable->GetNextCheck();
311 void CheckerComponent::NextCheckChangedHandler(const Checkable::Ptr& checkable)
313 boost::mutex::scoped_lock lock(m_Mutex);
315 /* remove and re-insert the object from the set in order to force an index update */
316 typedef boost::multi_index::nth_index<CheckableSet, 0>::type CheckableView;
317 CheckableView& idx = boost::get<0>(m_IdleCheckables);
319 auto it = idx.find(checkable);
324 idx.erase(checkable);
326 CheckableScheduleInfo csi = GetCheckableScheduleInfo(checkable);
332 unsigned long CheckerComponent::GetIdleCheckables()
334 boost::mutex::scoped_lock lock(m_Mutex);
336 return m_IdleCheckables.size();
339 unsigned long CheckerComponent::GetPendingCheckables()
341 boost::mutex::scoped_lock lock(m_Mutex);
343 return m_PendingCheckables.size();