]> granicus.if.org Git - icinga2/blob - lib/icinga/servicegroup.cpp
Merge pull request #5979 from Icinga/feature/enhance-console-5595
[icinga2] / lib / icinga / servicegroup.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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"
29
30 using namespace icinga;
31
32 REGISTER_TYPE(ServiceGroup);
33
34 INITIALIZE_ONCE([]() {
35         ObjectRule::RegisterType("ServiceGroup");
36 });
37
38 bool ServiceGroup::EvaluateObjectRule(const Service::Ptr& service, const ConfigItem::Ptr& group)
39 {
40         String group_name = group->GetName();
41
42         CONTEXT("Evaluating rule for group '" + group_name + "'");
43
44         Host::Ptr host = service->GetHost();
45
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);
51
52         if (!group->GetFilter()->Evaluate(frame).GetValue().ToBool())
53                 return false;
54
55         Log(LogDebug, "ServiceGroup")
56                 << "Assigning membership for group '" << group_name << "' to service '" << service->GetName() << "'";
57
58         Array::Ptr groups = service->GetGroups();
59         groups->Add(group_name);
60
61         return true;
62 }
63
64 void ServiceGroup::EvaluateObjectRules(const Service::Ptr& service)
65 {
66         CONTEXT("Evaluating group membership for service '" + service->GetName() + "'");
67
68         for (const ConfigItem::Ptr& group : ConfigItem::GetItems(ServiceGroup::TypeInstance))
69         {
70                 if (!group->GetFilter())
71                         continue;
72
73                 EvaluateObjectRule(service, group);
74         }
75 }
76
77 std::set<Service::Ptr> ServiceGroup::GetMembers() const
78 {
79         boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
80         return m_Members;
81 }
82
83 void ServiceGroup::AddMember(const Service::Ptr& service)
84 {
85         service->AddGroup(GetName());
86
87         boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
88         m_Members.insert(service);
89 }
90
91 void ServiceGroup::RemoveMember(const Service::Ptr& service)
92 {
93         boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
94         m_Members.erase(service);
95 }
96
97 bool ServiceGroup::ResolveGroupMembership(const Service::Ptr& service, bool add, int rstack) {
98
99         if (add && rstack > 20) {
100                 Log(LogWarning, "ServiceGroup")
101                         << "Too many nested groups for group '" << GetName() << "': Service '"
102                         << service->GetName() << "' membership assignment failed.";
103
104                 return false;
105         }
106
107         Array::Ptr groups = GetGroups();
108
109         if (groups && groups->GetLength() > 0) {
110                 ObjectLock olock(groups);
111
112                 for (const String& name : groups) {
113                         ServiceGroup::Ptr group = ServiceGroup::GetByName(name);
114
115                         if (group && !group->ResolveGroupMembership(service, add, rstack + 1))
116                                 return false;
117                 }
118         }
119
120         if (add)
121                 AddMember(service);
122         else
123                 RemoveMember(service);
124
125         return true;
126 }