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"
variables. For example, `host.address` would return the value of the host's
"address" attribute - or null if that attribute isn't set.
+#### <a id="group-membership-apply"></a> 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.
+
### <a id="boolean-values"></a> Boolean Values
The `assign where` and `ignore where` statements, the `!`, `&&` and `||`
/******************************************************************************
* 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 *
/******************************************************************************
* 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 *
/******************************************************************************
* 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 *
/******************************************************************************
* 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 *
/******************************************************************************
* 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 *
/******************************************************************************
* 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 *
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)
/******************************************************************************
* 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 *
--- /dev/null
+/******************************************************************************
+ * 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 <boost/foreach.hpp>
+
+using namespace icinga;
+
+INITIALIZE_ONCE(&HostGroup::RegisterApplyRuleHandler);
+
+void HostGroup::RegisterApplyRuleHandler(void)
+{
+ std::vector<String> 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<Dictionary>();
+ 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<ConfigItemBuilder>(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<HostGroup>(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<ApplyRule>& 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<Host>()) {
+ 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() + "'!");
+ }
+ }
+}
#include "icinga/i2-icinga.h"
#include "icinga/hostgroup.th"
#include "icinga/host.h"
+#include "config/applyrule.h"
namespace icinga
{
bool ResolveGroupMembership(Host::Ptr const& host, bool add = true, int rstack = 0);
+ static void RegisterApplyRuleHandler(void);
+
private:
mutable boost::mutex m_HostGroupMutex;
std::set<Host::Ptr> m_Members;
+
+ static bool EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule);
+ static void EvaluateApplyRules(const std::vector<ApplyRule>& rules);
};
}
/******************************************************************************
* 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 *
/******************************************************************************
* 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 *
/******************************************************************************
* 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 *
--- /dev/null
+/******************************************************************************
+ * 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 <boost/foreach.hpp>
+
+using namespace icinga;
+
+INITIALIZE_ONCE(&ServiceGroup::RegisterApplyRuleHandler);
+
+void ServiceGroup::RegisterApplyRuleHandler(void)
+{
+ std::vector<String> 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<Dictionary>();
+ 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<ConfigItemBuilder>(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<ServiceGroup>(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<ApplyRule>& 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<Service>()) {
+ 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() + "'!");
+ }
+ }
+}
#include "icinga/i2-icinga.h"
#include "icinga/servicegroup.th"
#include "icinga/service.h"
+#include "config/applyrule.h"
namespace icinga
{
bool ResolveGroupMembership(Service::Ptr const& service, bool add = true, int rstack = 0);
+ static void RegisterApplyRuleHandler(void);
+
private:
mutable boost::mutex m_ServiceGroupMutex;
std::set<Service::Ptr> m_Members;
+
+ static bool EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule);
+ static void EvaluateApplyRules(const std::vector<ApplyRule>& rules);
};
}
--- /dev/null
+/******************************************************************************
+ * 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 <boost/foreach.hpp>
+
+using namespace icinga;
+
+INITIALIZE_ONCE(&UserGroup::RegisterApplyRuleHandler);
+
+void UserGroup::RegisterApplyRuleHandler(void)
+{
+ std::vector<String> 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<Dictionary>();
+ 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<ConfigItemBuilder>(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<UserGroup>(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<ApplyRule>& 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<User>()) {
+ 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() + "'!");
+ }
+ }
+}
#include "icinga/i2-icinga.h"
#include "icinga/usergroup.th"
#include "icinga/user.h"
+#include "config/applyrule.h"
namespace icinga
{
bool ResolveGroupMembership(User::Ptr const& user, bool add = true, int rstack = 0);
+ static void RegisterApplyRuleHandler(void);
+
private:
mutable boost::mutex m_UserGroupMutex;
std::set<User::Ptr> m_Members;
+
+ static bool EvaluateApplyRule(const User::Ptr& user, const ApplyRule& rule);
+ static void EvaluateApplyRules(const std::vector<ApplyRule>& rules);
};
}
/******************************************************************************
* 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 *
/******************************************************************************
* 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 *
# 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
/******************************************************************************
* 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 *
/******************************************************************************
* 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 *