DB IDO: Add status/history references from local node to endpoint object.

Refs #5636
This commit is contained in:
Michael Friedrich 2014-03-20 17:40:27 +01:00
parent dfd08ec6ea
commit b4f0148871
5 changed files with 117 additions and 11 deletions

View File

@ -326,6 +326,8 @@ void DbConnection::PrepareDatabase(void)
//ClearConfigTable("contactstatus");
ClearConfigTable("customvariables");
ClearConfigTable("customvariablestatus");
ClearConfigTable("endpoints");
ClearConfigTable("endpointstatus");
ClearConfigTable("host_contactgroups");
ClearConfigTable("host_contacts");
ClearConfigTable("host_parenthosts");

View File

@ -109,16 +109,20 @@ void DbObject::SendStatusUpdate(void)
query.Category = DbCatState;
query.Fields = fields;
query.Fields->Set(GetType()->GetIDColumn(), GetObject());
/* do not override our own endpoint dbobject id */
if (GetType()->GetTable() != "endpoint") {
String node = IcingaApplication::GetInstance()->GetNodeName();
Log(LogDebug, "db_ido", "Endpoint node: '" + node + "' status update for '" + GetObject()->GetName() + "'");
Endpoint::Ptr endpoint = Endpoint::GetByName(node);
if (endpoint)
query.Fields->Set("endpoint_object_id", endpoint);
}
query.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
String node = IcingaApplication::GetInstance()->GetNodeName();
Log(LogWarning, "db_ido", "Endpoint node: '" + node + "'");
Endpoint::Ptr endpoint = Endpoint::GetByName(node);
if (endpoint)
query.Fields->Set("endpoint_object_id", endpoint);
query.Fields->Set("status_update_time", DbValue::FromTimestamp(Utility::GetTime()));
query.WhereCriteria = make_shared<Dictionary>();
query.WhereCriteria->Set(GetType()->GetIDColumn(), GetObject());

View File

@ -25,6 +25,7 @@
#include "base/initialize.h"
#include "base/dynamictype.h"
#include "base/utility.h"
#include "base/convert.h"
#include "base/logger_fwd.h"
#include <boost/foreach.hpp>
@ -38,7 +39,7 @@ INITIALIZE_ONCE(&EndpointDbObject::StaticInitialize);
void EndpointDbObject::StaticInitialize(void)
{
Endpoint::OnConnected.connect(boost::bind(&EndpointDbObject::UpdateConnectedStatus, _1));
Endpoint::OnDisconnected.connect(boost::bind(&EndpointDbObject::UpdateConnectedStatus, _1));
Endpoint::OnDisconnected.connect(boost::bind(&EndpointDbObject::UpdateDisconnectedStatus, _1));
}
EndpointDbObject::EndpointDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
@ -61,6 +62,8 @@ Dictionary::Ptr EndpointDbObject::GetStatusFields(void) const
Dictionary::Ptr fields = make_shared<Dictionary>();
Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(GetObject());
Log(LogDebug, "db_ido", "update status for endpoint '" + endpoint->GetName() + "'");
fields->Set("identity", endpoint->GetName());
fields->Set("node", IcingaApplication::GetInstance()->GetNodeName());
fields->Set("is_connected", EndpointIsConnected(endpoint));
@ -70,14 +73,24 @@ Dictionary::Ptr EndpointDbObject::GetStatusFields(void) const
void EndpointDbObject::UpdateConnectedStatus(const Endpoint::Ptr& endpoint)
{
Log(LogDebug, "db_ido", "update is_connected for endpoint '" + endpoint->GetName() + "'");
UpdateConnectedStatusInternal(endpoint, true);
}
void EndpointDbObject::UpdateDisconnectedStatus(const Endpoint::Ptr& endpoint)
{
UpdateConnectedStatusInternal(endpoint, false);
}
void EndpointDbObject::UpdateConnectedStatusInternal(const Endpoint::Ptr& endpoint, bool connected)
{
Log(LogDebug, "db_ido", "update is_connected=" + Convert::ToString(connected ? 1 : 0) + " for endpoint '" + endpoint->GetName() + "'");
DbQuery query1;
query1.Table = "endpointstatus";
query1.Type = DbQueryUpdate;
Dictionary::Ptr fields1 = make_shared<Dictionary>();
fields1->Set("is_connected", EndpointIsConnected(endpoint));
fields1->Set("is_connected", (connected ? 1 : 0));
fields1->Set("status_update_time", DbValue::FromTimestamp(Utility::GetTime()));
query1.Fields = fields1;
@ -98,3 +111,24 @@ int EndpointDbObject::EndpointIsConnected(const Endpoint::Ptr& endpoint)
return is_connected;
}
void EndpointDbObject::OnConfigUpdate(void)
{
/* update current status on config dump once */
Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(GetObject());
DbQuery query1;
query1.Table = "endpointstatus";
query1.Type = DbQueryInsert;
Dictionary::Ptr fields1 = make_shared<Dictionary>();
fields1->Set("identity", endpoint->GetName());
fields1->Set("node", IcingaApplication::GetInstance()->GetNodeName());
fields1->Set("is_connected", EndpointIsConnected(endpoint));
fields1->Set("status_update_time", DbValue::FromTimestamp(Utility::GetTime()));
fields1->Set("endpoint_object_id", endpoint);
fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
query1.Fields = fields1;
OnQuery(query1);
}

