Refactor the livestatus library.

This commit is contained in:
Gunnar Beutner 2013-09-25 09:31:52 +02:00
parent a6fd88c8af
commit 21999fe51e
7 changed files with 48 additions and 48 deletions

View File

@ -26,8 +26,6 @@ liblivestatus_la_SOURCES = \
commandstable.h \ commandstable.h \
commentstable.cpp \ commentstable.cpp \
commentstable.h \ commentstable.h \
component.cpp \
component.h \
contactgroupstable.cpp \ contactgroupstable.cpp \
contactgroupstable.h \ contactgroupstable.h \
contactstable.cpp \ contactstable.cpp \
@ -46,6 +44,8 @@ liblivestatus_la_SOURCES = \
invavgaggregator.h \ invavgaggregator.h \
invsumaggregator.cpp \ invsumaggregator.cpp \
invsumaggregator.h \ invsumaggregator.h \
listener.cpp \
listener.h \
livestatus-type.conf \ livestatus-type.conf \
logtable.cpp \ logtable.cpp \
logtable.h \ logtable.h \

View File

@ -1098,7 +1098,7 @@ Value HostsTable::ContactsAccessor(const Value& row)
std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin())); std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
BOOST_FOREACH(const UserGroup::Ptr& ug, notification->GetGroups()) { BOOST_FOREACH(const UserGroup::Ptr& ug, notification->GetUserGroups()) {
std::set<User::Ptr> members = ug->GetMembers(); std::set<User::Ptr> members = ug->GetMembers();
std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin())); std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
} }
@ -1607,7 +1607,7 @@ Value HostsTable::ContactGroupsAccessor(const Value& row)
BOOST_FOREACH(const Notification::Ptr& notification, hc->GetNotifications()) { BOOST_FOREACH(const Notification::Ptr& notification, hc->GetNotifications()) {
ObjectLock olock(notification); ObjectLock olock(notification);
BOOST_FOREACH(const UserGroup::Ptr& ug, notification->GetGroups()) { BOOST_FOREACH(const UserGroup::Ptr& ug, notification->GetUserGroups()) {
contactgroups->Add(ug->GetName()); contactgroups->Add(ug->GetName());
} }
} }

View File

