2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-12-09 13:17:27 +01:00
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
#include "remote/jsonrpcconnection.hpp"
|
2014-12-09 13:17:27 +01:00
|
|
|
#include "remote/messageorigin.hpp"
|
|
|
|
#include "remote/apifunction.hpp"
|
|
|
|
#include "base/initialize.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2014-12-10 09:56:32 +01:00
|
|
|
#include "base/logger.hpp"
|
2015-03-28 11:04:42 +01:00
|
|
|
#include "base/utility.hpp"
|
2019-02-20 12:28:49 +01:00
|
|
|
#include <boost/asio/spawn.hpp>
|
|
|
|
#include <boost/date_time/posix_time/posix_time_duration.hpp>
|
2019-06-05 10:32:20 +02:00
|
|
|
#include <boost/system/system_error.hpp>
|
2014-12-09 13:17:27 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
REGISTER_APIFUNCTION(Heartbeat, event, &JsonRpcConnection::HeartbeatAPIHandler);
|
2014-12-09 13:17:27 +01:00
|
|
|
|
2019-02-20 12:28:49 +01:00
|
|
|
void JsonRpcConnection::HandleAndWriteHeartbeats(boost::asio::yield_context yc)
|
|
|
|
{
|
2019-06-05 10:32:20 +02:00
|
|
|
boost::system::error_code ec;
|
2019-02-20 12:28:49 +01:00
|
|
|
|
|
|
|
for (;;) {
|
2020-07-09 13:16:48 +02:00
|
|
|
m_HeartbeatTimer.expires_from_now(boost::posix_time::seconds(20));
|
2019-06-05 10:32:20 +02:00
|
|
|
m_HeartbeatTimer.async_wait(yc[ec]);
|
2019-02-20 12:28:49 +01:00
|
|
|
|
|
|
|
if (m_ShuttingDown) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_NextHeartbeat != 0 && m_NextHeartbeat < Utility::GetTime()) {
|
2020-03-18 11:58:27 +01:00
|
|
|
{
|
|
|
|
Log logMsg (LogWarning, "JsonRpcConnection");
|
|
|
|
|
|
|
|
if (m_Endpoint) {
|
|
|
|
logMsg << "Client for endpoint '" << m_Endpoint->GetName() << "'";
|
|
|
|
} else {
|
|
|
|
logMsg << "Anonymous client";
|
|
|
|
}
|
|
|
|
|
|
|
|
logMsg << " has requested heartbeat message but hasn't responded in time. Closing connection.";
|
|
|
|
}
|
2019-02-20 12:28:49 +01:00
|
|
|
|
|
|
|
Disconnect();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-13 11:21:38 +01:00
|
|
|
if (m_Endpoint) {
|
|
|
|
SendMessageInternal(new Dictionary({
|
|
|
|
{ "jsonrpc", "2.0" },
|
|
|
|
{ "method", "event::Heartbeat" },
|
|
|
|
{ "params", new Dictionary({
|
|
|
|
{ "timeout", 120 }
|
|
|
|
}) }
|
|
|
|
}));
|
|
|
|
}
|
2019-02-20 12:28:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:47:44 +02:00
|
|
|
Value JsonRpcConnection::HeartbeatAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
|
2014-12-09 13:17:27 +01:00
|
|
|
{
|
2019-02-20 12:28:49 +01:00
|
|
|
Value vtimeout = params->Get("timeout");
|
|
|
|
|
|
|
|
if (!vtimeout.IsEmpty()) {
|
|
|
|
origin->FromClient->m_NextHeartbeat = Utility::GetTime() + vtimeout;
|
|
|
|
}
|
|
|
|
|
2014-12-09 15:07:49 +01:00
|
|
|
return Empty;
|
2014-12-09 13:17:27 +01:00
|
|
|
}
|
|
|
|
|