1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "icinga/servicegroup.h"
21 #include "config/objectrule.h"
22 #include "base/dynamictype.h"
23 #include "base/objectlock.h"
24 #include "base/logger_fwd.h"
25 #include "base/context.h"
26 #include <boost/foreach.hpp>
28 using namespace icinga;
30 REGISTER_TYPE(ServiceGroup);
32 INITIALIZE_ONCE(&ServiceGroup::RegisterObjectRuleHandler);
34 void ServiceGroup::RegisterObjectRuleHandler(void)
36 ObjectRule::RegisterType("ServiceGroup", &ServiceGroup::EvaluateObjectRules);
39 bool ServiceGroup::EvaluateObjectRule(const Service::Ptr service, const ObjectRule& rule)
41 DebugInfo di = rule.GetDebugInfo();
43 std::ostringstream msgbuf;
44 msgbuf << "Evaluating 'object' rule (" << di << ")";
45 CONTEXT(msgbuf.str());
47 Host::Ptr host = service->GetHost();
49 Dictionary::Ptr locals = make_shared<Dictionary>();
50 locals->Set("host", host);
51 locals->Set("service", service);
53 if (!rule.EvaluateFilter(locals))
56 std::ostringstream msgbuf2;
57 msgbuf2 << "Assigning membership for group '" << rule.GetName() << "' to service '" << service->GetName() << "' for rule " << di;
58 Log(LogDebug, "icinga", msgbuf2.str());
60 String group_name = rule.GetName();
61 ServiceGroup::Ptr group = ServiceGroup::GetByName(group_name);
64 Log(LogCritical, "icinga", "Invalid membership assignment. Group '" + group_name + "' does not exist.");
68 /* assign service group membership */
69 group->ResolveGroupMembership(service, true);
71 /* update groups attribute for apply */
72 service->AddGroup(group_name);
77 void ServiceGroup::EvaluateObjectRules(const std::vector<ObjectRule>& rules)
79 BOOST_FOREACH(const ObjectRule& rule, rules) {
80 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
81 CONTEXT("Evaluating group membership in '" + rule.GetName() + "' for service '" + service->GetName() + "'");
83 EvaluateObjectRule(service, rule);
88 std::set<Service::Ptr> ServiceGroup::GetMembers(void) const
90 boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
94 void ServiceGroup::AddMember(const Service::Ptr& service)
96 boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
97 m_Members.insert(service);
100 void ServiceGroup::RemoveMember(const Service::Ptr& service)
102 boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
103 m_Members.erase(service);
106 bool ServiceGroup::ResolveGroupMembership(Service::Ptr const& service, bool add, int rstack) {
108 if (add && rstack > 20) {
109 Log(LogWarning, "icinga", "Too many nested groups for group '" + GetName() + "': Service '" +
110 service->GetName() + "' membership assignment failed.");
115 Array::Ptr groups = GetGroups();
117 if (groups && groups->GetLength() > 0) {
118 ObjectLock olock(groups);
120 BOOST_FOREACH(const String& name, groups) {
121 ServiceGroup::Ptr group = ServiceGroup::GetByName(name);
123 if (group && !group->ResolveGroupMembership(service, add, rstack + 1))
131 RemoveMember(service);