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-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
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
time(&now);
|
|
|
|
|
2012-07-10 12:21:19 +02:00
|
|
|
Logger::Write(LogDebug, "checker", "CheckTimerHandler entered.");
|
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-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-06-22 12:22:36 +02:00
|
|
|
m_PendingServices.insert(service.GetConfigObject());
|
2012-06-21 12:51:50 +02:00
|
|
|
|
2012-07-14 15:59:59 +02:00
|
|
|
/*CheckTask::Ptr task = CheckTask::CreateTask(service, boost::bind(&CheckerComponent::CheckCompletedHandler, this, _1));
|
|
|
|
task->Start();*/
|
2012-06-18 17:23:48 +02:00
|
|
|
|
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
|
|
|
/* update service state */
|
|
|
|
service.ApplyCheckResult(result);
|
2012-07-14 15:59:59 +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-15 17:29:59 +02:00
|
|
|
ServiceStatusMessage params;
|
|
|
|
params.SetService(service.GetName());
|
|
|
|
params.SetState(service.GetState());
|
|
|
|
params.SetStateType(service.GetStateType());
|
|
|
|
params.SetCurrentCheckAttempt(service.GetCurrentCheckAttempt());
|
|
|
|
params.SetNextCheck(service.GetNextCheck());
|
|
|
|
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-13 21:00:54 +02:00
|
|
|
/* figure out when the next check is for this service */
|
|
|
|
service.UpdateNextCheck();
|
2012-06-25 15:42:46 +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. */
|
|
|
|
if (m_PendingServices.find(service.GetConfigObject()) != m_PendingServices.end()) {
|
|
|
|
m_PendingServices.erase(service.GetConfigObject());
|
|
|
|
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
|
|
|
|
|
|
|
MessagePart serviceMsg;
|
2012-07-09 16:19:56 +02:00
|
|
|
if (!params.Get("service", &serviceMsg))
|
2012-06-15 19:32:41 +02:00
|
|
|
return;
|
2012-06-14 16:09:04 +02:00
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
ConfigObject::Ptr object = boost::make_shared<ConfigObject>(serviceMsg.GetDictionary());
|
2012-06-14 16:09:04 +02:00
|
|
|
Service service(object);
|
|
|
|
m_Services.push(service);
|
|
|
|
|
2012-07-10 12:21:19 +02:00
|
|
|
Logger::Write(LogDebug, "checker", "Accepted delegation for service '" + service.GetName() + "'");
|
2012-06-14 16:09:04 +02:00
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
string id;
|
2012-06-16 03:42:54 +02:00
|
|
|
if (request.GetID(&id)) {
|
2012-06-15 19:32:41 +02:00
|
|
|
ResponseMessage rm;
|
|
|
|
rm.SetID(id);
|
|
|
|
|
|
|
|
MessagePart result;
|
|
|
|
rm.SetResult(result);
|
2012-07-02 11:07:54 +02:00
|
|
|
EndpointManager::GetInstance()->SendUnicastMessage(m_Endpoint, sender, rm);
|
2012-06-15 19:32:41 +02:00
|
|
|
}
|
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();
|
|
|
|
|
|
|
|
/* TODO: clear checks we've already sent to the thread pool */
|
2012-06-14 11:23:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_COMPONENT(checker, CheckerComponent);
|