icinga2/components/livestatus/component.cpp

79 lines
3.0 KiB
C++
Raw Normal View History

2013-03-10 03:09:01 +01:00
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* 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. *
******************************************************************************/
#include "i2-livestatus.h"
using namespace icinga;
using namespace livestatus;
2013-03-10 03:09:01 +01:00
REGISTER_TYPE(LivestatusComponent);
LivestatusComponent::LivestatusComponent(const Dictionary::Ptr& serializedUpdate)
: DynamicObject(serializedUpdate)
{
RegisterAttribute("socket_path", Attribute_Config, &m_SocketPath);
}
2013-03-10 03:09:01 +01:00
/**
* Starts the component.
*/
void LivestatusComponent::Start(void)
{
2013-03-11 13:45:08 +01:00
//#ifndef _WIN32
// UnixSocket::Ptr socket = boost::make_shared<UnixSocket>();
// socket->Bind(GetSocketPath());
//#else /* _WIN32 */
2013-03-10 05:10:51 +01:00
TcpSocket::Ptr socket = boost::make_shared<TcpSocket>();
2013-03-11 13:45:08 +01:00
socket->Bind("6558", AF_INET);
//#endif /* _WIN32 */
2013-03-10 05:10:51 +01:00
socket->OnNewClient.connect(boost::bind(&LivestatusComponent::NewClientHandler, this, _2));
2013-03-10 03:09:01 +01:00
socket->Listen();
socket->Start();
m_Listener = socket;
}
String LivestatusComponent::GetSocketPath(void) const
{
Value socketPath = m_SocketPath;
2013-03-10 03:09:01 +01:00
if (socketPath.IsEmpty())
return Application::GetLocalStateDir() + "/run/icinga2/livestatus";
else
return socketPath;
}
void LivestatusComponent::NewClientHandler(const Socket::Ptr& client)
{
Logger::Write(LogInformation, "livestatus", "Client connected");
LivestatusConnection::Ptr lconnection = boost::make_shared<LivestatusConnection>(client);
lconnection->OnClosed.connect(boost::bind(&LivestatusComponent::ClientClosedHandler, this, _1));
m_Connections.insert(lconnection);
client->Start();
}
void LivestatusComponent::ClientClosedHandler(const Connection::Ptr& connection)
{
LivestatusConnection::Ptr lconnection = static_pointer_cast<LivestatusConnection>(connection);
Logger::Write(LogInformation, "livestatus", "Client disconnected");
m_Connections.erase(lconnection);
}