2013-03-10 03:09:01 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 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"
|
2014-09-17 15:38:39 +02:00
|
|
|
#include "icinga/perfdatavalue.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "config/configcompilercontext.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/dynamictype.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/scriptfunction.hpp"
|
|
|
|
#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-09-30 10:07:49 +02:00
|
|
|
REGISTER_SCRIPTFUNCTION(ValidateSocketType, &LivestatusListener::ValidateSocketType);
|
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);
|
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
Value 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
|
|
|
|
2014-09-02 13:02:22 +02:00
|
|
|
BOOST_FOREACH(const LivestatusListener::Ptr& livestatuslistener, DynamicType::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
|
|
|
|
|
|
|
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
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
DynamicObject::Start();
|
|
|
|
|
2013-07-18 14:57:04 +02:00
|
|
|
if (GetSocketType() == "tcp") {
|
2014-11-08 21:17:16 +01:00
|
|
|
TcpSocket::Ptr socket = new TcpSocket();
|
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
|
|
|
|
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();
|
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();
|
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
|
|
|
}
|
|
|
|
|
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();
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 (;;) {
|
2014-06-05 16:17:53 +02:00
|
|
|
try {
|
|
|
|
Socket::Ptr client = server->Accept();
|
|
|
|
Log(LogNotice, "LivestatusListener", "Client connected");
|
2014-09-11 11:45:21 +02:00
|
|
|
Utility::QueueAsyncCallback(boost::bind(&LivestatusListener::ClientHandler, this, client), LowLatencyScheduler);
|
2014-06-05 16:17:53 +02:00
|
|
|
} catch (std::exception&) {
|
|
|
|
Log(LogCritical, "ListenerListener", "Cannot accept new connection.");
|
|
|
|
}
|
2013-04-04 16:08:02 +02:00
|
|
|
}
|
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;
|
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
|
|
|
|
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
|
|
|
|
2013-09-30 10:07:49 +02: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.");
|
|
|
|
}
|
2013-11-06 08:51:56 +01:00
|
|
|
}
|