]> granicus.if.org Git - icinga2/blob - lib/icinga/servicegroup.cpp
Fix build problems with Oracle Solaris Studio
[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_fwd.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         std::ostringstream msgbuf2;
58         msgbuf2 << "Assigning membership for group '" << rule.GetName() << "' to service '" << service->GetName() << "' for rule " << di;
59         Log(LogDebug, "ServiceGroup", msgbuf2.str());
60
61         String group_name = rule.GetName();
62         ServiceGroup::Ptr group = ServiceGroup::GetByName(group_name);
63
64         if (!group) {
65                 Log(LogCritical, "ServiceGroup", "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(Service::Ptr const& service, bool add, int rstack) {
117
118         if (add && rstack > 20) {
119                 Log(LogWarning, "ServiceGroup", "Too many nested groups for group '" + GetName() + "': Service '" +
120                     service->GetName() + "' membership assignment failed.");
121
122                 return false;
123         }
124
125         Array::Ptr groups = GetGroups();
126
127         if (groups && groups->GetLength() > 0) {
128                 ObjectLock olock(groups);
129
130                 BOOST_FOREACH(const String& name, groups) {
131                         ServiceGroup::Ptr group = ServiceGroup::GetByName(name);
132
133                         if (group && !group->ResolveGroupMembership(service, add, rstack + 1))
134                                 return false;
135                 }
136         }
137
138         if (add)
139                 AddMember(service);
140         else
141                 RemoveMember(service);
142
143         return true;
144 }