2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2012-05-10 06:06:41 -04:00
|
|
|
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "remote/endpoint.hpp"
|
2018-01-18 07:50:38 -05:00
|
|
|
#include "remote/endpoint-ti.cpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "remote/apilistener.hpp"
|
2015-06-22 05:11:21 -04:00
|
|
|
#include "remote/jsonrpcconnection.hpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "remote/zone.hpp"
|
2015-08-15 14:28:05 -04:00
|
|
|
#include "base/configtype.hpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/utility.hpp"
|
|
|
|
|
#include "base/exception.hpp"
|
2014-08-22 09:39:34 -04:00
|
|
|
#include "base/convert.hpp"
|
2012-04-06 02:56:52 -04:00
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
2013-03-01 06:07:52 -05:00
|
|
|
REGISTER_TYPE(Endpoint);
|
2012-09-03 04:28:14 -04:00
|
|
|
|
2015-06-22 05:11:21 -04: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;
|
2012-09-03 04:28:14 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
void Endpoint::OnAllConfigLoaded()
|
2012-04-18 09:22:25 -04:00
|
|
|
{
|
2016-08-14 16:10:30 -04:00
|
|
|
ObjectImpl<Endpoint>::OnAllConfigLoaded();
|
2012-04-18 09:22:25 -04:00
|
|
|
|
2014-05-03 14:02:22 -04:00
|
|
|
if (!m_Zone)
|
2015-09-22 11:58:12 -04:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Endpoint '" + GetName() +
|
2017-12-19 09:50:05 -05:00
|
|
|
"' does not belong to a zone.", GetDebugInfo()));
|
2012-09-03 04:28:14 -04:00
|
|
|
}
|
|
|
|
|
|
2016-08-16 07:53:45 -04:00
|
|
|
void Endpoint::SetCachedZone(const Zone::Ptr& zone)
|
|
|
|
|
{
|
|
|
|
|
if (m_Zone)
|
|
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Endpoint '" + GetName()
|
2017-12-19 09:50:05 -05:00
|
|
|
+ "' is in more than one zone.", GetDebugInfo()));
|
2016-08-16 07:53:45 -04:00
|
|
|
|
|
|
|
|
m_Zone = zone;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-22 05:11:21 -04:00
|
|
|
void Endpoint::AddClient(const JsonRpcConnection::Ptr& client)
|
2012-09-03 04:28:14 -04:00
|
|
|
{
|
2014-05-03 14:02:22 -04:00
|
|
|
bool was_master = ApiListener::GetInstance()->IsMaster();
|
2014-04-25 08:33:45 -04:00
|
|
|
|
2014-05-03 14:02:22 -04:00
|
|
|
{
|
2021-02-02 04:16:04 -05:00
|
|
|
std::unique_lock<std::mutex> lock(m_ClientsLock);
|
2014-05-03 14:02:22 -04:00
|
|
|
m_Clients.insert(client);
|
|
|
|
|
}
|
2013-10-17 04:56:42 -04:00
|
|
|
|
2014-05-03 14:02:22 -04:00
|
|
|
bool is_master = ApiListener::GetInstance()->IsMaster();
|
2013-03-02 03:07:47 -05:00
|
|
|
|
2014-05-03 14:02:22 -04:00
|
|
|
if (was_master != is_master)
|
|
|
|
|
ApiListener::OnMasterChanged(is_master);
|
2013-04-04 10:08:02 -04:00
|
|
|
|
2014-11-08 15:17:16 -05:00
|
|
|
OnConnected(this, client);
|
2012-04-18 09:22:25 -04:00
|
|
|
}
|
|
|
|
|
|
2015-06-22 05:11:21 -04:00
|
|
|
void Endpoint::RemoveClient(const JsonRpcConnection::Ptr& client)
|
2012-05-08 04:13:15 -04:00
|
|
|
{
|
2014-05-03 14:02:22 -04:00
|
|
|
bool was_master = ApiListener::GetInstance()->IsMaster();
|
2013-09-02 09:12:20 -04:00
|
|
|
|
2014-05-03 14:02:22 -04:00
|
|
|
{
|
2021-02-02 04:16:04 -05:00
|
|
|
std::unique_lock<std::mutex> lock(m_ClientsLock);
|
2014-05-03 14:02:22 -04:00
|
|
|
m_Clients.erase(client);
|
2014-08-22 09:39:34 -04:00
|
|
|
|
2014-10-20 04:09:57 -04:00
|
|
|
Log(LogWarning, "ApiListener")
|
2017-12-19 09:50:05 -05:00
|
|
|
<< "Removing API client for endpoint '" << GetName() << "'. " << m_Clients.size() << " API clients left.";
|
2015-02-26 05:25:01 -05:00
|
|
|
|
|
|
|
|
SetConnecting(false);
|
2014-05-03 14:02:22 -04:00
|
|
|
}
|
2012-09-03 04:28:14 -04:00
|
|
|
|
2014-05-03 14:02:22 -04:00
|
|
|
bool is_master = ApiListener::GetInstance()->IsMaster();
|
2012-09-03 04:28:14 -04:00
|
|
|
|
2014-05-03 14:02:22 -04:00
|
|
|
if (was_master != is_master)
|
|
|
|
|
ApiListener::OnMasterChanged(is_master);
|
2014-02-21 08:12:34 -05:00
|
|
|
|
2014-11-08 15:17:16 -05:00
|
|
|
OnDisconnected(this, client);
|
2012-09-03 04:28:14 -04:00
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
std::set<JsonRpcConnection::Ptr> Endpoint::GetClients() const
|
2012-09-03 04:28:14 -04:00
|
|
|
{
|
2021-02-02 04:16:04 -05:00
|
|
|
std::unique_lock<std::mutex> lock(m_ClientsLock);
|
2014-05-03 14:02:22 -04:00
|
|
|
return m_Clients;
|
|
|
|
|
}
|
2014-02-21 08:12:34 -05:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
Zone::Ptr Endpoint::GetZone() const
|
2014-05-03 14:02:22 -04:00
|
|
|
{
|
|
|
|
|
return m_Zone;
|
|
|
|
|
}
|
2013-04-04 10:08:02 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
bool Endpoint::GetConnected() const
|
2014-05-03 14:02:22 -04:00
|
|
|
{
|
2021-02-02 04:16:04 -05:00
|
|
|
std::unique_lock<std::mutex> lock(m_ClientsLock);
|
2014-05-03 14:02:22 -04:00
|
|
|
return !m_Clients.empty();
|
2012-09-03 04:28:14 -04:00
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
Endpoint::Ptr Endpoint::GetLocalEndpoint()
|
2013-09-12 04:03:48 -04:00
|
|
|
{
|
2014-05-03 14:02:22 -04:00
|
|
|
ApiListener::Ptr listener = ApiListener::GetInstance();
|
2013-09-12 04:03:48 -04:00
|
|
|
|
2014-05-03 14:02:22 -04:00
|
|
|
if (!listener)
|
2017-11-30 02:36:35 -05:00
|
|
|
return nullptr;
|
2013-09-12 04:03:48 -04:00
|
|
|
|
2015-11-24 09:25:55 -05:00
|
|
|
return listener->GetLocalEndpoint();
|
2013-09-12 04:03:48 -04:00
|
|
|
}
|
2017-11-13 10:30:29 -05: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);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
double Endpoint::GetMessagesSentPerSecond() const
|
2017-11-13 10:30:29 -05:00
|
|
|
{
|
|
|
|
|
return m_MessagesSent.CalculateRate(Utility::GetTime(), 60);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
double Endpoint::GetMessagesReceivedPerSecond() const
|
2017-11-13 10:30:29 -05:00
|
|
|
{
|
|
|
|
|
return m_MessagesReceived.CalculateRate(Utility::GetTime(), 60);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
double Endpoint::GetBytesSentPerSecond() const
|
2017-11-13 10:30:29 -05:00
|
|
|
{
|
|
|
|
|
return m_BytesSent.CalculateRate(Utility::GetTime(), 60);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
double Endpoint::GetBytesReceivedPerSecond() const
|
2017-11-13 10:30:29 -05:00
|
|
|
{
|
|
|
|
|
return m_BytesReceived.CalculateRate(Utility::GetTime(), 60);
|
|
|
|
|
}
|