1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
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 "icinga/servicegroup-ti.cpp"
22 #include "config/objectrule.hpp"
23 #include "config/configitem.hpp"
24 #include "base/configtype.hpp"
25 #include "base/objectlock.hpp"
26 #include "base/logger.hpp"
27 #include "base/context.hpp"
28 #include "base/workqueue.hpp"
30 using namespace icinga;
32 REGISTER_TYPE(ServiceGroup);
34 INITIALIZE_ONCE([]() {
35 ObjectRule::RegisterType("ServiceGroup");
38 bool ServiceGroup::EvaluateObjectRule(const Service::Ptr& service, const ConfigItem::Ptr& group)
40 String groupName = group->GetName();
42 CONTEXT("Evaluating rule for group '" + groupName + "'");
44 Host::Ptr host = service->GetHost();
46 ScriptFrame frame(true);
47 if (group->GetScope())
48 group->GetScope()->CopyTo(frame.Locals);
49 frame.Locals->Set("host", host);
50 frame.Locals->Set("service", service);
52 if (!group->GetFilter()->Evaluate(frame).GetValue().ToBool())
55 Log(LogDebug, "ServiceGroup")
56 << "Assigning membership for group '" << groupName << "' to service '" << service->GetName() << "'";
58 Array::Ptr groups = service->GetGroups();
60 if (groups && !groups->Contains(groupName))
61 groups->Add(groupName);
66 void ServiceGroup::EvaluateObjectRules(const Service::Ptr& service)
68 CONTEXT("Evaluating group membership for service '" + service->GetName() + "'");
70 for (const ConfigItem::Ptr& group : ConfigItem::GetItems(ServiceGroup::TypeInstance))
72 if (!group->GetFilter())
75 EvaluateObjectRule(service, group);
79 std::set<Service::Ptr> ServiceGroup::GetMembers() const
81 boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
85 void ServiceGroup::AddMember(const Service::Ptr& service)
87 service->AddGroup(GetName());
89 boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
90 m_Members.insert(service);
93 void ServiceGroup::RemoveMember(const Service::Ptr& service)
95 boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
96 m_Members.erase(service);
99 bool ServiceGroup::ResolveGroupMembership(const Service::Ptr& service, bool add, int rstack) {
101 if (add && rstack > 20) {
102 Log(LogWarning, "ServiceGroup")
103 << "Too many nested groups for group '" << GetName() << "': Service '"
104 << service->GetName() << "' membership assignment failed.";
109 Array::Ptr groups = GetGroups();
111 if (groups && groups->GetLength() > 0) {
112 ObjectLock olock(groups);
114 for (const String& name : groups) {
115 ServiceGroup::Ptr group = ServiceGroup::GetByName(name);
117 if (group && !group->ResolveGroupMembership(service, add, rstack + 1))
125 RemoveMember(service);