2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-02-21 14:12:34 +01:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "db_ido/endpointdbobject.hpp"
|
|
|
|
#include "db_ido/dbtype.hpp"
|
|
|
|
#include "db_ido/dbvalue.hpp"
|
|
|
|
#include "icinga/icingaapplication.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/initialize.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/convert.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-02-21 14:12:34 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_DBTYPE(Endpoint, "endpoint", DbObjectTypeEndpoint, "endpoint_object_id", EndpointDbObject);
|
|
|
|
|
|
|
|
INITIALIZE_ONCE(&EndpointDbObject::StaticInitialize);
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void EndpointDbObject::StaticInitialize()
|
2014-02-21 14:12:34 +01:00
|
|
|
{
|
2021-01-18 14:29:05 +01:00
|
|
|
Endpoint::OnConnected.connect([](const Endpoint::Ptr& endpoint, const JsonRpcConnection::Ptr&) { EndpointDbObject::UpdateConnectedStatus(endpoint); });
|
|
|
|
Endpoint::OnDisconnected.connect([](const Endpoint::Ptr& endpoint, const JsonRpcConnection::Ptr&) { EndpointDbObject::UpdateConnectedStatus(endpoint); });
|
2014-02-21 14:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EndpointDbObject::EndpointDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
|
|
|
|
: DbObject(type, name1, name2)
|
|
|
|
{ }
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Dictionary::Ptr EndpointDbObject::GetConfigFields() const
|
2014-02-21 14:12:34 +01:00
|
|
|
{
|
|
|
|
Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(GetObject());
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
return new Dictionary({
|
|
|
|
{ "identity", endpoint->GetName() },
|
|
|
|
{ "node", IcingaApplication::GetInstance()->GetNodeName() },
|
|
|
|
{ "zone_object_id", endpoint->GetZone() }
|
|
|
|
});
|
2014-02-21 14:12:34 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Dictionary::Ptr EndpointDbObject::GetStatusFields() const
|
2014-02-21 14:12:34 +01:00
|
|
|
{
|
|
|
|
Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(GetObject());
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "EndpointDbObject")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "update status for endpoint '" << endpoint->GetName() << "'";
|
2014-03-20 17:40:27 +01:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
return new Dictionary({
|
|
|
|
{ "identity", endpoint->GetName() },
|
|
|
|
{ "node", IcingaApplication::GetInstance()->GetNodeName() },
|
|
|
|
{ "zone_object_id", endpoint->GetZone() },
|
|
|
|
{ "is_connected", EndpointIsConnected(endpoint) }
|
|
|
|
});
|
2014-02-21 14:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EndpointDbObject::UpdateConnectedStatus(const Endpoint::Ptr& endpoint)
|
|
|
|
{
|
2014-05-03 20:02:22 +02:00
|
|
|
bool connected = EndpointIsConnected(endpoint);
|
2014-03-20 17:40:27 +01:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "EndpointDbObject")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "update is_connected=" << connected << " for endpoint '" << endpoint->GetName() << "'";
|
2014-02-21 14:12:34 +01:00
|
|
|
|
|
|
|
DbQuery query1;
|
|
|
|
query1.Table = "endpointstatus";
|
|
|
|
query1.Type = DbQueryUpdate;
|
2014-11-10 13:21:33 +01:00
|
|
|
query1.Category = DbCatState;
|
2014-02-21 14:12:34 +01:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
query1.Fields = new Dictionary({
|
|
|
|
{ "is_connected", (connected ? 1 : 0) },
|
|
|
|
{ "status_update_time", DbValue::FromTimestamp(Utility::GetTime()) }
|
|
|
|
});
|
2014-02-21 14:12:34 +01:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
query1.WhereCriteria = new Dictionary({
|
|
|
|
{ "endpoint_object_id", endpoint },
|
|
|
|
{ "instance_id", 0 } /* DbConnection class fills in real ID */
|
|
|
|
});
|
2014-02-21 14:12:34 +01:00
|
|
|
|
|
|
|
OnQuery(query1);
|
|
|
|
}
|
2014-03-07 17:52:24 +01:00
|
|
|
|
|
|
|
int EndpointDbObject::EndpointIsConnected(const Endpoint::Ptr& endpoint)
|
|
|
|
{
|
2015-10-22 10:52:38 +02:00
|
|
|
unsigned int is_connected = endpoint->GetConnected() ? 1 : 0;
|
2014-03-07 17:52:24 +01:00
|
|
|
|
|
|
|
/* if identity is equal to node, fake is_connected */
|
|
|
|
if (endpoint->GetName() == IcingaApplication::GetInstance()->GetNodeName())
|
|
|
|
is_connected = 1;
|
|
|
|
|
|
|
|
return is_connected;
|
|
|
|
}
|