Skip to content

Commit 3d64536

Browse files
committed
Provide collected cluster stats via /v1/status
1 parent bcdcd7d commit 3d64536

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

lib/remote/statsreporter.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "base/function.hpp"
2323
#include "base/objectlock.hpp"
2424
#include "base/scriptglobal.hpp"
25+
#include "base/statsfunction.hpp"
2526
#include "base/value.hpp"
2627
#include "remote/apifunction.hpp"
2728
#include "remote/endpoint.hpp"
@@ -36,6 +37,8 @@ using namespace icinga;
3637

3738
REGISTER_APIFUNCTION(ClusterStats, event, &StatsReporter::ClusterStatsAPIHandler);
3839

40+
REGISTER_STATSFUNCTION(StatsReporter, &StatsReporter::StatsFunc);
41+
3942
StatsReporter StatsReporter::m_Instance;
4043

4144
StatsReporter::StatsReporter()
@@ -62,20 +65,7 @@ void StatsReporter::ReportStats()
6265
for (auto& endpoint : zone->GetEndpoints()) {
6366
for (auto& client : endpoint->GetClients()) {
6467
if (!message) {
65-
Dictionary::Ptr allStats = new Dictionary;
66-
67-
auto stats (GenerateStats());
68-
69-
if (stats)
70-
allStats->Set(Endpoint::GetLocalEndpoint()->GetName(), stats);
71-
72-
{
73-
boost::mutex::scoped_lock lock (m_Mutex);
74-
75-
for (auto& endpointStats : m_SecondaryStats) {
76-
allStats->Set(endpointStats.first, endpointStats.second);
77-
}
78-
}
68+
auto allStats (GenerateStats());
7969

8070
if (!allStats->GetLength())
8171
return;
@@ -97,8 +87,11 @@ void StatsReporter::ReportStats()
9787
}
9888
}
9989

90+
static const String l_StatsReporterStatsFunctionName = "StatsReporter";
91+
10092
Dictionary::Ptr StatsReporter::GenerateStats()
10193
{
94+
auto allStats (new Dictionary);
10295
Dictionary::Ptr statsFunctions = ScriptGlobal::Get("StatsFunctions", &Empty);
10396

10497
if (statsFunctions) {
@@ -109,14 +102,24 @@ Dictionary::Ptr StatsReporter::GenerateStats()
109102
ObjectLock olock(statsFunctions);
110103

111104
for (const Dictionary::Pair &kv : statsFunctions) {
112-
((Function::Ptr) kv.second)->Invoke({status, perfdata});
105+
if (kv.first != l_StatsReporterStatsFunctionName) {
106+
((Function::Ptr) kv.second)->Invoke({status, perfdata});
107+
}
113108
}
114109
}
115110

116-
return status;
111+
allStats->Set(Endpoint::GetLocalEndpoint()->GetName(), status);
112+
}
113+
114+
{
115+
boost::mutex::scoped_lock lock (m_Mutex);
116+
117+
for (auto& endpointStats : m_SecondaryStats) {
118+
allStats->Set(endpointStats.first, endpointStats.second);
119+
}
117120
}
118121

119-
return nullptr;
122+
return allStats;
120123
}
121124

122125
Value StatsReporter::ClusterStatsAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
@@ -160,3 +163,8 @@ void StatsReporter::ClusterStatsHandler(const String& endpoint, const Dictionary
160163
boost::mutex::scoped_lock lock (m_Mutex);
161164
m_SecondaryStats[endpoint] = stats;
162165
}
166+
167+
void StatsReporter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata)
168+
{
169+
status->Set("cluster", m_Instance.GenerateStats());
170+
}

lib/remote/statsreporter.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#ifndef STATSREPORTER_H
2121
#define STATSREPORTER_H
2222

23+
#include "base/array.hpp"
2324
#include "base/dictionary.hpp"
2425
#include "base/string.hpp"
2526
#include "base/timer.hpp"
@@ -40,6 +41,7 @@ class StatsReporter
4041
{
4142
public:
4243
static Value ClusterStatsAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params);
44+
static void StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata);
4345

4446
private:
4547
StatsReporter();

0 commit comments

Comments
 (0)