2013-09-25 07:31:46 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2013-09-25 07:31:46 +02:00
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "compat/externalcommandlistener.hpp"
|
2015-03-28 11:04:42 +01:00
|
|
|
#include "compat/externalcommandlistener.tcpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/externalcommandprocessor.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/statsfunction.hpp"
|
2013-09-25 07:31:46 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_TYPE(ExternalCommandListener);
|
|
|
|
|
2015-09-21 11:44:58 +02:00
|
|
|
REGISTER_STATSFUNCTION(ExternalCommandListener, &ExternalCommandListener::StatsFunc);
|
2014-02-17 16:34:18 +01:00
|
|
|
|
2015-02-07 22:36:17 +01:00
|
|
|
void ExternalCommandListener::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
|
2014-02-17 16:34:18 +01:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr nodes = new Dictionary();
|
2014-02-18 10:53:44 +01:00
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
BOOST_FOREACH(const ExternalCommandListener::Ptr& externalcommandlistener, ConfigType::GetObjectsByType<ExternalCommandListener>()) {
|
2014-02-18 10:53:44 +01:00
|
|
|
nodes->Set(externalcommandlistener->GetName(), 1); //add more stats
|
|
|
|
}
|
|
|
|
|
|
|
|
status->Set("externalcommandlistener", nodes);
|
2014-02-17 16:34:18 +01:00
|
|
|
}
|
|
|
|
|
2013-09-25 07:31:46 +02:00
|
|
|
/**
|
|
|
|
* Starts the component.
|
|
|
|
*/
|
2015-08-20 17:18:48 +02:00
|
|
|
void ExternalCommandListener::Start(bool runtimeCreated)
|
2013-09-25 07:31:46 +02:00
|
|
|
{
|
2015-08-20 17:18:48 +02:00
|
|
|
ObjectImpl<ExternalCommandListener>::Start(runtimeCreated);
|
2013-09-25 07:31:46 +02:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
m_CommandThread = boost::thread(boost::bind(&ExternalCommandListener::CommandPipeThread, this, GetCommandPath()));
|
|
|
|
m_CommandThread.detach();
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
void ExternalCommandListener::CommandPipeThread(const String& commandPath)
|
|
|
|
{
|
|
|
|
Utility::SetThreadName("Command Pipe");
|
|
|
|
|
|
|
|
struct stat statbuf;
|
|
|
|
bool fifo_ok = false;
|
|
|
|
|
|
|
|
if (lstat(commandPath.CStr(), &statbuf) >= 0) {
|
|
|
|
if (S_ISFIFO(statbuf.st_mode) && access(commandPath.CStr(), R_OK) >= 0) {
|
|
|
|
fifo_ok = true;
|
|
|
|
} else {
|
|
|
|
if (unlink(commandPath.CStr()) < 0) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("unlink")
|
|
|
|
<< boost::errinfo_errno(errno)
|
|
|
|
<< boost::errinfo_file_name(commandPath));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-28 08:10:32 +02:00
|
|
|
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
|
2013-09-27 19:17:39 +02:00
|
|
|
|
2013-09-25 07:31:46 +02:00
|
|
|
if (!fifo_ok && mkfifo(commandPath.CStr(), S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "ExternalCommandListener")
|
|
|
|
<< "mkfifo() for fifo path '" << commandPath << "' failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
|
2014-06-05 17:43:34 +02:00
|
|
|
return;
|
2013-09-25 07:31:46 +02:00
|
|
|
}
|
|
|
|
|
2013-09-28 08:10:32 +02:00
|
|
|
/* mkfifo() uses umask to mask off some bits, which means we need to chmod() the
|
|
|
|
* fifo to get the right mask. */
|
|
|
|
if (chmod(commandPath.CStr(), mode) < 0) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "ExternalCommandListener")
|
|
|
|
<< "chmod() on fifo '" << commandPath << "' failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
|
2014-06-05 17:43:34 +02:00
|
|
|
return;
|
2013-09-28 08:10:32 +02:00
|
|
|
}
|
2013-09-27 19:17:39 +02:00
|
|
|
|
2013-09-25 07:31:46 +02:00
|
|
|
for (;;) {
|
2015-11-09 20:39:26 +01:00
|
|
|
int fd = open(commandPath.CStr(), O_RDONLY | O_NONBLOCK);
|
2014-08-20 13:37:06 +02:00
|
|
|
|
|
|
|
if (fd < 0) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "ExternalCommandListener")
|
|
|
|
<< "open() for fifo path '" << commandPath << "' failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
|
2014-08-20 13:37:06 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-08-20 13:32:07 +02:00
|
|
|
|
2015-11-10 11:41:21 +01:00
|
|
|
FIFO::Ptr fifo = new FIFO();
|
|
|
|
Socket::Ptr sock = new Socket(fd);
|
|
|
|
StreamReadContext src;
|
2015-11-09 20:39:26 +01:00
|
|
|
|
2015-11-10 11:41:21 +01:00
|
|
|
for (;;) {
|
|
|
|
sock->Poll(true, false);
|
2014-08-20 13:32:07 +02:00
|
|
|
|
2015-11-10 11:41:21 +01:00
|
|
|
char buffer[8192];
|
|
|
|
size_t rc = sock->Read(buffer, sizeof(buffer));
|
|
|
|
if (rc <= 0)
|
|
|
|
break;
|
2013-09-25 07:31:46 +02:00
|
|
|
|
2015-11-10 11:41:21 +01:00
|
|
|
fifo->Write(buffer, rc);
|
2013-09-25 07:31:46 +02:00
|
|
|
|
2015-11-10 11:41:21 +01:00
|
|
|
for (;;) {
|
|
|
|
String command;
|
|
|
|
StreamReadStatus srs = fifo->ReadLine(&command, src);
|
2013-09-25 07:31:46 +02:00
|
|
|
|
2015-11-10 11:41:21 +01:00
|
|
|
if (srs != StatusNewItem)
|
|
|
|
break;
|
2014-08-21 09:36:05 +02:00
|
|
|
|
2015-11-10 11:41:21 +01:00
|
|
|
try {
|
|
|
|
Log(LogInformation, "ExternalCommandListener")
|
|
|
|
<< "Executing external command: " << command;
|
2014-08-21 09:36:05 +02:00
|
|
|
|
2015-11-10 11:41:21 +01:00
|
|
|
ExternalCommandProcessor::Execute(command);
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
Log(LogWarning, "ExternalCommandListener")
|
|
|
|
<< "External command failed." << DiagnosticInformation(ex);
|
|
|
|
}
|
2014-08-21 09:36:05 +02:00
|
|
|
}
|
2014-08-20 13:37:06 +02:00
|
|
|
}
|
2013-09-25 07:31:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|