2012-06-13 13:42:55 +02:00
|
|
|
#include "i2-icinga.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
NagiosCheckTask::NagiosCheckTask(const Service& service)
|
|
|
|
{
|
|
|
|
string checkCommand = service.GetCheckCommand();
|
|
|
|
m_Command = MacroProcessor::ResolveMacros(checkCommand, service.GetMacros());
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckResult NagiosCheckTask::Execute(void) const
|
|
|
|
{
|
|
|
|
CheckResult cr;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
time(&cr.StartTime);
|
|
|
|
|
|
|
|
string command = m_Command + " 2>&1";
|
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
Application::Log(LogDebug, "icinga", "Nagios check command: " + command);
|
2012-06-14 16:31:38 +02:00
|
|
|
|
2012-06-13 13:42:55 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
fp = _popen(command.c_str(), "r");
|
|
|
|
#else /* _MSC_VER */
|
|
|
|
fp = popen(command.c_str(), "r");
|
|
|
|
#endif /* _MSC_VER */
|
|
|
|
|
|
|
|
stringstream output;
|
|
|
|
|
|
|
|
while (!feof(fp)) {
|
|
|
|
char buffer[128];
|
|
|
|
size_t read = fread(buffer, 1, sizeof(buffer), fp);
|
|
|
|
|
|
|
|
if (read == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
output << string(buffer, buffer + read);
|
|
|
|
}
|
|
|
|
|
|
|
|
cr.Output = output.str();
|
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
Application::Log(LogDebug, "icinga", "Nagios plugin output: " + cr.Output);
|
2012-06-14 16:31:38 +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)
|
|
|
|
{
|
|
|
|
assert(service.GetCheckType() == "nagios");
|
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
return boost::make_shared<NagiosCheckTask>(service);
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|