ido: Implement commanddbobjects.

This commit is contained in:
Michael Friedrich 2013-08-01 13:20:30 +02:00
parent 7a87fc0d5e
commit f1b7dbdca4
22 changed files with 174 additions and 39 deletions

View 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;

View 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:

View 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 \

View File

@ -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;
}

47
lib/ido/commanddbobject.h Normal file
View File

@ -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 */

View 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.
*

View 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;

View File

@ -22,11 +22,13 @@
#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>);
}

View File

@ -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

View 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;

View File

@ -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

View 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;

View File

@ -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

View 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;

View File

@ -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

View 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;

View File

@ -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

View 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;

View File

@ -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

View 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;

View File

@ -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

View 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;