2013-03-10 03:09:01 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2013-03-10 03:09:01 +01: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-28 14:25:12 +02:00
|
|
|
#include "livestatus/livestatuslistener.hpp"
|
2015-03-28 11:04:42 +01:00
|
|
|
#include "livestatus/livestatuslistener.tcpp"
|
2014-09-17 15:38:39 +02:00
|
|
|
#include "icinga/perfdatavalue.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/utility.hpp"
|
|
|
|
#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;
|
|
|
|
|
2014-02-17 16:34:18 +01:00
|
|
|
REGISTER_STATSFUNCTION(LivestatusListenerStats, &LivestatusListener::StatsFunc);
|
|
|
|
|
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
|
|
|
{
|
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 LivestatusListener::Ptr& livestatuslistener, ConfigType::GetObjectsByType<LivestatusListener>()) {
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr stats = new Dictionary();
|
2014-02-18 10:53:44 +01:00
|
|
|
stats->Set("connections", l_Connections);
|
|
|
|
|
|
|
|
nodes->Set(livestatuslistener->GetName(), stats);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
status->Set("livestatuslistener", nodes);
|
2014-02-17 16:34:18 +01:00
|
|
|
}
|
|
|
|
|
2013-03-10 03:09:01 +01:00
|
|
|
/**
|
|
|
|
* Starts the component.
|
|
|
|
*/
|
2013-09-25 09:31:52 +02:00
|
|
|
void LivestatusListener::Start(void)
|
2013-03-10 03:09:01 +01:00
|
|
|
{
|
2015-08-15 20:28:05 +02:00
|
|
|
ConfigObject::Start();
|
2013-08-20 11:06:04 +02: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")
|
|
|
|
<< "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;
|
|
|
|
|
2015-01-09 09:53:43 +01:00
|
|
|
m_Thread = boost::thread(boost::bind(&LivestatusListener::ServerThreadProc, this));
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogInformation, "LivestatusListener")
|
|
|
|
<< "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")
|
|
|
|
<< "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")
|
|
|
|
<< "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;
|
|
|
|
|
2015-01-09 09:53:43 +01:00
|
|
|
m_Thread = boost::thread(boost::bind(&LivestatusListener::ServerThreadProc, this));
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogInformation, "LivestatusListener")
|
|
|
|
<< "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-01-08 14:03:43 +01:00
|
|
|
void LivestatusListener::Stop(void)
|
|
|
|
{
|
2015-08-15 20:28:05 +02:00
|
|
|
ConfigObject::Stop();
|
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
|
|
|
}
|
|
|
|
|
2013-09-25 09:31:52 +02:00
|
|
|
int LivestatusListener::GetClientsConnected(void)
|
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;
|
|
|
|
}
|
|
|
|
|
2013-09-25 09:31:52 +02:00
|
|
|
int LivestatusListener::GetConnections(void)
|
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
|
|
|
}
|
|
|
|
|
2015-01-09 09:53:43 +01:00
|
|
|
void LivestatusListener::ServerThreadProc(void)
|
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");
|
|
|
|
Utility::QueueAsyncCallback(boost::bind(&LivestatusListener::ClientHandler, this, client), LowLatencyScheduler);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsActive())
|
|
|
|
break;
|
2014-06-05 16:17:53 +02:00
|
|
|
}
|
2015-01-09 09:53:43 +01:00
|
|
|
} catch (std::exception&) {
|
|
|
|
Log(LogCritical, "ListenerListener", "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
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
String line;
|
2015-02-14 16:34:36 +01:00
|
|
|
StreamReadContext context;
|
2013-04-04 16:08:02 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
void LivestatusListener::ValidateSocketType(const String& value, const ValidationUtils& utils)
|
2013-09-30 10:07:49 +02:00
|
|
|
{
|
2014-11-30 23:32:13 +01:00
|
|
|
ObjectImpl<LivestatusListener>::ValidateSocketType(value, utils);
|
2013-09-30 10:07:49 +02:00
|
|
|
|
2014-11-30 23:32:13 +01:00
|
|
|
if (value != "unix" && value != "tcp")
|
|
|
|
BOOST_THROW_EXCEPTION(ValidationError(this, boost::assign::list_of("socket_type"), "Socket type '" + value + "' is invalid."));
|
2013-11-06 08:51:56 +01:00
|
|
|
}
|