Skip to content

Commit 379efe5

Browse files
committed
Publish local stats to parents every 10s while connected (event::ClusterStats)
1 parent c38fd4a commit 379efe5

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-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: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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/array.hpp"
21+
#include "base/dictionary.hpp"
22+
#include "base/function.hpp"
23+
#include "base/objectlock.hpp"
24+
#include "base/scriptglobal.hpp"
25+
#include "base/utility.hpp"
26+
#include "remote/endpoint.hpp"
27+
#include "remote/statsreporter.hpp"
28+
#include <boost/bind.hpp>
29+
#include <boost/thread/mutex.hpp>
30+
31+
using namespace icinga;
32+
33+
EndpointCache::EndpointCache(StatsReporter& reporter, const Endpoint::Ptr& endpoint)
34+
{
35+
timer = new Timer;
36+
timer->OnTimerExpired.connect(std::bind(&StatsReporter::ReportStats, &reporter, endpoint));
37+
timer->SetInterval(10);
38+
timer->Start();
39+
timer->Reschedule(0);
40+
}
41+
42+
EndpointCache::~EndpointCache()
43+
{
44+
timer->Stop();
45+
}
46+
47+
StatsReporter StatsReporter::m_Instance;
48+
49+
StatsReporter::StatsReporter()
50+
{
51+
Endpoint::OnConnected.connect(std::bind(&StatsReporter::OnConnected, this, _1));
52+
Endpoint::OnDisconnected.connect(std::bind(&StatsReporter::OnDisconnected, this, _1));
53+
}
54+
55+
void StatsReporter::OnConnected(const Endpoint::Ptr& endpoint)
56+
{
57+
if (Endpoint::GetLocalEndpoint()->GetZone()->IsChildOf(endpoint->GetZone())) {
58+
boost::mutex::scoped_lock lock(m_Mutex);
59+
60+
m_Cache.emplace(endpoint, EndpointCache(*this, endpoint));
61+
}
62+
}
63+
64+
void StatsReporter::OnDisconnected(const Endpoint::Ptr& endpoint)
65+
{
66+
boost::mutex::scoped_lock lock (m_Mutex);
67+
68+
m_Cache.erase(endpoint);
69+
}
70+
71+
void StatsReporter::ReportStats(const Endpoint::Ptr& endpoint)
72+
{
73+
Dictionary::Ptr statsFunctions = ScriptGlobal::Get("StatsFunctions", &Empty);
74+
75+
if (statsFunctions) {
76+
Dictionary::Ptr status = new Dictionary();
77+
78+
{
79+
Array::Ptr perfdata = new Array();
80+
ObjectLock olock(statsFunctions);
81+
82+
for (const Dictionary::Pair &kv : statsFunctions) {
83+
((Function::Ptr) kv.second)->Invoke({status, perfdata});
84+
}
85+
}
86+
87+
for (auto& client : endpoint->GetClients()) {
88+
client->SendMessage(new Dictionary({
89+
{ "jsonrpc", "2.0" },
90+
{ "method", "event::ClusterStats" },
91+
{ "params", new Dictionary({
92+
{ "ctime", Utility::GetTime() },
93+
{ "stats", status }
94+
}) }
95+
}));
96+
97+
break;
98+
}
99+
}
100+
}

lib/remote/statsreporter.hpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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/timer.hpp"
25+
#include "remote/endpoint.hpp"
26+
#include <boost/thread/mutex.hpp>
27+
#include <map>
28+
29+
namespace icinga
30+
{
31+
32+
class StatsReporter;
33+
34+
/**
35+
* @ingroup remote
36+
*/
37+
struct EndpointCache
38+
{
39+
EndpointCache(StatsReporter& reporter, const Endpoint::Ptr& endpoint);
40+
~EndpointCache();
41+
42+
Timer::Ptr timer;
43+
44+
// TODO: actually cache something useful
45+
};
46+
47+
/**
48+
* @ingroup remote
49+
*/
50+
class StatsReporter
51+
{
52+
friend EndpointCache;
53+
54+
private:
55+
StatsReporter();
56+
57+
void OnConnected(const Endpoint::Ptr& endpoint);
58+
void OnDisconnected(const Endpoint::Ptr& endpoint);
59+
void ReportStats(const Endpoint::Ptr& endpoint);
60+
61+
static StatsReporter m_Instance;
62+
63+
boost::mutex m_Mutex;
64+
std::map<Endpoint::Ptr, EndpointCache> m_Cache;
65+
};
66+
67+
}
68+
69+
#endif /* STATSREPORTER_H */

0 commit comments

Comments
 (0)