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)
|
|
|
|
{
|
2012-07-02 11:07:54 +02:00
|
|
|
m_Endpoint = boost::make_shared<VirtualEndpoint>();
|
2012-08-03 14:07:25 +02:00
|
|
|
m_Endpoint->RegisterPublication("checker::ServiceStateChange");
|
2012-07-02 11:07:54 +02:00
|
|
|
EndpointManager::GetInstance()->RegisterEndpoint(m_Endpoint);
|
2012-06-14 11:23:25 +02:00
|
|
|
|
2012-08-03 23:03:58 +02:00
|
|
|
Service::OnCheckerChanged.connect(bind(&CheckerComponent::CheckerChangedHandler, this, _1));
|
|
|
|
DynamicObject::OnUnregistered.connect(bind(&CheckerComponent::ServiceRemovedHandler, this, _1));
|
|
|
|
|
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-24 16:30:16 +02:00
|
|
|
NagiosCheckTask::Register();
|
2012-07-16 15:10:42 +02:00
|
|
|
NullCheckTask::Register();
|
2012-06-14 11:23:25 +02:00
|
|
|
|
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-06-27 18:43:34 +02:00
|
|
|
EndpointManager::Ptr mgr = EndpointManager::GetInstance();
|
2012-06-14 11:23:25 +02:00
|
|
|
|
2012-06-14 13:21:40 +02:00
|
|
|
if (mgr)
|
2012-07-02 11:07:54 +02:00
|
|
|
mgr->UnregisterEndpoint(m_Endpoint);
|
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-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
|
|
|
|
2012-07-27 16:05:02 +02: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
|
|
|
|
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
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
m_PendingServices.insert(service);
|
2012-07-16 15:10:42 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
vector<Value> arguments;
|
2012-07-27 16:05:02 +02:00
|
|
|
arguments.push_back(service);
|
2012-07-14 15:59:59 +02:00
|
|
|
ScriptTask::Ptr task;
|
2012-07-27 16:05:02 +02:00
|
|
|
task = service->InvokeMethod("check", arguments, boost::bind(&CheckerComponent::CheckCompletedHandler, this, service, _1));
|
2012-07-30 10:17:29 +02:00
|
|
|
assert(task); /* TODO: gracefully handle missing methods */
|
2012-07-14 15:59:59 +02:00
|
|
|
|
2012-08-03 13:19:55 +02:00
|
|
|
service->Set("current_task", task);
|
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
|
|
|
|
|
|
|
stringstream msgbuf;
|
|
|
|
msgbuf << "CheckTimerHandler: created " << tasks << " tasks";
|
2012-07-10 12:21:19 +02:00
|
|
|
Logger::Write(LogInformation, "checker", msgbuf.str());
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
void CheckerComponent::CheckCompletedHandler(const Service::Ptr& service, const ScriptTask::Ptr& task)
|
2012-06-17 20:35:56 +02:00
|
|
|
{
|
2012-08-03 13:19:55 +02:00
|
|
|
service->Set("current_task", Empty);
|
2012-07-14 12:44:37 +02:00
|
|
|
|
2012-07-15 17:29:59 +02:00
|
|
|
try {
|
2012-08-02 09:38:08 +02:00
|
|
|
Value vresult = task->GetResult();
|
2012-06-19 19:05:24 +02:00
|
|
|
|
2012-07-15 17:29:59 +02:00
|
|
|
if (vresult.IsObjectType<Dictionary>()) {
|
2012-08-02 09:38:08 +02:00
|
|
|
Dictionary::Ptr result = vresult;
|
|
|
|
|
|
|
|
service->ApplyCheckResult(result);
|
2012-07-14 16:49:21 +02:00
|
|
|
|
2012-07-15 17:29:59 +02:00
|
|
|
RequestMessage rm;
|
2012-08-02 09:38:08 +02:00
|
|
|
rm.SetMethod("checker::ServiceStateChange");
|
2012-07-14 15:59:59 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
/* TODO: add _old_ state to message */
|
2012-08-03 14:07:25 +02:00
|
|
|
ServiceStateChangeMessage params;
|
2012-07-27 16:05:02 +02:00
|
|
|
params.SetService(service->GetName());
|
2012-06-19 19:05:24 +02:00
|
|
|
|
2012-07-15 17:29:59 +02:00
|
|
|
rm.SetParams(params);
|
2012-06-18 00:14:34 +02:00
|
|
|
|
2012-07-15 17:29:59 +02:00
|
|
|
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint, rm);
|
|
|
|
}
|
|
|
|
} catch (const exception& ex) {
|
|
|
|
stringstream msgbuf;
|
|
|
|
msgbuf << "Exception occured during check for service '"
|
2012-07-27 16:05:02 +02:00
|
|
|
<< service->GetName() << "': " << ex.what();
|
2012-07-15 17:29:59 +02:00
|
|
|
Logger::Write(LogWarning, "checker", msgbuf.str());
|
2012-07-14 16:49:21 +02:00
|
|
|
}
|
2012-06-20 10:46:18 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
/* figure out when the next check is for this service; the call to
|
|
|
|
* ApplyCheckResult() should've already done this but lets do it again
|
|
|
|
* just in case there was no check result. */
|
2012-07-27 16:05:02 +02:00
|
|
|
service->UpdateNextCheck();
|
2012-07-18 11:52:22 +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-08-03 23:03:58 +02:00
|
|
|
if (checker == EndpointManager::GetInstance()->GetIdentity() || checker == m_Endpoint->GetIdentity()) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-08-03 23:03:58 +02:00
|
|
|
void CheckerComponent::ServiceRemovedHandler(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);
|