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