mirror of https://github.com/Icinga/icinga2.git
Add group apply (creates object only once and sets membership).
Refs #5910
This commit is contained in:
parent
8d1ffe7aa2
commit
559a4fef67
|
@ -363,6 +363,7 @@ once they are set.
|
||||||
|
|
||||||
The `apply` keyword can be used to create new objects which are associated with
|
The `apply` keyword can be used to create new objects which are associated with
|
||||||
another group of objects.
|
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 {
|
apply Service "ping" to Host {
|
||||||
import "generic-service"
|
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
|
variables. For example, `host.address` would return the value of the host's
|
||||||
"address" attribute - or null if that attribute isn't set.
|
"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
|
### <a id="boolean-values"></a> Boolean Values
|
||||||
|
|
||||||
The `assign where` and `ignore where` statements, the `!`, `&&` and `||`
|
The `assign where` and `ignore where` statements, the `!`, `&&` and `||`
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Icinga 2 *
|
* 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 *
|
* This program is free software; you can redistribute it and/or *
|
||||||
* modify it under the terms of the GNU General Public License *
|
* modify it under the terms of the GNU General Public License *
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Icinga 2 *
|
* 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 *
|
* This program is free software; you can redistribute it and/or *
|
||||||
* modify it under the terms of the GNU General Public License *
|
* modify it under the terms of the GNU General Public License *
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Icinga 2 *
|
* 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 *
|
* This program is free software; you can redistribute it and/or *
|
||||||
* modify it under the terms of the GNU General Public License *
|
* modify it under the terms of the GNU General Public License *
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Icinga 2 *
|
* 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 *
|
* This program is free software; you can redistribute it and/or *
|
||||||
* modify it under the terms of the GNU General Public License *
|
* modify it under the terms of the GNU General Public License *
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Icinga 2 *
|
* 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 *
|
* This program is free software; you can redistribute it and/or *
|
||||||
* modify it under the terms of the GNU General Public License *
|
* modify it under the terms of the GNU General Public License *
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Icinga 2 *
|
* 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 *
|
* This program is free software; you can redistribute it and/or *
|
||||||
* modify it under the terms of the GNU General Public License *
|
* modify it under the terms of the GNU General Public License *
|
||||||
|
|
|
@ -41,19 +41,25 @@ mkclass_target(user.ti user.th)
|
||||||
mkembedconfig_target(icinga-type.conf icinga-type.cpp)
|
mkembedconfig_target(icinga-type.conf icinga-type.cpp)
|
||||||
|
|
||||||
add_library(icinga SHARED
|
add_library(icinga SHARED
|
||||||
api.cpp checkable.cpp checkable.th checkable-dependency.cpp checkable-downtime.cpp checkable-event.cpp
|
api.cpp
|
||||||
checkable-flapping.cpp checkcommand.cpp checkcommand.th checkresult.cpp checkresult.th
|
checkable.cpp checkable.th
|
||||||
cib.cpp command.cpp command.th comment.cpp comment.th compatutility.cpp dependency.cpp dependency.th
|
checkable-check.cpp checkable-comment.cpp checkable-dependency.cpp checkable-downtime.cpp
|
||||||
dependency-apply.cpp domain.cpp domain.th downtime.cpp downtime.th eventcommand.cpp eventcommand.th
|
checkable-event.cpp checkable-flapping.cpp checkable-notification.cpp
|
||||||
externalcommandprocessor.cpp host.cpp host.th hostgroup.cpp hostgroup.th
|
checkcommand.cpp checkcommand.th checkresult.cpp checkresult.th
|
||||||
icingaapplication.cpp icingaapplication.th icingastatuswriter.cpp
|
cib.cpp command.cpp command.th comment.cpp comment.th compatutility.cpp
|
||||||
icingastatuswriter.th legacytimeperiod.cpp
|
dependency.cpp dependency.th dependency-apply.cpp domain.cpp domain.th downtime.cpp downtime.th
|
||||||
macroprocessor.cpp macroresolver.cpp notificationcommand.cpp notificationcommand.th
|
eventcommand.cpp eventcommand.th externalcommandprocessor.cpp
|
||||||
notification.cpp notification.th notification-apply.cpp perfdatavalue.cpp perfdatavalue.th
|
host.cpp host.th hostgroup.cpp hostgroup.th hostgroup-apply.cpp
|
||||||
pluginutility.cpp scheduleddowntime.cpp scheduleddowntime.th scheduleddowntime-apply.cpp
|
icingaapplication.cpp icingaapplication.th icingastatuswriter.cpp icingastatuswriter.th
|
||||||
service-apply.cpp checkable-check.cpp checkable-comment.cpp service.cpp service.th
|
legacytimeperiod.cpp
|
||||||
servicegroup.cpp servicegroup.th checkable-notification.cpp timeperiod.cpp timeperiod.th user.cpp user.th
|
macroprocessor.cpp macroresolver.cpp
|
||||||
usergroup.cpp usergroup.th icinga-type.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)
|
target_link_libraries(icinga ${Boost_LIBRARIES} base config)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Icinga 2 *
|
* 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 *
|
* This program is free software; you can redistribute it and/or *
|
||||||
* modify it under the terms of the GNU General Public License *
|
* modify it under the terms of the GNU General Public License *
|
||||||
|
|
|
@ -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 <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() + "'!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,6 +23,7 @@
|
||||||
#include "icinga/i2-icinga.h"
|
#include "icinga/i2-icinga.h"
|
||||||
#include "icinga/hostgroup.th"
|
#include "icinga/hostgroup.th"
|
||||||
#include "icinga/host.h"
|
#include "icinga/host.h"
|
||||||
|
#include "config/applyrule.h"
|
||||||
|
|
||||||
namespace icinga
|
namespace icinga
|
||||||
{
|
{
|
||||||
|
@ -44,9 +45,14 @@ public:
|
||||||
|
|
||||||
bool ResolveGroupMembership(Host::Ptr const& host, bool add = true, int rstack = 0);
|
bool ResolveGroupMembership(Host::Ptr const& host, bool add = true, int rstack = 0);
|
||||||
|
|
||||||
|
static void RegisterApplyRuleHandler(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable boost::mutex m_HostGroupMutex;
|
mutable boost::mutex m_HostGroupMutex;
|
||||||
std::set<Host::Ptr> m_Members;
|
std::set<Host::Ptr> m_Members;
|
||||||
|
|
||||||
|
static bool EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule);
|
||||||
|
static void EvaluateApplyRules(const std::vector<ApplyRule>& rules);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Icinga 2 *
|
* 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 *
|
* This program is free software; you can redistribute it and/or *
|
||||||
* modify it under the terms of the GNU General Public License *
|
* modify it under the terms of the GNU General Public License *
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Icinga 2 *
|
* 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 *
|
* This program is free software; you can redistribute it and/or *
|
||||||
* modify it under the terms of the GNU General Public License *
|
* modify it under the terms of the GNU General Public License *
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Icinga 2 *
|
* 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 *
|
* This program is free software; you can redistribute it and/or *
|
||||||
* modify it under the terms of the GNU General Public License *
|
* modify it under the terms of the GNU General Public License *
|
||||||
|
|
|
@ -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 <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() + "'!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,6 +23,7 @@
|
||||||
#include "icinga/i2-icinga.h"
|
#include "icinga/i2-icinga.h"
|
||||||
#include "icinga/servicegroup.th"
|
#include "icinga/servicegroup.th"
|
||||||
#include "icinga/service.h"
|
#include "icinga/service.h"
|
||||||
|
#include "config/applyrule.h"
|
||||||
|
|
||||||
namespace icinga
|
namespace icinga
|
||||||
{
|
{
|
||||||
|
@ -44,9 +45,14 @@ public:
|
||||||
|
|
||||||
bool ResolveGroupMembership(Service::Ptr const& service, bool add = true, int rstack = 0);
|
bool ResolveGroupMembership(Service::Ptr const& service, bool add = true, int rstack = 0);
|
||||||
|
|
||||||
|
static void RegisterApplyRuleHandler(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable boost::mutex m_ServiceGroupMutex;
|
mutable boost::mutex m_ServiceGroupMutex;
|
||||||
std::set<Service::Ptr> m_Members;
|
std::set<Service::Ptr> m_Members;
|
||||||
|
|
||||||
|
static bool EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule);
|
||||||
|
static void EvaluateApplyRules(const std::vector<ApplyRule>& rules);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 <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() + "'!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,6 +23,7 @@
|
||||||
#include "icinga/i2-icinga.h"
|
#include "icinga/i2-icinga.h"
|
||||||
#include "icinga/usergroup.th"
|
#include "icinga/usergroup.th"
|
||||||
#include "icinga/user.h"
|
#include "icinga/user.h"
|
||||||
|
#include "config/applyrule.h"
|
||||||
|
|
||||||
namespace icinga
|
namespace icinga
|
||||||
{
|
{
|
||||||
|
@ -44,9 +45,14 @@ public:
|
||||||
|
|
||||||
bool ResolveGroupMembership(User::Ptr const& user, bool add = true, int rstack = 0);
|
bool ResolveGroupMembership(User::Ptr const& user, bool add = true, int rstack = 0);
|
||||||
|
|
||||||
|
static void RegisterApplyRuleHandler(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable boost::mutex m_UserGroupMutex;
|
mutable boost::mutex m_UserGroupMutex;
|
||||||
std::set<User::Ptr> m_Members;
|
std::set<User::Ptr> m_Members;
|
||||||
|
|
||||||
|
static bool EvaluateApplyRule(const User::Ptr& user, const ApplyRule& rule);
|
||||||
|
static void EvaluateApplyRules(const std::vector<ApplyRule>& rules);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Icinga 2 *
|
* 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 *
|
* This program is free software; you can redistribute it and/or *
|
||||||
* modify it under the terms of the GNU General Public License *
|
* modify it under the terms of the GNU General Public License *
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Icinga 2 *
|
* 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 *
|
* This program is free software; you can redistribute it and/or *
|
||||||
* modify it under the terms of the GNU General Public License *
|
* modify it under the terms of the GNU General Public License *
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
# Icinga 2
|
# 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
|
# This program is free software; you can redistribute it and/or
|
||||||
# modify it under the terms of the GNU General Public License
|
# modify it under the terms of the GNU General Public License
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Icinga 2 *
|
* 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 *
|
* This program is free software; you can redistribute it and/or *
|
||||||
* modify it under the terms of the GNU General Public License *
|
* modify it under the terms of the GNU General Public License *
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Icinga 2 *
|
* 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 *
|
* This program is free software; you can redistribute it and/or *
|
||||||
* modify it under the terms of the GNU General Public License *
|
* modify it under the terms of the GNU General Public License *
|
||||||
|
|
Loading…
Reference in New Issue