]> granicus.if.org Git - icinga2/commitdiff
ido: Implement commanddbobjects.
authorMichael Friedrich <michael.friedrich@netways.de>
Thu, 1 Aug 2013 11:20:30 +0000 (13:20 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Thu, 1 Aug 2013 12:14:54 +0000 (14:14 +0200)
22 files changed:
lib/icinga/compatutility.cpp
lib/icinga/compatutility.h
lib/ido/Makefile.am
lib/ido/commanddbobject.cpp [new file with mode: 0644]
lib/ido/commanddbobject.h [new file with mode: 0644]
lib/ido/dbobject.h
lib/ido/dbtype.cpp
lib/ido/dbtype.h
lib/ido/hostdbobject.cpp
lib/ido/hostdbobject.h
lib/ido/hostgroupdbobject.cpp
lib/ido/hostgroupdbobject.h
lib/ido/servicedbobject.cpp
lib/ido/servicedbobject.h
lib/ido/servicegroupdbobject.cpp
lib/ido/servicegroupdbobject.h
lib/ido/timeperioddbobject.cpp
lib/ido/timeperioddbobject.h
lib/ido/userdbobject.cpp
lib/ido/userdbobject.h
lib/ido/usergroupdbobject.cpp
lib/ido/usergroupdbobject.h

index 62c932c359d842fa1592cce8a321d1838a129594..303f8769410fbb7d7f7a372b3a0043ad27f57601 100644 (file)
@@ -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<Dictionary>();
+
+       Value commandLine = command->GetCommandLine();
+
+       String commandline;
+       if (commandLine.IsObjectType<Array>()) {
+               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 = "<internal>";
+       }
+
+       attr->Set("command_line", commandline);
+
+       return attr;
+}
+
 String CompatUtility::EscapeString(const String& str)
 {
        String result = str;
index 93188cf0c1cc02f61fff5bdf3573ccf8140f5a9a..96d495917bf1027495a6070a5f160a62fb9e3b85 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "icinga/i2-icinga.h"
 #include "icinga/service.h"
+#include "icinga/checkcommand.h"
 #include "base/dictionary.h"
 #include <vector>
 
@@ -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:
index 8bb0b6312df3b63d338759c0ec6919e05c5720d6..e26f8d6118537d96e37dfdc5cc7142ba5a0b506b 100644 (file)
@@ -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 (file)
index 0000000..f7adba7
--- /dev/null
@@ -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 <boost/foreach.hpp>
+
+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<Dictionary>();
+       Command::Ptr command = static_pointer_cast<Command>(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 (file)
index 0000000..bca6c43
--- /dev/null
@@ -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<DbType>& type, const String& name1, const String& name2);
+
+       virtual Dictionary::Ptr GetConfigFields(void) const;
+       virtual Dictionary::Ptr GetStatusFields(void) const;
+};
+
+}
+
+#endif /* COMMANDDBOBJECT_H */
index 69486e70f69d63b2a22b5411f59dfdf0110a911d..446b98e7a7342f2abfe22aa8fdabaa04d0b78cb5 100644 (file)
@@ -22,8 +22,8 @@
 
 #include "ido/dbreference.h"
 #include "ido/dbquery.h"
+#include "ido/dbtype.h"
 #include "base/dynamicobject.h"
-#include <boost/smart_ptr/make_shared.hpp>
 
 namespace icinga
 {
@@ -34,8 +34,6 @@ enum DbObjectUpdateType
        DbObjectRemoved
 };
 
