1 /******************************************************************************
3 * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
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 "i2-checker.h"
22 using namespace icinga;
24 void CheckerComponent::Start(void)
26 m_Endpoint = Endpoint::MakeEndpoint("checker", false);
28 /* dummy registration so the delegation module knows this is a checker
29 TODO: figure out a better way for this */
30 m_Endpoint->RegisterSubscription("checker");
32 Service::OnCheckerChanged.connect(bind(&CheckerComponent::CheckerChangedHandler, this, _1));
33 Service::OnNextCheckChanged.connect(bind(&CheckerComponent::NextCheckChangedHandler, this, _1));
34 DynamicObject::OnUnregistered.connect(bind(&CheckerComponent::ObjectRemovedHandler, this, _1));
36 m_CheckTimer = boost::make_shared<Timer>();
37 m_CheckTimer->SetInterval(1);
38 m_CheckTimer->OnTimerExpired.connect(boost::bind(&CheckerComponent::CheckTimerHandler, this));
39 m_CheckTimer->Start();
41 m_ResultTimer = boost::make_shared<Timer>();
42 m_ResultTimer->SetInterval(5);
43 m_ResultTimer->OnTimerExpired.connect(boost::bind(&CheckerComponent::ResultTimerHandler, this));
44 m_ResultTimer->Start();
47 void CheckerComponent::Stop(void)
49 m_Endpoint->Unregister();
52 void CheckerComponent::CheckTimerHandler(void)
54 Logger::Write(LogDebug, "checker", "CheckTimerHandler entered.");
56 double now = Utility::GetTime();
59 int missedServices = 0, missedChecks = 0;
61 while (!m_IdleServices.empty()) {
62 typedef nth_index<ServiceSet, 1>::type CheckTimeView;
63 CheckTimeView& idx = boost::get<1>(m_IdleServices);
65 CheckTimeView::iterator it = idx.begin();
66 Service::Ptr service = *it;
68 if (service->GetNextCheck() > now)
73 /* reschedule the service if checks are currently disabled
74 * for it and this is not a forced check */
75 if (!service->GetEnableActiveChecks()) {
76 if (!service->GetForceNextCheck()) {
77 Logger::Write(LogDebug, "checker", "Ignoring service check for disabled service: " + service->GetName());
79 service->UpdateNextCheck();
87 service->SetForceNextCheck(false);
89 Dictionary::Ptr cr = service->GetLastCheckResult();
92 double lastCheck = cr->Get("execution_end");
93 int missed = (Utility::GetTime() - lastCheck) / service->GetCheckInterval() - 1;
95 if (missed > 0 && !service->GetFirstCheck()) {
96 missedChecks += missed;
101 service->SetFirstCheck(false);
103 Logger::Write(LogDebug, "checker", "Executing service check for '" + service->GetName() + "'");
105 m_IdleServices.erase(service);
106 m_PendingServices.insert(service);
109 service->BeginExecuteCheck(boost::bind(&CheckerComponent::CheckCompletedHandler, this, service));
110 } catch (const exception& ex) {
111 Logger::Write(LogCritical, "checker", "Exception occured while checking service '" + service->GetName() + "': " + diagnostic_information(ex));
117 Logger::Write(LogDebug, "checker", "CheckTimerHandler: past loop.");
119 if (missedServices > 0) {
121 msgbuf << "Missed " << missedChecks << " checks for " << missedServices << " services";;
122 Logger::Write(LogWarning, "checker", msgbuf.str());
127 msgbuf << "CheckTimerHandler: created " << tasks << " task(s)";
128 Logger::Write(LogInformation, "checker", msgbuf.str());
132 void CheckerComponent::CheckCompletedHandler(const Service::Ptr& service)
134 /* remove the service from the list of pending services; if it's not in the
135 * list this was a manual (i.e. forced) check and we must not re-add the
136 * service to the services list because it's already there. */
137 CheckerComponent::ServiceSet::iterator it;
138 it = m_PendingServices.find(service);
139 if (it != m_PendingServices.end()) {
140 m_PendingServices.erase(it);
141 m_IdleServices.insert(service);
144 Logger::Write(LogDebug, "checker", "Check finished for service '" + service->GetName() + "'");
147 void CheckerComponent::ResultTimerHandler(void)
149 Logger::Write(LogDebug, "checker", "ResultTimerHandler entered.");
152 msgbuf << "Pending services: " << m_PendingServices.size() << "; Idle services: " << m_IdleServices.size();
153 Logger::Write(LogInformation, "checker", msgbuf.str());
156 void CheckerComponent::CheckerChangedHandler(const Service::Ptr& service)
158 String checker = service->GetChecker();
160 if (checker == EndpointManager::GetInstance()->GetIdentity() || checker == m_Endpoint->GetName()) {
161 if (m_PendingServices.find(service) != m_PendingServices.end())
164 m_IdleServices.insert(service);
166 m_IdleServices.erase(service);
167 m_PendingServices.erase(service);
171 void CheckerComponent::NextCheckChangedHandler(const Service::Ptr& service)
173 /* remove and re-insert the service from the set in order to force an index update */
174 typedef nth_index<ServiceSet, 0>::type ServiceView;
175 ServiceView& idx = boost::get<0>(m_IdleServices);
177 ServiceView::iterator it = idx.find(service);
185 void CheckerComponent::ObjectRemovedHandler(const DynamicObject::Ptr& object)
187 Service::Ptr service = dynamic_pointer_cast<Service>(object);
189 /* ignore it if the removed object is not a service */
193 m_IdleServices.erase(service);
194 m_PendingServices.erase(service);
197 EXPORT_COMPONENT(checker, CheckerComponent);