]> granicus.if.org Git - icinga2/blob - lib/icinga/servicegroup.cpp
Merge pull request #6699 from Icinga/feature/update-icinga-com
[icinga2] / lib / icinga / servicegroup.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://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 groupName = group->GetName();
41
42         CONTEXT("Evaluating rule for group '" + groupName + "'");
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 '" << groupName << "' to service '" << service->GetName() << "'";
57
58         Array::Ptr groups = service->GetGroups();
59
60         if (groups && !groups->Contains(groupName))
61                 groups->Add(groupName);
62
63         return true;
64 }
65
66 void ServiceGroup::EvaluateObjectRules(const Service::Ptr& service)
67 {
68         CONTEXT("Evaluating group membership for service '" + service->GetName() + "'");
69
70         for (const ConfigItem::Ptr& group : ConfigItem::GetItems(ServiceGroup::TypeInstance))
71         {
72                 if (!group->GetFilter())
73                         continue;
74
75                 EvaluateObjectRule(service, group);
76         }
77 }
78
79 std::set<Service::Ptr> ServiceGroup::GetMembers() const
80 {
81         boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
82         return m_Members;
83 }
84
85 void ServiceGroup::AddMember(const Service::Ptr& service)
86 {
87         service->AddGroup(GetName());
88
89         boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
90         m_Members.insert(service);
91 }
92
93 void ServiceGroup::RemoveMember(const Service::Ptr& service)
94 {
95         boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
96         m_Members.erase(service);
97 }
98
99 bool ServiceGroup::ResolveGroupMembership(const Service::Ptr& service, bool add, int rstack) {
100
101         if (add && rstack > 20) {
102                 Log(LogWarning, "ServiceGroup")
103                         << "Too many nested groups for group '" << GetName() << "': Service '"
104                         << service->GetName() << "' membership assignment failed.";
105
106                 return false;
107         }
108
109         Array::Ptr groups = GetGroups();
110
111         if (groups && groups->GetLength() > 0) {
112                 ObjectLock olock(groups);
113
114                 for (const String& name : groups) {
115                         ServiceGroup::Ptr group = ServiceGroup::GetByName(name);
116
117                         if (group && !group->ResolveGroupMembership(service, add, rstack + 1))
118                                 return false;
119                 }
120         }
121
122         if (add)
123                 AddMember(service);
124         else
125                 RemoveMember(service);
126
127         return true;
128 }