icinga2/icinga/nagioschecktask.cpp

135 lines
2.9 KiB
C++
Raw Normal View History

#include "i2-icinga.h"
#include "popen_noshell.h"
using namespace icinga;
2012-06-19 12:23:52 +02:00
list<ThreadPoolTask::Ptr> NagiosCheckTask::m_QueuedTasks;
2012-06-19 09:38:20 +02:00
boost::mutex NagiosCheckTask::m_FinishedTasksMutex;
vector<CheckTask::Ptr> NagiosCheckTask::m_FinishedTasks;
NagiosCheckTask::NagiosCheckTask(const Service& service)
: CheckTask(service)
{
string checkCommand = service.GetCheckCommand();
m_Command = MacroProcessor::ResolveMacros(checkCommand, service.GetMacros()); // + " 2>&1";
2012-06-17 20:35:56 +02:00
}
void NagiosCheckTask::Enqueue(void)
2012-06-17 20:35:56 +02:00
{
2012-06-19 12:23:52 +02:00
time(&m_Result.StartTime);
m_QueuedTasks.push_back(static_pointer_cast<ThreadPoolTask>(static_cast<NagiosCheckTask::Ptr>(GetSelf())));
// m_QueuedTasks.push_back(new ThreadPool:Task(bind(&NagiosCheckTask::Execute, static_cast<NagiosCheckTask::Ptr>(GetSelf()))));
}
void NagiosCheckTask::FlushQueue(void)
2012-06-17 20:35:56 +02:00
{
ThreadPool::GetDefaultPool()->EnqueueTasks(m_QueuedTasks);
m_QueuedTasks.clear();
2012-06-17 20:35:56 +02:00
}
2012-06-19 09:38:20 +02:00
void NagiosCheckTask::GetFinishedTasks(vector<CheckTask::Ptr>& tasks)
2012-06-17 20:35:56 +02:00
{
2012-06-19 12:23:52 +02:00
mutex::scoped_lock lock(m_FinishedTasksMutex);
2012-06-19 09:38:20 +02:00
std::copy(m_FinishedTasks.begin(), m_FinishedTasks.end(), back_inserter(tasks));
m_FinishedTasks.clear();
2012-06-17 20:35:56 +02:00
}
CheckResult NagiosCheckTask::GetResult(void)
{
2012-06-19 09:38:20 +02:00
return m_Result;
2012-06-17 20:35:56 +02:00
}
void NagiosCheckTask::Execute(void)
{
2012-06-19 12:23:52 +02:00
RunCheck();
2012-06-19 09:38:20 +02:00
{
2012-06-19 12:23:52 +02:00
mutex::scoped_lock lock(m_FinishedTasksMutex);
2012-06-19 09:38:20 +02:00
m_FinishedTasks.push_back(GetSelf());
}
}
2012-06-19 12:23:52 +02:00
void NagiosCheckTask::RunCheck(void)
{
FILE *fp;
#ifdef _MSC_VER
2012-06-17 20:35:56 +02:00
fp = _popen(m_Command.c_str(), "r");
#else /* _MSC_VER */
2012-06-21 00:10:10 +02:00
bool use_libc_popen = false;
popen_noshell_pass_to_pclose pclose_arg;
2012-06-21 00:10:10 +02:00
if (!use_libc_popen) {
fp = popen_noshell_compat(m_Command.c_str(), "r", &pclose_arg);
if (fp == NULL) // TODO: add check for valgrind
use_libc_popen = true;
}
if (use_libc_popen)
fp = popen(m_Command.c_str(), "r");
#endif /* _MSC_VER */
stringstream outputbuf;
while (!feof(fp)) {
char buffer[512];
size_t read = fread(buffer, 1, sizeof(buffer), fp);
if (read == 0)
break;
outputbuf.write(buffer, read);
}
m_Result.Output = outputbuf.str();
boost::algorithm::trim(m_Result.Output);
2012-06-14 13:04:22 +02:00
int status, exitcode;
#ifdef _MSC_VER
status = _pclose(fp);
#else /* _MSC_VER */
2012-06-21 00:10:10 +02:00
if (use_libc_popen)
status = pclose(fp);
else
status = pclose_noshell(&pclose_arg);
#endif /* _MSC_VER */
#ifndef _MSC_VER
if (WIFEXITED(status)) {
exitcode = WEXITSTATUS(status);
#else /* _MSC_VER */
2012-06-14 13:04:22 +02:00
exitcode = status;
#endif /* _MSC_VER */
2012-06-14 13:04:22 +02:00
switch (exitcode) {
case 0:
2012-06-19 12:23:52 +02:00
m_Result.State = StateOK;
break;
case 1:
2012-06-19 12:23:52 +02:00
m_Result.State = StateWarning;
break;
case 2:
2012-06-19 12:23:52 +02:00
m_Result.State = StateCritical;
break;
default:
2012-06-19 12:23:52 +02:00
m_Result.State = StateUnknown;
break;
}
#ifndef _MSC_VER
} else if (WIFSIGNALED(status)) {
2012-06-19 12:23:52 +02:00
m_Result.Output = "Process was terminated by signal " + WTERMSIG(status);
m_Result.State = StateUnknown;
}
#endif /* _MSC_VER */
2012-06-19 12:23:52 +02:00
time(&m_Result.EndTime);
}
CheckTask::Ptr NagiosCheckTask::CreateTask(const Service& service)
{
2012-06-15 19:32:41 +02:00
return boost::make_shared<NagiosCheckTask>(service);
}