]> granicus.if.org Git - icinga2/blob - lib/icinga/servicegroup.cpp
Fix code style
[icinga2] / lib / icinga / servicegroup.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://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 "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>
28
29 using namespace icinga;
30
31 REGISTER_TYPE(ServiceGroup);
32
33 INITIALIZE_ONCE(&ServiceGroup::RegisterObjectRuleHandler);
34
35 void ServiceGroup::RegisterObjectRuleHandler(void)
36 {
37         ObjectRule::RegisterType("ServiceGroup", &ServiceGroup::EvaluateObjectRules);
38 }
39
40 bool ServiceGroup::EvaluateObjectRuleOne(const Service::Ptr& service, const ObjectRule& rule)
41 {
42         DebugInfo di = rule.GetDebugInfo();
43
44         std::ostringstream msgbuf;
45         msgbuf << "Evaluating 'object' rule (" << di << ")";
46         CONTEXT(msgbuf.str());
47
48         Host::Ptr host = service->GetHost();
49
50         Dictionary::Ptr locals = make_shared<Dictionary>();
51         locals->Set("host", host);
52         locals->Set("service", service);
53
54         if (!rule.EvaluateFilter(locals))
55                 return false;
56
57         Log(LogDebug, "ServiceGroup")
58             << "Assigning membership for group '" << rule.GetName() << "' to service '" << service->GetName() << "' for rule " << di;
59
60         String group_name = rule.GetName();
61         ServiceGroup::Ptr group = ServiceGroup::GetByName(group_name);
62
63         if (!group) {
64                 Log(LogCritical, "ServiceGroup")
65                     << "Invalid membership assignment. Group '" << group_name << "' does not exist.";
66                 return false;
67         }
68
69         /* assign service group membership */
70         group->ResolveGroupMembership(service, true);
71
72         /* update groups attribute for apply */
73         service->AddGroup(group_name);
74
75         return true;
76 }
77
78 void ServiceGroup::EvaluateObjectRule(const ObjectRule& rule)
79 {
80         BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjectsByType<Service>()) {
81                 CONTEXT("Evaluating group membership in '" + rule.GetName() + "' for service '" + service->GetName() + "'");
82
83                 EvaluateObjectRuleOne(service, rule);
84         }
85 }
86
87 void ServiceGroup::EvaluateObjectRules(const std::vector<ObjectRule>& rules)
88 {
89         ParallelWorkQueue upq;
90
91         BOOST_FOREACH(const ObjectRule& rule, rules) {
92                 upq.Enqueue(boost::bind(ServiceGroup::EvaluateObjectRule, boost::cref(rule)));
93         }
94
95         upq.Join();
96 }
97
98 std::set<Service::Ptr> ServiceGroup::GetMembers(void) const
99 {
100         boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
101         return m_Members;
102 }
103
104 void ServiceGroup::AddMember(const Service::Ptr& service)
105 {
106         boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
107         m_Members.insert(service);
108 }
109
110 void ServiceGroup::RemoveMember(const Service::Ptr& service)
111 {
112         boost::mutex::scoped_lock lock(m_ServiceGroupMutex);
113         m_Members.erase(service);
114 }
115
116 bool ServiceGroup::ResolveGroupMembership(const Service::Ptr& service, bool add, int rstack) {
117
118         if (add && rstack > 20) {
119                 Log(LogWarning, "ServiceGroup")
120                     << "Too many nested groups for group '" << GetName() << "': Service '"
121                     << service->GetName() << "' membership assignment failed.";
122
123                 return false;
124         }
125
126         Array::Ptr groups = GetGroups();
127
128         if (groups && groups->GetLength() > 0) {
129                 ObjectLock olock(groups);
130
131                 BOOST_FOREACH(const String& name, groups) {
132                         ServiceGroup::Ptr group = ServiceGroup::GetByName(name);
133
134                         if (group && !group->ResolveGroupMembership(service, add, rstack + 1))
135                                 return false;
136                 }
137         }
138
139         if (add)
140                 AddMember(service);
141         else
142                 RemoveMember(service);
143
144         return true;
145 }