]> granicus.if.org Git - icinga2/blob - lib/icinga/servicegroup.cpp
Remove the built-in attributes notes_url and action_url.
[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 #include "icinga/servicegroup.h"
22 #include "base/dynamictype.h"
23 #include "base/objectlock.h"
24 #include "base/logger_fwd.h"
25 #include "base/timer.h"
26 #include "base/utility.h"
27 #include <boost/smart_ptr/make_shared.hpp>
28 #include <boost/foreach.hpp>
29
30 using namespace icinga;
31
32 static boost::mutex l_Mutex;
33 static std::map<String, std::vector<Service::WeakPtr> > l_MembersCache;
34 static bool l_MembersCacheNeedsUpdate = false;
35 static Timer::Ptr l_MembersCacheTimer;
36
37 REGISTER_TYPE(ServiceGroup);
38
39 ServiceGroup::ServiceGroup(const Dictionary::Ptr& serializedUpdate)
40         : DynamicObject(serializedUpdate)
41 {
42         RegisterAttribute("display_name", Attribute_Config, &m_DisplayName);
43 }
44
45 ServiceGroup::~ServiceGroup(void)
46 {
47         InvalidateMembersCache();
48 }
49
50 void ServiceGroup::OnRegistrationCompleted(void)
51 {
52         ASSERT(!OwnsLock());
53
54         InvalidateMembersCache();
55 }
56
57 String ServiceGroup::GetDisplayName(void) const
58 {
59         if (!m_DisplayName.Get().IsEmpty())
60                 return m_DisplayName;
61         else
62                 return GetName();
63 }
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(std::invalid_argument("ServiceGroup '" + name + "' does not exist."));
71
72         return dynamic_pointer_cast<ServiceGroup>(configObject);
73 }
74
75 std::set<Service::Ptr> ServiceGroup::GetMembers(void) const
76 {
77         std::set<Service::Ptr> services;
78
79         {
80                 boost::mutex::scoped_lock lock(l_Mutex);
81
82                 BOOST_FOREACH(const Service::WeakPtr& wservice, l_MembersCache[GetName()]) {
83                         Service::Ptr service = wservice.lock();
84
85                         if (!service)
86                                 continue;
87
88                         services.insert(service);
89                 }
90         }
91
92         return services;
93 }
94
95 void ServiceGroup::InvalidateMembersCache(void)
96 {
97         boost::mutex::scoped_lock lock(l_Mutex);
98
99         if (l_MembersCacheNeedsUpdate)
100                 return; /* Someone else has already requested a refresh. */
101
102         if (!l_MembersCacheTimer) {
103                 l_MembersCacheTimer = boost::make_shared<Timer>();
104                 l_MembersCacheTimer->SetInterval(0.5);
105                 l_MembersCacheTimer->OnTimerExpired.connect(boost::bind(&ServiceGroup::RefreshMembersCache));
106                 l_MembersCacheTimer->Start();
107         }
108
109         l_MembersCacheNeedsUpdate = true;
110 }
111
112 void ServiceGroup::RefreshMembersCache(void)
113 {
114         {
115                 boost::mutex::scoped_lock lock(l_Mutex);
116
117                 if (!l_MembersCacheNeedsUpdate)
118                         return;
119
120                 l_MembersCacheNeedsUpdate = false;
121         }
122
123         Log(LogDebug, "icinga", "Updating ServiceGroup members cache.");
124
125         std::map<String, std::vector<Service::WeakPtr> > newMembersCache;
126
127         BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
128                 const Service::Ptr& service = static_pointer_cast<Service>(object);
129
130                 Array::Ptr groups = service->GetGroups();
131
132                 if (groups) {
133                         ObjectLock mlock(groups);
134                         BOOST_FOREACH(const Value& group, groups) {
135                                 newMembersCache[group].push_back(service);
136                         }
137                 }
138         }
139
140         boost::mutex::scoped_lock lock(l_Mutex);
141         l_MembersCache.swap(newMembersCache);
142 }