icinga2/components/livestatus/component.cpp

104 lines
3.3 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. *
******************************************************************************/
2013-03-17 20:19:29 +01:00
#include "livestatus/component.h"
2013-03-16 21:18:53 +01:00
#include "base/dynamictype.h"
#include "base/logger_fwd.h"
2013-03-17 20:19:29 +01:00
#include "base/tcpsocket.h"
2013-04-04 16:08:02 +02:00
#include "base/networkstream.h"
2013-03-17 20:19:29 +01:00
#include "base/application.h"
2013-03-16 21:18:53 +01:00
#include <boost/smart_ptr/make_shared.hpp>
2013-03-10 03:09:01 +01:00
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
2013-03-10 03:09:01 +01:00
m_Listener = socket;
2013-04-04 16:08:02 +02:00
boost::thread thread(boost::bind(&LivestatusComponent::ServerThreadProc, this, socket));
thread.detach();
2013-03-10 03:09:01 +01:00
}
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;
}
2013-04-04 16:08:02 +02:00
void LivestatusComponent::ServerThreadProc(const Socket::Ptr& server)
2013-03-10 03:09:01 +01:00
{
2013-04-04 16:08:02 +02:00
server->Listen();
for (;;) {
Socket::Ptr client = server->Accept();
2013-03-10 03:09:01 +01:00
2013-04-04 16:08:02 +02:00
Log(LogInformation, "livestatus", "Client connected");
2013-03-10 03:09:01 +01:00
2013-04-04 16:08:02 +02:00
boost::thread thread(boost::bind(&LivestatusComponent::ClientThreadProc, this, client));
thread.detach();
}
2013-03-10 03:09:01 +01:00
}
2013-04-04 16:08:02 +02:00
void LivestatusComponent::ClientThreadProc(const Socket::Ptr& client)
2013-03-10 03:09:01 +01:00
{
2013-04-04 16:08:02 +02:00
Stream::Ptr stream = boost::make_shared<NetworkStream>(client);
for (;;) {
String line;
bool read_line = false;
std::vector<String> lines;
while (stream->ReadLine(&line)) {
read_line = true;
if (line.GetLength() > 0)
lines.push_back(line);
else
break;
}
2013-03-10 03:09:01 +01:00
2013-04-04 16:08:02 +02:00
Query::Ptr query = boost::make_shared<Query>(lines);
query->Execute(stream);
}
2013-03-10 03:09:01 +01:00
}