View File

@ -44,8 +44,13 @@ public:
virtual Dictionary::Ptr GetConfigFields(void) const;
virtual Dictionary::Ptr GetStatusFields(void) const;
protected:
virtual void OnConfigUpdate(void);
private:
static void UpdateConnectedStatus(const Endpoint::Ptr& endpoint);
static void UpdateDisconnectedStatus(const Endpoint::Ptr& endpoint);
static void UpdateConnectedStatusInternal(const Endpoint::Ptr& endpoint, bool connected);
static int EndpointIsConnected(const Endpoint::Ptr& endpoint);
};

View File

@ -25,6 +25,7 @@
#include "base/initialize.h"
#include "base/dynamictype.h"
#include "base/utility.h"
#include "remote/endpoint.h"
#include "icinga/notification.h"
#include "icinga/checkcommand.h"
#include "icinga/eventcommand.h"
@ -398,6 +399,12 @@ void ServiceDbObject::AddCommentByType(const DynamicObject::Ptr& object, const C
fields1->Set("expiration_time", DbValue::FromTimestamp(comment->GetExpireTime()));
fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
String node = IcingaApplication::GetInstance()->GetNodeName();
Endpoint::Ptr endpoint = Endpoint::GetByName(node);
if (endpoint)
fields1->Set("endpoint_object_id", endpoint);
DbQuery query1;
if (!historical) {
query1.Table = "comments";
@ -563,6 +570,12 @@ void ServiceDbObject::AddDowntimeByType(const DynamicObject::Ptr& object, const
fields1->Set("trigger_time", DbValue::FromTimestamp(downtime->GetTriggerTime()));
fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
String node = IcingaApplication::GetInstance()->GetNodeName();
Endpoint::Ptr endpoint = Endpoint::GetByName(node);
if (endpoint)
fields1->Set("endpoint_object_id", endpoint);
DbQuery query1;
if (!historical) {
query1.Table = "scheduleddowntime";
@ -746,6 +759,12 @@ void ServiceDbObject::AddAcknowledgementHistory(const Service::Ptr& service, con
fields1->Set("end_time", DbValue::FromTimestamp(end_time));
fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
String node = IcingaApplication::GetInstance()->GetNodeName();
Endpoint::Ptr endpoint = Endpoint::GetByName(node);
if (endpoint)
fields1->Set("endpoint_object_id", endpoint);
query1.Fields = fields1;
OnQuery(query1);
@ -796,6 +815,12 @@ void ServiceDbObject::AddNotificationHistory(const Notification::Ptr& notificati
fields1->Set("contacts_notified", static_cast<long>(users.size()));
fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
String node = IcingaApplication::GetInstance()->GetNodeName();
Endpoint::Ptr endpoint = Endpoint::GetByName(node);
if (endpoint)
fields1->Set("endpoint_object_id", endpoint);
query1.Fields = fields1;
OnQuery(query1);
@ -867,6 +892,12 @@ void ServiceDbObject::AddStateChangeHistory(const Service::Ptr& service, const C
fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
String node = IcingaApplication::GetInstance()->GetNodeName();
Endpoint::Ptr endpoint = Endpoint::GetByName(node);
if (endpoint)
fields1->Set("endpoint_object_id", endpoint);
query1.Fields = fields1;
OnQuery(query1);
@ -1170,6 +1201,12 @@ void ServiceDbObject::AddLogHistory(const Service::Ptr& service, String buffer,
fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
String node = IcingaApplication::GetInstance()->GetNodeName();
Endpoint::Ptr endpoint = Endpoint::GetByName(node);
if (endpoint)
fields1->Set("endpoint_object_id", endpoint);
query1.Fields = fields1;
OnQuery(query1);
@ -1226,6 +1263,12 @@ void ServiceDbObject::AddFlappingHistory(const Service::Ptr& service, FlappingSt
fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
String node = IcingaApplication::GetInstance()->GetNodeName();
Endpoint::Ptr endpoint = Endpoint::GetByName(node);
if (endpoint)
fields1->Set("endpoint_object_id", endpoint);
query1.Fields = fields1;
OnQuery(query1);
@ -1285,6 +1328,12 @@ void ServiceDbObject::AddServiceCheckHistory(const Service::Ptr& service, const
fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
fields1->Set("service_object_id", service);
String node = IcingaApplication::GetInstance()->GetNodeName();
Endpoint::Ptr endpoint = Endpoint::GetByName(node);
if (endpoint)
fields1->Set("endpoint_object_id", endpoint);
query1.Fields = fields1;
OnQuery(query1);
@ -1331,6 +1380,12 @@ void ServiceDbObject::AddEventHandlerHistory(const Service::Ptr& service)
fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
String node = IcingaApplication::GetInstance()->GetNodeName();
Endpoint::Ptr endpoint = Endpoint::GetByName(node);
if (endpoint)
fields1->Set("endpoint_object_id", endpoint);
query1.Fields = fields1;
OnQuery(query1);
@ -1364,6 +1419,12 @@ void ServiceDbObject::AddExternalCommandHistory(double time, const String& comma
fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
String node = IcingaApplication::GetInstance()->GetNodeName();
Endpoint::Ptr endpoint = Endpoint::GetByName(node);
if (endpoint)
fields1->Set("endpoint_object_id", endpoint);
query1.Fields = fields1;
OnQuery(query1);
}