@ -17,7 +17,7 @@
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/ ******************************************************************************/
#include "livestatus/component.h" #include "livestatus/listener.h"
#include "base/objectlock.h" #include "base/objectlock.h"
#include "base/dynamictype.h" #include "base/dynamictype.h"
#include "base/logger_fwd.h" #include "base/logger_fwd.h"
@ -30,7 +30,7 @@
using namespace icinga; using namespace icinga;
using namespace livestatus; using namespace livestatus;
REGISTER_TYPE(LivestatusComponent); REGISTER_TYPE(LivestatusListener);
static int l_ClientsConnected = 0; static int l_ClientsConnected = 0;
static int l_Connections = 0; static int l_Connections = 0;
@ -39,15 +39,15 @@ static boost::mutex l_ComponentMutex;
/** /**
* Starts the component. * Starts the component.
*/ */
void LivestatusComponent::Start(void) void LivestatusListener::Start(void)
{ {
DynamicObject::Start(); DynamicObject::Start();
if (GetSocketType() == "tcp") { if (GetSocketType() == "tcp") {
TcpSocket::Ptr socket = boost::make_shared<TcpSocket>(); TcpSocket::Ptr socket = boost::make_shared<TcpSocket>();
socket->Bind(GetHost(), GetPort(), AF_INET); socket->Bind(GetBindHost(), GetBindPort(), AF_INET);
boost::thread thread(boost::bind(&LivestatusComponent::ServerThreadProc, this, socket)); boost::thread thread(boost::bind(&LivestatusListener::ServerThreadProc, this, socket));
thread.detach(); thread.detach();
} }
else if (GetSocketType() == "unix") { else if (GetSocketType() == "unix") {
@ -55,7 +55,7 @@ void LivestatusComponent::Start(void)
UnixSocket::Ptr socket = boost::make_shared<UnixSocket>(); UnixSocket::Ptr socket = boost::make_shared<UnixSocket>();
socket->Bind(GetSocketPath()); socket->Bind(GetSocketPath());
boost::thread thread(boost::bind(&LivestatusComponent::ServerThreadProc, this, socket)); boost::thread thread(boost::bind(&LivestatusListener::ServerThreadProc, this, socket));
thread.detach(); thread.detach();
#else #else
/* no unix sockets on windows */ /* no unix sockets on windows */
@ -65,7 +65,7 @@ void LivestatusComponent::Start(void)
} }
} }
String LivestatusComponent::GetSocketType(void) const String LivestatusListener::GetSocketType(void) const
{ {
Value socketType = m_SocketType; Value socketType = m_SocketType;
if (socketType.IsEmpty()) if (socketType.IsEmpty())
@ -74,7 +74,7 @@ String LivestatusComponent::GetSocketType(void) const
return socketType; return socketType;
} }
String LivestatusComponent::GetSocketPath(void) const String LivestatusListener::GetSocketPath(void) const
{ {
Value socketPath = m_SocketPath; Value socketPath = m_SocketPath;
if (socketPath.IsEmpty()) if (socketPath.IsEmpty())
@ -83,39 +83,37 @@ String LivestatusComponent::GetSocketPath(void) const
return socketPath; return socketPath;
} }
String LivestatusComponent::GetHost(void) const String LivestatusListener::GetBindHost(void) const
{ {
Value node = m_Host; if (m_BindHost.IsEmpty())
if (node.IsEmpty())
return "127.0.0.1"; return "127.0.0.1";
else else
return node; return m_BindHost;
} }
String LivestatusComponent::GetPort(void) const String LivestatusListener::GetBindPort(void) const
{ {
Value service = m_Port; if (m_BindPort.IsEmpty())
if (service.IsEmpty())
return "6558"; return "6558";
else else
return service; return m_BindPort;
} }
int LivestatusComponent::GetClientsConnected(void) int LivestatusListener::GetClientsConnected(void)
{ {
boost::mutex::scoped_lock lock(l_ComponentMutex); boost::mutex::scoped_lock lock(l_ComponentMutex);
return l_ClientsConnected; return l_ClientsConnected;
} }
int LivestatusComponent::GetConnections(void) int LivestatusListener::GetConnections(void)
{ {
boost::mutex::scoped_lock lock(l_ComponentMutex); boost::mutex::scoped_lock lock(l_ComponentMutex);
return l_Connections; return l_Connections;
} }
void LivestatusComponent::ServerThreadProc(const Socket::Ptr& server) void LivestatusListener::ServerThreadProc(const Socket::Ptr& server)
{ {
server->Listen(); server->Listen();
@ -124,12 +122,12 @@ void LivestatusComponent::ServerThreadProc(const Socket::Ptr& server)
Log(LogInformation, "livestatus", "Client connected"); Log(LogInformation, "livestatus", "Client connected");
boost::thread thread(boost::bind(&LivestatusComponent::ClientThreadProc, this, client)); boost::thread thread(boost::bind(&LivestatusListener::ClientThreadProc, this, client));
thread.detach(); thread.detach();
} }
} }
void LivestatusComponent::ClientThreadProc(const Socket::Ptr& client) void LivestatusListener::ClientThreadProc(const Socket::Ptr& client)
{ {
{ {
boost::mutex::scoped_lock lock(l_ComponentMutex); boost::mutex::scoped_lock lock(l_ComponentMutex);
@ -163,26 +161,26 @@ void LivestatusComponent::ClientThreadProc(const Socket::Ptr& client)
} }
} }
void LivestatusComponent::InternalSerialize(const Dictionary::Ptr& bag, int attributeTypes) const void LivestatusListener::InternalSerialize(const Dictionary::Ptr& bag, int attributeTypes) const
{ {
DynamicObject::InternalSerialize(bag, attributeTypes); DynamicObject::InternalSerialize(bag, attributeTypes);
if (attributeTypes & Attribute_Config) { if (attributeTypes & Attribute_Config) {
bag->Set("socket_type", m_SocketType); bag->Set("socket_type", m_SocketType);
bag->Set("socket_path", m_SocketPath); bag->Set("socket_path", m_SocketPath);
bag->Set("host", m_Host); bag->Set("bind_host", m_BindHost);
bag->Set("port", m_Port); bag->Set("bind_port", m_BindPort);
} }
} }
void LivestatusComponent::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes) void LivestatusListener::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes)
{ {
DynamicObject::InternalDeserialize(bag, attributeTypes); DynamicObject::InternalDeserialize(bag, attributeTypes);
if (attributeTypes & Attribute_Config) { if (attributeTypes & Attribute_Config) {
m_SocketType = bag->Get("socket_type"); m_SocketType = bag->Get("socket_type");
m_SocketPath = bag->Get("socket_path"); m_SocketPath = bag->Get("socket_path");
m_Host = bag->Get("host"); m_BindHost = bag->Get("bind_host");
m_Port = bag->Get("port"); m_BindPort = bag->Get("bind_port");
} }
} }

View File

