icinga2/lib/icingadb/icingadb-stats.cpp

54 lines
1.3 KiB
C++
Raw Normal View History

2019-11-02 14:00:06 +01:00
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2018-06-08 11:38:36 +02:00
2019-10-29 18:36:16 +01:00
#include "icingadb/icingadb.hpp"
#include "base/application.hpp"
2018-06-08 11:38:36 +02:00
#include "base/json.hpp"
#include "base/logger.hpp"
#include "base/serializer.hpp"
#include "base/statsfunction.hpp"
#include "base/convert.hpp"
using namespace icinga;
Dictionary::Ptr IcingaDB::GetStats()
2018-06-08 11:38:36 +02:00
{
2018-06-08 12:47:45 +02:00
Dictionary::Ptr stats = new Dictionary();
2018-06-08 11:38:36 +02:00
//TODO: Figure out if more stats can be useful here.
2018-09-18 15:47:44 +02:00
Namespace::Ptr statsFunctions = ScriptGlobal::Get("StatsFunctions", &Empty);
2018-06-08 11:38:36 +02:00
if (!statsFunctions)
Dictionary::Ptr();
2018-06-08 12:47:45 +02:00
ObjectLock olock(statsFunctions);
2018-09-18 15:47:44 +02:00
for (auto& kv : statsFunctions)
2018-06-08 12:47:45 +02:00
{
Function::Ptr func = kv.second.Val;
2018-06-08 11:38:36 +02:00
2018-06-08 12:47:45 +02:00
if (!func)
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid status function name."));
2018-06-08 11:38:36 +02:00
2018-06-08 12:47:45 +02:00
Dictionary::Ptr status = new Dictionary();
Array::Ptr perfdata = new Array();
func->Invoke({ status, perfdata });
2018-06-08 11:38:36 +02:00
2018-06-08 12:47:45 +02:00
stats->Set(kv.first, new Dictionary({
{ "status", status },
{ "perfdata", Serialize(perfdata, FAState) }
}));
}
2018-06-08 11:38:36 +02:00
typedef Dictionary::Ptr DP;
DP app = DP(DP(DP(stats->Get("IcingaApplication"))->Get("status"))->Get("icingaapplication"))->Get("app");
app->Set("program_start", TimestampToMilliseconds(Application::GetStartTime()));
auto localEndpoint (Endpoint::GetLocalEndpoint());
if (localEndpoint) {
app->Set("endpoint_id", GetObjectIdentifier(localEndpoint));
}
2018-06-08 12:47:45 +02:00
return stats;
2018-06-08 11:38:36 +02:00
}