icinga2/lib/remote/endpoint.cpp

139 lines
3.2 KiB
C++
Raw Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2014-05-25 16:23:35 +02:00
#include "remote/endpoint.hpp"
2018-01-18 13:50:38 +01:00
#include "remote/endpoint-ti.cpp"
2014-05-25 16:23:35 +02:00
#include "remote/apilistener.hpp"
2015-06-22 11:11:21 +02:00
#include "remote/jsonrpcconnection.hpp"
2014-05-25 16:23:35 +02:00
#include "remote/zone.hpp"
#include "base/configtype.hpp"
2014-05-25 16:23:35 +02:00
#include "base/utility.hpp"
#include "base/exception.hpp"
#include "base/convert.hpp"
using namespace icinga;
2013-03-01 12:07:52 +01:00
REGISTER_TYPE(Endpoint);
2015-06-22 11:11:21 +02:00
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()
2012-04-18 15:22:25 +02:00
{
ObjectImpl<Endpoint>::OnAllConfigLoaded();
2012-04-18 15:22:25 +02:00
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;
}
2015-06-22 11:11:21 +02:00
void Endpoint::AddClient(const JsonRpcConnection::Ptr& client)
{
bool was_master = ApiListener::GetInstance()->IsMaster();
{
2021-02-02 10:16:04 +01:00
std::unique_lock<std::mutex> lock(m_ClientsLock);
m_Clients.insert(client);
}
2013-10-17 10:56:42 +02:00
bool is_master = ApiListener::GetInstance()->IsMaster();
2013-03-02 09:07:47 +01:00
if (was_master != is_master)
ApiListener::OnMasterChanged(is_master);
2013-04-04 16:08:02 +02:00
OnConnected(this, client);
2012-04-18 15:22:25 +02:00
}
2015-06-22 11:11:21 +02:00
void Endpoint::RemoveClient(const JsonRpcConnection::Ptr& client)
2012-05-08 10:13:15 +02:00
{
bool was_master = ApiListener::GetInstance()->IsMaster();
{
2021-02-02 10:16:04 +01:00
std::unique_lock<std::mutex> lock(m_ClientsLock);
m_Clients.erase(client);
2014-10-20 10:09:57 +02:00
Log(LogWarning, "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
{
2021-02-02 10:16:04 +01:00
std::unique_lock<std::mutex> lock(m_ClientsLock);
return m_Clients;
}
Zone::Ptr Endpoint::GetZone() const
{
return m_Zone;
}
2013-04-04 16:08:02 +02:00
bool Endpoint::GetConnected() const
{
2021-02-02 10:16:04 +01:00
std::unique_lock<std::mutex> lock(m_ClientsLock);
return !m_Clients.empty();
}
Endpoint::Ptr Endpoint::GetLocalEndpoint()
2013-09-12 10:03:48 +02:00
{
ApiListener::Ptr listener = ApiListener::GetInstance();
2013-09-12 10:03:48 +02:00
if (!listener)
2017-11-30 08:36:35 +01:00
return nullptr;
2013-09-12 10:03:48 +02:00
return listener->GetLocalEndpoint();
2013-09-12 10:03:48 +02:00
}
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);
}
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);
}