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;
|
|
|
|
|
|
|
|
string CheckerComponent::GetName(void) const
|
|
|
|
{
|
2012-06-14 13:21:40 +02:00
|
|
|
return "checker";
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckerComponent::Start(void)
|
|
|
|
{
|
2012-07-02 11:07:54 +02:00
|
|
|
m_Endpoint = boost::make_shared<VirtualEndpoint>();
|
|
|
|
m_Endpoint->RegisterTopicHandler("checker::AssignService",
|
2012-06-16 03:42:54 +02:00
|
|
|
boost::bind(&CheckerComponent::AssignServiceRequestHandler, this, _2, _3));
|
2012-07-02 11:07:54 +02:00
|
|
|
m_Endpoint->RegisterTopicHandler("checker::ClearServices",
|
2012-06-16 03:42:54 +02:00
|
|
|
boost::bind(&CheckerComponent::ClearServicesRequestHandler, this, _2, _3));
|
2012-07-02 11:07:54 +02:00
|
|
|
m_Endpoint->RegisterPublication("checker::CheckResult");
|
|
|
|
EndpointManager::GetInstance()->RegisterEndpoint(m_Endpoint);
|
2012-06-14 11:23:25 +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-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-06-19 09:38:20 +02:00
|
|
|
while (!m_Services.empty()) {
|
2012-06-14 11:23:25 +02:00
|
|
|
Service service = m_Services.top();
|
|
|
|
|
2012-06-19 09:38:20 +02:00
|
|
|
if (service.GetNextCheck() > now)
|
2012-06-14 11:23:25 +02:00
|
|
|
break;
|
|
|
|
|
2012-06-17 22:46:40 +02:00
|
|
|
m_Services.pop();
|
|
|
|
|
2012-07-10 12:21:19 +02:00
|
|
|
Logger::Write(LogDebug, "checker", "Executing service check for '" + service.GetName() + "'");
|
2012-06-17 20:35:56 +02:00
|
|
|
|
2012-07-16 15:10:42 +02:00
|
|
|
m_PendingServices.insert(service.GetConfigObject());
|
|
|
|
|
2012-07-14 15:59:59 +02:00
|
|
|
vector<Variant> arguments;
|
|
|
|
arguments.push_back(service.GetConfigObject());
|
|
|
|
ScriptTask::Ptr task;
|
2012-07-16 08:19:51 +02:00
|
|
|
task = service.InvokeMethod("check", arguments, boost::bind(&CheckerComponent::CheckCompletedHandler, this, service, _1));
|
2012-07-14 15:59:59 +02:00
|
|
|
assert(task); /* TODO: gracefully handle missing hooks */
|
|
|
|
|
2012-07-14 12:44:37 +02:00
|
|
|
service.SetTag("current_task", task);
|
|
|
|
|
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-15 10:58:03 +02:00
|
|
|
void CheckerComponent::CheckCompletedHandler(Service service, const ScriptTask::Ptr& task)
|
2012-06-17 20:35:56 +02:00
|
|
|
{
|
2012-07-14 12:44:37 +02:00
|
|
|
service.RemoveTag("current_task");
|
|
|
|
|
2012-07-15 17:29:59 +02:00
|
|
|
try {
|
|
|
|
Variant vresult = task->GetResult();
|
2012-06-19 19:05:24 +02:00
|
|
|
|
2012-07-15 17:29:59 +02:00
|
|
|
if (vresult.IsObjectType<Dictionary>()) {
|
|
|
|
CheckResult result = CheckResult(static_cast<Dictionary::Ptr>(vresult));
|
2012-07-14 16:49:21 +02:00
|
|
|
|
2012-07-15 17:29:59 +02:00
|
|
|
RequestMessage rm;
|
|
|
|
rm.SetMethod("checker::CheckResult");
|
2012-07-14 15:59:59 +02:00
|
|
|
|
2012-07-25 09:03:02 +02:00
|
|
|
CheckResultMessage params;
|
2012-07-15 17:29:59 +02:00
|
|
|
params.SetService(service.GetName());
|
|
|
|
params.SetCheckResult(result);
|
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 '"
|
|
|
|
<< service.GetName() << "': " << ex.what();
|
|
|
|
Logger::Write(LogWarning, "checker", msgbuf.str());
|
2012-07-14 16:49:21 +02:00
|
|
|
}
|
2012-06-20 10:46:18 +02:00
|
|
|
|
2012-07-18 11:52:22 +02:00
|
|
|
/* figure out when the next check is for this service; the local
|
|
|
|
* cibsync component should've already done this as part of processing
|
|
|
|
* the CheckResult message, but lets do it again to be sure */
|
|
|
|
service.UpdateNextCheck();
|
|
|
|
|
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-07-17 19:10:14 +02:00
|
|
|
set<ConfigObject::Ptr>::iterator it;
|
|
|
|
it = m_PendingServices.find(service.GetConfigObject());
|
|
|
|
if (it != m_PendingServices.end()) {
|
|
|
|
m_PendingServices.erase(it);
|
2012-07-15 17:29:59 +02:00
|
|
|
m_Services.push(service);
|
|
|
|
}
|
2012-06-25 14:13:24 +02:00
|
|
|
|
2012-07-14 16:49:21 +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;
|
|
|
|
msgbuf << "Pending services: " << m_PendingServices.size() << "; Idle services: " << m_Services.size();
|
|
|
|
Logger::Write(LogInformation, "checker", msgbuf.str());
|
2012-06-17 20:35:56 +02:00
|
|
|
}
|
|
|
|
|
2012-06-16 03:42:54 +02:00
|
|
|
void CheckerComponent::AssignServiceRequestHandler(const Endpoint::Ptr& sender, const RequestMessage& request)
|
2012-06-14 11:23:25 +02:00
|
|
|
{
|
2012-06-14 16:09:04 +02:00
|
|
|
MessagePart params;
|
2012-06-16 03:42:54 +02:00
|
|
|
if (!request.GetParams(¶ms))
|
2012-06-15 19:32:41 +02:00
|
|
|
return;
|
2012-06-14 16:09:04 +02:00
|
|
|
|
2012-07-17 12:57:21 +02:00
|
|
|
string service;
|
|
|
|
if (!params.Get("service", &service))
|
2012-06-15 19:32:41 +02:00
|
|
|
return;
|
2012-06-14 16:09:04 +02:00
|
|
|
|
2012-07-17 12:57:21 +02:00
|
|
|
ConfigObject::Ptr object = ConfigObject::GetObject("service", service);
|
2012-06-14 16:09:04 +02:00
|
|
|
|
2012-07-17 12:57:21 +02:00
|
|
|
if (!object) {
|
|
|
|
Logger::Write(LogWarning, "checker", "Ignoring delegation request for unknown service '" + service + "'.");
|
|
|
|
return;
|
|
|
|
}
|
2012-06-14 16:09:04 +02:00
|
|
|
|
2012-07-17 12:57:21 +02:00
|
|
|
m_Services.push(object);
|
2012-06-15 19:32:41 +02:00
|
|
|
|
2012-07-17 12:57:21 +02:00
|
|
|
Logger::Write(LogDebug, "checker", "Accepted delegation for service '" + service + "'");
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
2012-06-16 03:42:54 +02:00
|
|
|
void CheckerComponent::ClearServicesRequestHandler(const Endpoint::Ptr& sender, const RequestMessage& request)
|
2012-06-14 11:23:25 +02:00
|
|
|
{
|
2012-07-10 12:21:19 +02:00
|
|
|
Logger::Write(LogInformation, "checker", "Clearing service delegations.");
|
2012-06-21 12:51:50 +02:00
|
|
|
|
|
|
|
/* clear the services lists */
|
2012-06-15 19:32:41 +02:00
|
|
|
m_Services = ServiceQueue();
|
2012-06-21 12:51:50 +02:00
|
|
|
m_PendingServices.clear();
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_COMPONENT(checker, CheckerComponent);
|