From 62d0b6c30c0ee44750eace050d432ac7d9c9fd7d Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Fri, 9 Aug 2013 13:55:50 +0200 Subject: [PATCH] ido: Add host/service customvariables. refs #4378 --- lib/ido/hostdbobject.cpp | 44 ++++++++++++++++++++++++++++++++++++- lib/ido/servicedbobject.cpp | 41 ++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/lib/ido/hostdbobject.cpp b/lib/ido/hostdbobject.cpp index a245341aa..164ed52f3 100644 --- a/lib/ido/hostdbobject.cpp +++ b/lib/ido/hostdbobject.cpp @@ -26,8 +26,10 @@ #include "icinga/checkcommand.h" #include "icinga/eventcommand.h" #include "icinga/compatutility.h" +#include "base/convert.h" #include "base/objectlock.h" #include +#include using namespace icinga; @@ -191,7 +193,7 @@ void HostDbObject::OnConfigUpdate(void) { Host::Ptr host = static_pointer_cast(GetObject()); - /* safety delete */ + /* parents, host dependencies */ DbQuery query_del1; query_del1.Table = GetType()->GetTable() + "_parenthosts"; query_del1.Type = DbQueryDelete; @@ -233,6 +235,46 @@ void HostDbObject::OnConfigUpdate(void) query2.Fields = fields2; OnQuery(query2); } + + /* custom variables */ + Log(LogDebug, "ido", "host customvars for '" + host->GetName() + "'"); + + DbQuery query_del3; + query_del3.Table = "customvariables"; + query_del3.Type = DbQueryDelete; + query_del3.WhereCriteria = boost::make_shared(); + query_del3.WhereCriteria->Set("object_id", host); + OnQuery(query_del3); + + Dictionary::Ptr customvars; + { + ObjectLock olock(host); + customvars = CompatUtility::GetCustomVariableConfig(host); + } + + if (customvars) { + ObjectLock olock (customvars); + + String key; + Value value; + BOOST_FOREACH(boost::tie(key, value), customvars) { + Log(LogDebug, "ido", "host customvar key: '" + key + "' value: '" + Convert::ToString(value) + "'"); + + Dictionary::Ptr fields3 = boost::make_shared(); + fields3->Set("varname", Convert::ToString(key)); + fields3->Set("varvalue", Convert::ToString(value)); + fields3->Set("config_type", 1); + fields3->Set("has_been_modified", 0); + fields3->Set("object_id", host); + fields3->Set("instance_id", 0); /* DbConnection class fills in real ID */ + + DbQuery query3; + query3.Table = "customvariables"; + query3.Type = DbQueryInsert; + query3.Fields = fields3; + OnQuery(query3); + } + } } void HostDbObject::OnStatusUpdate(void) diff --git a/lib/ido/servicedbobject.cpp b/lib/ido/servicedbobject.cpp index cba8fefca..42eca6cc3 100644 --- a/lib/ido/servicedbobject.cpp +++ b/lib/ido/servicedbobject.cpp @@ -20,6 +20,7 @@ #include "ido/servicedbobject.h" #include "ido/dbtype.h" #include "ido/dbvalue.h" +#include "base/convert.h" #include "base/objectlock.h" #include "base/initialize.h" #include "base/dynamictype.h" @@ -204,6 +205,46 @@ void ServiceDbObject::OnConfigUpdate(void) OnQuery(query1); } + /* custom variables */ + Log(LogDebug, "ido", "service customvars for '" + service->GetName() + "'"); + + DbQuery query_del2; + query_del2.Table = "customvariables"; + query_del2.Type = DbQueryDelete; + query_del2.WhereCriteria = boost::make_shared(); + query_del2.WhereCriteria->Set("object_id", service); + OnQuery(query_del2); + + Dictionary::Ptr customvars; + { + ObjectLock olock(service); + customvars = CompatUtility::GetCustomVariableConfig(service); + } + + if (customvars) { + ObjectLock olock (customvars); + + String key; + Value value; + BOOST_FOREACH(boost::tie(key, value), customvars) { + Log(LogDebug, "ido", "service customvar key: '" + key + "' value: '" + Convert::ToString(value) + "'"); + + Dictionary::Ptr fields2 = boost::make_shared(); + fields2->Set("varname", Convert::ToString(key)); + fields2->Set("varvalue", Convert::ToString(value)); + fields2->Set("config_type", 1); + fields2->Set("has_been_modified", 0); + fields2->Set("object_id", service); + fields2->Set("instance_id", 0); /* DbConnection class fills in real ID */ + + DbQuery query2; + query2.Table = "customvariables"; + query2.Type = DbQueryInsert; + query2.Fields = fields2; + OnQuery(query2); + } + } + /* update comments */ //OnCommentsChanged(service, Empty, CommentChangedUpdated);