2012-06-14 11:23:25 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "i2-checker.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
void CheckerComponent::Start(void)
|
|
|
|
{
|
2013-01-18 09:36:28 +01:00
|
|
|
m_Endpoint = Endpoint::MakeEndpoint("checker", false);
|
2012-08-14 10:53:04 +02:00
|
|
|
|
|
|
|
/* dummy registration so the delegation module knows this is a checker
|
|
|
|
TODO: figure out a better way for this */
|
|
|
|
m_Endpoint->RegisterSubscription("checker");
|
|
|
|
|
2012-08-03 23:03:58 +02:00
|
|
|
Service::OnCheckerChanged.connect(bind(&CheckerComponent::CheckerChangedHandler, this, _1));
|
2013-01-22 12:44:23 +01:00
|
|
|
Service::OnNextCheckChanged.connect(bind(&CheckerComponent::NextCheckChangedHandler, this, _1));
|
2012-09-21 09:43:06 +02:00
|
|
|
DynamicObject::OnUnregistered.connect(bind(&CheckerComponent::ObjectRemovedHandler, this, _1));
|
2012-08-03 23:03:58 +02:00
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
m_CheckTimer = boost::make_shared<Timer>();
|
2012-07-14 12:44:37 +02:00
|
|
|
m_CheckTimer->SetInterval(1);
|
2012-06-15 19:32:41 +02:00
|
|
|
m_CheckTimer->OnTimerExpired.connect(boost::bind(&CheckerComponent::CheckTimerHandler, this));
|
2012-06-14 11:23:25 +02:00
|
|
|
m_CheckTimer->Start();
|
|
|
|
|
2012-06-17 20:35:56 +02:00
|
|
|
m_ResultTimer = boost::make_shared<Timer>();
|
2012-06-17 23:10:03 +02:00
|
|
|
m_ResultTimer->SetInterval(5);
|
2012-06-17 20:35:56 +02:00
|
|
|
m_ResultTimer->OnTimerExpired.connect(boost::bind(&CheckerComponent::ResultTimerHandler, this));
|
|
|
|
m_ResultTimer->Start();
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckerComponent::Stop(void)
|
|
|
|
{
|
2012-09-03 10:28:14 +02:00
|
|
|
m_Endpoint->Unregister();
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
void CheckerComponent::CheckTimerHandler(void)
|
2012-06-14 11:23:25 +02:00
|
|
|
{
|
2012-07-10 12:21:19 +02:00
|
|
|
Logger::Write(LogDebug, "checker", "CheckTimerHandler entered.");
|
2012-06-18 17:23:48 +02:00
|
|
|
|
2012-07-25 12:59:17 +02:00
|
|
|
double now = Utility::GetTime();
|
2012-06-18 17:23:48 +02:00
|
|
|
long tasks = 0;
|
|
|
|
|
2012-09-03 12:36:35 +02:00
|
|
|
int missedServices = 0, missedChecks = 0;
|
|
|
|
|
2012-08-04 13:49:25 +02:00
|
|
|
while (!m_IdleServices.empty()) {
|
|
|
|
typedef nth_index<ServiceSet, 1>::type CheckTimeView;
|
|
|
|
CheckTimeView& idx = boost::get<1>(m_IdleServices);
|
|
|
|
|
|
|
|
CheckTimeView::iterator it = idx.begin();
|
2012-08-03 23:03:58 +02:00
|
|
|
Service::Ptr service = *it;
|
2012-06-14 11:23:25 +02:00
|
|
|
|
2013-01-22 11:07:09 +01:00
|
|
|
if (service->GetNextCheck() > now)
|
2012-06-14 11:23:25 +02:00
|
|
|
break;
|
|
|
|
|
2012-08-04 13:49:25 +02:00
|
|
|
idx.erase(it);
|
2012-06-17 22:46:40 +02:00
|
|
|
|
2013-01-22 16:01:08 +01:00
|
|
|
/* reschedule the service if checks are currently disabled
|
|
|
|
* for it and this is not a forced check */
|
2013-01-24 23:10:07 +01:00
|
|
|
if (!service->GetEnableActiveChecks()) {
|
2013-01-22 16:01:08 +01:00
|
|
|
if (!service->GetForceNextCheck()) {
|
2013-01-24 11:07:37 +01:00
|
|
|
Logger::Write(LogDebug, "checker", "Ignoring service check for disabled service: " + service->GetName());
|
|
|
|
|
2013-01-22 16:01:08 +01:00
|
|
|
service->UpdateNextCheck();
|
|
|
|
|
|
|
|
idx.insert(service);
|
|
|
|
|
2013-01-24 11:07:37 +01:00
|
|
|
continue;
|
2013-01-22 16:01:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-28 13:24:51 +01:00
|
|
|
service->SetForceNextCheck(false);
|
|
|
|
|
2012-09-03 12:36:35 +02:00
|
|
|
Dictionary::Ptr cr = service->GetLastCheckResult();
|
|
|
|
|
|
|
|
if (cr) {
|
|
|
|
double lastCheck = cr->Get("execution_end");
|
2012-09-04 10:45:00 +02:00
|
|
|
int missed = (Utility::GetTime() - lastCheck) / service->GetCheckInterval() - 1;
|
2012-09-03 12:36:35 +02:00
|
|
|
|
2013-01-23 15:25:00 +01:00
|
|
|
if (missed > 0 && !service->GetFirstCheck()) {
|
2012-09-03 12:36:35 +02:00
|
|
|
missedChecks += missed;
|
|
|
|
missedServices++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 15:25:00 +01:00
|
|
|
service->SetFirstCheck(false);
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
Logger::Write(LogDebug, "checker", "Executing service check for '" + service->GetName() + "'");
|
2012-06-17 20:35:56 +02:00
|
|
|
|
2013-01-24 11:14:17 +01:00
|
|
|
m_IdleServices.erase(service);
|
2012-07-27 16:05:02 +02:00
|
|
|
m_PendingServices.insert(service);
|
2012-07-16 15:10:42 +02:00
|
|
|
|
2013-02-01 19:27:36 +01:00
|
|
|
try {
|
|
|
|
service->BeginExecuteCheck(boost::bind(&CheckerComponent::CheckCompletedHandler, this, service));
|
|
|
|
} catch (const exception& ex) {
|
2013-02-06 12:51:12 +01:00
|
|
|
Logger::Write(LogCritical, "checker", "Exception occured while checking service '" + service->GetName() + "': " + diagnostic_information(ex));
|
2013-02-01 19:27:36 +01:00
|
|
|
}
|
2012-07-14 12:44:37 +02:00
|
|
|
|
2012-06-18 17:23:48 +02:00
|
|
|
tasks++;
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
2012-07-10 12:21:19 +02:00
|
|
|
Logger::Write(LogDebug, "checker", "CheckTimerHandler: past loop.");
|
2012-06-18 17:23:48 +02:00
|
|
|
|
2012-09-03 12:36:35 +02:00
|
|
|
if (missedServices > 0) {
|
|
|
|
stringstream msgbuf;
|
|
|
|
msgbuf << "Missed " << missedChecks << " checks for " << missedServices << " services";;
|
|
|
|
Logger::Write(LogWarning, "checker", msgbuf.str());
|
|
|
|
}
|
|
|
|
|
2012-10-15 08:52:31 +02:00
|
|
|
if (tasks > 0) {
|
|
|
|
stringstream msgbuf;
|
2012-10-15 08:54:08 +02:00
|
|
|
msgbuf << "CheckTimerHandler: created " << tasks << " task(s)";
|
2012-10-15 08:52:31 +02:00
|
|
|
Logger::Write(LogInformation, "checker", msgbuf.str());
|
|
|
|
}
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
2013-01-22 11:07:09 +01:00
|
|
|
void CheckerComponent::CheckCompletedHandler(const Service::Ptr& service)
|
2012-06-17 20:35:56 +02:00
|
|
|
{
|
2012-07-15 17:29:59 +02:00
|
|
|
/* remove the service from the list of pending services; if it's not in the
|
|
|
|
* list this was a manual (i.e. forced) check and we must not re-add the
|
|
|
|
* service to the services list because it's already there. */
|
2012-08-04 13:49:25 +02:00
|
|
|
CheckerComponent::ServiceSet::iterator it;
|
2012-07-27 16:05:02 +02:00
|
|
|
it = m_PendingServices.find(service);
|
2012-07-17 19:10:14 +02:00
|
|
|
if (it != m_PendingServices.end()) {
|
|
|
|
m_PendingServices.erase(it);
|
2012-08-04 13:49:25 +02:00
|
|
|
m_IdleServices.insert(service);
|
2012-07-15 17:29:59 +02:00
|
|
|
}
|
2012-06-25 14:13:24 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
Logger::Write(LogDebug, "checker", "Check finished for service '" + service->GetName() + "'");
|
2012-07-13 21:00:54 +02:00
|
|
|
}
|
2012-06-20 15:23:31 +02:00
|
|
|
|
2012-07-13 21:00:54 +02:00
|
|
|
void CheckerComponent::ResultTimerHandler(void)
|
|
|
|
{
|
|
|
|
Logger::Write(LogDebug, "checker", "ResultTimerHandler entered.");
|
2012-06-22 07:24:50 +02:00
|
|
|
|
2012-07-13 21:00:54 +02:00
|
|
|
stringstream msgbuf;
|
2012-08-04 13:49:25 +02:00
|
|
|
msgbuf << "Pending services: " << m_PendingServices.size() << "; Idle services: " << m_IdleServices.size();
|
2012-07-13 21:00:54 +02:00
|
|
|
Logger::Write(LogInformation, "checker", msgbuf.str());
|
2012-06-17 20:35:56 +02:00
|
|
|
}
|
|
|
|
|
2012-08-03 23:03:58 +02:00
|
|
|
void CheckerComponent::CheckerChangedHandler(const Service::Ptr& service)
|
2012-06-14 11:23:25 +02:00
|
|
|
{
|
2012-08-03 23:03:58 +02:00
|
|
|
String checker = service->GetChecker();
|
2012-06-14 16:09:04 +02:00
|
|
|
|
2012-09-03 10:28:14 +02:00
|
|
|
if (checker == EndpointManager::GetInstance()->GetIdentity() || checker == m_Endpoint->GetName()) {
|
2012-08-03 23:03:58 +02:00
|
|
|
if (m_PendingServices.find(service) != m_PendingServices.end())
|
|
|
|
return;
|
2012-06-14 16:09:04 +02:00
|
|
|
|
2012-08-04 13:49:25 +02:00
|
|
|
m_IdleServices.insert(service);
|
2012-08-03 23:03:58 +02:00
|
|
|
} else {
|
2012-08-04 13:49:25 +02:00
|
|
|
m_IdleServices.erase(service);
|
2012-08-03 23:03:58 +02:00
|
|
|
m_PendingServices.erase(service);
|
2012-07-17 12:57:21 +02:00
|
|
|
}
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
2013-01-22 12:44:23 +01:00
|
|
|
void CheckerComponent::NextCheckChangedHandler(const Service::Ptr& service)
|
|
|
|
{
|
|
|
|
/* remove and re-insert the service from the set in order to force an index update */
|
|
|
|
typedef nth_index<ServiceSet, 0>::type ServiceView;
|
|
|
|
ServiceView& idx = boost::get<0>(m_IdleServices);
|
|
|
|
|
|
|
|
ServiceView::iterator it = idx.find(service);
|
|
|
|
if (it == idx.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
idx.erase(it);
|
|
|
|
idx.insert(service);
|
|
|
|
}
|
|
|
|
|
2012-09-21 09:43:06 +02:00
|
|
|
void CheckerComponent::ObjectRemovedHandler(const DynamicObject::Ptr& object)
|
2012-06-14 11:23:25 +02:00
|
|
|
{
|
2012-08-03 23:03:58 +02:00
|
|
|
Service::Ptr service = dynamic_pointer_cast<Service>(object);
|
|
|
|
|
|
|
|
/* ignore it if the removed object is not a service */
|
|
|
|
if (!service)
|
|
|
|
return;
|
2012-06-21 12:51:50 +02:00
|
|
|
|
2012-08-04 13:49:25 +02:00
|
|
|
m_IdleServices.erase(service);
|
2012-08-03 23:03:58 +02:00
|
|
|
m_PendingServices.erase(service);
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_COMPONENT(checker, CheckerComponent);
|
2012-09-21 09:43:06 +02:00
|
|
|
|