1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "remote/endpoint.hpp"
#include "remote/endpoint-ti.cpp"
#include "remote/apifunction.hpp"
#include "remote/apilistener.hpp"
#include "remote/jsonrpcconnection.hpp"
#include "remote/zone.hpp"
#include "base/configtype.hpp"
#include "base/utility.hpp"
#include "base/exception.hpp"
#include "base/convert.hpp"
using namespace icinga;
REGISTER_TYPE(Endpoint);
boost::signals2::signal<void(const Endpoint::Ptr&, const JsonRpcConnection::Ptr&)> Endpoint::OnConnected;
boost::signals2::signal<void(const Endpoint::Ptr&, const JsonRpcConnection::Ptr&)> Endpoint::OnDisconnected;
void Endpoint::OnAllConfigLoaded()
{
ObjectImpl<Endpoint>::OnAllConfigLoaded();
if (!m_Zone)
BOOST_THROW_EXCEPTION(ScriptError("Endpoint '" + GetName() +
"' does not belong to a zone.", GetDebugInfo()));
}
void Endpoint::SetCachedZone(const Zone::Ptr& zone)
{
if (m_Zone)
BOOST_THROW_EXCEPTION(ScriptError("Endpoint '" + GetName()
+ "' is in more than one zone.", GetDebugInfo()));
m_Zone = zone;
}
Endpoint::Endpoint()
{
for (auto& [name, afunc] : ApiFunctionRegistry::GetInstance()->GetItems()) {
m_MessageCounters.emplace(afunc, 0);
}
}
void Endpoint::AddClient(const JsonRpcConnection::Ptr& client)
{
bool was_master = ApiListener::GetInstance()->IsMaster();
{
std::unique_lock<std::mutex> lock(m_ClientsLock);
m_Clients.insert(client);
}
bool is_master = ApiListener::GetInstance()->IsMaster();
if (was_master != is_master)
ApiListener::OnMasterChanged(is_master);
OnConnected(this, client);
}
void Endpoint::RemoveClient(const JsonRpcConnection::Ptr& client)
{
bool was_master = ApiListener::GetInstance()->IsMaster();
{
std::unique_lock<std::mutex> lock(m_ClientsLock);
m_Clients.erase(client);
Log(LogInformation, "ApiListener")
<< "Removing API client for endpoint '" << GetName() << "'. " << m_Clients.size() << " API clients left.";
SetConnecting(false);
}
bool is_master = ApiListener::GetInstance()->IsMaster();
if (was_master != is_master)
ApiListener::OnMasterChanged(is_master);
OnDisconnected(this, client);
}
std::set<JsonRpcConnection::Ptr> Endpoint::GetClients() const
{
std::unique_lock<std::mutex> lock(m_ClientsLock);
return m_Clients;
}
Zone::Ptr Endpoint::GetZone() const
{
return m_Zone;
}
bool Endpoint::GetConnected() const
{
std::unique_lock<std::mutex> lock(m_ClientsLock);
return !m_Clients.empty();
}
Endpoint::Ptr Endpoint::GetLocalEndpoint()
{
ApiListener::Ptr listener = ApiListener::GetInstance();
if (!listener)
return nullptr;
return listener->GetLocalEndpoint();
}
void Endpoint::AddMessageSent(int bytes)
{
double time = Utility::GetTime();
m_MessagesSent.InsertValue(time, 1);
m_BytesSent.InsertValue(time, bytes);
SetLastMessageSent(time);
}
void Endpoint::AddMessageReceived(int bytes)
{
double time = Utility::GetTime();
m_MessagesReceived.InsertValue(time, 1);
m_BytesReceived.InsertValue(time, bytes);
SetLastMessageReceived(time);
}
void Endpoint::AddMessageReceived(const intrusive_ptr<ApiFunction>& method)
{
m_MessageCounters.at(method).fetch_add(1, std::memory_order_relaxed);
}
void Endpoint::AddMessageProcessed(const AtomicDuration::Clock::duration& duration)
{
m_InputProcessingTime += duration;
}
double Endpoint::GetMessagesSentPerSecond() const
{
return m_MessagesSent.CalculateRate(Utility::GetTime(), 60);
}
double Endpoint::GetMessagesReceivedPerSecond() const
{
return m_MessagesReceived.CalculateRate(Utility::GetTime(), 60);
}
double Endpoint::GetBytesSentPerSecond() const
{
return m_BytesSent.CalculateRate(Utility::GetTime(), 60);
}
double Endpoint::GetBytesReceivedPerSecond() const
{
return m_BytesReceived.CalculateRate(Utility::GetTime(), 60);
}
Dictionary::Ptr Endpoint::GetMessagesReceivedPerType() const
{
DictionaryData result;
for (auto& [afunc, cnt] : m_MessageCounters) {
if (auto v (cnt.load(std::memory_order_relaxed)); v) {
result.emplace_back(afunc->GetName(), v);
}
}
return new Dictionary(std::move(result));
}
double Endpoint::GetSecondsProcessingMessages() const
{
return m_InputProcessingTime;
}
|