|
| 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 | + auto parent (Zone::GetLocalZone()->GetParent()); |
| 63 | + |
| 64 | + if (parent) { |
| 65 | + Dictionary::Ptr message; |
| 66 | + |
| 67 | + for (auto& endpoint : parent->GetEndpoints()) { |
| 68 | + for (auto& client : endpoint->GetClients()) { |
| 69 | + if (!message) { |
| 70 | + message = new Dictionary({ |
| 71 | + { "jsonrpc", "2.0" }, |
| 72 | + { "method", "event::ClusterStats" }, |
| 73 | + { "params", new Dictionary({ |
| 74 | + { "stats", GenerateStats() } |
| 75 | + }) } |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + client->SendMessage(message); |
| 80 | + |
| 81 | + break; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +Dictionary::Ptr StatsReporter::GenerateStats() |
| 88 | +{ |
| 89 | + auto allStats (new Dictionary); |
| 90 | + |
| 91 | + { |
| 92 | + boost::mutex::scoped_lock lock (m_Mutex); |
| 93 | + |
| 94 | + for (auto& endpointStats : m_SecondaryStats) { |
| 95 | + allStats->Set(endpointStats.first, endpointStats.second); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + auto localZone (Zone::GetLocalZone()); |
| 100 | + auto parentZone (localZone->GetParent()); |
| 101 | + auto unorderedZones (ConfigType::GetObjectsByType<Zone>()); |
| 102 | + std::set<Zone::Ptr> zones (unorderedZones.begin(), unorderedZones.end()); |
| 103 | + std::set<Endpoint::Ptr> endpoints; |
| 104 | + auto ourStatus (new Dictionary); |
| 105 | + auto now (Utility::GetTime()); |
| 106 | + |
| 107 | + unorderedZones.clear(); |
| 108 | + |
| 109 | + for (auto zone (zones.begin()); zone != zones.end();) { |
| 110 | + if ((*zone)->GetParent() == localZone) { |
| 111 | + ++zone; |
| 112 | + } else { |
| 113 | + zones.erase(zone++); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + zones.emplace(localZone); |
| 118 | + |
| 119 | + if (parentZone) |
| 120 | + zones.emplace(parentZone); |
| 121 | + |
| 122 | + for (auto& zone : zones) { |
| 123 | + auto zoneEndpoints (zone->GetEndpoints()); |
| 124 | + endpoints.insert(zoneEndpoints.begin(), zoneEndpoints.end()); |
| 125 | + } |
| 126 | + |
| 127 | + endpoints.erase(Endpoint::GetLocalEndpoint()); |
| 128 | + |
| 129 | + for (auto& endpoint : endpoints) { |
| 130 | + ourStatus->Set(endpoint->GetName(), new Dictionary({ |
| 131 | + {"connected", endpoint->GetConnected()}, |
| 132 | + {"last_message_received", endpoint->GetLastMessageReceived()} |
| 133 | + })); |
| 134 | + } |
| 135 | + |
| 136 | + allStats->Set(Endpoint::GetLocalEndpoint()->GetName(), new Dictionary({ |
| 137 | + {"mtime", now}, |
| 138 | + {"version", Application::GetAppVersion()}, |
| 139 | + {"uptime", now - Application::GetStartTime()}, |
| 140 | + {"endpoints", ourStatus} |
| 141 | + })); |
| 142 | + |
| 143 | + return allStats; |
| 144 | +} |
| 145 | + |
| 146 | +Value StatsReporter::ClusterStatsAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params) |
| 147 | +{ |
| 148 | + auto endpoint (origin->FromClient->GetEndpoint()); |
| 149 | + |
| 150 | + if (endpoint && endpoint->GetZone()->IsChildOf(Zone::GetLocalZone())) { |
| 151 | + auto rawStats (params->Get("stats")); |
| 152 | + |
| 153 | + if (rawStats.IsObject()) { |
| 154 | + Dictionary::Ptr allStats (rawStats); |
| 155 | + |
| 156 | + if (allStats) { |
| 157 | + // Don't permit any child to speak in our zone's name |
| 158 | + std::set<String> neighborhood; |
| 159 | + |
| 160 | + for (auto& endpoint : Zone::GetLocalZone()->GetEndpoints()) { |
| 161 | + neighborhood.emplace(endpoint->GetName()); |
| 162 | + } |
| 163 | + |
| 164 | + ObjectLock lock (allStats); |
| 165 | + |
| 166 | + for (auto& endpointStats : allStats) { |
| 167 | + if (endpointStats.second.IsObject()) { |
| 168 | + Dictionary::Ptr stats (endpointStats.second); |
| 169 | + |
| 170 | + if (stats && neighborhood.find(endpointStats.first) == neighborhood.end()) { |
| 171 | + m_Instance.ClusterStatsHandler(endpointStats.first, endpointStats.second); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return Empty; |
| 180 | +} |
| 181 | + |
| 182 | +void StatsReporter::ClusterStatsHandler(const String& endpoint, const Dictionary::Ptr& stats) |
| 183 | +{ |
| 184 | + boost::mutex::scoped_lock lock (m_Mutex); |
| 185 | + m_SecondaryStats[endpoint] = stats; |
| 186 | +} |
0 commit comments