1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "livestatus/livestatuslistener.hpp"
21 #include "livestatus/livestatuslistener-ti.cpp"
22 #include "base/utility.hpp"
23 #include "base/perfdatavalue.hpp"
24 #include "base/objectlock.hpp"
25 #include "base/configtype.hpp"
26 #include "base/logger.hpp"
27 #include "base/exception.hpp"
28 #include "base/tcpsocket.hpp"
29 #include "base/unixsocket.hpp"
30 #include "base/networkstream.hpp"
31 #include "base/application.hpp"
32 #include "base/function.hpp"
33 #include "base/statsfunction.hpp"
34 #include "base/convert.hpp"
36 using namespace icinga;
38 REGISTER_TYPE(LivestatusListener);
40 static int l_ClientsConnected = 0;
41 static int l_Connections = 0;
42 static boost::mutex l_ComponentMutex;
44 REGISTER_STATSFUNCTION(LivestatusListener, &LivestatusListener::StatsFunc);
46 void LivestatusListener::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata)
50 for (const LivestatusListener::Ptr& livestatuslistener : ConfigType::GetObjectsByType<LivestatusListener>()) {
51 nodes.emplace_back(livestatuslistener->GetName(), new Dictionary({
52 { "connections", l_Connections }
55 perfdata->Add(new PerfdataValue("livestatuslistener_" + livestatuslistener->GetName() + "_connections", l_Connections));
58 status->Set("livestatuslistener", new Dictionary(std::move(nodes)));
62 * Starts the component.
64 void LivestatusListener::Start(bool runtimeCreated)
66 ObjectImpl<LivestatusListener>::Start(runtimeCreated);
68 Log(LogInformation, "LivestatusListener")
69 << "'" << GetName() << "' started.";
71 if (GetSocketType() == "tcp") {
72 TcpSocket::Ptr socket = new TcpSocket();
75 socket->Bind(GetBindHost(), GetBindPort(), AF_UNSPEC);
76 } catch (std::exception&) {
77 Log(LogCritical, "LivestatusListener")
78 << "Cannot bind TCP socket on host '" << GetBindHost() << "' port '" << GetBindPort() << "'.";
84 m_Thread = std::thread(std::bind(&LivestatusListener::ServerThreadProc, this));
86 Log(LogInformation, "LivestatusListener")
87 << "Created TCP socket listening on host '" << GetBindHost() << "' port '" << GetBindPort() << "'.";
89 else if (GetSocketType() == "unix") {
91 UnixSocket::Ptr socket = new UnixSocket();
94 socket->Bind(GetSocketPath());
95 } catch (std::exception&) {
96 Log(LogCritical, "LivestatusListener")
97 << "Cannot bind UNIX socket to '" << GetSocketPath() << "'.";
101 /* group must be able to write */
102 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
104 if (chmod(GetSocketPath().CStr(), mode) < 0) {
105 Log(LogCritical, "LivestatusListener")
106 << "chmod() on unix socket '" << GetSocketPath() << "' failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
112 m_Thread = std::thread(std::bind(&LivestatusListener::ServerThreadProc, this));
114 Log(LogInformation, "LivestatusListener")
115 << "Created UNIX socket in '" << GetSocketPath() << "'.";
117 /* no UNIX sockets on windows */
118 Log(LogCritical, "LivestatusListener", "Unix sockets are not supported on Windows.");
124 void LivestatusListener::Stop(bool runtimeRemoved)
126 ObjectImpl<LivestatusListener>::Stop(runtimeRemoved);
128 Log(LogInformation, "LivestatusListener")
129 << "'" << GetName() << "' stopped.";
133 if (m_Thread.joinable())
137 int LivestatusListener::GetClientsConnected()
139 boost::mutex::scoped_lock lock(l_ComponentMutex);
141 return l_ClientsConnected;
144 int LivestatusListener::GetConnections()
146 boost::mutex::scoped_lock lock(l_ComponentMutex);
148 return l_Connections;
151 void LivestatusListener::ServerThreadProc()
153 m_Listener->Listen();
157 timeval tv = { 0, 500000 };
159 if (m_Listener->Poll(true, false, &tv)) {
160 Socket::Ptr client = m_Listener->Accept();
161 Log(LogNotice, "LivestatusListener", "Client connected");
162 Utility::QueueAsyncCallback(std::bind(&LivestatusListener::ClientHandler, this, client), LowLatencyScheduler);
168 } catch (std::exception&) {
169 Log(LogCritical, "LivestatusListener", "Cannot accept new connection.");
175 void LivestatusListener::ClientHandler(const Socket::Ptr& client)
178 boost::mutex::scoped_lock lock(l_ComponentMutex);
179 l_ClientsConnected++;
183 Stream::Ptr stream = new NetworkStream(client);
185 StreamReadContext context;
190 std::vector<String> lines;
193 StreamReadStatus srs = stream->ReadLine(&line, context);
195 if (srs == StatusEof)
198 if (srs != StatusNewItem)
201 if (line.GetLength() > 0)
202 lines.push_back(line);
210 LivestatusQuery::Ptr query = new LivestatusQuery(lines, GetCompatLogPath());
211 if (!query->Execute(stream))
216 boost::mutex::scoped_lock lock(l_ComponentMutex);
217 l_ClientsConnected--;
222 void LivestatusListener::ValidateSocketType(const Lazy<String>& lvalue, const ValidationUtils& utils)
224 ObjectImpl<LivestatusListener>::ValidateSocketType(lvalue, utils);
226 if (lvalue() != "unix" && lvalue() != "tcp")
227 BOOST_THROW_EXCEPTION(ValidationError(this, { "socket_type" }, "Socket type '" + lvalue() + "' is invalid."));