@ -17,8 +17,8 @@
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/ ******************************************************************************/
#ifndef LIVESTATUSCOMPONENT_H #ifndef LIVESTATUSLISTENER_H
#define LIVESTATUSCOMPONENT_H #define LIVESTATUSLISTENER_H
#include "livestatus/query.h" #include "livestatus/query.h"
#include "base/dynamicobject.h" #include "base/dynamicobject.h"
@ -33,13 +33,15 @@ namespace livestatus
/** /**
* @ingroup livestatus * @ingroup livestatus
*/ */
class LivestatusComponent : public DynamicObject class LivestatusListener : public DynamicObject
{ {
public: public:
DECLARE_PTR_TYPEDEFS(LivestatusListener);
String GetSocketType(void) const; String GetSocketType(void) const;
String GetSocketPath(void) const; String GetSocketPath(void) const;
String GetHost(void) const; String GetBindHost(void) const;
String GetPort(void) const; String GetBindPort(void) const;
static int GetClientsConnected(void); static int GetClientsConnected(void);
static int GetConnections(void); static int GetConnections(void);
@ -53,8 +55,8 @@ protected:
private: private:
String m_SocketType; String m_SocketType;
String m_SocketPath; String m_SocketPath;
String m_Host; String m_BindHost;
String m_Port; String m_BindPort;
void ServerThreadProc(const Socket::Ptr& server); void ServerThreadProc(const Socket::Ptr& server);
void ClientThreadProc(const Socket::Ptr& client); void ClientThreadProc(const Socket::Ptr& client);
@ -62,4 +64,4 @@ private:
} }
#endif /* LIVESTATUSCOMPONENT_H */ #endif /* LIVESTATUSLISTENER_H */

View File

@ -17,10 +17,10 @@
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/ ******************************************************************************/
type LivestatusComponent { type LivestatusListener {
%attribute string "socket_type", %attribute string "socket_type",
%attribute string "socket_path", %attribute string "socket_path",
%attribute string "host", %attribute string "bind_host",
%attribute string "port", %attribute string "bind_port",
} }

View File

@ -762,7 +762,7 @@ Value ServicesTable::ContactsAccessor(const Value& row)
std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin())); std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
BOOST_FOREACH(const UserGroup::Ptr& ug, notification->GetGroups()) { BOOST_FOREACH(const UserGroup::Ptr& ug, notification->GetUserGroups()) {
std::set<User::Ptr> members = ug->GetMembers(); std::set<User::Ptr> members = ug->GetMembers();
std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin())); std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
} }
@ -1029,7 +1029,7 @@ Value ServicesTable::ContactGroupsAccessor(const Value& row)
BOOST_FOREACH(const Notification::Ptr& notification, static_cast<Service::Ptr>(row)->GetNotifications()) { BOOST_FOREACH(const Notification::Ptr& notification, static_cast<Service::Ptr>(row)->GetNotifications()) {
ObjectLock olock(notification); ObjectLock olock(notification);
BOOST_FOREACH(const UserGroup::Ptr& ug, notification->GetGroups()) { BOOST_FOREACH(const UserGroup::Ptr& ug, notification->GetUserGroups()) {
contactgroups->Add(ug->GetName()); contactgroups->Add(ug->GetName());
} }
} }

View File

@ -18,7 +18,7 @@
******************************************************************************/ ******************************************************************************/
#include "livestatus/statustable.h" #include "livestatus/statustable.h"
#include "livestatus/component.h" #include "livestatus/listener.h"
#include "icinga/icingaapplication.h" #include "icinga/icingaapplication.h"
#include "icinga/cib.h" #include "icinga/cib.h"
#include "icinga/host.h" #include "icinga/host.h"
@ -138,12 +138,12 @@ Value StatusTable::RequestsRateAccessor(const Value& row)
Value StatusTable::ConnectionsAccessor(const Value& row) Value StatusTable::ConnectionsAccessor(const Value& row)
{ {
return LivestatusComponent::GetConnections(); return LivestatusListener::GetConnections();
} }
Value StatusTable::ConnectionsRateAccessor(const Value& row) Value StatusTable::ConnectionsRateAccessor(const Value& row)
{ {
return (LivestatusComponent::GetConnections() / (Utility::GetTime() - IcingaApplication::GetInstance()->GetStartTime())); return (LivestatusListener::GetConnections() / (Utility::GetTime() - IcingaApplication::GetInstance()->GetStartTime()));
} }
Value StatusTable::ServiceChecksAccessor(const Value& row) Value StatusTable::ServiceChecksAccessor(const Value& row)
@ -380,7 +380,7 @@ Value StatusTable::LivestatusVersionAccessor(const Value& row)
Value StatusTable::LivestatusActiveConnectionsAccessor(const Value& row) Value StatusTable::LivestatusActiveConnectionsAccessor(const Value& row)
{ {
return LivestatusComponent::GetClientsConnected(); return LivestatusListener::GetClientsConnected();
} }
Value StatusTable::LivestatusQueuedConnectionsAccessor(const Value& row) Value StatusTable::LivestatusQueuedConnectionsAccessor(const Value& row)