2013-07-05 09:37:04 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2013-07-05 09:37:04 +02: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/dbobject.hpp"
|
|
|
|
#include "db_ido/dbtype.hpp"
|
|
|
|
#include "db_ido/dbvalue.hpp"
|
|
|
|
#include "icinga/customvarobject.hpp"
|
|
|
|
#include "icinga/service.hpp"
|
|
|
|
#include "icinga/compatutility.hpp"
|
|
|
|
#include "remote/endpoint.hpp"
|
|
|
|
#include "base/dynamicobject.hpp"
|
|
|
|
#include "base/dynamictype.hpp"
|
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/initialize.hpp"
|
|
|
|
#include "base/logger_fwd.hpp"
|
2013-07-05 09:37:04 +02:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-07-22 12:08:49 +02:00
|
|
|
boost::signals2::signal<void (const DbQuery&)> DbObject::OnQuery;
|
2013-07-05 09:37:04 +02:00
|
|
|
|
2013-11-08 21:12:47 +01:00
|
|
|
INITIALIZE_ONCE(&DbObject::StaticInitialize);
|
2013-08-01 11:07:26 +02:00
|
|
|
|
2013-07-17 11:29:51 +02:00
|
|
|
DbObject::DbObject(const shared_ptr<DbType>& type, const String& name1, const String& name2)
|
2013-07-24 10:55:04 +02:00
|
|
|
: m_Name1(name1), m_Name2(name2), m_Type(type), m_LastConfigUpdate(0), m_LastStatusUpdate(0)
|
2013-07-05 09:37:04 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
void DbObject::StaticInitialize(void)
|
|
|
|
{
|
2014-03-09 18:06:24 +01:00
|
|
|
/* triggered in ProcessCheckResult(), requires UpdateNextCheck() to be called before */
|
2014-03-20 15:41:45 +01:00
|
|
|
DynamicObject::OnStateChanged.connect(boost::bind(&DbObject::StateChangedHandler, _1));
|
2014-05-12 16:45:25 +02:00
|
|
|
CustomVarObject::OnVarsChanged.connect(boost::bind(&DbObject::VarsChangedHandler, _1));
|
2013-07-05 09:37:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DbObject::SetObject(const DynamicObject::Ptr& object)
|
|
|
|
{
|
|
|
|
m_Object = object;
|
|
|
|
}
|
|
|
|
|
|
|
|
DynamicObject::Ptr DbObject::GetObject(void) const
|
|
|
|
{
|
|
|
|
return m_Object;
|
|
|
|
}
|
|
|
|
|
|
|
|
String DbObject::GetName1(void) const
|
|
|
|
{
|
|
|
|
return m_Name1;
|
|
|
|
}
|
|
|
|
|
|
|
|
String DbObject::GetName2(void) const
|
|
|
|
{
|
|
|
|
return m_Name2;
|
|
|
|
}
|
|
|
|
|
2013-07-17 11:29:51 +02:00
|
|
|
DbType::Ptr DbObject::GetType(void) const
|
|
|
|
{
|
|
|
|
return m_Type;
|
|
|
|
}
|
|
|
|
|
2013-07-22 12:08:49 +02:00
|
|
|
void DbObject::SendConfigUpdate(void)
|
2013-07-17 11:29:51 +02:00
|
|
|
{
|
2014-04-15 17:33:56 +02:00
|
|
|
/* update custom var config for all objects */
|
|
|
|
SendVarsConfigUpdate();
|
|
|
|
|
|
|
|
/* config objects */
|
2013-08-01 09:32:14 +02:00
|
|
|
Dictionary::Ptr fields = GetConfigFields();
|
|
|
|
|
|
|
|
if (!fields)
|
|
|
|
return;
|
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
DbQuery query;
|
|
|
|
query.Table = GetType()->GetTable() + "s";
|
|
|
|
query.Type = DbQueryInsert | DbQueryUpdate;
|
2013-11-05 12:40:25 +01:00
|
|
|
query.Category = DbCatConfig;
|
2013-08-02 08:56:36 +02:00
|
|
|
query.Fields = fields;
|
|
|
|
query.Fields->Set(GetType()->GetIDColumn(), GetObject());
|
|
|
|
query.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
|
|
|
|
query.Fields->Set("config_type", 1);
|
2013-11-06 08:51:56 +01:00
|
|
|
query.WhereCriteria = make_shared<Dictionary>();
|
2013-08-02 08:56:36 +02:00
|
|
|
query.WhereCriteria->Set(GetType()->GetIDColumn(), GetObject());
|
|
|
|
query.Object = GetSelf();
|
2013-08-02 15:45:50 +02:00
|
|
|
query.ConfigUpdate = true;
|
2013-08-02 08:56:36 +02:00
|
|
|
OnQuery(query);
|
2013-07-24 10:55:04 +02:00
|
|
|
|
|
|
|
m_LastConfigUpdate = Utility::GetTime();
|
2013-07-25 08:30:02 +02:00
|
|
|
|
|
|
|
OnConfigUpdate();
|
2013-07-22 12:08:49 +02:00
|
|
|
}
|
|
|
|
|
2014-03-20 15:41:45 +01:00
|
|
|
void DbObject::SendStatusUpdate(void)
|
2013-07-22 12:08:49 +02:00
|
|
|
{
|
2014-04-15 17:33:56 +02:00
|
|
|
/* update custom var status for all objects */
|
|
|
|
SendVarsStatusUpdate();
|
|
|
|
|
|
|
|
/* status objects */
|
2013-08-01 09:32:14 +02:00
|
|
|
Dictionary::Ptr fields = GetStatusFields();
|
|
|
|
|
|
|
|
if (!fields)
|
|
|
|
return;
|
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
DbQuery query;
|
|
|
|
query.Table = GetType()->GetTable() + "status";
|
|
|
|
query.Type = DbQueryInsert | DbQueryUpdate;
|
2013-10-29 15:54:43 +01:00
|
|
|
query.Category = DbCatState;
|
2013-08-02 08:56:36 +02:00
|
|
|
query.Fields = fields;
|
|
|
|
query.Fields->Set(GetType()->GetIDColumn(), GetObject());
|
2014-02-21 17:26:56 +01:00
|
|
|
|
2014-03-20 17:40:27 +01:00
|
|
|
/* do not override our own endpoint dbobject id */
|
|
|
|
if (GetType()->GetTable() != "endpoint") {
|
|
|
|
String node = IcingaApplication::GetInstance()->GetNodeName();
|
|
|
|
|
2014-05-28 14:34:26 +02:00
|
|
|
Log(LogDebug, "DbObject", "Endpoint node: '" + node + "' status update for '" + GetObject()->GetName() + "'");
|
2014-03-20 15:41:45 +01:00
|
|
|
|
2014-03-20 17:40:27 +01:00
|
|
|
Endpoint::Ptr endpoint = Endpoint::GetByName(node);
|
|
|
|
if (endpoint)
|
|
|
|
query.Fields->Set("endpoint_object_id", endpoint);
|
|
|
|
}
|
2014-03-20 15:41:45 +01:00
|
|
|
|
2014-03-20 17:40:27 +01:00
|
|
|
query.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
|
2014-02-21 17:26:56 +01:00
|
|
|
|
2013-08-02 08:56:36 +02:00
|
|
|
query.Fields->Set("status_update_time", DbValue::FromTimestamp(Utility::GetTime()));
|
2013-11-06 08:51:56 +01:00
|
|
|
query.WhereCriteria = make_shared<Dictionary>();
|
2013-08-02 08:56:36 +02:00
|
|
|
query.WhereCriteria->Set(GetType()->GetIDColumn(), GetObject());
|
2013-08-02 09:57:45 +02:00
|
|
|
query.Object = GetSelf();
|
2013-08-02 15:45:50 +02:00
|
|
|
query.StatusUpdate = true;
|
2013-08-02 08:56:36 +02:00
|
|
|
OnQuery(query);
|
2013-07-24 10:55:04 +02:00
|
|
|
|
|
|
|
m_LastStatusUpdate = Utility::GetTime();
|
2013-07-25 08:30:02 +02:00
|
|
|
|
|
|
|
OnStatusUpdate();
|
2013-07-24 10:55:04 +02:00
|
|
|
}
|
|
|
|
|
2014-04-15 17:33:56 +02:00
|
|
|
void DbObject::SendVarsConfigUpdate(void)
|
|
|
|
{
|
|
|
|
DynamicObject::Ptr obj = GetObject();
|
|
|
|
|
2014-05-12 16:45:25 +02:00
|
|
|
CustomVarObject::Ptr custom_var_object = dynamic_pointer_cast<CustomVarObject>(obj);
|
|
|
|
|
|
|
|
if (!custom_var_object)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Dictionary::Ptr vars = CompatUtility::GetCustomAttributeConfig(custom_var_object);
|
2014-04-15 17:33:56 +02:00
|
|
|
|
|
|
|
if (vars) {
|
2014-05-28 14:34:26 +02:00
|
|
|
Log(LogDebug, "DbObject", "Updating object vars for '" + custom_var_object->GetName() + "'");
|
2014-04-15 17:33:56 +02:00
|
|
|
|
|
|
|
ObjectLock olock (vars);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, vars) {
|
|
|
|
if (!kv.first.IsEmpty()) {
|
2014-05-12 16:45:25 +02:00
|
|
|
int overridden = custom_var_object->IsVarOverridden(kv.first) ? 1 : 0;
|
2014-04-16 17:48:33 +02:00
|
|
|
|
2014-05-28 14:34:26 +02:00
|
|
|
Log(LogDebug, "DbObject", "object customvar key: '" + kv.first + "' value: '" + Convert::ToString(kv.second) +
|
2014-04-16 17:48:33 +02:00
|
|
|
"' overridden: " + Convert::ToString(overridden));
|
2014-04-15 17:33:56 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr fields = make_shared<Dictionary>();
|
|
|
|
fields->Set("varname", Convert::ToString(kv.first));
|
|
|
|
fields->Set("varvalue", Convert::ToString(kv.second));
|
|
|
|
fields->Set("config_type", 1);
|
2014-04-16 17:48:33 +02:00
|
|
|
fields->Set("has_been_modified", overridden);
|
2014-04-15 17:33:56 +02:00
|
|
|
fields->Set("object_id", obj);
|
|
|
|
fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
|
|
|
|
|
|
|
|
DbQuery query;
|
|
|
|
query.Table = "customvariables";
|
|
|
|
query.Type = DbQueryInsert;
|
|
|
|
query.Category = DbCatConfig;
|
|
|
|
query.Fields = fields;
|
|
|
|
OnQuery(query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DbObject::SendVarsStatusUpdate(void)
|
|
|
|
{
|
|
|
|
DynamicObject::Ptr obj = GetObject();
|
|
|
|
|
2014-05-12 16:45:25 +02:00
|
|
|
CustomVarObject::Ptr custom_var_object = dynamic_pointer_cast<CustomVarObject>(obj);
|
|
|
|
|
|
|
|
if (!custom_var_object)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Dictionary::Ptr vars = CompatUtility::GetCustomAttributeConfig(custom_var_object);
|
2014-04-15 17:33:56 +02:00
|
|
|
|
|
|
|
if (vars) {
|
2014-05-28 14:34:26 +02:00
|
|
|
Log(LogDebug, "DbObject", "Updating object vars for '" + custom_var_object->GetName() + "'");
|
2014-04-15 17:33:56 +02:00
|
|
|
|
|
|
|
ObjectLock olock (vars);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, vars) {
|
|
|
|
if (!kv.first.IsEmpty()) {
|
2014-05-12 16:45:25 +02:00
|
|
|
int overridden = custom_var_object->IsVarOverridden(kv.first) ? 1 : 0;
|
2014-04-15 17:33:56 +02:00
|
|
|
|
2014-05-28 14:34:26 +02:00
|
|
|
Log(LogDebug, "DbObject", "object customvar key: '" + kv.first + "' value: '" + Convert::ToString(kv.second) +
|
2014-04-15 17:33:56 +02:00
|
|
|
"' overridden: " + Convert::ToString(overridden));
|
|
|
|
|
|
|
|
Dictionary::Ptr fields = make_shared<Dictionary>();
|
|
|
|
fields->Set("varname", Convert::ToString(kv.first));
|
|
|
|
fields->Set("varvalue", Convert::ToString(kv.second));
|
|
|
|
fields->Set("has_been_modified", overridden);
|
|
|
|
fields->Set("status_update_time", DbValue::FromTimestamp(Utility::GetTime()));
|
|
|
|
fields->Set("object_id", obj);
|
|
|
|
fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
|
|
|
|
|
|
|
|
DbQuery query;
|
|
|
|
query.Table = "customvariablestatus";
|
|
|
|
query.Type = DbQueryInsert | DbQueryUpdate;
|
|
|
|
query.Category = DbCatState;
|
|
|
|
query.Fields = fields;
|
|
|
|
|
|
|
|
query.WhereCriteria = make_shared<Dictionary>();
|
|
|
|
query.WhereCriteria->Set("object_id", obj);
|
|
|
|
query.WhereCriteria->Set("varname", Convert::ToString(kv.first));
|
|
|
|
query.Object = GetSelf();
|
|
|
|
|
|
|
|
OnQuery(query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-24 10:55:04 +02:00
|
|
|
double DbObject::GetLastConfigUpdate(void) const
|
|
|
|
{
|
|
|
|
return m_LastConfigUpdate;
|
|
|
|
}
|
|
|
|
|
|
|
|
double DbObject::GetLastStatusUpdate(void) const
|
|
|
|
{
|
|
|
|
return m_LastStatusUpdate;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DbObject::IsStatusAttribute(const String&) const
|
|
|
|
{
|
|
|
|
return false;
|
2013-07-17 11:29:51 +02:00
|
|
|
}
|
|
|
|
|
2013-07-25 08:30:02 +02:00
|
|
|
void DbObject::OnConfigUpdate(void)
|
|
|
|
{
|
|
|
|
/* Default handler does nothing. */
|
|
|
|
}
|
|
|
|
|
|
|
|
void DbObject::OnStatusUpdate(void)
|
|
|
|
{
|
|
|
|
/* Default handler does nothing. */
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:37:04 +02:00
|
|
|
DbObject::Ptr DbObject::GetOrCreateByObject(const DynamicObject::Ptr& object)
|
|
|
|
{
|
|
|
|
DbObject::Ptr dbobj = static_pointer_cast<DbObject>(object->GetExtension("DbObject"));
|
|
|
|
|
|
|
|
if (dbobj)
|
|
|
|
return dbobj;
|
|
|
|
|
|
|
|
DbType::Ptr dbtype = DbType::GetByName(object->GetType()->GetName());
|
|
|
|
|
|
|
|
if (!dbtype)
|
|
|
|
return DbObject::Ptr();
|
|
|
|
|
2013-07-17 11:29:51 +02:00
|
|
|
Service::Ptr service;
|
|
|
|
String name1, name2;
|
|
|
|
|
|
|
|
service = dynamic_pointer_cast<Service>(object);
|
|
|
|
|
|
|
|
if (service) {
|
|
|
|
Host::Ptr host = service->GetHost();
|
|
|
|
|
|
|
|
name1 = service->GetHost()->GetName();
|
|
|
|
name2 = service->GetShortName();
|
|
|
|
} else {
|
2014-05-12 14:30:15 +02:00
|
|
|
if (object->GetType() == DynamicType::GetByName("CheckCommand") ||
|
|
|
|
object->GetType() == DynamicType::GetByName("EventCommand") ||
|
|
|
|
object->GetType() == DynamicType::GetByName("NotificationCommand")) {
|
|
|
|
Command::Ptr command = dynamic_pointer_cast<Command>(object);
|
|
|
|
name1 = CompatUtility::GetCommandName(command);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
name1 = object->GetName();
|
2013-07-17 11:29:51 +02:00
|
|
|
}
|
|
|
|
|
2013-07-24 10:55:04 +02:00
|
|
|
dbobj = dbtype->GetOrCreateObjectByName(name1, name2);
|
2013-07-05 09:37:04 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(object);
|
|
|
|
dbobj->SetObject(object);
|
|
|
|
object->SetExtension("DbObject", dbobj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbobj;
|
|
|
|
}
|
|
|
|
|
2014-03-20 15:41:45 +01:00
|
|
|
void DbObject::StateChangedHandler(const DynamicObject::Ptr& object)
|
2013-07-24 10:55:04 +02:00
|
|
|
{
|
|
|
|
DbObject::Ptr dbobj = GetOrCreateByObject(object);
|
|
|
|
|
|
|
|
if (!dbobj)
|
|
|
|
return;
|
|
|
|
|
2014-03-20 15:41:45 +01:00
|
|
|
dbobj->SendStatusUpdate();
|
2013-07-24 10:55:04 +02:00
|
|
|
}
|
2014-04-15 17:33:56 +02:00
|
|
|
|
2014-05-12 16:45:25 +02:00
|
|
|
void DbObject::VarsChangedHandler(const CustomVarObject::Ptr& object)
|
2014-04-15 17:33:56 +02:00
|
|
|
{
|
|
|
|
DbObject::Ptr dbobj = GetOrCreateByObject(object);
|
|
|
|
|
2014-05-28 14:34:26 +02:00
|
|
|
Log(LogDebug, "DbObject", "Vars changed for object '" + object->GetName() + "'");
|
2014-04-15 17:33:56 +02:00
|
|
|
|
|
|
|
if (!dbobj)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dbobj->SendVarsStatusUpdate();
|
|
|
|
}
|