From: Michael Friedrich Date: Thu, 1 Aug 2013 11:20:30 +0000 (+0200) Subject: ido: Implement commanddbobjects. X-Git-Tag: v0.0.3~761 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f1b7dbdca43583a35af7cfc36ede52d9b7dd318d;p=icinga2 ido: Implement commanddbobjects. --- diff --git a/lib/icinga/compatutility.cpp b/lib/icinga/compatutility.cpp index 62c932c35..303f87694 100644 --- a/lib/icinga/compatutility.cpp +++ b/lib/icinga/compatutility.cpp @@ -349,6 +349,33 @@ Dictionary::Ptr CompatUtility::GetServiceConfigAttributes(const Service::Ptr& se } +Dictionary::Ptr CompatUtility::GetCommandConfigAttributes(const Command::Ptr& command) +{ + Dictionary::Ptr attr = boost::make_shared(); + + Value commandLine = command->GetCommandLine(); + + String commandline; + if (commandLine.IsObjectType()) { + Array::Ptr args = commandLine; + + ObjectLock olock(args); + String arg; + BOOST_FOREACH(arg, args) { + // This is obviously incorrect for non-trivial cases. + commandline = " \"" + CompatUtility::EscapeString(arg) + "\""; + } + } else if (!commandLine.IsEmpty()) { + commandline = CompatUtility::EscapeString(Convert::ToString(commandLine)); + } else { + commandline = ""; + } + + attr->Set("command_line", commandline); + + return attr; +} + String CompatUtility::EscapeString(const String& str) { String result = str; diff --git a/lib/icinga/compatutility.h b/lib/icinga/compatutility.h index 93188cf0c..96d495917 100644 --- a/lib/icinga/compatutility.h +++ b/lib/icinga/compatutility.h @@ -22,6 +22,7 @@ #include "icinga/i2-icinga.h" #include "icinga/service.h" +#include "icinga/checkcommand.h" #include "base/dictionary.h" #include @@ -48,6 +49,7 @@ public: static Dictionary::Ptr GetServiceStatusAttributes(const Service::Ptr& service, CompatObjectType type); static Dictionary::Ptr GetServiceConfigAttributes(const Service::Ptr& service, CompatObjectType type); + static Dictionary::Ptr GetCommandConfigAttributes(const Command::Ptr& command); static String EscapeString(const String& str); private: diff --git a/lib/ido/Makefile.am b/lib/ido/Makefile.am index 8bb0b6312..e26f8d611 100644 --- a/lib/ido/Makefile.am +++ b/lib/ido/Makefile.am @@ -10,6 +10,8 @@ EXTRA_DIST = \ $(top_builddir)/tools/mkembedconfig/mkembedconfig $< $@ libido_la_SOURCES = \ + commanddbobject.cpp \ + commanddbobject.h \ dbconnection.cpp \ dbconnection.h \ dbobject.cpp \ diff --git a/lib/ido/commanddbobject.cpp b/lib/ido/commanddbobject.cpp new file mode 100644 index 000000000..f7adba737 --- /dev/null +++ b/lib/ido/commanddbobject.cpp @@ -0,0 +1,57 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) * + * * + * 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. * + ******************************************************************************/ + +#include "ido/commanddbobject.h" +#include "ido/dbtype.h" +#include "ido/dbvalue.h" +#include "icinga/command.h" +#include "icinga/compatutility.h" +#include "base/objectlock.h" +#include + +using namespace icinga; + +REGISTER_DBTYPE(CheckCommand, "command", 12, CommandDbObject); +REGISTER_DBTYPE(EventCommand, "command", 12, CommandDbObject); +REGISTER_DBTYPE(NotificationCommand, "command", 12, CommandDbObject); + +CommandDbObject::CommandDbObject(const DbType::Ptr& type, const String& name1, const String& name2) + : DbObject(type, name1, name2) +{ } + +Dictionary::Ptr CommandDbObject::GetConfigFields(void) const +{ + Dictionary::Ptr fields = boost::make_shared(); + Command::Ptr command = static_pointer_cast(GetObject()); + + Dictionary::Ptr attrs; + + attrs = CompatUtility::GetCommandConfigAttributes(command); + + Log(LogDebug, "ido", "get command"); + + fields->Set("command_line", attrs->Get("command_line")); + + return fields; +} + +Dictionary::Ptr CommandDbObject::GetStatusFields(void) const +{ + return Empty; +} diff --git a/lib/ido/commanddbobject.h b/lib/ido/commanddbobject.h new file mode 100644 index 000000000..bca6c43b1 --- /dev/null +++ b/lib/ido/commanddbobject.h @@ -0,0 +1,47 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) * + * * + * 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. * + ******************************************************************************/ + +#ifndef COMMANDDBOBJECT_H +#define COMMANDDBOBJECT_H + +#include "ido/dbobject.h" +#include "base/dynamicobject.h" + +namespace icinga +{ + +/** + * A Command database object. + * + * @ingroup ido + */ +class CommandDbObject : public DbObject +{ +public: + DECLARE_PTR_TYPEDEFS(CommandDbObject); + + CommandDbObject(const boost::shared_ptr& type, const String& name1, const String& name2); + + virtual Dictionary::Ptr GetConfigFields(void) const; + virtual Dictionary::Ptr GetStatusFields(void) const; +}; + +} + +#endif /* COMMANDDBOBJECT_H */ diff --git a/lib/ido/dbobject.h b/lib/ido/dbobject.h index 69486e70f..446b98e7a 100644 --- a/lib/ido/dbobject.h +++ b/lib/ido/dbobject.h @@ -22,8 +22,8 @@ #include "ido/dbreference.h" #include "ido/dbquery.h" +#include "ido/dbtype.h" #include "base/dynamicobject.h" -#include namespace icinga { @@ -34,8 +34,6 @@ enum DbObjectUpdateType DbObjectRemoved }; -class DbType; - /** * A database object. * diff --git a/lib/ido/dbtype.cpp b/lib/ido/dbtype.cpp index 112fa26a8..3136f39b2 100644 --- a/lib/ido/dbtype.cpp +++ b/lib/ido/dbtype.cpp @@ -88,7 +88,7 @@ DbObject::Ptr DbType::GetOrCreateObjectByName(const String& name1, const String& if (it != GetObjects().end()) return it->second; - DbObject::Ptr dbobj = m_ObjectFactory(name1, name2); + DbObject::Ptr dbobj = m_ObjectFactory(GetSelf(), name1, name2); GetObjects()[std::make_pair(name1, name2)] = dbobj; return dbobj; diff --git a/lib/ido/dbtype.h b/lib/ido/dbtype.h index 005d06f08..a5cbafd76 100644 --- a/lib/ido/dbtype.h +++ b/lib/ido/dbtype.h @@ -22,11 +22,13 @@ #include "base/object.h" #include "base/registry.h" -#include "ido/dbobject.h" +#include namespace icinga { +class DbObject; + /** * A database object type. * @@ -37,9 +39,9 @@ class DbType : public Object public: DECLARE_PTR_TYPEDEFS(DbType); - typedef boost::function ObjectFactory; + typedef boost::function (const boost::shared_ptr&, const String&, const String&)> ObjectFactory; typedef std::map TypeMap; - typedef std::map, DbObject::Ptr, pair_string_iless> ObjectMap; + typedef std::map, boost::shared_ptr, pair_string_iless> ObjectMap; DbType(const String& name, const String& table, long tid, const ObjectFactory& factory); @@ -52,7 +54,7 @@ public: static DbType::Ptr GetByName(const String& name); static DbType::Ptr GetByID(long tid); - DbObject::Ptr GetOrCreateObjectByName(const String& name1, const String& name2); + boost::shared_ptr GetOrCreateObjectByName(const String& name1, const String& name2); private: String m_Name; @@ -95,13 +97,13 @@ public: * @ingroup ido */ template -shared_ptr DbObjectFactory(const String& name1, const String& name2) +shared_ptr DbObjectFactory(const DbType::Ptr& type, const String& name1, const String& name2) { - return boost::make_shared(name1, name2); + return boost::make_shared(type, name1, name2); } #define REGISTER_DBTYPE(name, table, tid, type) \ - I2_EXPORT icinga::RegisterDbTypeHelper g_RegisterDBT_ ## type(name, table, tid, DbObjectFactory); + I2_EXPORT icinga::RegisterDbTypeHelper g_RegisterDBT_ ## name(#name, table, tid, DbObjectFactory); } diff --git a/lib/ido/hostdbobject.cpp b/lib/ido/hostdbobject.cpp index ced211f57..636ace097 100644 --- a/lib/ido/hostdbobject.cpp +++ b/lib/ido/hostdbobject.cpp @@ -31,10 +31,10 @@ using namespace icinga; -REGISTER_DBTYPE("Host", "host", 1, HostDbObject); +REGISTER_DBTYPE(Host, "host", 1, HostDbObject); -HostDbObject::HostDbObject(const String& name1, const String& name2) - : DbObject(DbType::GetByName("Host"), name1, name2) +HostDbObject::HostDbObject(const DbType::Ptr& type, const String& name1, const String& name2) + : DbObject(type, name1, name2) { } Dictionary::Ptr HostDbObject::GetConfigFields(void) const diff --git a/lib/ido/hostdbobject.h b/lib/ido/hostdbobject.h index 0a58aec1e..e7370ba43 100644 --- a/lib/ido/hostdbobject.h +++ b/lib/ido/hostdbobject.h @@ -36,7 +36,7 @@ class HostDbObject : public DbObject public: DECLARE_PTR_TYPEDEFS(HostDbObject); - HostDbObject(const String& name1, const String& name2); + HostDbObject(const DbType::Ptr& type, const String& name1, const String& name2); virtual Dictionary::Ptr GetConfigFields(void) const; virtual Dictionary::Ptr GetStatusFields(void) const; diff --git a/lib/ido/hostgroupdbobject.cpp b/lib/ido/hostgroupdbobject.cpp index cf9e114d3..be52a08a9 100644 --- a/lib/ido/hostgroupdbobject.cpp +++ b/lib/ido/hostgroupdbobject.cpp @@ -26,10 +26,10 @@ using namespace icinga; -REGISTER_DBTYPE("HostGroup", "hostgroup", 3, HostGroupDbObject); +REGISTER_DBTYPE(HostGroup, "hostgroup", 3, HostGroupDbObject); -HostGroupDbObject::HostGroupDbObject(const String& name1, const String& name2) - : DbObject(DbType::GetByName("HostGroup"), name1, name2) +HostGroupDbObject::HostGroupDbObject(const DbType::Ptr& type, const String& name1, const String& name2) + : DbObject(type, name1, name2) { } Dictionary::Ptr HostGroupDbObject::GetConfigFields(void) const diff --git a/lib/ido/hostgroupdbobject.h b/lib/ido/hostgroupdbobject.h index 48c43d53d..c9ce9a7ee 100644 --- a/lib/ido/hostgroupdbobject.h +++ b/lib/ido/hostgroupdbobject.h @@ -36,7 +36,7 @@ class HostGroupDbObject : public DbObject public: DECLARE_PTR_TYPEDEFS(HostGroupDbObject); - HostGroupDbObject(const String& name1, const String& name2); + HostGroupDbObject(const DbType::Ptr& type, const String& name1, const String& name2); virtual Dictionary::Ptr GetConfigFields(void) const; virtual Dictionary::Ptr GetStatusFields(void) const; diff --git a/lib/ido/servicedbobject.cpp b/lib/ido/servicedbobject.cpp index 595b8dc09..a68324352 100644 --- a/lib/ido/servicedbobject.cpp +++ b/lib/ido/servicedbobject.cpp @@ -29,10 +29,10 @@ using namespace icinga; -REGISTER_DBTYPE("Service", "service", 2, ServiceDbObject); +REGISTER_DBTYPE(Service, "service", 2, ServiceDbObject); -ServiceDbObject::ServiceDbObject(const String& name1, const String& name2) - : DbObject(DbType::GetByName("Service"), name1, name2) +ServiceDbObject::ServiceDbObject(const DbType::Ptr& type, const String& name1, const String& name2) + : DbObject(type, name1, name2) { } Dictionary::Ptr ServiceDbObject::GetConfigFields(void) const diff --git a/lib/ido/servicedbobject.h b/lib/ido/servicedbobject.h index 7346e1afa..a00f75613 100644 --- a/lib/ido/servicedbobject.h +++ b/lib/ido/servicedbobject.h @@ -36,7 +36,7 @@ class ServiceDbObject : public DbObject public: DECLARE_PTR_TYPEDEFS(ServiceDbObject); - ServiceDbObject(const String& name1, const String& name2); + ServiceDbObject(const DbType::Ptr& type, const String& name1, const String& name2); virtual Dictionary::Ptr GetConfigFields(void) const; virtual Dictionary::Ptr GetStatusFields(void) const; diff --git a/lib/ido/servicegroupdbobject.cpp b/lib/ido/servicegroupdbobject.cpp index 9a7ec5a3a..0155d5714 100644 --- a/lib/ido/servicegroupdbobject.cpp +++ b/lib/ido/servicegroupdbobject.cpp @@ -26,10 +26,10 @@ using namespace icinga; -REGISTER_DBTYPE("ServiceGroup", "servicegroup", 4, ServiceGroupDbObject); +REGISTER_DBTYPE(ServiceGroup, "servicegroup", 4, ServiceGroupDbObject); -ServiceGroupDbObject::ServiceGroupDbObject(const String& name1, const String& name2) - : DbObject(DbType::GetByName("ServiceGroup"), name1, name2) +ServiceGroupDbObject::ServiceGroupDbObject(const DbType::Ptr& type, const String& name1, const String& name2) + : DbObject(type, name1, name2) { } Dictionary::Ptr ServiceGroupDbObject::GetConfigFields(void) const diff --git a/lib/ido/servicegroupdbobject.h b/lib/ido/servicegroupdbobject.h index a85247b25..aa99917c0 100644 --- a/lib/ido/servicegroupdbobject.h +++ b/lib/ido/servicegroupdbobject.h @@ -36,7 +36,7 @@ class ServiceGroupDbObject : public DbObject public: DECLARE_PTR_TYPEDEFS(ServiceGroupDbObject); - ServiceGroupDbObject(const String& name1, const String& name2); + ServiceGroupDbObject(const DbType::Ptr& type, const String& name1, const String& name2); virtual Dictionary::Ptr GetConfigFields(void) const; virtual Dictionary::Ptr GetStatusFields(void) const; diff --git a/lib/ido/timeperioddbobject.cpp b/lib/ido/timeperioddbobject.cpp index 59b5c0706..c5f2beb09 100644 --- a/lib/ido/timeperioddbobject.cpp +++ b/lib/ido/timeperioddbobject.cpp @@ -26,10 +26,10 @@ using namespace icinga; -REGISTER_DBTYPE("TimePeriod", "timeperiod", 9, TimePeriodDbObject); +REGISTER_DBTYPE(TimePeriod, "timeperiod", 9, TimePeriodDbObject); -TimePeriodDbObject::TimePeriodDbObject(const String& name1, const String& name2) - : DbObject(DbType::GetByName("TimePeriod"), name1, name2) +TimePeriodDbObject::TimePeriodDbObject(const DbType::Ptr& type, const String& name1, const String& name2) + : DbObject(type, name1, name2) { } Dictionary::Ptr TimePeriodDbObject::GetConfigFields(void) const diff --git a/lib/ido/timeperioddbobject.h b/lib/ido/timeperioddbobject.h index f4707da47..dc9f0d1c1 100644 --- a/lib/ido/timeperioddbobject.h +++ b/lib/ido/timeperioddbobject.h @@ -36,7 +36,7 @@ class TimePeriodDbObject : public DbObject public: DECLARE_PTR_TYPEDEFS(TimePeriodDbObject); - TimePeriodDbObject(const String& name1, const String& name2); + TimePeriodDbObject(const DbType::Ptr& type, const String& name1, const String& name2); virtual Dictionary::Ptr GetConfigFields(void) const; virtual Dictionary::Ptr GetStatusFields(void) const; diff --git a/lib/ido/userdbobject.cpp b/lib/ido/userdbobject.cpp index bde9883ec..8b5571be6 100644 --- a/lib/ido/userdbobject.cpp +++ b/lib/ido/userdbobject.cpp @@ -26,10 +26,10 @@ using namespace icinga; -REGISTER_DBTYPE("User", "contact", 10, UserDbObject); +REGISTER_DBTYPE(User, "contact", 10, UserDbObject); -UserDbObject::UserDbObject(const String& name1, const String& name2) - : DbObject(DbType::GetByName("User"), name1, name2) +UserDbObject::UserDbObject(const DbType::Ptr& type, const String& name1, const String& name2) + : DbObject(type, name1, name2) { } Dictionary::Ptr UserDbObject::GetConfigFields(void) const diff --git a/lib/ido/userdbobject.h b/lib/ido/userdbobject.h index c8eaf0c2c..e3e9a3ba5 100644 --- a/lib/ido/userdbobject.h +++ b/lib/ido/userdbobject.h @@ -36,7 +36,7 @@ class UserDbObject : public DbObject public: DECLARE_PTR_TYPEDEFS(UserDbObject); - UserDbObject(const String& name1, const String& name2); + UserDbObject(const DbType::Ptr& type, const String& name1, const String& name2); virtual Dictionary::Ptr GetConfigFields(void) const; virtual Dictionary::Ptr GetStatusFields(void) const; diff --git a/lib/ido/usergroupdbobject.cpp b/lib/ido/usergroupdbobject.cpp index 6a4bbf1fc..a512944e3 100644 --- a/lib/ido/usergroupdbobject.cpp +++ b/lib/ido/usergroupdbobject.cpp @@ -26,10 +26,10 @@ using namespace icinga; -REGISTER_DBTYPE("UserGroup", "contactgroup", 11, UserGroupDbObject); +REGISTER_DBTYPE(UserGroup, "contactgroup", 11, UserGroupDbObject); -UserGroupDbObject::UserGroupDbObject(const String& name1, const String& name2) - : DbObject(DbType::GetByName("UserGroup"), name1, name2) +UserGroupDbObject::UserGroupDbObject(const DbType::Ptr& type, const String& name1, const String& name2) + : DbObject(type, name1, name2) { } Dictionary::Ptr UserGroupDbObject::GetConfigFields(void) const diff --git a/lib/ido/usergroupdbobject.h b/lib/ido/usergroupdbobject.h index 21288ff6c..84123eb1b 100644 --- a/lib/ido/usergroupdbobject.h +++ b/lib/ido/usergroupdbobject.h @@ -36,7 +36,7 @@ class UserGroupDbObject : public DbObject public: DECLARE_PTR_TYPEDEFS(UserGroupDbObject); - UserGroupDbObject(const String& name1, const String& name2); + UserGroupDbObject(const DbType::Ptr& type, const String& name1, const String& name2); virtual Dictionary::Ptr GetConfigFields(void) const; virtual Dictionary::Ptr GetStatusFields(void) const;