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.tcpp"
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)
48 Dictionary::Ptr nodes = new Dictionary();
50 for (const LivestatusListener::Ptr& livestatuslistener : ConfigType::GetObjectsByType<LivestatusListener>()) {
51 Dictionary::Ptr stats = new Dictionary();
52 stats->Set("connections", l_Connections);
54 nodes->Set(livestatuslistener->GetName(), stats);
56 perfdata->Add(new PerfdataValue("livestatuslistener_" + livestatuslistener->GetName() + "_connections", l_Connections));
59 status->Set("livestatuslistener", nodes);
63 * Starts the component.
65 void LivestatusListener::Start(bool runtimeCreated)
67 ObjectImpl<LivestatusListener>::Start(runtimeCreated);
69 Log(LogInformation, "LivestatusListener")
70 << "'" << GetName() << "' started.";
72 if (GetSocketType() == "tcp") {
73 TcpSocket::Ptr socket = new TcpSocket();
76 socket->Bind(GetBindHost(), GetBindPort(), AF_UNSPEC);
77 } catch (std::exception&) {
78 Log(LogCritical, "LivestatusListener")
79 << "Cannot bind TCP socket on host '" << GetBindHost() << "' port '" << GetBindPort() << "'.";
85 m_Thread = std::thread(std::bind(&LivestatusListener::ServerThreadProc, this));
87 Log(LogInformation, "LivestatusListener")
88 << "Created TCP socket listening on host '" << GetBindHost() << "' port '" << GetBindPort() << "'.";
90 else if (GetSocketType() == "unix") {
92 UnixSocket::Ptr socket = new UnixSocket();
95 socket->Bind(GetSocketPath());
96 } catch (std::exception&) {
97 Log(LogCritical, "LivestatusListener")
98 << "Cannot bind UNIX socket to '" << GetSocketPath() << "'.";
102 /* group must be able to write */
103 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
105 if (chmod(GetSocketPath().CStr(), mode) < 0) {
106 Log(LogCritical, "LivestatusListener")
107 << "chmod() on unix socket '" << GetSocketPath() << "' failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
113 m_Thread = std::thread(std::bind(&LivestatusListener::ServerThreadProc, this));
115 Log(LogInformation, "LivestatusListener")
116 << "Created UNIX socket in '" << GetSocketPath() << "'.";
118 /* no UNIX sockets on windows */
119 Log(LogCritical, "LivestatusListener", "Unix sockets are not supported on Windows.");
125 void LivestatusListener::Stop(bool runtimeRemoved)
127 ObjectImpl<LivestatusListener>::Stop(runtimeRemoved);
129 Log(LogInformation, "LivestatusListener")
130 << "'" << GetName() << "' stopped.";
134 if (m_Thread.joinable())
138 int LivestatusListener::GetClientsConnected()
140 boost::mutex::scoped_lock lock(l_ComponentMutex);
142 return l_ClientsConnected;
145 int LivestatusListener::GetConnections()
147 boost::mutex::scoped_lock lock(l_ComponentMutex);
149 return l_Connections;
152 void LivestatusListener::ServerThreadProc()
154 m_Listener->Listen();
158 timeval tv = { 0, 500000 };
160 if (m_Listener->Poll(true, false, &tv)) {
161 Socket::Ptr client = m_Listener->Accept();
162 Log(LogNotice, "LivestatusListener", "Client connected");
163 Utility::QueueAsyncCallback(std::bind(&LivestatusListener::ClientHandler, this, client), LowLatencyScheduler);
169 } catch (std::exception&) {
170 Log(LogCritical, "LivestatusListener", "Cannot accept new connection.");
176 void LivestatusListener::ClientHandler(const Socket::Ptr& client)
179 boost::mutex::scoped_lock lock(l_ComponentMutex);
180 l_ClientsConnected++;
184 Stream::Ptr stream = new NetworkStream(client);
186 StreamReadContext context;
191 std::vector<String> lines;
194 StreamReadStatus srs = stream->ReadLine(&line, context);
196 if (srs == StatusEof)
199 if (srs != StatusNewItem)
202 if (line.GetLength() > 0)
203 lines.push_back(line);
211 LivestatusQuery::Ptr query = new LivestatusQuery(lines, GetCompatLogPath());
212 if (!query->Execute(stream))
217 boost::mutex::scoped_lock lock(l_ComponentMutex);
218 l_ClientsConnected--;
223 void LivestatusListener::ValidateSocketType(const Lazy<String>& lvalue, const ValidationUtils& utils)
225 ObjectImpl<LivestatusListener>::ValidateSocketType(lvalue, utils);
227 if (lvalue() != "unix" && lvalue() != "tcp")
228 BOOST_THROW_EXCEPTION(ValidationError(this, { "socket_type" }, "Socket type '" + lvalue() + "' is invalid."));