1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "remote/endpoint.hpp"
4 #include "remote/endpoint-ti.cpp"
5 #include "remote/apilistener.hpp"
6 #include "remote/jsonrpcconnection.hpp"
7 #include "remote/zone.hpp"
8 #include "base/configtype.hpp"
9 #include "base/utility.hpp"
10 #include "base/exception.hpp"
11 #include "base/convert.hpp"
12
13 using namespace icinga;
14
15 REGISTER_TYPE(Endpoint);
16
17 boost::signals2::signal<void(const Endpoint::Ptr&, const JsonRpcConnection::Ptr&)> Endpoint::OnConnected;
18 boost::signals2::signal<void(const Endpoint::Ptr&, const JsonRpcConnection::Ptr&)> Endpoint::OnDisconnected;
19
OnAllConfigLoaded()20 void Endpoint::OnAllConfigLoaded()
21 {
22 ObjectImpl<Endpoint>::OnAllConfigLoaded();
23
24 if (!m_Zone)
25 BOOST_THROW_EXCEPTION(ScriptError("Endpoint '" + GetName() +
26 "' does not belong to a zone.", GetDebugInfo()));
27 }
28
SetCachedZone(const Zone::Ptr & zone)29 void Endpoint::SetCachedZone(const Zone::Ptr& zone)
30 {
31 if (m_Zone)
32 BOOST_THROW_EXCEPTION(ScriptError("Endpoint '" + GetName()
33 + "' is in more than one zone.", GetDebugInfo()));
34
35 m_Zone = zone;
36 }
37
AddClient(const JsonRpcConnection::Ptr & client)38 void Endpoint::AddClient(const JsonRpcConnection::Ptr& client)
39 {
40 bool was_master = ApiListener::GetInstance()->IsMaster();
41
42 {
43 std::unique_lock<std::mutex> lock(m_ClientsLock);
44 m_Clients.insert(client);
45 }
46
47 bool is_master = ApiListener::GetInstance()->IsMaster();
48
49 if (was_master != is_master)
50 ApiListener::OnMasterChanged(is_master);
51
52 OnConnected(this, client);
53 }
54
RemoveClient(const JsonRpcConnection::Ptr & client)55 void Endpoint::RemoveClient(const JsonRpcConnection::Ptr& client)
56 {
57 bool was_master = ApiListener::GetInstance()->IsMaster();
58
59 {
60 std::unique_lock<std::mutex> lock(m_ClientsLock);
61 m_Clients.erase(client);
62
63 Log(LogWarning, "ApiListener")
64 << "Removing API client for endpoint '" << GetName() << "'. " << m_Clients.size() << " API clients left.";
65
66 SetConnecting(false);
67 }
68
69 bool is_master = ApiListener::GetInstance()->IsMaster();
70
71 if (was_master != is_master)
72 ApiListener::OnMasterChanged(is_master);
73
74 OnDisconnected(this, client);
75 }
76
GetClients() const77 std::set<JsonRpcConnection::Ptr> Endpoint::GetClients() const
78 {
79 std::unique_lock<std::mutex> lock(m_ClientsLock);
80 return m_Clients;
81 }
82
GetZone() const83 Zone::Ptr Endpoint::GetZone() const
84 {
85 return m_Zone;
86 }
87
GetConnected() const88 bool Endpoint::GetConnected() const
89 {
90 std::unique_lock<std::mutex> lock(m_ClientsLock);
91 return !m_Clients.empty();
92 }
93
GetLocalEndpoint()94 Endpoint::Ptr Endpoint::GetLocalEndpoint()
95 {
96 ApiListener::Ptr listener = ApiListener::GetInstance();
97
98 if (!listener)
99 return nullptr;
100
101 return listener->GetLocalEndpoint();
102 }
103
AddMessageSent(int bytes)104 void Endpoint::AddMessageSent(int bytes)
105 {
106 double time = Utility::GetTime();
107 m_MessagesSent.InsertValue(time, 1);
108 m_BytesSent.InsertValue(time, bytes);
109 SetLastMessageSent(time);
110 }
111
AddMessageReceived(int bytes)112 void Endpoint::AddMessageReceived(int bytes)
113 {
114 double time = Utility::GetTime();
115 m_MessagesReceived.InsertValue(time, 1);
116 m_BytesReceived.InsertValue(time, bytes);
117 SetLastMessageReceived(time);
118 }
119
GetMessagesSentPerSecond() const120 double Endpoint::GetMessagesSentPerSecond() const
121 {
122 return m_MessagesSent.CalculateRate(Utility::GetTime(), 60);
123 }
124
GetMessagesReceivedPerSecond() const125 double Endpoint::GetMessagesReceivedPerSecond() const
126 {
127 return m_MessagesReceived.CalculateRate(Utility::GetTime(), 60);
128 }
129
GetBytesSentPerSecond() const130 double Endpoint::GetBytesSentPerSecond() const
131 {
132 return m_BytesSent.CalculateRate(Utility::GetTime(), 60);
133 }
134
GetBytesReceivedPerSecond() const135 double Endpoint::GetBytesReceivedPerSecond() const
136 {
137 return m_BytesReceived.CalculateRate(Utility::GetTime(), 60);
138 }
139