Bugfixes for the ScriptTask feature.

This commit is contained in:
Gunnar Beutner 2012-07-14 16:49:21 +02:00
parent 30aa16d9dc
commit fe237e0145
6 changed files with 24 additions and 151 deletions

View File

@ -6,8 +6,6 @@ pkglib_LTLIBRARIES = \
libcib_la_SOURCES = \
checkresult.cpp \
checkresult.h \
checktask.cpp \
checktask.h \
cib.cpp \
cib.h \
configobjectadapter.cpp \

View File

@ -1,58 +0,0 @@
/******************************************************************************
* 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-cib.h"
using namespace icinga;
map<string, CheckTaskType> CheckTask::m_Types;
CheckTask::CheckTask(const Service& service, const CompletionCallback& completionCallback)
: AsyncTask(completionCallback), m_Service(service)
{ }
Service& CheckTask::GetService(void)
{
return m_Service;
}
CheckResult& CheckTask::GetResult(void)
{
return m_Result;
}
void CheckTask::RegisterType(string type, Factory factory)
{
CheckTaskType ctt;
ctt.Factory = factory;
m_Types[type] = ctt;
}
CheckTask::Ptr CheckTask::CreateTask(const Service& service, const CompletionCallback& completionCallback)
{
map<string, CheckTaskType>::iterator it;
it = m_Types.find(service.GetCheckType());
if (it == m_Types.end())
throw runtime_error("Invalid check type specified for service '" + service.GetName() + "'");
return it->second.Factory(service, completionCallback);
}

View File

@ -1,63 +0,0 @@
/******************************************************************************
* 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. *
******************************************************************************/
#ifndef CHECKTASK_H
#define CHECKTASK_H
namespace icinga
{
struct CheckTaskType;
class I2_CIB_API CheckTask : public AsyncTask
{
public:
typedef shared_ptr<CheckTask> Ptr;
typedef weak_ptr<CheckTask> WeakPtr;
typedef function<CheckTask::Ptr(const Service&, const CompletionCallback&)> Factory;
Service& GetService(void);
CheckResult& GetResult(void);
static void RegisterType(string type, Factory factory);
static CheckTask::Ptr CreateTask(const Service& service, const CompletionCallback& completionCallback);
static int GetTaskHistogramSlots(void);
protected:
CheckTask(const Service& service, const CompletionCallback& completionCallback);
virtual void Run(void) = 0;
private:
Service m_Service;
CheckResult m_Result;
static map<string, CheckTaskType> m_Types;
};
struct CheckTaskType
{
CheckTask::Factory Factory;
};
}
#endif /* CHECKTASK_H */

View File

@ -44,7 +44,6 @@
#include "macroprocessor.h"
#include "checkresult.h"
#include "checktask.h"
#include "nagioschecktask.h"
#include "servicestatusmessage.h"

View File

@ -99,7 +99,7 @@ void CheckerComponent::CheckTimerHandler(void)
Logger::Write(LogInformation, "checker", msgbuf.str());
}
void CheckerComponent::CheckCompletedHandler(Service& service, const AsyncTask::Ptr& atask)
void CheckerComponent::CheckCompletedHandler(Service service, const AsyncTask::Ptr& atask)
{
ScriptTask::Ptr task = static_pointer_cast<ScriptTask>(atask);
@ -110,41 +110,38 @@ void CheckerComponent::CheckCompletedHandler(Service& service, const AsyncTask::
if (m_PendingServices.find(service.GetConfigObject()) == m_PendingServices.end())
return;
/* remove the service from the list of pending services */
m_PendingServices.erase(service.GetConfigObject());
m_Services.push(service);
Variant vresult = task->GetResult();
if (!vresult.IsObjectType<Dictionary>())
return;
bool hasResult = false;
if (vresult.IsObjectType<Dictionary>()) {
CheckResult result = CheckResult(static_cast<Dictionary::Ptr>(vresult));
CheckResult result = static_cast<Dictionary::Ptr>(vresult);
/* update service state */
service.ApplyCheckResult(result);
Logger::Write(LogDebug, "checker", "Got result for service '" + service.GetName() + "'");
RequestMessage rm;
rm.SetMethod("checker::CheckResult");
long execution_time = result.GetExecutionEnd() - result.GetExecutionStart();
long latency = (result.GetScheduleEnd() - result.GetScheduleStart()) - execution_time;
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);
/* update service state */
service.ApplyCheckResult(result);
rm.SetParams(params);
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint, rm);
}
/* figure out when the next check is for this service */
service.UpdateNextCheck();
RequestMessage rm;
rm.SetMethod("checker::CheckResult");
/* remove the service from the list of pending services */
m_PendingServices.erase(service.GetConfigObject());
m_Services.push(service);
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);
rm.SetParams(params);
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint, rm);
Logger::Write(LogDebug, "checker", "Check finished for service '" + service.GetName() + "'");
}
void CheckerComponent::ResultTimerHandler(void)

View File

@ -60,7 +60,7 @@ private:
void CheckTimerHandler(void);
void ResultTimerHandler(void);
void CheckCompletedHandler(Service& service, const AsyncTask::Ptr& atask);
void CheckCompletedHandler(Service service, const AsyncTask::Ptr& atask);
void AdjustCheckTimer(void);