icinga2/components/livestatus/listener.cpp

176 lines
5.5 KiB
C++
Raw Normal View History

2013-03-10 03:09:01 +01:00
/******************************************************************************
* Icinga 2 *
2014-01-09 00:32:11 +01:00
* Copyright (C) 2012-present 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"
#include "config/configcompilercontext.h"
#include "base/utility.h"
#include "base/objectlock.h"
2013-03-16 21:18:53 +01:00
#include "base/dynamictype.h"
#include "base/logger_fwd.h"
#include "base/exception.h"
2013-03-17 20:19:29 +01:00
#include "base/tcpsocket.h"
#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"
#include "base/scriptfunction.h"
#include "base/statsfunction.h"
2013-10-29 13:44:43 +01:00
#include "base/convert.h"
2013-03-10 03:09:01 +01:00
using namespace icinga;
2013-09-25 09:31:52 +02:00
REGISTER_TYPE(LivestatusListener);
REGISTER_SCRIPTFUNCTION(ValidateSocketType, &LivestatusListener::ValidateSocketType);
static int l_ClientsConnected = 0;
static int l_Connections = 0;
static boost::mutex l_ComponentMutex;
REGISTER_STATSFUNCTION(LivestatusListenerStats, &LivestatusListener::StatsFunc);
Value LivestatusListener::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata)
{
Dictionary::Ptr nodes = make_shared<Dictionary>();
BOOST_FOREACH(const LivestatusListener::Ptr& livestatuslistener, DynamicType::GetObjects<LivestatusListener>()) {
Dictionary::Ptr stats = make_shared<Dictionary>();
stats->Set("connections", l_Connections);
nodes->Set(livestatuslistener->GetName(), stats);
perfdata->Set("livestatuslistener_" + livestatuslistener->GetName() + "_connections", l_Connections);
}
status->Set("livestatuslistener", nodes);
return 0;
}
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
{
DynamicObject::Start();
if (GetSocketType() == "tcp") {
TcpSocket::Ptr socket = make_shared<TcpSocket>();
2013-09-25 09:31:52 +02:00
socket->Bind(GetBindHost(), GetBindPort(), AF_INET);
2013-09-25 09:31:52 +02:00
boost::thread thread(boost::bind(&LivestatusListener::ServerThreadProc, this, socket));
thread.detach();
Log(LogInformation, "livestatus", "Created tcp socket listening on host '" + GetBindHost() + "' port '" + GetBindPort() + "'.");
}
else if (GetSocketType() == "unix") {
#ifndef _WIN32
UnixSocket::Ptr socket = make_shared<UnixSocket>();
socket->Bind(GetSocketPath());
/* group must be able to write */
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-25 09:31:52 +02:00
boost::thread thread(boost::bind(&LivestatusListener::ServerThreadProc, this, socket));
thread.detach();
Log(LogInformation, "livestatus", "Created unix socket in '" + GetSocketPath() + "'.");
#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
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;
}
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
Utility::QueueAsyncCallback(boost::bind(&LivestatusListener::ClientHandler, this, client));
2013-04-04 16:08:02 +02:00
}
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 = make_shared<NetworkStream>(client);
2013-04-04 16:08:02 +02:00
for (;;) {
String line;
ReadLineContext context;
2013-04-04 16:08:02 +02:00
std::vector<String> lines;
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
Query::Ptr query = make_shared<Query>(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& location, const Dictionary::Ptr& attrs)
{
Value socket_type = attrs->Get("socket_type");
if (!socket_type.IsEmpty() && socket_type != "unix" && socket_type != "tcp") {
ConfigCompilerContext::GetInstance()->AddMessage(true, "Validation failed for " +
location + ": Socket type '" + socket_type + "' is invalid.");
}
}