2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-03-10 03:09:01 +01:00
|
|
|
|
2014-05-28 14:25:12 +02:00
|
|
|
#include "livestatus/livestatuslistener.hpp"
|
2018-01-18 13:50:38 +01:00
|
|
|
#include "livestatus/livestatuslistener-ti.cpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/utility.hpp"
|
2017-05-15 15:51:39 +02:00
|
|
|
#include "base/perfdatavalue.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/objectlock.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/tcpsocket.hpp"
|
|
|
|
#include "base/unixsocket.hpp"
|
|
|
|
#include "base/networkstream.hpp"
|
|
|
|
#include "base/application.hpp"
|
2015-01-21 08:47:45 +01:00
|
|
|
#include "base/function.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/statsfunction.hpp"
|
|
|
|
#include "base/convert.hpp"
|
2013-09-28 12:54:26 +02:00
|
|
|
|
2013-03-10 03:09:01 +01:00
|
|
|
using namespace icinga;
|
|
|
|
|
2013-09-25 09:31:52 +02:00
|
|
|
REGISTER_TYPE(LivestatusListener);
|
2013-03-12 13:45:54 +01:00
|
|
|
|
2013-07-19 15:42:00 +02:00
|
|
|
static int l_ClientsConnected = 0;
|
|
|
|
static int l_Connections = 0;
|
|
|
|
static boost::mutex l_ComponentMutex;
|
|
|
|
|
2015-09-21 11:44:58 +02:00
|
|
|
REGISTER_STATSFUNCTION(LivestatusListener, &LivestatusListener::StatsFunc);
|
2014-02-17 16:34:18 +01:00
|
|
|
|
2015-02-07 22:36:17 +01:00
|
|
|
void LivestatusListener::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata)
|
2014-02-17 16:34:18 +01:00
|
|
|
{
|
2018-01-11 11:17:38 +01:00
|
|
|
DictionaryData nodes;
|
2014-02-18 10:53:44 +01:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const LivestatusListener::Ptr& livestatuslistener : ConfigType::GetObjectsByType<LivestatusListener>()) {
|
2018-01-11 11:17:38 +01:00
|
|
|
nodes.emplace_back(livestatuslistener->GetName(), new Dictionary({
|
|
|
|
{ "connections", l_Connections }
|
|
|
|
}));
|
2014-02-18 10:53:44 +01:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
perfdata->Add(new PerfdataValue("livestatuslistener_" + livestatuslistener->GetName() + "_connections", l_Connections));
|
2014-02-18 10:53:44 +01:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
status->Set("livestatuslistener", new Dictionary(std::move(nodes)));
|
2014-02-17 16:34:18 +01:00
|
|
|
}
|
|
|
|
|
2013-03-10 03:09:01 +01:00
|
|
|
/**
|
|
|
|
* Starts the component.
|
|
|
|
*/
|
2015-08-20 17:18:48 +02:00
|
|
|
void LivestatusListener::Start(bool runtimeCreated)
|
2013-03-10 03:09:01 +01:00
|
|
|
{
|
2015-08-20 17:18:48 +02:00
|
|
|
ObjectImpl<LivestatusListener>::Start(runtimeCreated);
|
2013-08-20 11:06:04 +02:00
|
|
|
|
2017-02-08 14:53:52 +01:00
|
|
|
Log(LogInformation, "LivestatusListener")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "'" << GetName() << "' started.";
|
2017-02-08 14:53:52 +01:00
|
|
|
|
2013-07-18 14:57:04 +02:00
|
|
|
if (GetSocketType() == "tcp") {
|
2014-11-08 21:17:16 +01:00
|
|
|
TcpSocket::Ptr socket = new TcpSocket();
|
2015-01-08 14:03:43 +01:00
|
|
|
|
2014-06-05 14:08:01 +02:00
|
|
|
try {
|
|
|
|
socket->Bind(GetBindHost(), GetBindPort(), AF_UNSPEC);
|
|
|
|
} catch (std::exception&) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "LivestatusListener")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Cannot bind TCP socket on host '" << GetBindHost() << "' port '" << GetBindPort() << "'.";
|
2014-06-05 14:08:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-07-18 14:57:04 +02:00
|
|
|
|
2015-01-08 14:03:43 +01:00
|
|
|
m_Listener = socket;
|
|
|
|
|
2017-11-21 12:12:58 +01:00
|
|
|
m_Thread = std::thread(std::bind(&LivestatusListener::ServerThreadProc, this));
|
2015-01-09 09:53:43 +01:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogInformation, "LivestatusListener")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Created TCP socket listening on host '" << GetBindHost() << "' port '" << GetBindPort() << "'.";
|
2013-07-18 14:57:04 +02:00
|
|
|
}
|
|
|
|
else if (GetSocketType() == "unix") {
|
|
|
|
#ifndef _WIN32
|
2014-11-08 21:17:16 +01:00
|
|
|
UnixSocket::Ptr socket = new UnixSocket();
|
2015-01-08 14:03:43 +01:00
|
|
|
|
2014-06-05 14:08:01 +02:00
|
|
|
try {
|
|
|
|
socket->Bind(GetSocketPath());
|
|
|
|
} catch (std::exception&) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "LivestatusListener")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Cannot bind UNIX socket to '" << GetSocketPath() << "'.";
|
2014-06-05 14:08:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-07-18 14:57:04 +02:00
|
|
|
|
2013-09-27 19:39:46 +02:00
|
|
|
/* group must be able to write */
|
2013-09-28 12:54:26 +02:00
|
|
|
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
|
|
|
|
|
|
|
|
if (chmod(GetSocketPath().CStr(), mode) < 0) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "LivestatusListener")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "chmod() on unix socket '" << GetSocketPath() << "' failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
|
2014-06-05 17:43:34 +02:00
|
|
|
return;
|
2013-09-27 19:39:46 +02:00
|
|
|
}
|
|
|
|
|
2015-01-08 14:03:43 +01:00
|
|
|
m_Listener = socket;
|
|
|
|
|
2017-11-21 12:12:58 +01:00
|
|
|
m_Thread = std::thread(std::bind(&LivestatusListener::ServerThreadProc, this));
|
2015-01-09 09:53:43 +01:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogInformation, "LivestatusListener")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Created UNIX socket in '" << GetSocketPath() << "'.";
|
2013-07-18 14:57:04 +02:00
|
|
|
#else
|
2014-10-19 17:52:17 +02:00
|
|
|
/* no UNIX sockets on windows */
|
2014-05-28 13:03:44 +02:00
|
|
|
Log(LogCritical, "LivestatusListener", "Unix sockets are not supported on Windows.");
|
2013-07-18 14:57:04 +02:00
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
void LivestatusListener::Stop(bool runtimeRemoved)
|
2015-01-08 14:03:43 +01:00
|
|
|
{
|
2015-08-20 17:18:48 +02:00
|
|
|
ObjectImpl<LivestatusListener>::Stop(runtimeRemoved);
|
2015-01-08 14:03:43 +01:00
|
|
|
|
2017-02-08 14:53:52 +01:00
|
|
|
Log(LogInformation, "LivestatusListener")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "'" << GetName() << "' stopped.";
|
2017-02-08 14:53:52 +01:00
|
|
|
|
2015-01-08 14:03:43 +01:00
|
|
|
m_Listener->Close();
|
2015-01-09 09:53:43 +01:00
|
|
|
|
|
|
|
if (m_Thread.joinable())
|
|
|
|
m_Thread.join();
|
2015-01-08 14:03:43 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
int LivestatusListener::GetClientsConnected()
|
2013-07-18 14:57:04 +02:00
|
|
|
{
|
2013-07-19 15:42:00 +02:00
|
|
|
boost::mutex::scoped_lock lock(l_ComponentMutex);
|
|
|
|
|
|
|
|
return l_ClientsConnected;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
int LivestatusListener::GetConnections()
|
2013-07-19 15:42:00 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(l_ComponentMutex);
|
|
|
|
|
|
|
|
return l_Connections;
|
2013-07-18 14:57:04 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void LivestatusListener::ServerThreadProc()
|
2013-03-10 03:09:01 +01:00
|
|
|
{
|
2015-01-09 09:53:43 +01:00
|
|
|
m_Listener->Listen();
|
2013-04-04 16:08:02 +02:00
|
|
|
|
2015-01-09 09:53:43 +01:00
|
|
|
try {
|
|
|
|
for (;;) {
|
2015-01-29 11:39:06 +01:00
|
|
|
timeval tv = { 0, 500000 };
|
|
|
|
|
|
|
|
if (m_Listener->Poll(true, false, &tv)) {
|
|
|
|
Socket::Ptr client = m_Listener->Accept();
|
|
|
|
Log(LogNotice, "LivestatusListener", "Client connected");
|
2017-11-21 11:52:55 +01:00
|
|
|
Utility::QueueAsyncCallback(std::bind(&LivestatusListener::ClientHandler, this, client), LowLatencyScheduler);
|
2015-01-29 11:39:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsActive())
|
|
|
|
break;
|
2014-06-05 16:17:53 +02:00
|
|
|
}
|
2015-01-09 09:53:43 +01:00
|
|
|
} catch (std::exception&) {
|
2015-10-02 11:23:45 +02:00
|
|
|
Log(LogCritical, "LivestatusListener", "Cannot accept new connection.");
|
2013-04-04 16:08:02 +02:00
|
|
|
}
|
2015-01-09 09:53:43 +01:00
|
|
|
|
|
|
|
m_Listener->Close();
|
2013-03-10 03:09:01 +01:00
|
|
|
}
|
|
|
|
|
2013-12-12 12:07:47 +01:00
|
|
|
void LivestatusListener::ClientHandler(const Socket::Ptr& client)
|
2013-03-10 03:09:01 +01:00
|
|
|
{
|
2013-07-19 15:42:00 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(l_ComponentMutex);
|
|
|
|
l_ClientsConnected++;
|
|
|
|
l_Connections++;
|
|
|
|
}
|
2013-07-18 14:57:04 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Stream::Ptr stream = new NetworkStream(client);
|
2013-04-04 16:08:02 +02:00
|
|
|
|
2015-10-16 15:11:32 +02:00
|
|
|
StreamReadContext context;
|
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
for (;;) {
|
|
|
|
String line;
|
|
|
|
|
|
|
|
std::vector<String> lines;
|
|
|
|
|
2015-02-14 16:34:36 +01:00
|
|
|
for (;;) {
|
|
|
|
StreamReadStatus srs = stream->ReadLine(&line, context);
|
|
|
|
|
|
|
|
if (srs == StatusEof)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (srs != StatusNewItem)
|
|
|
|
continue;
|
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
if (line.GetLength() > 0)
|
|
|
|
lines.push_back(line);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2013-03-10 03:09:01 +01:00
|
|
|
|
2014-03-10 09:44:53 +01:00
|
|
|
if (lines.empty())
|
|
|
|
break;
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
LivestatusQuery::Ptr query = new LivestatusQuery(lines, GetCompatLogPath());
|
2013-12-12 12:07:47 +01:00
|
|
|
if (!query->Execute(stream))
|
|
|
|
break;
|
2013-04-04 16:08:02 +02:00
|
|
|
}
|
2013-07-18 14:57:04 +02:00
|
|
|
|
2013-07-19 15:42:00 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(l_ComponentMutex);
|
|
|
|
l_ClientsConnected--;
|
|
|
|
}
|
2013-03-10 03:09:01 +01:00
|
|
|
}
|
2013-08-20 11:06:04 +02:00
|
|
|
|
2013-10-29 13:44:43 +01:00
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
void LivestatusListener::ValidateSocketType(const Lazy<String>& lvalue, const ValidationUtils& utils)
|
2013-09-30 10:07:49 +02:00
|
|
|
{
|
2018-01-11 07:08:09 +01:00
|
|
|
ObjectImpl<LivestatusListener>::ValidateSocketType(lvalue, utils);
|
2013-09-30 10:07:49 +02:00
|
|
|
|
2018-01-11 07:08:09 +01:00
|
|
|
if (lvalue() != "unix" && lvalue() != "tcp")
|
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, { "socket_type" }, "Socket type '" + lvalue() + "' is invalid."));
|
2013-11-06 08:51:56 +01:00
|
|
|
}
|