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 \
commentstable.cpp \
commentstable.h \
component.cpp \
component.h \
contactgroupstable.cpp \
contactgroupstable.h \
contactstable.cpp \
@ -46,6 +44,8 @@ liblivestatus_la_SOURCES = \
invavgaggregator.h \
invsumaggregator.cpp \
invsumaggregator.h \
listener.cpp \
listener.h \
livestatus-type.conf \
logtable.cpp \
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()));
BOOST_FOREACH(const UserGroup::Ptr& ug, notification->GetGroups()) {
BOOST_FOREACH(const UserGroup::Ptr& ug, notification->GetUserGroups()) {
std::set<User::Ptr> members = ug->GetMembers();
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()) {
ObjectLock olock(notification);
BOOST_FOREACH(const UserGroup::Ptr& ug, notification->GetGroups()) {
BOOST_FOREACH(const UserGroup::Ptr& ug, notification->GetUserGroups()) {
contactgroups->Add(ug->GetName());
}
}

View File

@ -17,7 +17,7 @@
* 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/dynamictype.h"
#include "base/logger_fwd.h"
@ -30,7 +30,7 @@
using namespace icinga;
using namespace livestatus;
REGISTER_TYPE(LivestatusComponent);
REGISTER_TYPE(LivestatusListener);
static int l_ClientsConnected = 0;
static int l_Connections = 0;
@ -39,15 +39,15 @@ static boost::mutex l_ComponentMutex;
/**
* Starts the component.
*/
void LivestatusComponent::Start(void)
void LivestatusListener::Start(void)
{
DynamicObject::Start();
if (GetSocketType() == "tcp") {
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();
}
else if (GetSocketType() == "unix") {
@ -55,7 +55,7 @@ void LivestatusComponent::Start(void)
UnixSocket::Ptr socket = boost::make_shared<UnixSocket>();
socket->Bind(GetSocketPath());
boost::thread thread(boost::bind(&LivestatusComponent::ServerThreadProc, this, socket));
boost::thread thread(boost::bind(&LivestatusListener::ServerThreadProc, this, socket));
thread.detach();
#else
/* 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;
if (socketType.IsEmpty())
@ -74,7 +74,7 @@ String LivestatusComponent::GetSocketType(void) const
return socketType;
}
String LivestatusComponent::GetSocketPath(void) const
String LivestatusListener::GetSocketPath(void) const
{
Value socketPath = m_SocketPath;
if (socketPath.IsEmpty())
@ -83,39 +83,37 @@ String LivestatusComponent::GetSocketPath(void) const
return socketPath;
}
String LivestatusComponent::GetHost(void) const
String LivestatusListener::GetBindHost(void) const
{
Value node = m_Host;
if (node.IsEmpty())
if (m_BindHost.IsEmpty())
return "127.0.0.1";
else
return node;
return m_BindHost;
}
String LivestatusComponent::GetPort(void) const
String LivestatusListener::GetBindPort(void) const
{
Value service = m_Port;
if (service.IsEmpty())
if (m_BindPort.IsEmpty())
return "6558";
else
return service;
return m_BindPort;
}
int LivestatusComponent::GetClientsConnected(void)
int LivestatusListener::GetClientsConnected(void)
{
boost::mutex::scoped_lock lock(l_ComponentMutex);
return l_ClientsConnected;
}
int LivestatusComponent::GetConnections(void)
int LivestatusListener::GetConnections(void)
{
boost::mutex::scoped_lock lock(l_ComponentMutex);
return l_Connections;
}
void LivestatusComponent::ServerThreadProc(const Socket::Ptr& server)
void LivestatusListener::ServerThreadProc(const Socket::Ptr& server)
{
server->Listen();
@ -124,12 +122,12 @@ void LivestatusComponent::ServerThreadProc(const Socket::Ptr& server)
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();
}
}
void LivestatusComponent::ClientThreadProc(const Socket::Ptr& client)
void LivestatusListener::ClientThreadProc(const Socket::Ptr& client)
{
{
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);
if (attributeTypes & Attribute_Config) {
bag->Set("socket_type", m_SocketType);
bag->Set("socket_path", m_SocketPath);
bag->Set("host", m_Host);
bag->Set("port", m_Port);
bag->Set("bind_host", m_BindHost);
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);
if (attributeTypes & Attribute_Config) {
m_SocketType = bag->Get("socket_type");
m_SocketPath = bag->Get("socket_path");
m_Host = bag->Get("host");
m_Port = bag->Get("port");
m_BindHost = bag->Get("bind_host");
m_BindPort = bag->Get("bind_port");
}
}

View File

@ -17,8 +17,8 @@
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#ifndef LIVESTATUSCOMPONENT_H
#define LIVESTATUSCOMPONENT_H
#ifndef LIVESTATUSLISTENER_H
#define LIVESTATUSLISTENER_H
#include "livestatus/query.h"
#include "base/dynamicobject.h"
@ -33,13 +33,15 @@ namespace livestatus
/**
* @ingroup livestatus
*/
class LivestatusComponent : public DynamicObject
class LivestatusListener : public DynamicObject
{
public:
DECLARE_PTR_TYPEDEFS(LivestatusListener);
String GetSocketType(void) const;
String GetSocketPath(void) const;
String GetHost(void) const;
String GetPort(void) const;
String GetBindHost(void) const;
String GetBindPort(void) const;
static int GetClientsConnected(void);
static int GetConnections(void);
@ -53,8 +55,8 @@ protected:
private:
String m_SocketType;
String m_SocketPath;
String m_Host;
String m_Port;
String m_BindHost;
String m_BindPort;
void ServerThreadProc(const Socket::Ptr& server);
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. *
******************************************************************************/
type LivestatusComponent {
type LivestatusListener {
%attribute string "socket_type",
%attribute string "socket_path",
%attribute string "host",
%attribute string "port",
%attribute string "bind_host",
%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()));
BOOST_FOREACH(const UserGroup::Ptr& ug, notification->GetGroups()) {
BOOST_FOREACH(const UserGroup::Ptr& ug, notification->GetUserGroups()) {
std::set<User::Ptr> members = ug->GetMembers();
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()) {
ObjectLock olock(notification);
BOOST_FOREACH(const UserGroup::Ptr& ug, notification->GetGroups()) {
BOOST_FOREACH(const UserGroup::Ptr& ug, notification->GetUserGroups()) {
contactgroups->Add(ug->GetName());
}
}

View File

@ -18,7 +18,7 @@
******************************************************************************/
#include "livestatus/statustable.h"
#include "livestatus/component.h"
#include "livestatus/listener.h"
#include "icinga/icingaapplication.h"
#include "icinga/cib.h"
#include "icinga/host.h"
@ -138,12 +138,12 @@ Value StatusTable::RequestsRateAccessor(const Value& row)
Value StatusTable::ConnectionsAccessor(const Value& row)
{
return LivestatusComponent::GetConnections();
return LivestatusListener::GetConnections();
}
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)
@ -380,7 +380,7 @@ Value StatusTable::LivestatusVersionAccessor(const Value& row)
Value StatusTable::LivestatusActiveConnectionsAccessor(const Value& row)
{
return LivestatusComponent::GetClientsConnected();
return LivestatusListener::GetClientsConnected();
}
Value StatusTable::LivestatusQueuedConnectionsAccessor(const Value& row)