icinga2/lib/livestatus/livestatuslistener.cpp

223 lines
6.4 KiB
C++
Raw Normal View History

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"
#include "livestatus/livestatuslistener.tcpp"
#include "icinga/perfdatavalue.hpp"
2014-05-25 16:23:35 +02:00
#include "base/utility.hpp"
#include "base/objectlock.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/tcpsocket.hpp"
#include "base/unixsocket.hpp"
#include "base/networkstream.hpp"
#include "base/application.hpp"
#include "base/function.hpp"
2014-05-25 16:23:35 +02:00
#include "base/statsfunction.hpp"
#include "base/convert.hpp"
2013-03-10 03:09:01 +01:00
using namespace icinga;
2013-09-25 09:31:52 +02:00
REGISTER_TYPE(LivestatusListener);
static int l_ClientsConnected = 0;
static int l_Connections = 0;
static boost::mutex l_ComponentMutex;
REGISTER_STATSFUNCTION(LivestatusListenerStats, &LivestatusListener::StatsFunc);
void LivestatusListener::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata)
{
Dictionary::Ptr nodes = new Dictionary();
BOOST_FOREACH(const LivestatusListener::Ptr& livestatuslistener, ConfigType::GetObjectsByType<LivestatusListener>()) {
Dictionary::Ptr stats = new Dictionary();
stats->Set("connections", l_Connections);
nodes->Set(livestatuslistener->GetName(), stats);
perfdata->Add(new PerfdataValue("livestatuslistener_" + livestatuslistener->GetName() + "_connections", l_Connections));
}
status->Set("livestatuslistener", nodes);
}
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
{
ConfigObject::Start();
if (GetSocketType() == "tcp") {
TcpSocket::Ptr socket = new TcpSocket();
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() << "'.";
return;
}
m_Listener = socket;
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() << "'.";
}
else if (GetSocketType() == "unix") {
#ifndef _WIN32
UnixSocket::Ptr socket = new UnixSocket();
try {
socket->Bind(GetSocketPath());
} catch (std::exception&) {
2014-10-19 17:52:17 +02:00
Log(LogCritical, "LivestatusListener")
<< "Cannot bind UNIX socket to '" << GetSocketPath() << "'.";
return;
}
/* group must be able to write */
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) << "\"";
return;
}
m_Listener = socket;
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() << "'.";
#else
2014-10-19 17:52:17 +02:00
/* no UNIX sockets on windows */
Log(LogCritical, "LivestatusListener", "Unix sockets are not supported on Windows.");
return;
#endif
}
}
void LivestatusListener::Stop(void)
{
ConfigObject::Stop();
m_Listener->Close();
if (m_Thread.joinable())
m_Thread.join();
}
2013-09-25 09:31:52 +02:00
int LivestatusListener::GetClientsConnected(void)
{
boost::mutex::scoped_lock lock(l_ComponentMutex);
return l_ClientsConnected;
}
2013-09-25 09:31:52 +02:00
int LivestatusListener::GetConnections(void)
{
boost::mutex::scoped_lock lock(l_ComponentMutex);
return l_Connections;
}
void LivestatusListener::ServerThreadProc(void)
2013-03-10 03:09:01 +01:00
{
m_Listener->Listen();
2013-04-04 16:08:02 +02:00
try {
for (;;) {
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;
}
} catch (std::exception&) {
Log(LogCritical, "ListenerListener", "Cannot accept new connection.");
2013-04-04 16:08:02 +02:00
}
m_Listener->Close();
2013-03-10 03:09:01 +01:00
}
void LivestatusListener::ClientHandler(const Socket::Ptr& client)
2013-03-10 03:09:01 +01:00
{
{
boost::mutex::scoped_lock lock(l_ComponentMutex);
l_ClientsConnected++;
l_Connections++;
}
Stream::Ptr stream = new NetworkStream(client);
2013-04-04 16:08:02 +02:00
for (;;) {
String line;
StreamReadContext context;
2013-04-04 16:08:02 +02:00
std::vector<String> lines;
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
if (lines.empty())
break;
LivestatusQuery::Ptr query = new LivestatusQuery(lines, GetCompatLogPath());
if (!query->Execute(stream))
break;
2013-04-04 16:08:02 +02:00
}
{
boost::mutex::scoped_lock lock(l_ComponentMutex);
l_ClientsConnected--;
}
2013-03-10 03:09:01 +01:00
}
2013-10-29 13:44:43 +01:00
void LivestatusListener::ValidateSocketType(const String& value, const ValidationUtils& utils)
{
ObjectImpl<LivestatusListener>::ValidateSocketType(value, utils);
if (value != "unix" && value != "tcp")
BOOST_THROW_EXCEPTION(ValidationError(this, boost::assign::list_of("socket_type"), "Socket type '" + value + "' is invalid."));
}