1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "livestatus/livestatuslistener.hpp"
4 #include "livestatus/livestatuslistener-ti.cpp"
5 #include "base/utility.hpp"
6 #include "base/perfdatavalue.hpp"
7 #include "base/objectlock.hpp"
8 #include "base/configtype.hpp"
9 #include "base/logger.hpp"
10 #include "base/exception.hpp"
11 #include "base/tcpsocket.hpp"
12 #include "base/unixsocket.hpp"
13 #include "base/networkstream.hpp"
14 #include "base/application.hpp"
15 #include "base/function.hpp"
16 #include "base/statsfunction.hpp"
17 #include "base/convert.hpp"
19 using namespace icinga;
21 REGISTER_TYPE(LivestatusListener);
23 static int l_ClientsConnected = 0;
24 static int l_Connections = 0;
25 static boost::mutex l_ComponentMutex;
27 REGISTER_STATSFUNCTION(LivestatusListener, &LivestatusListener::StatsFunc);
29 void LivestatusListener::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata)
33 for (const LivestatusListener::Ptr& livestatuslistener : ConfigType::GetObjectsByType<LivestatusListener>()) {
34 nodes.emplace_back(livestatuslistener->GetName(), new Dictionary({
35 { "connections", l_Connections }
38 perfdata->Add(new PerfdataValue("livestatuslistener_" + livestatuslistener->GetName() + "_connections", l_Connections));
41 status->Set("livestatuslistener", new Dictionary(std::move(nodes)));
45 * Starts the component.
47 void LivestatusListener::Start(bool runtimeCreated)
49 ObjectImpl<LivestatusListener>::Start(runtimeCreated);
51 Log(LogInformation, "LivestatusListener")
52 << "'" << GetName() << "' started.";
54 if (GetSocketType() == "tcp") {
55 TcpSocket::Ptr socket = new TcpSocket();
58 socket->Bind(GetBindHost(), GetBindPort(), AF_UNSPEC);
59 } catch (std::exception&) {
60 Log(LogCritical, "LivestatusListener")
61 << "Cannot bind TCP socket on host '" << GetBindHost() << "' port '" << GetBindPort() << "'.";
67 m_Thread = std::thread(std::bind(&LivestatusListener::ServerThreadProc, this));
69 Log(LogInformation, "LivestatusListener")
70 << "Created TCP socket listening on host '" << GetBindHost() << "' port '" << GetBindPort() << "'.";
72 else if (GetSocketType() == "unix") {
74 UnixSocket::Ptr socket = new UnixSocket();
77 socket->Bind(GetSocketPath());
78 } catch (std::exception&) {
79 Log(LogCritical, "LivestatusListener")
80 << "Cannot bind UNIX socket to '" << GetSocketPath() << "'.";
84 /* group must be able to write */
85 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
87 if (chmod(GetSocketPath().CStr(), mode) < 0) {
88 Log(LogCritical, "LivestatusListener")
89 << "chmod() on unix socket '" << GetSocketPath() << "' failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
95 m_Thread = std::thread(std::bind(&LivestatusListener::ServerThreadProc, this));
97 Log(LogInformation, "LivestatusListener")
98 << "Created UNIX socket in '" << GetSocketPath() << "'.";
100 /* no UNIX sockets on windows */
101 Log(LogCritical, "LivestatusListener", "Unix sockets are not supported on Windows.");
107 void LivestatusListener::Stop(bool runtimeRemoved)
109 ObjectImpl<LivestatusListener>::Stop(runtimeRemoved);
111 Log(LogInformation, "LivestatusListener")
112 << "'" << GetName() << "' stopped.";
116 if (m_Thread.joinable())
120 int LivestatusListener::GetClientsConnected()
122 boost::mutex::scoped_lock lock(l_ComponentMutex);
124 return l_ClientsConnected;
127 int LivestatusListener::GetConnections()
129 boost::mutex::scoped_lock lock(l_ComponentMutex);
131 return l_Connections;
134 void LivestatusListener::ServerThreadProc()
136 m_Listener->Listen();
140 timeval tv = { 0, 500000 };
142 if (m_Listener->Poll(true, false, &tv)) {
143 Socket::Ptr client = m_Listener->Accept();
144 Log(LogNotice, "LivestatusListener", "Client connected");
145 Utility::QueueAsyncCallback(std::bind(&LivestatusListener::ClientHandler, this, client), LowLatencyScheduler);
151 } catch (std::exception&) {
152 Log(LogCritical, "LivestatusListener", "Cannot accept new connection.");
158 void LivestatusListener::ClientHandler(const Socket::Ptr& client)
161 boost::mutex::scoped_lock lock(l_ComponentMutex);
162 l_ClientsConnected++;
166 Stream::Ptr stream = new NetworkStream(client);
168 StreamReadContext context;
173 std::vector<String> lines;
176 StreamReadStatus srs = stream->ReadLine(&line, context);
178 if (srs == StatusEof)
181 if (srs != StatusNewItem)
184 if (line.GetLength() > 0)
185 lines.push_back(line);
193 LivestatusQuery::Ptr query = new LivestatusQuery(lines, GetCompatLogPath());
194 if (!query->Execute(stream))
199 boost::mutex::scoped_lock lock(l_ComponentMutex);
200 l_ClientsConnected--;
205 void LivestatusListener::ValidateSocketType(const Lazy<String>& lvalue, const ValidationUtils& utils)
207 ObjectImpl<LivestatusListener>::ValidateSocketType(lvalue, utils);
209 if (lvalue() != "unix" && lvalue() != "tcp")
210 BOOST_THROW_EXCEPTION(ValidationError(this, { "socket_type" }, "Socket type '" + lvalue() + "' is invalid."));