icinga2/lib/compat/externalcommandlistener.cpp

150 lines
4.2 KiB
C++
Raw Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2013-09-25 07:31:46 +02:00
2014-05-25 16:23:35 +02:00
#include "compat/externalcommandlistener.hpp"
2018-01-18 13:50:38 +01:00
#include "compat/externalcommandlistener-ti.cpp"
2014-05-25 16:23:35 +02:00
#include "icinga/externalcommandprocessor.hpp"
#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);
REGISTER_STATSFUNCTION(ExternalCommandListener, &ExternalCommandListener::StatsFunc);
void ExternalCommandListener::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
{
DictionaryData nodes;
for (const ExternalCommandListener::Ptr& externalcommandlistener : ConfigType::GetObjectsByType<ExternalCommandListener>()) {
nodes.emplace_back(externalcommandlistener->GetName(), 1); //add more stats
}
status->Set("externalcommandlistener", new Dictionary(std::move(nodes)));
}
2013-09-25 07:31:46 +02:00
/**
* Starts the component.
*/
void ExternalCommandListener::Start(bool runtimeCreated)
2013-09-25 07:31:46 +02:00
{
ObjectImpl<ExternalCommandListener>::Start(runtimeCreated);
2013-09-25 07:31:46 +02:00
Log(LogInformation, "ExternalCommandListener")
<< "'" << GetName() << "' started.";
Log(LogWarning, "ExternalCommandListener")
<< "This feature is DEPRECATED and will be removed in future releases. Check the roadmap at https://github.com/Icinga/icinga2/milestones";
2013-09-25 07:31:46 +02:00
#ifndef _WIN32
2017-11-21 12:12:58 +01:00
m_CommandThread = std::thread(std::bind(&ExternalCommandListener::CommandPipeThread, this, GetCommandPath()));
2013-09-25 07:31:46 +02:00
m_CommandThread.detach();
#endif /* _WIN32 */
}
/**
* Stops the component.
*/
void ExternalCommandListener::Stop(bool runtimeRemoved)
{
Log(LogInformation, "ExternalCommandListener")
<< "'" << GetName() << "' stopped.";
ObjectImpl<ExternalCommandListener>::Stop(runtimeRemoved);
}
2013-09-25 07:31:46 +02:00
#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 {
Utility::Remove(commandPath);
2013-09-25 07:31:46 +02:00
}
}
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
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) << "\"";
return;
2013-09-25 07:31:46 +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) << "\"";
return;
}
2013-09-25 07:31:46 +02:00
for (;;) {
int fd = open(commandPath.CStr(), O_RDWR | O_NONBLOCK);
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) << "\"";
return;
}
FIFO::Ptr fifo = new FIFO();
Socket::Ptr sock = new Socket(fd);
StreamReadContext src;
for (;;) {
sock->Poll(true, false);
char buffer[8192];
size_t rc;
try {
rc = sock->Read(buffer, sizeof(buffer));
} catch (const std::exception& ex) {
/* We have read all data. */
if (errno == EAGAIN)
continue;
Log(LogWarning, "ExternalCommandListener")
<< "Cannot read from command pipe." << DiagnosticInformation(ex);
break;
}
/* Empty pipe (EOF) */
if (rc == 0)
continue;
2013-09-25 07:31:46 +02:00
fifo->Write(buffer, rc);
2013-09-25 07:31:46 +02:00
for (;;) {
String command;
StreamReadStatus srs = fifo->ReadLine(&command, src);
2013-09-25 07:31:46 +02:00
if (srs != StatusNewItem)
break;
try {
Log(LogInformation, "ExternalCommandListener")
<< "Executing external command: " << command;
ExternalCommandProcessor::Execute(command);
} catch (const std::exception& ex) {
Log(LogWarning, "ExternalCommandListener")
<< "External command failed: " << DiagnosticInformation(ex, false);
Log(LogNotice, "ExternalCommandListener")
<< "External command failed: " << DiagnosticInformation(ex, true);
}
}
}
2013-09-25 07:31:46 +02:00
}
}
#endif /* _WIN32 */