2012-06-13 13:42:55 +02:00
|
|
|
#include "i2-icinga.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-06-18 17:23:48 +02:00
|
|
|
vector<ThreadPool::Task> NagiosCheckTask::m_QueuedTasks;
|
|
|
|
|
2012-06-19 09:38:20 +02:00
|
|
|
boost::mutex NagiosCheckTask::m_FinishedTasksMutex;
|
|
|
|
vector<CheckTask::Ptr> NagiosCheckTask::m_FinishedTasks;
|
|
|
|
|
2012-06-13 13:42:55 +02:00
|
|
|
NagiosCheckTask::NagiosCheckTask(const Service& service)
|
2012-06-17 22:46:40 +02:00
|
|
|
: CheckTask(service)
|
2012-06-13 13:42:55 +02:00
|
|
|
{
|
|
|
|
string checkCommand = service.GetCheckCommand();
|
2012-06-17 20:35:56 +02:00
|
|
|
m_Command = MacroProcessor::ResolveMacros(checkCommand, service.GetMacros()) + " 2>&1";
|
|
|
|
}
|
|
|
|
|
2012-06-18 17:23:48 +02:00
|
|
|
void NagiosCheckTask::Enqueue(void)
|
2012-06-17 20:35:56 +02:00
|
|
|
{
|
2012-06-19 09:38:20 +02:00
|
|
|
m_QueuedTasks.push_back(bind(&NagiosCheckTask::Execute, static_cast<NagiosCheckTask::Ptr>(GetSelf())));
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|
|
|
|
|
2012-06-18 17:23:48 +02:00
|
|
|
void NagiosCheckTask::FlushQueue(void)
|
2012-06-17 20:35:56 +02:00
|
|
|
{
|
2012-06-18 17:23:48 +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 09:38:20 +02:00
|
|
|
unique_lock<mutex> lock(m_FinishedTasksMutex);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-06-18 17:23:48 +02:00
|
|
|
void NagiosCheckTask::Execute(void)
|
|
|
|
{
|
2012-06-19 09:38:20 +02:00
|
|
|
m_Result = RunCheck();
|
|
|
|
|
|
|
|
{
|
|
|
|
unique_lock<mutex> lock(m_FinishedTasksMutex);
|
|
|
|
m_FinishedTasks.push_back(GetSelf());
|
|
|
|
}
|
2012-06-18 17:23:48 +02:00
|
|
|
}
|
|
|
|
|
2012-06-17 20:35:56 +02:00
|
|
|
CheckResult NagiosCheckTask::RunCheck(void) const
|
2012-06-13 13:42:55 +02:00
|
|
|
{
|
|
|
|
CheckResult cr;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
time(&cr.StartTime);
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2012-06-17 20:35:56 +02:00
|
|
|
fp = _popen(m_Command.c_str(), "r");
|
2012-06-13 13:42:55 +02:00
|
|
|
#else /* _MSC_VER */
|
2012-06-17 20:35:56 +02:00
|
|
|
fp = popen(m_Command.c_str(), "r");
|
2012-06-13 13:42:55 +02:00
|
|
|
#endif /* _MSC_VER */
|
|
|
|
|
2012-06-16 16:54:55 +02:00
|
|
|
stringstream outputbuf;
|
2012-06-13 13:42:55 +02:00
|
|
|
|
|
|
|
while (!feof(fp)) {
|
|
|
|
char buffer[128];
|
|
|
|
size_t read = fread(buffer, 1, sizeof(buffer), fp);
|
|
|
|
|
|
|
|
if (read == 0)
|
|
|
|
break;
|
|
|
|
|
2012-06-16 16:54:55 +02:00
|
|
|
outputbuf << string(buffer, buffer + read);
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|
|
|
|
|
2012-06-16 16:54:55 +02:00
|
|
|
cr.Output = outputbuf.str();
|
|
|
|
boost::algorithm::trim(cr.Output);
|
2012-06-13 13:42:55 +02:00
|
|
|
|
2012-06-14 13:04:22 +02:00
|
|
|
int status, exitcode;
|
2012-06-13 13:42:55 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
status = _pclose(fp);
|
|
|
|
#else /* _MSC_VER */
|
|
|
|
status = pclose(fp);
|
|
|
|
#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;
|
2012-06-13 13:42:55 +02:00
|
|
|
#endif /* _MSC_VER */
|
|
|
|
|
2012-06-14 13:04:22 +02:00
|
|
|
switch (exitcode) {
|
2012-06-13 13:42:55 +02:00
|
|
|
case 0:
|
|
|
|
cr.State = StateOK;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
cr.State = StateWarning;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
cr.State = StateCritical;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cr.State = StateUnknown;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
|
|
|
cr.Output = "Process was terminated by signal " + WTERMSIG(status);
|
2012-06-14 13:04:22 +02:00
|
|
|
cr.State = StateUnknown;
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|
|
|
|
#endif /* _MSC_VER */
|
|
|
|
|
|
|
|
time(&cr.EndTime);
|
|
|
|
|
|
|
|
return cr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckTask::Ptr NagiosCheckTask::CreateTask(const Service& service)
|
|
|
|
{
|
2012-06-15 19:32:41 +02:00
|
|
|
return boost::make_shared<NagiosCheckTask>(service);
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|