]> granicus.if.org Git - icinga2/blob - lib/icinga/servicegroup.cpp
Performance improvements for *Group::GetMembers().
[icinga2] / lib / icinga / servicegroup.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 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 "i2-icinga.h"
21
22 using namespace icinga;
23
24 map<String, vector<Service::WeakPtr> > ServiceGroup::m_MembersCache;
25 bool ServiceGroup::m_MembersCacheValid;
26
27 static AttributeDescription serviceGroupAttributes[] = {
28         { "alias", Attribute_Config },
29         { "notes_url", Attribute_Config },
30         { "action_url",  Attribute_Config }
31 };
32
33 REGISTER_TYPE(ServiceGroup, serviceGroupAttributes);
34
35 String ServiceGroup::GetAlias(void) const
36 {
37         String value = Get("alias");
38
39         if (!value.IsEmpty())
40                 return value;
41         else
42                 return GetName();
43 }
44
45 String ServiceGroup::GetNotesUrl(void) const
46 {
47         return Get("notes_url");
48 }
49
50 String ServiceGroup::GetActionUrl(void) const
51 {
52         return Get("action_url");
53 }
54
55 bool ServiceGroup::Exists(const String& name)
56 {
57         return (DynamicObject::GetObject("ServiceGroup", name));
58 }
59
60 ServiceGroup::Ptr ServiceGroup::GetByName(const String& name)
61 {
62         DynamicObject::Ptr configObject = DynamicObject::GetObject("ServiceGroup", name);
63
64         if (!configObject)
65                 throw_exception(invalid_argument("ServiceGroup '" + name + "' does not exist."));
66
67         return dynamic_pointer_cast<ServiceGroup>(configObject);
68 }
69
70 set<Service::Ptr> ServiceGroup::GetMembers(void) const
71 {
72         set<Service::Ptr> services;
73
74         ValidateMembersCache();
75
76         BOOST_FOREACH(const Service::WeakPtr& svc, m_MembersCache[GetName()]) {
77                 Service::Ptr service = svc.lock();
78
79                 if (!service)
80                         continue;
81
82                 services.insert(service);
83         }
84
85         return services;
86 }
87
88 void ServiceGroup::InvalidateMembersCache(void)
89 {
90         m_MembersCacheValid = false;
91         m_MembersCache.clear();
92 }
93
94 void ServiceGroup::ValidateMembersCache(void)
95 {
96         if (m_MembersCacheValid)
97                 return;
98
99         m_MembersCache.clear();
100
101         DynamicObject::Ptr object;
102         BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Service")->GetObjects()) {
103                 const Service::Ptr& service = static_pointer_cast<Service>(object);
104
105                 Dictionary::Ptr dict;
106                 dict = service->GetGroups();
107
108                 if (dict) {
109                         Value servicegroup;
110                         BOOST_FOREACH(tie(tuples::ignore, servicegroup), dict) {
111                                 if (!ServiceGroup::Exists(servicegroup))
112                                         Logger::Write(LogWarning, "icinga", "Service group '" + static_cast<String>(servicegroup) + "' used but not defined.");
113
114                                 m_MembersCache[servicegroup].push_back(service);
115                         }
116                 }
117         }
118
119         m_MembersCacheValid = true;
120 }
121