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.hpp"
21 #include "config/objectrule.hpp"
22 #include "base/dynamictype.hpp"
23 #include "base/objectlock.hpp"
24 #include "base/logger.hpp"
25 #include "base/context.hpp"
26 #include "base/workqueue.hpp"
27 #include <boost/foreach.hpp>
29 using namespace icinga;
31 REGISTER_TYPE(ServiceGroup);
33 INITIALIZE_ONCE(&ServiceGroup::RegisterObjectRuleHandler);
35 void ServiceGroup::RegisterObjectRuleHandler(void)
37 ObjectRule::RegisterType("ServiceGroup", &ServiceGroup::EvaluateObjectRules);
40 bool ServiceGroup::EvaluateObjectRuleOne(const Service::Ptr& service, const ObjectRule& rule)
42 DebugInfo di = rule.GetDebugInfo();
44 std::ostringstream msgbuf;
45 msgbuf << "Evaluating 'object' rule (" << di << ")";
46 CONTEXT(msgbuf.str());
48 Host::Ptr host = service->GetHost();
50 Dictionary::Ptr locals = make_shared<Dictionary>();
51 locals->Set("host", host);
52 locals->Set("service", service);
54 if (!rule.EvaluateFilter(locals))
57 Log(LogDebug, "ServiceGroup")
58 << "Assigning membership for group '" << rule.GetName() << "' to service '" << service->GetName() << "' for rule " << di;
60 String group_name = rule.GetName();
61 ServiceGroup::Ptr group = ServiceGroup::GetByName(group_name);
64 Log(LogCritical, "ServiceGroup")
65 << "Invalid membership assignment. Group '" << group_name << "' does not exist.";
69 /* assign service group membership */
70 group->ResolveGroupMembership(service, true);
72 /* update groups attribute for apply */
73 service->AddGroup(group_name);
78 void ServiceGroup::EvaluateObjectRule(const ObjectRule& rule)
80 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjectsByType<Service>()) {
81 CONTEXT("Evaluating group membership in '" + rule.GetName() + "' for service '" + service->GetName() + "'");
83 EvaluateObjectRuleOne(service, rule);
87 void ServiceGroup::EvaluateObjectRules(const std::vector<ObjectRule>& rules)
89 ParallelWorkQueue upq;
91 BOOST_FOREACH(const ObjectRule& rule, rules) {
92 upq.Enqueue(boost::bind(ServiceGroup::EvaluateObjectRule, boost::cref(rule)));
98 std::set<Service::Ptr> ServiceGroup::GetMembers(void) const
100 boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
104 void ServiceGroup::AddMember(const Service::Ptr& service)
106 boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
107 m_Members.insert(service);
110 void ServiceGroup::RemoveMember(const Service::Ptr& service)
112 boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
113 m_Members.erase(service);
116 bool ServiceGroup::ResolveGroupMembership(const Service::Ptr& service, bool add, int rstack) {
118 if (add && rstack > 20) {
119 Log(LogWarning, "ServiceGroup")
120 << "Too many nested groups for group '" << GetName() << "': Service '"
121 << service->GetName() << "' membership assignment failed.";
126 Array::Ptr groups = GetGroups();
128 if (groups && groups->GetLength() > 0) {
129 ObjectLock olock(groups);
131 BOOST_FOREACH(const String& name, groups) {
132 ServiceGroup::Ptr group = ServiceGroup::GetByName(name);
134 if (group && !group->ResolveGroupMembership(service, add, rstack + 1))
142 RemoveMember(service);