2013-03-10 03:09:01 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2013-09-25 07:43:57 +02:00
|
|
|
* Copyright (C) 2012-2013 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-09-25 09:31:52 +02:00
|
|
|
#include "livestatus/listener.h"
|
2013-07-19 15:42:00 +02:00
|
|
|
#include "base/objectlock.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/dynamictype.h"
|
|
|
|
#include "base/logger_fwd.h"
|
2013-09-28 12:54:26 +02:00
|
|
|
#include "base/exception.h"
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "base/tcpsocket.h"
|
2013-07-18 14:57:04 +02:00
|
|
|
#include "base/unixsocket.h"
|
2013-04-04 16:08:02 +02:00
|
|
|
#include "base/networkstream.h"
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "base/application.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/smart_ptr/make_shared.hpp>
|
2013-09-28 12:54:26 +02:00
|
|
|
#include <boost/exception/diagnostic_information.hpp>
|
|
|
|
|
2013-03-10 03:09:01 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
2013-03-10 09:23:13 +01:00
|
|
|
using namespace livestatus;
|
2013-03-10 03:09:01 +01:00
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
DynamicObject::Start();
|
|
|
|
|
2013-07-18 14:57:04 +02:00
|
|
|
if (GetSocketType() == "tcp") {
|
|
|
|
TcpSocket::Ptr socket = boost::make_shared<TcpSocket>();
|
2013-09-25 09:31:52 +02:00
|
|
|
socket->Bind(GetBindHost(), GetBindPort(), AF_INET);
|
2013-07-18 14:57:04 +02:00
|
|
|
|
2013-09-25 09:31:52 +02:00
|
|
|
boost::thread thread(boost::bind(&LivestatusListener::ServerThreadProc, this, socket));
|
2013-07-18 14:57:04 +02:00
|
|
|
thread.detach();
|
2013-09-28 12:54:26 +02:00
|
|
|
Log(LogInformation, "livestatus", "Created tcp socket listening on host '" + GetBindHost() + "' port '" + GetBindPort() + "'.");
|
2013-07-18 14:57:04 +02:00
|
|
|
}
|
|
|
|
else if (GetSocketType() == "unix") {
|
|
|
|
#ifndef _WIN32
|
|
|
|
UnixSocket::Ptr socket = boost::make_shared<UnixSocket>();
|
|
|
|
socket->Bind(GetSocketPath());
|
|
|
|
|
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) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("chmod")
|
|
|
|
<< boost::errinfo_errno(errno)
|
|
|
|
<< boost::errinfo_file_name(GetSocketPath()));
|
2013-09-27 19:39:46 +02:00
|
|
|
}
|
|
|
|
|
2013-09-25 09:31:52 +02:00
|
|
|
boost::thread thread(boost::bind(&LivestatusListener::ServerThreadProc, this, socket));
|
2013-07-18 14:57:04 +02:00
|
|
|
thread.detach();
|
2013-09-28 12:54:26 +02:00
|
|
|
Log(LogInformation, "livestatus", "Created unix socket in '" + GetSocketPath() + "'.");
|
2013-07-18 14:57:04 +02:00
|
|
|
#else
|
|
|
|
/* no unix sockets on windows */
|
|
|
|
Log(LogCritical, "livestatus", "Unix sockets are not supported on Windows.");
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-25 09:31:52 +02:00
|
|
|
String LivestatusListener::GetSocketType(void) const
|
2013-07-18 14:57:04 +02:00
|
|
|
{
|
|
|
|
Value socketType = m_SocketType;
|
|
|
|
if (socketType.IsEmpty())
|
|
|
|
return "unix";
|
|
|
|
else
|
|
|
|
return socketType;
|
2013-03-10 03:09:01 +01:00
|
|
|
}
|
|
|
|
|
2013-09-25 09:31:52 +02:00
|
|
|
String LivestatusListener::GetSocketPath(void) const
|
2013-03-10 03:09:01 +01:00
|
|
|
{
|
2013-03-12 13:45:54 +01:00
|
|
|
Value socketPath = m_SocketPath;
|
2013-03-10 03:09:01 +01:00
|
|
|
if (socketPath.IsEmpty())
|
2013-09-27 19:39:46 +02:00
|
|
|
return Application::GetLocalStateDir() + "/run/icinga2/rw/livestatus";
|
2013-03-10 03:09:01 +01:00
|
|
|
else
|
|
|
|
return socketPath;
|
|
|
|
}
|
|
|
|
|
2013-09-25 09:31:52 +02:00
|
|
|
String LivestatusListener::GetBindHost(void) const
|
2013-07-18 14:57:04 +02:00
|
|
|
{
|
2013-09-25 09:31:52 +02:00
|
|
|
if (m_BindHost.IsEmpty())
|
2013-07-18 14:57:04 +02:00
|
|
|
return "127.0.0.1";
|
|
|
|
else
|
2013-09-25 09:31:52 +02:00
|
|
|
return m_BindHost;
|
2013-07-18 14:57:04 +02:00
|
|
|
}
|
|
|
|
|
2013-09-25 09:31:52 +02:00
|
|
|
String LivestatusListener::GetBindPort(void) const
|
2013-07-18 14:57:04 +02:00
|
|
|
{
|
2013-09-25 09:31:52 +02:00
|
|
|
if (m_BindPort.IsEmpty())
|
2013-07-18 14:57:04 +02:00
|
|
|
return "6558";
|
|
|
|
else
|
2013-09-25 09:31:52 +02:00
|
|
|
return m_BindPort;
|
2013-07-18 14:57:04 +02: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
|
|
|
}
|
|
|
|
|
2013-09-25 09:31:52 +02:00
|
|
|
void LivestatusListener::ServerThreadProc(const Socket::Ptr& server)
|
2013-03-10 03:09:01 +01:00
|
|
|
{
|
2013-04-04 16:08:02 +02:00
|
|
|
server->Listen();
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
Socket::Ptr client = server->Accept();
|
2013-03-10 03:09:01 +01:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
Log(LogInformation, "livestatus", "Client connected");
|
2013-03-10 03:09:01 +01:00
|
|
|
|
2013-09-25 09:31:52 +02:00
|
|
|
boost::thread thread(boost::bind(&LivestatusListener::ClientThreadProc, this, client));
|
2013-04-04 16:08:02 +02:00
|
|
|
thread.detach();
|
|
|
|
}
|
2013-03-10 03:09:01 +01:00
|
|
|
}
|
|
|
|
|
2013-09-25 09:31:52 +02:00
|
|
|
void LivestatusListener::ClientThreadProc(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
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
Stream::Ptr stream = boost::make_shared<NetworkStream>(client);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
String line;
|
2013-07-03 16:16:38 +02:00
|
|
|
ReadLineContext context;
|
2013-04-04 16:08:02 +02:00
|
|
|
|
|
|
|
std::vector<String> lines;
|
|
|
|
|
2013-07-03 16:16:38 +02:00
|
|
|
while (stream->ReadLine(&line, context)) {
|
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
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
Query::Ptr query = boost::make_shared<Query>(lines);
|
2013-07-04 09:45:44 +02: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-09-25 09:31:52 +02:00
|
|
|
void LivestatusListener::InternalSerialize(const Dictionary::Ptr& bag, int attributeTypes) const
|
2013-08-20 11:06:04 +02:00
|
|
|
{
|
|
|
|
DynamicObject::InternalSerialize(bag, attributeTypes);
|
|
|
|
|
|
|
|
if (attributeTypes & Attribute_Config) {
|
|
|
|
bag->Set("socket_type", m_SocketType);
|
|
|
|
bag->Set("socket_path", m_SocketPath);
|
2013-09-25 09:31:52 +02:00
|
|
|
bag->Set("bind_host", m_BindHost);
|
|
|
|
bag->Set("bind_port", m_BindPort);
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-25 09:31:52 +02:00
|
|
|
void LivestatusListener::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes)
|
2013-08-20 11:06:04 +02:00
|
|
|
{
|
|
|
|
DynamicObject::InternalDeserialize(bag, attributeTypes);
|
|
|
|
|
|
|
|
if (attributeTypes & Attribute_Config) {
|
|
|
|
m_SocketType = bag->Get("socket_type");
|
|
|
|
m_SocketPath = bag->Get("socket_path");
|
2013-09-25 09:31:52 +02:00
|
|
|
m_BindHost = bag->Get("bind_host");
|
|
|
|
m_BindPort = bag->Get("bind_port");
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
|
|
|
}
|