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