Skip to content

Commit 1b21e4b

Browse files
committed
Publish cluster stats to parents every 10s while connected (event::ClusterStats)
1 parent 33366fc commit 1b21e4b

File tree

3 files changed

+248
-0
lines changed

3 files changed

+248
-0
lines changed

lib/remote/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ set(remote_SOURCES
5555
modifyobjecthandler.cpp modifyobjecthandler.hpp
5656
objectqueryhandler.cpp objectqueryhandler.hpp
5757
pkiutility.cpp pkiutility.hpp
58+
statsreporter.cpp statsreporter.hpp
5859
statushandler.cpp statushandler.hpp
5960
templatequeryhandler.cpp templatequeryhandler.hpp
6061
typequeryhandler.cpp typequeryhandler.hpp

lib/remote/statsreporter.cpp

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/******************************************************************************
2+
* Icinga 2 *
3+
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
4+
* *
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. *
9+
* *
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. *
14+
* *
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+
******************************************************************************/
19+
20+
#include "base/application.hpp"
21+
#include "base/array.hpp"
22+
#include "base/configtype.hpp"
23+
#include "base/dictionary.hpp"
24+
#include "base/function.hpp"
25+
#include "base/objectlock.hpp"
26+
#include "base/scriptglobal.hpp"
27+
#include "base/utility.hpp"
28+
#include "base/value.hpp"
29+
#include "remote/apifunction.hpp"
30+
#include "remote/endpoint.hpp"
31+
#include "remote/messageorigin.hpp"
32+
#include "remote/statsreporter.hpp"
33+
#include "remote/zone.hpp"
34+
#include <boost/bind.hpp>
35+
#include <boost/thread/mutex.hpp>
36+
#include <set>
37+
38+
using namespace icinga;
39+
40+
REGISTER_APIFUNCTION(ClusterStats, event, &StatsReporter::ClusterStatsAPIHandler);
41+
42+
StatsReporter StatsReporter::m_Instance;
43+
44+
StatsReporter::StatsReporter()
45+
{
46+
Endpoint::OnConnected.connect(std::bind(&StatsReporter::OnConnected, this, _1));
47+
}
48+
49+
void StatsReporter::OnConnected(const Endpoint::Ptr& endpoint)
50+
{
51+
if (!m_HasBeenInitialized.test_and_set()) {
52+
timer = new Timer;
53+
timer->OnTimerExpired.connect(std::bind(&StatsReporter::ReportStats, this));
54+
timer->SetInterval(10);
55+
timer->Start();
56+
timer->Reschedule(1);
57+
}
58+
}
59+
60+
void StatsReporter::ReportStats()
61+
{
62+
Dictionary::Ptr message;
63+
64+
for (auto& zone : Zone::GetLocalZone()->GetAllParents()) {
65+
for (auto& endpoint : zone->GetEndpoints()) {
66+
for (auto& client : endpoint->GetClients()) {
67+
if (!message) {
68+
message = new Dictionary({
69+
{ "jsonrpc", "2.0" },
70+
{ "method", "event::ClusterStats" },
71+
{ "params", new Dictionary({
72+
{ "stats", GenerateStats() }
73+
}) }
74+
});
75+
}
76+
77+
client->SendMessage(message);
78+
79+
break;
80+
}
81+
}
82+
}
83+
}
84+
85+
Dictionary::Ptr StatsReporter::GenerateStats()
86+
{
87+
auto allStats (new Dictionary);
88+
89+
{
90+
boost::mutex::scoped_lock lock (m_Mutex);
91+
92+
for (auto& endpointStats : m_SecondaryStats) {
93+
allStats->Set(endpointStats.first, endpointStats.second);
94+
}
95+
}
96+
97+
auto localZone (Zone::GetLocalZone());
98+
auto parentZone (localZone->GetParent());
99+
auto unorderedZones (ConfigType::GetObjectsByType<Zone>());
100+
std::set<Zone::Ptr> zones (unorderedZones.begin(), unorderedZones.end());
101+
std::set<Endpoint::Ptr> endpoints;
102+
auto ourStatus (new Dictionary);
103+
auto now (Utility::GetTime());
104+
105+
unorderedZones.clear();
106+
107+
for (auto zone (zones.begin()); zone != zones.end();) {
108+
if ((*zone)->GetParent() == localZone) {
109+
++zone;
110+
} else {
111+
zones.erase(zone++);
112+
}
113+
}
114+
115+
zones.emplace(localZone);
116+
117+
if (parentZone)
118+
zones.emplace(parentZone);
119+
120+
for (auto& zone : zones) {
121+
auto zoneEndpoints (zone->GetEndpoints());
122+
endpoints.insert(zoneEndpoints.begin(), zoneEndpoints.end());
123+
}
124+
125+
endpoints.erase(Endpoint::GetLocalEndpoint());
126+
127+
for (auto& endpoint : endpoints) {
128+
ourStatus->Set(endpoint->GetName(), new Dictionary({
129+
{"connected", endpoint->GetConnected()},
130+
{"last_message_received", endpoint->GetLastMessageReceived()}
131+
}));
132+
}
133+
134+
allStats->Set(Endpoint::GetLocalEndpoint()->GetName(), new Dictionary({
135+
{"mtime", now},
136+
{"version", Application::GetAppVersion()},
137+
{"uptime", now - Application::GetStartTime()},
138+
{"endpoints", ourStatus}
139+
}));
140+
141+
return allStats;
142+
}
143+
144+
Value StatsReporter::ClusterStatsAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
145+
{
146+
auto endpoint (origin->FromClient->GetEndpoint());
147+
148+
if (endpoint && endpoint->GetZone()->IsChildOf(Zone::GetLocalZone())) {
149+
auto rawStats (params->Get("stats"));
150+
151+
if (rawStats.IsObject()) {
152+
Dictionary::Ptr allStats (rawStats);
153+
154+
if (allStats) {
155+
// For security reasons
156+
std::set<String> neighborhood;
157+
158+
for (auto& endpoint : Zone::GetLocalZone()->GetEndpoints()) {
159+
neighborhood.emplace(endpoint->GetName());
160+
}
161+
162+
ObjectLock lock (allStats);
163+
164+
for (auto& endpointStats : allStats) {
165+
if (endpointStats.second.IsObject()) {
166+
Dictionary::Ptr stats (endpointStats.second);
167+
168+
if (stats && neighborhood.find(endpointStats.first) == neighborhood.end()) {
169+
m_Instance.ClusterStatsHandler(endpointStats.first, endpointStats.second);
170+
}
171+
}
172+
}
173+
}
174+
}
175+
}
176+
177+
return Empty;
178+
}
179+
180+
void StatsReporter::ClusterStatsHandler(const String& endpoint, const Dictionary::Ptr& stats)
181+
{
182+
boost::mutex::scoped_lock lock (m_Mutex);
183+
m_SecondaryStats[endpoint] = stats;
184+
}

lib/remote/statsreporter.hpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/******************************************************************************
2+
* Icinga 2 *
3+
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
4+
* *
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. *
9+
* *
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. *
14+
* *
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+
******************************************************************************/
19+
20+
#ifndef STATSREPORTER_H
21+
#define STATSREPORTER_H
22+
23+
#include "base/dictionary.hpp"
24+
#include "base/string.hpp"
25+
#include "base/timer.hpp"
26+
#include "base/value.hpp"
27+
#include "remote/endpoint.hpp"
28+
#include "remote/messageorigin.hpp"
29+
#include <atomic>
30+
#include <boost/thread/mutex.hpp>
31+
#include <map>
32+
33+
namespace icinga
34+
{
35+
36+
/**
37+
* @ingroup remote
38+
*/
39+
class StatsReporter
40+
{
41+
public:
42+
static Value ClusterStatsAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
43+
44+
private:
45+
StatsReporter();
46+
47+
void OnConnected(const Endpoint::Ptr& endpoint);
48+
void ReportStats();
49+
Dictionary::Ptr GenerateStats();
50+
void ClusterStatsHandler(const String& endpoint, const Dictionary::Ptr& stats);
51+
52+
static StatsReporter m_Instance;
53+
54+
std::atomic_flag m_HasBeenInitialized = ATOMIC_FLAG_INIT;
55+
Timer::Ptr timer;
56+
57+
boost::mutex m_Mutex;
58+
std::map<String, Dictionary::Ptr> m_SecondaryStats;
59+
};
60+
61+
}
62+
63+
#endif /* STATSREPORTER_H */

0 commit comments

Comments
 (0)