2014-02-21 14:12:34 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2016-01-12 08:29:59 +01:00
|
|
|
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
|
2014-02-21 14:12:34 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
void EndpointDbObject::StaticInitialize(void)
|
|
|
|
{
|
|
|
|
Endpoint::OnConnected.connect(boost::bind(&EndpointDbObject::UpdateConnectedStatus, _1));
|
2014-05-03 20:02:22 +02:00
|
|
|
Endpoint::OnDisconnected.connect(boost::bind(&EndpointDbObject::UpdateConnectedStatus, _1));
|
2014-02-21 14:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EndpointDbObject::EndpointDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
|
|
|
|
: DbObject(type, name1, name2)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
Dictionary::Ptr EndpointDbObject::GetConfigFields(void) const
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr fields = new Dictionary();
|
2014-02-21 14:12:34 +01:00
|
|
|
Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(GetObject());
|
|
|
|
|
|
|
|
fields->Set("identity", endpoint->GetName());
|
|
|
|
fields->Set("node", IcingaApplication::GetInstance()->GetNodeName());
|
2015-06-17 15:45:45 +02:00
|
|
|
fields->Set("zone_object_id", endpoint->GetZone());
|
2014-02-21 14:12:34 +01:00
|
|
|
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary::Ptr EndpointDbObject::GetStatusFields(void) const
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr fields = new Dictionary();
|
2014-02-21 14:12:34 +01:00
|
|
|
Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(GetObject());
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "EndpointDbObject")
|
|
|
|
<< "update status for endpoint '" << endpoint->GetName() << "'";
|
2014-03-20 17:40:27 +01:00
|
|
|
|
2014-02-21 14:12:34 +01:00
|
|
|
fields->Set("identity", endpoint->GetName());
|
|
|
|
fields->Set("node", IcingaApplication::GetInstance()->GetNodeName());
|
2015-06-17 15:45:45 +02:00
|
|
|
fields->Set("zone_object_id", endpoint->GetZone());
|
2014-03-07 17:52:24 +01:00
|
|
|
fields->Set("is_connected", EndpointIsConnected(endpoint));
|
2014-02-21 14:12:34 +01:00
|
|
|
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
<< "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
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr fields1 = new Dictionary();
|
2014-03-20 17:40:27 +01:00
|
|
|
fields1->Set("is_connected", (connected ? 1 : 0));
|
2014-02-21 14:12:34 +01:00
|
|
|
fields1->Set("status_update_time", DbValue::FromTimestamp(Utility::GetTime()));
|
|
|
|
query1.Fields = fields1;
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
query1.WhereCriteria = new Dictionary();
|
2014-02-21 14:12:34 +01:00
|
|
|
query1.WhereCriteria->Set("endpoint_object_id", endpoint);
|
|
|
|
query1.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|