icinga2/cib/nagioschecktask.cpp

307 lines
7.3 KiB
C++
Raw Normal View History

/******************************************************************************
* 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"
2012-06-24 16:39:49 +02:00
#ifndef _MSC_VER
# include "popen_noshell.h"
#endif /* _MSC_VER */
using namespace icinga;
boost::mutex NagiosCheckTask::m_Mutex;
2012-06-24 17:07:38 +02:00
vector<NagiosCheckTask::Ptr> NagiosCheckTask::m_PendingTasks;
deque<NagiosCheckTask::Ptr> NagiosCheckTask::m_Tasks;
condition_variable NagiosCheckTask::m_TasksCV;
2012-06-19 09:38:20 +02:00
NagiosCheckTask::NagiosCheckTask(const Service& service)
2012-06-24 16:32:50 +02:00
: CheckTask(service), m_FP(NULL), m_UsePopen(false)
{
string checkCommand = service.GetCheckCommand();
2012-07-13 11:29:23 +02:00
vector<Dictionary::Ptr> macroDicts;
macroDicts.push_back(service.GetMacros());
macroDicts.push_back(service.GetHost().GetMacros());
macroDicts.push_back(IcingaApplication::GetInstance()->GetMacros());
m_Command = MacroProcessor::ResolveMacros(checkCommand, macroDicts);
2012-06-17 20:35:56 +02:00
}
void NagiosCheckTask::Enqueue(void)
2012-06-17 20:35:56 +02:00
{
time_t now;
time(&now);
2012-06-27 23:38:50 +02:00
GetResult().SetScheduleStart(now);
2012-06-24 17:07:38 +02:00
m_PendingTasks.push_back(GetSelf());
2012-06-17 20:35:56 +02:00
}
void NagiosCheckTask::FlushQueue(void)
2012-06-17 20:35:56 +02:00
{
2012-06-24 17:07:38 +02:00
{
mutex::scoped_lock lock(m_Mutex);
std::copy(m_PendingTasks.begin(), m_PendingTasks.end(), back_inserter(m_Tasks));
m_PendingTasks.clear();
m_TasksCV.notify_all();
}
2012-06-17 20:35:56 +02:00
}
void NagiosCheckTask::CheckThreadProc(void)
{
mutex::scoped_lock lock(m_Mutex);
2012-06-19 09:38:20 +02:00
map<int, NagiosCheckTask::Ptr> tasks;
for (;;) {
2012-06-24 18:44:07 +02:00
while (m_Tasks.empty() || tasks.size() >= MaxChecksPerThread) {
lock.unlock();
map<int, NagiosCheckTask::Ptr>::iterator it, prev;
#ifndef _MSC_VER
fd_set readfds;
int nfds = 0;
FD_ZERO(&readfds);
for (it = tasks.begin(); it != tasks.end(); it++) {
if (it->first > nfds)
nfds = it->first;
FD_SET(it->first, &readfds);
}
timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
select(nfds + 1, &readfds, NULL, NULL, &tv);
#else /* _MSC_VER */
Sleep(1000);
#endif /* _MSC_VER */
for (it = tasks.begin(); it != tasks.end(); ) {
2012-06-27 23:38:50 +02:00
int fd = it->first;
NagiosCheckTask::Ptr task = it->second;
#ifndef _MSC_VER
2012-06-27 23:38:50 +02:00
if (!FD_ISSET(fd, &readfds)) {
it++;
continue;
}
#endif /* _MSC_VER */
2012-06-27 23:38:50 +02:00
if (!task->RunTask()) {
time_t now;
time(&now);
task->GetResult().SetScheduleEnd(now);
CheckTask::FinishTask(task);
2012-06-24 16:36:46 +02:00
prev = it;
it++;
tasks.erase(prev);
} else {
it++;
}
}
lock.lock();
}
while (!m_Tasks.empty() && tasks.size() < MaxChecksPerThread) {
NagiosCheckTask::Ptr task = m_Tasks.front();
m_Tasks.pop_front();
if (!task->InitTask()) {
2012-06-28 00:07:58 +02:00
time_t now;
2012-06-27 23:38:50 +02:00
time(&now);
task->GetResult().SetScheduleEnd(now);
CheckTask::FinishTask(task);
} else {
int fd = task->GetFD();
if (fd >= 0)
tasks[fd] = task;
}
}
2012-06-19 09:38:20 +02:00
}
}
bool NagiosCheckTask::InitTask(void)
{
2012-06-28 00:07:58 +02:00
time_t now;
time(&now);
GetResult().SetExecutionStart(now);
#ifdef _MSC_VER
m_FP = _popen(m_Command.c_str(), "r");
#else /* _MSC_VER */
2012-06-24 16:32:50 +02:00
if (!m_UsePopen) {
2012-06-24 16:39:49 +02:00
m_PCloseArg = new popen_noshell_pass_to_pclose;
m_FP = popen_noshell_compat(m_Command.c_str(), "r", (popen_noshell_pass_to_pclose *)m_PCloseArg);
2012-06-21 00:10:10 +02:00
if (m_FP == NULL) // TODO: add check for valgrind
2012-06-24 16:32:50 +02:00
m_UsePopen = true;
2012-06-21 00:10:10 +02:00
}
2012-06-24 16:32:50 +02:00
if (m_UsePopen)
m_FP = popen(m_Command.c_str(), "r");
#endif /* _MSC_VER */
2012-06-27 23:38:50 +02:00
if (m_FP == NULL) {
time_t now;
time(&now);
GetResult().SetExecutionEnd(now);
return false;
}
return true;
}
void NagiosCheckTask::ProcessCheckOutput(const string& output)
{
string text;
string perfdata;
vector<string> lines;
boost::algorithm::split(lines, output, is_any_of("\r\n"));
vector<string>::iterator it;
for (it = lines.begin(); it != lines.end(); it++) {
const string& line = *it;
string::size_type delim = line.find('|');
if (!text.empty())
text.append("\n");
if (delim != string::npos) {
2012-07-07 18:19:16 +02:00
text.append(line, 0, delim);
if (!perfdata.empty())
perfdata.append(" ");
perfdata.append(line, delim + 1, line.size());
} else {
text.append(line);
}
}
GetResult().SetOutput(text);
GetResult().SetPerformanceDataRaw(perfdata);
}
bool NagiosCheckTask::RunTask(void)
{
char buffer[512];
size_t read = fread(buffer, 1, sizeof(buffer), m_FP);
if (read > 0)
m_OutputStream.write(buffer, read);
if (!feof(m_FP))
return true;
string output = m_OutputStream.str();
boost::algorithm::trim(output);
ProcessCheckOutput(output);
2012-06-14 13:04:22 +02:00
int status, exitcode;
#ifdef _MSC_VER
status = _pclose(m_FP);
#else /* _MSC_VER */
2012-06-24 16:39:49 +02:00
if (m_UsePopen) {
2012-06-24 16:36:46 +02:00
status = pclose(m_FP);
2012-06-24 16:39:49 +02:00
} else {
status = pclose_noshell((popen_noshell_pass_to_pclose *)m_PCloseArg);
delete (popen_noshell_pass_to_pclose *)m_PCloseArg;
}
#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-07-03 14:18:46 +02:00
/* cmd.exe returns error code 1 (warning) when the plugin
* could not be executed - change the exit status to "unknown"
* when we have no plugin output. */
if (output.empty())
exitcode = 128;
#endif /* _MSC_VER */
2012-06-25 15:42:46 +02:00
ServiceState state;
2012-06-14 13:04:22 +02:00
switch (exitcode) {
case 0:
state = StateOK;
break;
case 1:
state = StateWarning;
break;
case 2:
state = StateCritical;
break;
default:
state = StateUnknown;
break;
}
2012-06-27 23:38:50 +02:00
GetResult().SetState(state);
#ifndef _MSC_VER
} else if (WIFSIGNALED(status)) {
stringstream outputbuf;
outputbuf << "Process was terminated by signal " << WTERMSIG(status);
2012-06-27 23:38:50 +02:00
GetResult().SetOutput(outputbuf.str());
GetResult().SetState(StateUnknown);
}
#endif /* _MSC_VER */
time_t now;
time(&now);
2012-06-27 23:38:50 +02:00
GetResult().SetExecutionEnd(now);
return false;
}
int NagiosCheckTask::GetFD(void) const
{
return fileno(m_FP);
}
CheckTask::Ptr NagiosCheckTask::CreateTask(const Service& service)
{
2012-06-15 19:32:41 +02:00
return boost::make_shared<NagiosCheckTask>(service);
}
void NagiosCheckTask::Register(void)
{
CheckTask::RegisterType("nagios", NagiosCheckTask::CreateTask, NagiosCheckTask::FlushQueue);
2012-06-24 18:44:07 +02:00
int numThreads = boost::thread::hardware_concurrency();
if (numThreads < 4)
numThreads = 4;
2012-06-24 17:07:38 +02:00
for (int i = 0; i < numThreads; i++) {
thread t(&NagiosCheckTask::CheckThreadProc);
t.detach();
}
}