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