-class DbType;
-
 /**
  * A database object.
  *
index 112fa26a83759f0a8fe96ea823d5a5d1bdd94615..3136f39b20fdccaf6c3e83786faa5c0e1d0a6b0c 100644 (file)
@@ -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;
index 005d06f083d565b3abbffdb8c822d856ec18a8f7..a5cbafd769f2bf89d51ecb5692fae76898093dba 100644 (file)
 
 #include "base/object.h"
 #include "base/registry.h"
-#include "ido/dbobject.h"
+#include <boost/smart_ptr/make_shared.hpp>
 
 namespace icinga
 {
 
+class DbObject;
+
 /**
  * A database object type.
  *
@@ -37,9 +39,9 @@ class DbType : public Object
 public:
        DECLARE_PTR_TYPEDEFS(DbType);
 
-       typedef boost::function<DbObject::Ptr (const String&, const String&)> ObjectFactory;
+       typedef boost::function<boost::shared_ptr<DbObject> (const boost::shared_ptr<DbType>&, const String&, const String&)> ObjectFactory;
        typedef std::map<String, DbType::Ptr, string_iless> TypeMap;
-       typedef std::map<std::pair<String, String>, DbObject::Ptr, pair_string_iless> ObjectMap;
+       typedef std::map<std::pair<String, String>, boost::shared_ptr<DbObject>, 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<DbObject> GetOrCreateObjectByName(const String& name1, const String& name2);
 
 private:
        String m_Name;
@@ -95,13 +97,13 @@ public:
  * @ingroup ido
  */
 template<typename T>
-shared_ptr<T> DbObjectFactory(const String& name1, const String& name2)
+shared_ptr<T> DbObjectFactory(const DbType::Ptr& type, const String& name1, const String& name2)
 {
-       return boost::make_shared<T>(name1, name2);
+       return boost::make_shared<T>(type, name1, name2);
 }
 
 #define REGISTER_DBTYPE(name, table, tid, type) \
-       I2_EXPORT icinga::RegisterDbTypeHelper g_RegisterDBT_ ## type(name, table, tid, DbObjectFactory<type>);
+       I2_EXPORT icinga::RegisterDbTypeHelper g_RegisterDBT_ ## name(#name, table, tid, DbObjectFactory<type>);
 
 }
 
index ced211f5771011b64101edf339de7062dbdccd73..636ace0978b137350e24f3239719f286f9cc2034 100644 (file)
 
 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
index 0a58aec1ec8dce8de44ddb56ff1a7c805a929712..e7370ba4324176e1ac917440e43afe9b18ac5e8e 100644 (file)
@@ -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;
index cf9e114d34769fa1315d9b10e3685e0c494c24f2..be52a08a9e1f222f7238b5ce4c7778ed10287751 100644 (file)
 
 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
index 48c43d53d5bfe46ff05413df2871bda9168eb903..c9ce9a7ee61235ae7d4203c8a8b0e5a0e004f3d7 100644 (file)
@@ -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;
index 595b8dc09cbf48d0bfd1485c6382b12334d30616..a683243521d207e7f7351e024d5edd8968b8f5a2 100644 (file)
 
 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
index 7346e1afade6f6d6f2186b1ea3669a43e0b417e3..a00f756139bbbe9a6d133504be647a8b45b053a0 100644 (file)
@@ -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;
index 9a7ec5a3a32d65a97fe91bcce87e43e9f8643e17..0155d5714294926af871acb70017c7683ac7c806 100644 (file)
 
 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
index a85247b25bde32b8ff823809060437325e2802c0..aa99917c0fc57f0297b3fbcedb5c619702d3374d 100644 (file)
@@ -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;
index 59b5c070643f53c3cb9583cfa89d86cc828efd59..c5f2beb090506b2d6ee3082d49303db8f9171401 100644 (file)
 
 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
index f4707da47a31f6e8d5007eef1c8ba14cd329d7ab..dc9f0d1c1aec1a294f1b9aa72fc4578228b23c4d 100644 (file)
@@ -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;
index bde9883ecb41e3ac51f3405ae74b9df74bd51a94..8b5571be6678587e70c840eea600b9983a5b608b 100644 (file)
 
 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
index c8eaf0c2c882a909c2887c40aae7f75a492adb53..e3e9a3ba56c82284fce2dc6efde8636b323984a5 100644 (file)
@@ -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;
index 6a4bbf1fca253f04c4c73e167a7f14a33db1fb57..a512944e335522cc937dbeea3faf585602fa63ca 100644 (file)
 
 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
index 21288ff6c7a3629d35a0bbbc9719764f99e6680c..84123eb1b16f329b86b6bc650f0c4adc91209a46 100644 (file)
@@ -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;