]> granicus.if.org Git - icinga2/blob - lib/icinga/servicegroup.cpp
Make attribute lookups O(1).
[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 boost::mutex ServiceGroup::m_Mutex;
25 map<String, vector<Service::WeakPtr> > ServiceGroup::m_MembersCache;
26 bool ServiceGroup::m_MembersCacheValid;
27
28 REGISTER_TYPE(ServiceGroup, NULL);
29
30 ServiceGroup::ServiceGroup(const Dictionary::Ptr& properties)
31         : DynamicObject(properties)
32 {
33         RegisterAttribute("display_name", Attribute_Config, &m_DisplayName);
34         RegisterAttribute("notes_url", Attribute_Config, &m_NotesUrl);
35         RegisterAttribute("action_url", Attribute_Config, &m_ActionUrl);
36 }
37
38 String ServiceGroup::GetDisplayName(void) const
39 {
40         if (!m_DisplayName.Get().IsEmpty())
41                 return m_DisplayName;
42         else
43                 return GetName();
44 }
45
46 String ServiceGroup::GetNotesUrl(void) const
47 {
48         return m_NotesUrl;
49 }
50
51 String ServiceGroup::GetActionUrl(void) const
52 {
53         return m_ActionUrl;
54 }
55
56 /**
57  * @threadsafety Always.
58  */
59 bool ServiceGroup::Exists(const String& name)
60 {
61         return (DynamicObject::GetObject("ServiceGroup", name));
62 }
63
64 /**
65  * @threadsafety Always.
66  */
67 ServiceGroup::Ptr ServiceGroup::GetByName(const String& name)
68 {
69         DynamicObject::Ptr configObject = DynamicObject::GetObject("ServiceGroup", name);
70
71         if (!configObject)
72                 BOOST_THROW_EXCEPTION(invalid_argument("ServiceGroup '" + name + "' does not exist."));
73
74         return dynamic_pointer_cast<ServiceGroup>(configObject);
75 }
76
77 set<Service::Ptr> ServiceGroup::GetMembers(const ServiceGroup::Ptr& self)
78 {
79         String name;
80
81         {
82                 ObjectLock olock(self);
83                 name = self->GetName();
84         }
85
86         set<Service::Ptr> services;
87
88         {
89                 boost::mutex::scoped_lock lock(m_Mutex);
90
91                 ValidateMembersCache();
92
93                 BOOST_FOREACH(const Service::WeakPtr& svc, m_MembersCache[name]) {
94                         Service::Ptr service = svc.lock();
95
96                         if (!service)
97                                 continue;
98
99                         services.insert(service);
100                 }
101         }
102
103         return services;
104 }
105
106 void ServiceGroup::InvalidateMembersCache(void)
107 {
108         boost::mutex::scoped_lock lock(m_Mutex);
109         m_MembersCacheValid = false;
110         m_MembersCache.clear();
111 }
112
113 /**
114  * @threadsafety Caller must hold m_Mutex.
115  */
116 void ServiceGroup::ValidateMembersCache(void)
117 {
118         if (m_MembersCacheValid)
119                 return;
120
121         m_MembersCache.clear();
122
123         BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
124                 const Service::Ptr& service = static_pointer_cast<Service>(object);
125                 ObjectLock olock(service);
126
127                 Dictionary::Ptr dict;
128                 dict = service->GetGroups();
129
130                 if (dict) {
131                         ObjectLock mlock(dict);
132                         Value servicegroup;
133                         BOOST_FOREACH(tie(tuples::ignore, servicegroup), dict) {
134                                 if (!ServiceGroup::Exists(servicegroup))
135                                         Logger::Write(LogWarning, "icinga", "Service group '" + static_cast<String>(servicegroup) + "' used but not defined.");
136
137                                 m_MembersCache[servicegroup].push_back(service);
138                         }
139                 }
140         }
141
142         m_MembersCacheValid = true;
143 }