From 540489e408f78f622725e73839546f21ffa23438 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Tue, 9 Jul 2013 18:33:27 +0200 Subject: [PATCH] livestatus: add host and service group getters refs #4372 --- components/livestatus/Makefile.am | 4 ++ components/livestatus/hostgroupstable.cpp | 72 +++++++++++++++++++ components/livestatus/hostgroupstable.h | 55 ++++++++++++++ components/livestatus/livestatus.vcxproj | 4 ++ .../livestatus/livestatus.vcxproj.filters | 12 ++++ components/livestatus/servicegroupstable.cpp | 72 +++++++++++++++++++ components/livestatus/servicegroupstable.h | 55 ++++++++++++++ components/livestatus/table.cpp | 4 ++ 8 files changed, 278 insertions(+) create mode 100644 components/livestatus/hostgroupstable.cpp create mode 100644 components/livestatus/hostgroupstable.h create mode 100644 components/livestatus/servicegroupstable.cpp create mode 100644 components/livestatus/servicegroupstable.h diff --git a/components/livestatus/Makefile.am b/components/livestatus/Makefile.am index 2bfaa58d9..2512fc5ec 100644 --- a/components/livestatus/Makefile.am +++ b/components/livestatus/Makefile.am @@ -32,6 +32,8 @@ liblivestatus_la_SOURCES = \ downtimestable.h \ filter.cpp \ filter.h \ + hostgroupstable.cpp \ + hostgroupstable.h \ hoststable.cpp \ hoststable.h \ livestatus-type.cpp \ @@ -43,6 +45,8 @@ liblivestatus_la_SOURCES = \ orfilter.h \ query.cpp \ query.h \ + servicegroupstable.cpp \ + servicegroupstable.h \ servicestable.cpp \ servicestable.h \ statustable.cpp \ diff --git a/components/livestatus/hostgroupstable.cpp b/components/livestatus/hostgroupstable.cpp new file mode 100644 index 000000000..84931114c --- /dev/null +++ b/components/livestatus/hostgroupstable.cpp @@ -0,0 +1,72 @@ +/****************************************************************************** + * 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 "livestatus/hostgroupstable.h" +#include "icinga/hostgroup.h" +#include "base/dynamictype.h" +#include + +using namespace icinga; +using namespace livestatus; + +HostGroupsTable::HostGroupsTable(void) +{ + AddColumns(this); +} + +void HostGroupsTable::AddColumns(Table *table, const String& prefix, + const Column::ObjectAccessor& objectAccessor) +{ + table->AddColumn(prefix + "name", Column(&HostGroupsTable::NameAccessor, objectAccessor)); + table->AddColumn(prefix + "alias", Column(&HostGroupsTable::AliasAccessor, objectAccessor)); + table->AddColumn(prefix + "members", Column(&HostGroupsTable::MembersAccessor, objectAccessor)); +} + +String HostGroupsTable::GetName(void) const +{ + return "hostgroups"; +} + +void HostGroupsTable::FetchRows(const AddRowFunction& addRowFn) +{ + BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("HostGroup")) { + addRowFn(object); + } +} + +Value HostGroupsTable::NameAccessor(const Object::Ptr& object) +{ + return static_pointer_cast(object)->GetName(); +} + +Value HostGroupsTable::AliasAccessor(const Object::Ptr& object) +{ + return static_pointer_cast(object)->GetName(); +} + +Value HostGroupsTable::MembersAccessor(const Object::Ptr& object) +{ + Array::Ptr members = boost::make_shared(); + + BOOST_FOREACH(const Host::Ptr& host, static_pointer_cast(object)->GetMembers()) { + members->Add(host->GetName()); + } + + return members; +} diff --git a/components/livestatus/hostgroupstable.h b/components/livestatus/hostgroupstable.h new file mode 100644 index 000000000..628057438 --- /dev/null +++ b/components/livestatus/hostgroupstable.h @@ -0,0 +1,55 @@ +/****************************************************************************** + * 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 HOSTGROUPSTABLE_H +#define HOSTGROUPSTABLE_H + +#include "livestatus/table.h" + +using namespace icinga; + +namespace livestatus +{ + +/** + * @ingroup livestatus + */ +class HostGroupsTable : public Table +{ +public: + DECLARE_PTR_TYPEDEFS(HostGroupsTable); + + HostGroupsTable(void); + + static void AddColumns(Table *table, const String& prefix = String(), + const Column::ObjectAccessor& objectAccessor = Column::ObjectAccessor()); + + virtual String GetName(void) const; + +protected: + virtual void FetchRows(const AddRowFunction& addRowFn); + + static Value NameAccessor(const Object::Ptr& object); + static Value AliasAccessor(const Object::Ptr& object); + static Value MembersAccessor(const Object::Ptr& object); +}; + +} + +#endif /* HOSTGROUPSTABLE_H */ diff --git a/components/livestatus/livestatus.vcxproj b/components/livestatus/livestatus.vcxproj index f6e66b707..81ebdfc55 100644 --- a/components/livestatus/livestatus.vcxproj +++ b/components/livestatus/livestatus.vcxproj @@ -29,12 +29,14 @@ + + @@ -52,11 +54,13 @@ + + diff --git a/components/livestatus/livestatus.vcxproj.filters b/components/livestatus/livestatus.vcxproj.filters index 38b49902c..4245d9d31 100644 --- a/components/livestatus/livestatus.vcxproj.filters +++ b/components/livestatus/livestatus.vcxproj.filters @@ -51,12 +51,18 @@ Headerdateien + + Headerdateien + Headerdateien Headerdateien + + Headerdateien + Headerdateien @@ -110,12 +116,18 @@ Quelldateien + + Quelldateien + Quelldateien Quelldateien + + Quelldateien + Quelldateien diff --git a/components/livestatus/servicegroupstable.cpp b/components/livestatus/servicegroupstable.cpp new file mode 100644 index 000000000..305a6dc0e --- /dev/null +++ b/components/livestatus/servicegroupstable.cpp @@ -0,0 +1,72 @@ +/****************************************************************************** + * 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 "livestatus/servicegroupstable.h" +#include "icinga/servicegroup.h" +#include "base/dynamictype.h" +#include + +using namespace icinga; +using namespace livestatus; + +ServiceGroupsTable::ServiceGroupsTable(void) +{ + AddColumns(this); +} + +void ServiceGroupsTable::AddColumns(Table *table, const String& prefix, + const Column::ObjectAccessor& objectAccessor) +{ + table->AddColumn(prefix + "name", Column(&ServiceGroupsTable::NameAccessor, objectAccessor)); + table->AddColumn(prefix + "alias", Column(&ServiceGroupsTable::AliasAccessor, objectAccessor)); + table->AddColumn(prefix + "members", Column(&ServiceGroupsTable::MembersAccessor, objectAccessor)); +} + +String ServiceGroupsTable::GetName(void) const +{ + return "servicegroups"; +} + +void ServiceGroupsTable::FetchRows(const AddRowFunction& addRowFn) +{ + BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("UserGroup")) { + addRowFn(object); + } +} + +Value ServiceGroupsTable::NameAccessor(const Object::Ptr& object) +{ + return static_pointer_cast(object)->GetName(); +} + +Value ServiceGroupsTable::AliasAccessor(const Object::Ptr& object) +{ + return static_pointer_cast(object)->GetName(); +} + +Value ServiceGroupsTable::MembersAccessor(const Object::Ptr& object) +{ + Array::Ptr members = boost::make_shared(); + + BOOST_FOREACH(const Service::Ptr& service, static_pointer_cast(object)->GetMembers()) { + members->Add(service->GetName()); + } + + return members; +} diff --git a/components/livestatus/servicegroupstable.h b/components/livestatus/servicegroupstable.h new file mode 100644 index 000000000..4df140afc --- /dev/null +++ b/components/livestatus/servicegroupstable.h @@ -0,0 +1,55 @@ +/****************************************************************************** + * 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 SERVICEGROUPSTABLE_H +#define SERVICEGROUPSTABLE_H + +#include "livestatus/table.h" + +using namespace icinga; + +namespace livestatus +{ + +/** + * @ingroup livestatus + */ +class ServiceGroupsTable : public Table +{ +public: + DECLARE_PTR_TYPEDEFS(ServiceGroupsTable); + + ServiceGroupsTable(void); + + static void AddColumns(Table *table, const String& prefix = String(), + const Column::ObjectAccessor& objectAccessor = Column::ObjectAccessor()); + + virtual String GetName(void) const; + +protected: + virtual void FetchRows(const AddRowFunction& addRowFn); + + static Value NameAccessor(const Object::Ptr& object); + static Value AliasAccessor(const Object::Ptr& object); + static Value MembersAccessor(const Object::Ptr& object); +}; + +} + +#endif /* SERVICEGROUPSTABLE_H */ diff --git a/components/livestatus/table.cpp b/components/livestatus/table.cpp index 7c6a18fe2..3d0d45e0a 100644 --- a/components/livestatus/table.cpp +++ b/components/livestatus/table.cpp @@ -49,8 +49,12 @@ Table::Ptr Table::GetByName(const String& name) return boost::make_shared(); else if (name == "contacts") return boost::make_shared(); + else if (name == "hostgroups") + return boost::make_shared(); else if (name == "hosts") return boost::make_shared(); + else if (name == "servicegroups") + return boost::make_shared(); else if (name == "services") return boost::make_shared(); else if (name == "commands") -- 2.40.0