From c36ea86ac99c07ef8e8f1ede7d9fa174a9ae998f Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Thu, 18 Jul 2013 14:57:04 +0200 Subject: [PATCH] livestatus: add unix and tcp socket support use socket_type and then either socket_path or address and port. fixes #4376 --- components/livestatus/component.cpp | 70 ++++++++++++++++++---- components/livestatus/component.h | 10 +++- components/livestatus/livestatus-type.conf | 5 ++ 3 files changed, 74 insertions(+), 11 deletions(-) diff --git a/components/livestatus/component.cpp b/components/livestatus/component.cpp index bc10ca534..4ac0c4723 100644 --- a/components/livestatus/component.cpp +++ b/components/livestatus/component.cpp @@ -21,6 +21,7 @@ #include "base/dynamictype.h" #include "base/logger_fwd.h" #include "base/tcpsocket.h" +#include "base/unixsocket.h" #include "base/networkstream.h" #include "base/application.h" #include @@ -33,7 +34,10 @@ REGISTER_TYPE(LivestatusComponent); LivestatusComponent::LivestatusComponent(const Dictionary::Ptr& serializedUpdate) : DynamicObject(serializedUpdate) { + RegisterAttribute("socket_type", Attribute_Config, &m_SocketType); RegisterAttribute("socket_path", Attribute_Config, &m_SocketPath); + RegisterAttribute("address", Attribute_Config, &m_Address); + RegisterAttribute("port", Attribute_Config, &m_Port); } /** @@ -41,18 +45,37 @@ LivestatusComponent::LivestatusComponent(const Dictionary::Ptr& serializedUpdate */ void LivestatusComponent::Start(void) { -//#ifndef _WIN32 -// UnixSocket::Ptr socket = boost::make_shared(); -// socket->Bind(GetSocketPath()); -//#else /* _WIN32 */ - TcpSocket::Ptr socket = boost::make_shared(); - socket->Bind("6558", AF_INET); -//#endif /* _WIN32 */ + if (GetSocketType() == "tcp") { + TcpSocket::Ptr socket = boost::make_shared(); + socket->Bind(GetAddress(), GetPort(), AF_INET); - m_Listener = socket; + boost::thread thread(boost::bind(&LivestatusComponent::ServerThreadProc, this, socket)); + thread.detach(); + } + else if (GetSocketType() == "unix") { +#ifndef _WIN32 + UnixSocket::Ptr socket = boost::make_shared(); + socket->Bind(GetSocketPath()); - boost::thread thread(boost::bind(&LivestatusComponent::ServerThreadProc, this, socket)); - thread.detach(); + boost::thread thread(boost::bind(&LivestatusComponent::ServerThreadProc, this, socket)); + thread.detach(); +#else + /* no unix sockets on windows */ + Log(LogCritical, "livestatus", "Unix sockets are not supported on Windows."); + return; +#endif + } + + m_ClientsConnected = 0; +} + +String LivestatusComponent::GetSocketType(void) const +{ + Value socketType = m_SocketType; + if (socketType.IsEmpty()) + return "unix"; + else + return socketType; } String LivestatusComponent::GetSocketPath(void) const @@ -64,6 +87,29 @@ String LivestatusComponent::GetSocketPath(void) const return socketPath; } +String LivestatusComponent::GetAddress(void) const +{ + Value node = m_Address; + if (node.IsEmpty()) + return "127.0.0.1"; + else + return node; +} + +String LivestatusComponent::GetPort(void) const +{ + Value service = m_Port; + if (service.IsEmpty()) + return "6558"; + else + return service; +} + +int LivestatusComponent::GetClientsConnected(void) const +{ + return m_ClientsConnected; +} + void LivestatusComponent::ServerThreadProc(const Socket::Ptr& server) { server->Listen(); @@ -80,6 +126,8 @@ void LivestatusComponent::ServerThreadProc(const Socket::Ptr& server) void LivestatusComponent::ClientThreadProc(const Socket::Ptr& client) { + m_ClientsConnected++; + Stream::Ptr stream = boost::make_shared(client); for (;;) { @@ -99,4 +147,6 @@ void LivestatusComponent::ClientThreadProc(const Socket::Ptr& client) if (!query->Execute(stream)) break; } + + m_ClientsConnected--; } diff --git a/components/livestatus/component.h b/components/livestatus/component.h index 5b7c3326d..3903d7575 100644 --- a/components/livestatus/component.h +++ b/components/livestatus/component.h @@ -40,12 +40,20 @@ public: virtual void Start(void); + String GetSocketType(void) const; String GetSocketPath(void) const; + String GetAddress(void) const; + String GetPort(void) const; + + int GetClientsConnected(void) const; private: + Attribute m_SocketType; Attribute m_SocketPath; + Attribute m_Address; + Attribute m_Port; - Socket::Ptr m_Listener; + int m_ClientsConnected; void ServerThreadProc(const Socket::Ptr& server); void ClientThreadProc(const Socket::Ptr& client); diff --git a/components/livestatus/livestatus-type.conf b/components/livestatus/livestatus-type.conf index 966237ca9..4403bcb24 100644 --- a/components/livestatus/livestatus-type.conf +++ b/components/livestatus/livestatus-type.conf @@ -18,4 +18,9 @@ ******************************************************************************/ type LivestatusComponent { + %attribute string "socket_type", + + %attribute string "socket_path", + %attribute string "address", + %attribute string "port", }