|
| 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 | +} |
0 commit comments