2012-07-09 20:32:02 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-07-02 12:34:54 +02:00
|
|
|
#include "i2-cib.h"
|
2012-06-13 13:42:55 +02:00
|
|
|
|
2012-06-24 16:39:49 +02:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
# include "popen_noshell.h"
|
|
|
|
#endif /* _MSC_VER */
|
|
|
|
|
2012-06-13 13:42:55 +02:00
|
|
|
using namespace icinga;
|
|
|
|
|
2012-06-24 16:30:16 +02:00
|
|
|
boost::mutex NagiosCheckTask::m_Mutex;
|
2012-06-24 17:07:38 +02:00
|
|
|
vector<NagiosCheckTask::Ptr> NagiosCheckTask::m_PendingTasks;
|
2012-06-24 16:30:16 +02:00
|
|
|
deque<NagiosCheckTask::Ptr> NagiosCheckTask::m_Tasks;
|
|
|
|
condition_variable NagiosCheckTask::m_TasksCV;
|
2012-06-19 09:38:20 +02:00
|
|
|
|
2012-06-13 13:42:55 +02:00
|
|
|
NagiosCheckTask::NagiosCheckTask(const Service& service)
|
2012-06-24 16:32:50 +02:00
|
|
|
: CheckTask(service), m_FP(NULL), m_UsePopen(false)
|
2012-06-13 13:42:55 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-06-18 17:23:48 +02:00
|
|
|
void NagiosCheckTask::Enqueue(void)
|
2012-06-17 20:35:56 +02:00
|
|
|
{
|
2012-06-25 14:13:24 +02:00
|
|
|
time_t now;
|
|
|
|
time(&now);
|
2012-06-27 23:38:50 +02:00
|
|
|
GetResult().SetScheduleStart(now);
|
2012-06-25 14:13:24 +02:00
|
|
|
|
2012-06-24 17:07:38 +02:00
|
|
|
m_PendingTasks.push_back(GetSelf());
|
2012-06-17 20:35:56 +02:00
|
|
|
}
|
|
|
|
|
2012-06-24 16:30:16 +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
|
|
|
}
|
|
|
|
|
2012-06-24 16:30:16 +02:00
|
|
|
void NagiosCheckTask::CheckThreadProc(void)
|
2012-06-18 17:23:48 +02:00
|
|
|
{
|
2012-06-24 16:30:16 +02:00
|
|
|
mutex::scoped_lock lock(m_Mutex);
|
2012-06-19 09:38:20 +02:00
|
|
|
|
2012-06-24 16:30:16 +02:00
|
|
|
map<int, NagiosCheckTask::Ptr> tasks;
|
|
|
|
|
|
|
|
for (;;) {
|
2012-06-24 18:44:07 +02:00
|
|
|
while (m_Tasks.empty() || tasks.size() >= MaxChecksPerThread) {
|
2012-06-24 16:30:16 +02:00
|
|
|
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;
|
|
|
|
|
2012-06-24 16:30:16 +02:00
|
|
|
#ifndef _MSC_VER
|
2012-06-27 23:38:50 +02:00
|
|
|
if (!FD_ISSET(fd, &readfds)) {
|
2012-06-24 16:30:16 +02:00
|
|
|
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);
|
2012-06-24 16:30:16 +02:00
|
|
|
} else {
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lock.lock();
|
|
|
|
}
|
|
|
|
|
2012-06-25 14:13:24 +02:00
|
|
|
while (!m_Tasks.empty() && tasks.size() < MaxChecksPerThread) {
|
2012-06-24 16:30:16 +02:00
|
|
|
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);
|
|
|
|
|
2012-06-24 16:30:16 +02:00
|
|
|
CheckTask::FinishTask(task);
|
|
|
|
} else {
|
|
|
|
int fd = task->GetFD();
|
|
|
|
if (fd >= 0)
|
|
|
|
tasks[fd] = task;
|
|
|
|
}
|
|
|
|
}
|
2012-06-19 09:38:20 +02:00
|
|
|
}
|
2012-06-18 17:23:48 +02:00
|
|
|
}
|
|
|
|
|
2012-06-24 16:30:16 +02:00
|
|
|
bool NagiosCheckTask::InitTask(void)
|
2012-06-13 13:42:55 +02:00
|
|
|
{
|
2012-06-28 00:07:58 +02:00
|
|
|
time_t now;
|
|
|
|
time(&now);
|
|
|
|
GetResult().SetExecutionStart(now);
|
|
|
|
|
2012-06-13 13:42:55 +02:00
|
|
|
#ifdef _MSC_VER
|
2012-06-24 16:30:16 +02:00
|
|
|
m_FP = _popen(m_Command.c_str(), "r");
|
2012-06-13 13:42:55 +02:00
|
|
|
#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
|
|
|
|
2012-06-24 16:30:16 +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)
|
2012-06-24 16:30:16 +02:00
|
|
|
m_FP = popen(m_Command.c_str(), "r");
|
2012-06-13 13:42:55 +02:00
|
|
|
#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;
|
2012-06-24 16:30:16 +02:00
|
|
|
}
|
2012-06-13 13:42:55 +02:00
|
|
|
|
2012-06-29 14:14:51 +02:00
|
|
|
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);
|
2012-06-29 14:14:51 +02:00
|
|
|
|
|
|
|
if (!perfdata.empty())
|
|
|
|
perfdata.append(" ");
|
|
|
|
|
|
|
|
perfdata.append(line, delim + 1, line.size());
|
|
|
|
} else {
|
|
|
|
text.append(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GetResult().SetOutput(text);
|
|
|
|
GetResult().SetPerformanceDataRaw(perfdata);
|
|
|
|
}
|
|
|
|
|
2012-06-24 16:30:16 +02:00
|
|
|
bool NagiosCheckTask::RunTask(void)
|
|
|
|
{
|
|
|
|
char buffer[512];
|
|
|
|
size_t read = fread(buffer, 1, sizeof(buffer), m_FP);
|
2012-06-13 13:42:55 +02:00
|
|
|
|
2012-06-24 16:30:16 +02:00
|
|
|
if (read > 0)
|
|
|
|
m_OutputStream.write(buffer, read);
|
2012-06-13 13:42:55 +02:00
|
|
|
|
2012-06-24 16:30:16 +02:00
|
|
|
if (!feof(m_FP))
|
|
|
|
return true;
|
2012-06-13 13:42:55 +02:00
|
|
|
|
2012-06-25 14:13:24 +02:00
|
|
|
string output = m_OutputStream.str();
|
|
|
|
boost::algorithm::trim(output);
|
2012-06-29 14:14:51 +02:00
|
|
|
ProcessCheckOutput(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
|
2012-06-24 16:30:16 +02:00
|
|
|
status = _pclose(m_FP);
|
2012-06-13 13:42:55 +02:00
|
|
|
#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;
|
|
|
|
}
|
2012-06-13 13:42:55 +02:00
|
|
|
#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;
|
2012-06-13 13:42:55 +02:00
|
|
|
#endif /* _MSC_VER */
|
|
|
|
|
2012-06-25 15:42:46 +02:00
|
|
|
ServiceState state;
|
2012-06-25 14:13:24 +02:00
|
|
|
|
2012-06-14 13:04:22 +02:00
|
|
|
switch (exitcode) {
|
2012-06-13 13:42:55 +02:00
|
|
|
case 0:
|
2012-06-25 14:13:24 +02:00
|
|
|
state = StateOK;
|
2012-06-13 13:42:55 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
2012-06-25 14:13:24 +02:00
|
|
|
state = StateWarning;
|
2012-06-13 13:42:55 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2012-06-25 14:13:24 +02:00
|
|
|
state = StateCritical;
|
2012-06-13 13:42:55 +02:00
|
|
|
break;
|
|
|
|
default:
|
2012-06-25 14:13:24 +02:00
|
|
|
state = StateUnknown;
|
2012-06-13 13:42:55 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-06-25 14:13:24 +02:00
|
|
|
|
2012-06-27 23:38:50 +02:00
|
|
|
GetResult().SetState(state);
|
2012-06-13 13:42:55 +02:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
2012-06-25 14:13:24 +02:00
|
|
|
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);
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|
|
|
|
#endif /* _MSC_VER */
|
|
|
|
|
2012-06-25 14:13:24 +02:00
|
|
|
time_t now;
|
|
|
|
time(&now);
|
2012-06-27 23:38:50 +02:00
|
|
|
GetResult().SetExecutionEnd(now);
|
2012-06-24 16:30:16 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NagiosCheckTask::GetFD(void) const
|
|
|
|
{
|
|
|
|
return fileno(m_FP);
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2012-06-24 16:30:16 +02:00
|
|
|
|
|
|
|
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++) {
|
2012-06-24 16:30:16 +02:00
|
|
|
thread t(&NagiosCheckTask::CheckThreadProc);
|
|
|
|
t.detach();
|
|
|
|
}
|
2012-06-25 14:13:24 +02:00
|
|
|
}
|