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