From 559a4fef677a2075ac53e52b795a717c61fafefc Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Tue, 15 Apr 2014 13:45:44 +0200 Subject: [PATCH] Add group apply (creates object only once and sets membership). Refs #5910 --- doc/4.1-configuration-syntax.md | 30 ++++++ lib/base/scriptutils.cpp | 2 +- lib/base/scriptutils.h | 2 +- lib/config/applyrule.cpp | 2 +- lib/config/applyrule.h | 2 +- lib/db_ido/endpointdbobject.cpp | 2 +- lib/db_ido/endpointdbobject.h | 2 +- lib/icinga/CMakeLists.txt | 32 ++++--- lib/icinga/dependency-apply.cpp | 2 +- lib/icinga/hostgroup-apply.cpp | 122 ++++++++++++++++++++++++ lib/icinga/hostgroup.h | 6 ++ lib/icinga/notification-apply.cpp | 2 +- lib/icinga/scheduleddowntime-apply.cpp | 2 +- lib/icinga/service-apply.cpp | 2 +- lib/icinga/servicegroup-apply.cpp | 124 +++++++++++++++++++++++++ lib/icinga/servicegroup.h | 6 ++ lib/icinga/usergroup-apply.cpp | 116 +++++++++++++++++++++++ lib/icinga/usergroup.h | 6 ++ lib/methods/castfuncs.cpp | 2 +- lib/methods/castfuncs.h | 2 +- lib/remote/CMakeLists.txt | 2 +- lib/remote/i2-remote.h | 2 +- lib/remote/remote-type.conf | 2 +- 23 files changed, 444 insertions(+), 28 deletions(-) create mode 100644 lib/icinga/hostgroup-apply.cpp create mode 100644 lib/icinga/servicegroup-apply.cpp create mode 100644 lib/icinga/usergroup-apply.cpp diff --git a/doc/4.1-configuration-syntax.md b/doc/4.1-configuration-syntax.md index 0a4bfd611..7990ee76a 100644 --- a/doc/4.1-configuration-syntax.md +++ b/doc/4.1-configuration-syntax.md @@ -363,6 +363,7 @@ once they are set. The `apply` keyword can be used to create new objects which are associated with another group of objects. +Applying group membership for objects can be done in a similar way using [group apply](#group-apply) apply Service "ping" to Host { import "generic-service" @@ -396,6 +397,35 @@ Any valid config attribute can be accessed using the `host` and `service` variables. For example, `host.address` would return the value of the host's "address" attribute - or null if that attribute isn't set. +#### Group Membership Apply + +The `apply` keyword can be used for groups too. Instead of creating a new object +(for example a new `HostGroup` object for every matching `Host`) it will create +the group object only once and add the membership for the matching rule object. + + apply HostGroup "www" to Host { + display_name = "Web Server" + + assign where match("*www*", host.name) + } + +In this example the `assign where` condition is a boolean expression which is +evaluated for all object of type `Host`. A new host group with name "www" is created +and each matching host is added to this host group. + +Depending on the group object type used in the `apply` expression additional local +variables may be available for use in the `where` condition: + +Source Type | Target Type | Variables +------------------|-------------|-------------- +HostGroup | Host | host +ServiceGroup | Service | host, service +UserGroup | User | user + +Any valid config attribute can be accessed using the `host`, `service` and `user` +variables. For example, `user.vars.sla` would return the value of the user's custom +attribute "sla" - or null if that attribute isn't set. + ### Boolean Values The `assign where` and `ignore where` statements, the `!`, `&&` and `||` diff --git a/lib/base/scriptutils.cpp b/lib/base/scriptutils.cpp index 4eaacff9a..6cefd1d5c 100644 --- a/lib/base/scriptutils.cpp +++ b/lib/base/scriptutils.cpp @@ -1,6 +1,6 @@ /****************************************************************************** * Icinga 2 * - * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) * + * Copyright (C) 2012-2014 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 * diff --git a/lib/base/scriptutils.h b/lib/base/scriptutils.h index 904706ea1..2e753560a 100644 --- a/lib/base/scriptutils.h +++ b/lib/base/scriptutils.h @@ -1,6 +1,6 @@ /****************************************************************************** * Icinga 2 * - * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) * + * Copyright (C) 2012-2014 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 * diff --git a/lib/config/applyrule.cpp b/lib/config/applyrule.cpp index 87a250cc3..a9eba3026 100644 --- a/lib/config/applyrule.cpp +++ b/lib/config/applyrule.cpp @@ -1,6 +1,6 @@ /****************************************************************************** * Icinga 2 * - * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) * + * Copyright (C) 2012-2014 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 * diff --git a/lib/config/applyrule.h b/lib/config/applyrule.h index 10b4970c6..cdbe4bb3b 100644 --- a/lib/config/applyrule.h +++ b/lib/config/applyrule.h @@ -1,6 +1,6 @@ /****************************************************************************** * Icinga 2 * - * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) * + * Copyright (C) 2012-2014 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 * diff --git a/lib/db_ido/endpointdbobject.cpp b/lib/db_ido/endpointdbobject.cpp index 94a0c8814..b95e1d177 100644 --- a/lib/db_ido/endpointdbobject.cpp +++ b/lib/db_ido/endpointdbobject.cpp @@ -1,6 +1,6 @@ /****************************************************************************** * Icinga 2 * - * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) * + * Copyright (C) 2012-2014 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 * diff --git a/lib/db_ido/endpointdbobject.h b/lib/db_ido/endpointdbobject.h index fa1982f26..7a69c548b 100644 --- a/lib/db_ido/endpointdbobject.h +++ b/lib/db_ido/endpointdbobject.h @@ -1,6 +1,6 @@ /****************************************************************************** * Icinga 2 * - * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) * + * Copyright (C) 2012-2014 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 * diff --git a/lib/icinga/CMakeLists.txt b/lib/icinga/CMakeLists.txt index 590ae474a..acd043454 100644 --- a/lib/icinga/CMakeLists.txt +++ b/lib/icinga/CMakeLists.txt @@ -41,19 +41,25 @@ mkclass_target(user.ti user.th) mkembedconfig_target(icinga-type.conf icinga-type.cpp) add_library(icinga SHARED - api.cpp checkable.cpp checkable.th checkable-dependency.cpp checkable-downtime.cpp checkable-event.cpp - checkable-flapping.cpp checkcommand.cpp checkcommand.th checkresult.cpp checkresult.th - cib.cpp command.cpp command.th comment.cpp comment.th compatutility.cpp dependency.cpp dependency.th - dependency-apply.cpp domain.cpp domain.th downtime.cpp downtime.th eventcommand.cpp eventcommand.th - externalcommandprocessor.cpp host.cpp host.th hostgroup.cpp hostgroup.th - icingaapplication.cpp icingaapplication.th icingastatuswriter.cpp - icingastatuswriter.th legacytimeperiod.cpp - macroprocessor.cpp macroresolver.cpp notificationcommand.cpp notificationcommand.th - notification.cpp notification.th notification-apply.cpp perfdatavalue.cpp perfdatavalue.th - pluginutility.cpp scheduleddowntime.cpp scheduleddowntime.th scheduleddowntime-apply.cpp - service-apply.cpp checkable-check.cpp checkable-comment.cpp service.cpp service.th - servicegroup.cpp servicegroup.th checkable-notification.cpp timeperiod.cpp timeperiod.th user.cpp user.th - usergroup.cpp usergroup.th icinga-type.cpp + api.cpp + checkable.cpp checkable.th + checkable-check.cpp checkable-comment.cpp checkable-dependency.cpp checkable-downtime.cpp + checkable-event.cpp checkable-flapping.cpp checkable-notification.cpp + checkcommand.cpp checkcommand.th checkresult.cpp checkresult.th + cib.cpp command.cpp command.th comment.cpp comment.th compatutility.cpp + dependency.cpp dependency.th dependency-apply.cpp domain.cpp domain.th downtime.cpp downtime.th + eventcommand.cpp eventcommand.th externalcommandprocessor.cpp + host.cpp host.th hostgroup.cpp hostgroup.th hostgroup-apply.cpp + icingaapplication.cpp icingaapplication.th icingastatuswriter.cpp icingastatuswriter.th + legacytimeperiod.cpp + macroprocessor.cpp macroresolver.cpp + notificationcommand.cpp notificationcommand.th notification.cpp notification.th notification-apply.cpp + perfdatavalue.cpp perfdatavalue.th pluginutility.cpp + scheduleddowntime.cpp scheduleddowntime.th scheduleddowntime-apply.cpp + service-apply.cpp service.cpp service.th servicegroup.cpp servicegroup.th servicegroup-apply.cpp + timeperiod.cpp timeperiod.th + user.cpp user.th usergroup.cpp usergroup.th usergroup-apply.cpp + icinga-type.cpp ) target_link_libraries(icinga ${Boost_LIBRARIES} base config) diff --git a/lib/icinga/dependency-apply.cpp b/lib/icinga/dependency-apply.cpp index d00700225..feb111926 100644 --- a/lib/icinga/dependency-apply.cpp +++ b/lib/icinga/dependency-apply.cpp @@ -1,6 +1,6 @@ /****************************************************************************** * Icinga 2 * - * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) * + * Copyright (C) 2012-2014 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 * diff --git a/lib/icinga/hostgroup-apply.cpp b/lib/icinga/hostgroup-apply.cpp new file mode 100644 index 000000000..2371a3575 --- /dev/null +++ b/lib/icinga/hostgroup-apply.cpp @@ -0,0 +1,122 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2014 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 "icinga/hostgroup.h" +#include "icinga/service.h" +#include "config/configitembuilder.h" +#include "base/initialize.h" +#include "base/dynamictype.h" +#include "base/convert.h" +#include "base/logger_fwd.h" +#include "base/context.h" +#include + +using namespace icinga; + +INITIALIZE_ONCE(&HostGroup::RegisterApplyRuleHandler); + +void HostGroup::RegisterApplyRuleHandler(void) +{ + std::vector targets; + targets.push_back("Host"); + ApplyRule::RegisterType("HostGroup", targets, &HostGroup::EvaluateApplyRules); +} + +bool HostGroup::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule) +{ + DebugInfo di = rule.GetDebugInfo(); + + std::ostringstream msgbuf; + msgbuf << "Evaluating 'apply' rule (" << di << ")"; + CONTEXT(msgbuf.str()); + + Host::Ptr host; + Service::Ptr service; + tie(host, service) = GetHostService(checkable); + + Dictionary::Ptr locals = make_shared(); + locals->Set("host", host); + if (service) + locals->Set("service", service); + + if (!rule.EvaluateFilter(locals)) + return false; + + std::ostringstream msgbuf2; + msgbuf2 << "Applying hostgroup '" << rule.GetName() << "' to object '" << checkable->GetName() << "' for rule " << di; + Log(LogDebug, "icinga", msgbuf2.str()); + + + String group_name = rule.GetName(); + + ConfigItemBuilder::Ptr builder = make_shared(di); + builder->SetType("HostGroup"); + builder->SetName(group_name); + builder->SetScope(rule.GetScope()); + + builder->AddExpression(rule.GetExpression()); + + HostGroup::Ptr group = HostGroup::GetByName(group_name); + + /* if group does not exist, create it only once */ + if (!group) { + ConfigItem::Ptr hostgroupItem = builder->Compile(); + hostgroupItem->Register(); + DynamicObject::Ptr dobj = hostgroupItem->Commit(); + + group = dynamic_pointer_cast(dobj); + + if (!group) { + Log(LogCritical, "icinga", "Unable to create group '" + group_name + "' for apply rule."); + return false; + } + + Log(LogDebug, "icinga", "Group '" + group_name + "' created for apply rule."); + } else + Log(LogDebug, "icinga", "Group '" + group_name + "' already exists. Skipping apply rule creation."); + + /* assign host group membership */ + group->AddMember(host); + + return true; +} + +void HostGroup::EvaluateApplyRules(const std::vector& rules) +{ + int apply_count = 0; + + BOOST_FOREACH(const ApplyRule& rule, rules) { + if (rule.GetTargetType() == "Host") { + apply_count = 0; + + BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects()) { + CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'"); + + if (EvaluateApplyRule(host, rule)) + apply_count++; + } + + if (apply_count == 0) + Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for host does not match anywhere!"); + + } else { + Log(LogWarning, "icinga", "Wrong target type for apply rule '" + rule.GetName() + "'!"); + } + } +} diff --git a/lib/icinga/hostgroup.h b/lib/icinga/hostgroup.h index c0187c7db..7294e469a 100644 --- a/lib/icinga/hostgroup.h +++ b/lib/icinga/hostgroup.h @@ -23,6 +23,7 @@ #include "icinga/i2-icinga.h" #include "icinga/hostgroup.th" #include "icinga/host.h" +#include "config/applyrule.h" namespace icinga { @@ -44,9 +45,14 @@ public: bool ResolveGroupMembership(Host::Ptr const& host, bool add = true, int rstack = 0); + static void RegisterApplyRuleHandler(void); + private: mutable boost::mutex m_HostGroupMutex; std::set m_Members; + + static bool EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule); + static void EvaluateApplyRules(const std::vector& rules); }; } diff --git a/lib/icinga/notification-apply.cpp b/lib/icinga/notification-apply.cpp index ad501818a..8037cc72c 100644 --- a/lib/icinga/notification-apply.cpp +++ b/lib/icinga/notification-apply.cpp @@ -1,6 +1,6 @@ /****************************************************************************** * Icinga 2 * - * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) * + * Copyright (C) 2012-2014 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 * diff --git a/lib/icinga/scheduleddowntime-apply.cpp b/lib/icinga/scheduleddowntime-apply.cpp index 29442b72f..1142d5652 100644 --- a/lib/icinga/scheduleddowntime-apply.cpp +++ b/lib/icinga/scheduleddowntime-apply.cpp @@ -1,6 +1,6 @@ /****************************************************************************** * Icinga 2 * - * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) * + * Copyright (C) 2012-2014 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 * diff --git a/lib/icinga/service-apply.cpp b/lib/icinga/service-apply.cpp index 889ea66dc..bf6a93036 100644 --- a/lib/icinga/service-apply.cpp +++ b/lib/icinga/service-apply.cpp @@ -1,6 +1,6 @@ /****************************************************************************** * Icinga 2 * - * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) * + * Copyright (C) 2012-2014 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 * diff --git a/lib/icinga/servicegroup-apply.cpp b/lib/icinga/servicegroup-apply.cpp new file mode 100644 index 000000000..d56105549 --- /dev/null +++ b/lib/icinga/servicegroup-apply.cpp @@ -0,0 +1,124 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2014 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 "icinga/servicegroup.h" +#include "icinga/service.h" +#include "config/configitembuilder.h" +#include "base/initialize.h" +#include "base/dynamictype.h" +#include "base/convert.h" +#include "base/logger_fwd.h" +#include "base/context.h" +#include + +using namespace icinga; + +INITIALIZE_ONCE(&ServiceGroup::RegisterApplyRuleHandler); + +void ServiceGroup::RegisterApplyRuleHandler(void) +{ + std::vector targets; + targets.push_back("Service"); + ApplyRule::RegisterType("ServiceGroup", targets, &ServiceGroup::EvaluateApplyRules); +} + +bool ServiceGroup::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule) +{ + DebugInfo di = rule.GetDebugInfo(); + + std::ostringstream msgbuf; + msgbuf << "Evaluating 'apply' rule (" << di << ")"; + CONTEXT(msgbuf.str()); + + Host::Ptr host; + Service::Ptr service; + tie(host, service) = GetHostService(checkable); + + Dictionary::Ptr locals = make_shared(); + locals->Set("host", host); + if (service) + locals->Set("service", service); + else + return false; + + if (!rule.EvaluateFilter(locals)) + return false; + + std::ostringstream msgbuf2; + msgbuf2 << "Applying hostgroup '" << rule.GetName() << "' to object '" << checkable->GetName() << "' for rule " << di; + Log(LogDebug, "icinga", msgbuf2.str()); + + + String group_name = rule.GetName(); + + ConfigItemBuilder::Ptr builder = make_shared(di); + builder->SetType("ServiceGroup"); + builder->SetName(group_name); + builder->SetScope(rule.GetScope()); + + builder->AddExpression(rule.GetExpression()); + + ServiceGroup::Ptr group = ServiceGroup::GetByName(group_name); + + /* if group does not exist, create it only once */ + if (!group) { + ConfigItem::Ptr servicegroupItem = builder->Compile(); + servicegroupItem->Register(); + DynamicObject::Ptr dobj = servicegroupItem->Commit(); + + group = dynamic_pointer_cast(dobj); + + if (!group) { + Log(LogCritical, "icinga", "Unable to create ServiceGroup '" + group_name + "' for apply rule."); + return false; + } + + Log(LogDebug, "icinga", "ServiceGroup '" + group_name + "' created for apply rule."); + } else + Log(LogDebug, "icinga", "ServiceGroup '" + group_name + "' already exists. Skipping apply rule creation."); + + /* assign service group membership */ + group->AddMember(service); + + return true; +} + +void ServiceGroup::EvaluateApplyRules(const std::vector& rules) +{ + int apply_count = 0; + + BOOST_FOREACH(const ApplyRule& rule, rules) { + if (rule.GetTargetType() == "Service") { + apply_count = 0; + + BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects()) { + CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'"); + + if(EvaluateApplyRule(service, rule)) + apply_count++; + } + + if (apply_count == 0) + Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for service does not match anywhere!"); + + } else { + Log(LogWarning, "icinga", "Wrong target type for apply rule '" + rule.GetName() + "'!"); + } + } +} diff --git a/lib/icinga/servicegroup.h b/lib/icinga/servicegroup.h index 7e93ea5eb..32c2123e2 100644 --- a/lib/icinga/servicegroup.h +++ b/lib/icinga/servicegroup.h @@ -23,6 +23,7 @@ #include "icinga/i2-icinga.h" #include "icinga/servicegroup.th" #include "icinga/service.h" +#include "config/applyrule.h" namespace icinga { @@ -44,9 +45,14 @@ public: bool ResolveGroupMembership(Service::Ptr const& service, bool add = true, int rstack = 0); + static void RegisterApplyRuleHandler(void); + private: mutable boost::mutex m_ServiceGroupMutex; std::set m_Members; + + static bool EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule); + static void EvaluateApplyRules(const std::vector& rules); }; } diff --git a/lib/icinga/usergroup-apply.cpp b/lib/icinga/usergroup-apply.cpp new file mode 100644 index 000000000..66d4ede38 --- /dev/null +++ b/lib/icinga/usergroup-apply.cpp @@ -0,0 +1,116 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2014 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 "icinga/hostgroup.h" +#include "icinga/service.h" +#include "config/configitembuilder.h" +#include "base/initialize.h" +#include "base/dynamictype.h" +#include "base/convert.h" +#include "base/logger_fwd.h" +#include "base/context.h" +#include + +using namespace icinga; + +INITIALIZE_ONCE(&UserGroup::RegisterApplyRuleHandler); + +void UserGroup::RegisterApplyRuleHandler(void) +{ + std::vector targets; + targets.push_back("User"); + ApplyRule::RegisterType("UserGroup", targets, &UserGroup::EvaluateApplyRules); +} + +bool UserGroup::EvaluateApplyRule(const User::Ptr& user, const ApplyRule& rule) +{ + DebugInfo di = rule.GetDebugInfo(); + + std::ostringstream msgbuf; + msgbuf << "Evaluating 'apply' rule (" << di << ")"; + CONTEXT(msgbuf.str()); + + Dictionary::Ptr locals = make_shared(); + locals->Set("user", user); + + if (!rule.EvaluateFilter(locals)) + return false; + + std::ostringstream msgbuf2; + msgbuf2 << "Applying usergroup '" << rule.GetName() << "' to object '" << user->GetName() << "' for rule " << di; + Log(LogDebug, "icinga", msgbuf2.str()); + + + String group_name = rule.GetName(); + + ConfigItemBuilder::Ptr builder = make_shared(di); + builder->SetType("UserGroup"); + builder->SetName(group_name); + builder->SetScope(rule.GetScope()); + + builder->AddExpression(rule.GetExpression()); + + UserGroup::Ptr group = UserGroup::GetByName(group_name); + + /* if group does not exist, create it only once */ + if (!group) { + ConfigItem::Ptr usergroupItem = builder->Compile(); + usergroupItem->Register(); + DynamicObject::Ptr dobj = usergroupItem->Commit(); + + group = dynamic_pointer_cast(dobj); + + if (!group) { + Log(LogCritical, "icinga", "Unable to create UserGroup '" + group_name + "' for apply rule."); + return false; + } + + Log(LogDebug, "icinga", "UserGroup '" + group_name + "' created for apply rule."); + } else + Log(LogDebug, "icinga", "UserGroup '" + group_name + "' already exists. Skipping apply rule creation."); + + /* assign user group membership */ + group->AddMember(user); + + return true; +} + +void UserGroup::EvaluateApplyRules(const std::vector& rules) +{ + int apply_count = 0; + + BOOST_FOREACH(const ApplyRule& rule, rules) { + if (rule.GetTargetType() == "User") { + apply_count = 0; + + BOOST_FOREACH(const User::Ptr& user, DynamicType::GetObjects()) { + CONTEXT("Evaluating 'apply' rules for User '" + user->GetName() + "'"); + + if(EvaluateApplyRule(user, rule)) + apply_count++; + } + + if (apply_count == 0) + Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for user does not match anywhere!"); + + } else { + Log(LogWarning, "icinga", "Wrong target type for apply rule '" + rule.GetName() + "'!"); + } + } +} diff --git a/lib/icinga/usergroup.h b/lib/icinga/usergroup.h index 3338c6cda..1c4968315 100644 --- a/lib/icinga/usergroup.h +++ b/lib/icinga/usergroup.h @@ -23,6 +23,7 @@ #include "icinga/i2-icinga.h" #include "icinga/usergroup.th" #include "icinga/user.h" +#include "config/applyrule.h" namespace icinga { @@ -44,9 +45,14 @@ public: bool ResolveGroupMembership(User::Ptr const& user, bool add = true, int rstack = 0); + static void RegisterApplyRuleHandler(void); + private: mutable boost::mutex m_UserGroupMutex; std::set m_Members; + + static bool EvaluateApplyRule(const User::Ptr& user, const ApplyRule& rule); + static void EvaluateApplyRules(const std::vector& rules); }; } diff --git a/lib/methods/castfuncs.cpp b/lib/methods/castfuncs.cpp index 4b35b2f53..c4c7a7aa1 100644 --- a/lib/methods/castfuncs.cpp +++ b/lib/methods/castfuncs.cpp @@ -1,6 +1,6 @@ /****************************************************************************** * Icinga 2 * - * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) * + * Copyright (C) 2012-2014 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 * diff --git a/lib/methods/castfuncs.h b/lib/methods/castfuncs.h index 7d314bba9..cd3ea4c9d 100644 --- a/lib/methods/castfuncs.h +++ b/lib/methods/castfuncs.h @@ -1,6 +1,6 @@ /****************************************************************************** * Icinga 2 * - * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) * + * Copyright (C) 2012-2014 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 * diff --git a/lib/remote/CMakeLists.txt b/lib/remote/CMakeLists.txt index c96631bd5..367fdfb92 100644 --- a/lib/remote/CMakeLists.txt +++ b/lib/remote/CMakeLists.txt @@ -1,5 +1,5 @@ # Icinga 2 -# Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) +# Copyright (C) 2012-2014 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 diff --git a/lib/remote/i2-remote.h b/lib/remote/i2-remote.h index d2e884f35..045d89476 100644 --- a/lib/remote/i2-remote.h +++ b/lib/remote/i2-remote.h @@ -1,6 +1,6 @@ /****************************************************************************** * Icinga 2 * - * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) * + * Copyright (C) 2012-2014 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 * diff --git a/lib/remote/remote-type.conf b/lib/remote/remote-type.conf index f12945825..e007068c1 100644 --- a/lib/remote/remote-type.conf +++ b/lib/remote/remote-type.conf @@ -1,6 +1,6 @@ /****************************************************************************** * Icinga 2 * - * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) * + * Copyright (C) 2012-2014 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 * -- 2.40.0