]> granicus.if.org Git - icinga2/commitdiff
ido: Add host/service customvariables.
authorMichael Friedrich <michael.friedrich@netways.de>
Fri, 9 Aug 2013 11:55:50 +0000 (13:55 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Fri, 9 Aug 2013 11:55:50 +0000 (13:55 +0200)
refs #4378

lib/ido/hostdbobject.cpp
lib/ido/servicedbobject.cpp

index a245341aa64c8eaab6b3798e6c5ba52de9beb88b..164ed52f32388262cd4850bc364f551b8e080093 100644 (file)
 #include "icinga/checkcommand.h"
 #include "icinga/eventcommand.h"
 #include "icinga/compatutility.h"
+#include "base/convert.h"
 #include "base/objectlock.h"
 #include <boost/foreach.hpp>
+#include <boost/tuple/tuple.hpp>
 
 using namespace icinga;
 
@@ -191,7 +193,7 @@ void HostDbObject::OnConfigUpdate(void)
 {
        Host::Ptr host = static_pointer_cast<Host>(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<Dictionary>();
+       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<Dictionary>();
+                       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)
index cba8fefcaf0e00b92bf6ae0d2c95cf72afb28340..42eca6cc390a9fdeb1c51e8c35dfb6555f6691a0 100644 (file)
@@ -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<Dictionary>();
+       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<Dictionary>();
+                       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);