1 /******************************************************************************
3 * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
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. *
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. *
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 ******************************************************************************/
20 #include "i2-icinga.h"
22 using namespace icinga;
24 boost::mutex ServiceGroup::m_Mutex;
25 map<String, vector<Service::WeakPtr> > ServiceGroup::m_MembersCache;
26 bool ServiceGroup::m_MembersCacheNeedsUpdate = false;
27 Timer::Ptr ServiceGroup::m_MembersCacheTimer;
29 REGISTER_TYPE(ServiceGroup);
31 ServiceGroup::ServiceGroup(const Dictionary::Ptr& properties)
32 : DynamicObject(properties)
34 RegisterAttribute("display_name", Attribute_Config, &m_DisplayName);
35 RegisterAttribute("notes_url", Attribute_Config, &m_NotesUrl);
36 RegisterAttribute("action_url", Attribute_Config, &m_ActionUrl);
39 ServiceGroup::~ServiceGroup(void)
41 InvalidateMembersCache();
45 * @threadsafety Always.
47 void ServiceGroup::OnRegistrationCompleted(void)
51 InvalidateMembersCache();
55 * @threadsafety Always.
57 String ServiceGroup::GetDisplayName(void) const
59 if (!m_DisplayName.Get().IsEmpty())
66 * @threadsafety Always.
68 String ServiceGroup::GetNotesUrl(void) const
74 * @threadsafety Always.
76 String ServiceGroup::GetActionUrl(void) const
82 * @threadsafety Always.
84 ServiceGroup::Ptr ServiceGroup::GetByName(const String& name)
86 DynamicObject::Ptr configObject = DynamicObject::GetObject("ServiceGroup", name);
89 BOOST_THROW_EXCEPTION(invalid_argument("ServiceGroup '" + name + "' does not exist."));
91 return dynamic_pointer_cast<ServiceGroup>(configObject);
95 * @threadsafety Always.
97 set<Service::Ptr> ServiceGroup::GetMembers(void) const
99 set<Service::Ptr> services;
102 boost::mutex::scoped_lock lock(m_Mutex);
104 BOOST_FOREACH(const Service::WeakPtr& wservice, m_MembersCache[GetName()]) {
105 Service::Ptr service = wservice.lock();
110 services.insert(service);
118 * @threadsafety Always.
120 void ServiceGroup::InvalidateMembersCache(void)
122 boost::mutex::scoped_lock lock(m_Mutex);
124 if (m_MembersCacheNeedsUpdate)
125 return; /* Someone else has already requested a refresh. */
127 if (!m_MembersCacheTimer) {
128 m_MembersCacheTimer = boost::make_shared<Timer>();
129 m_MembersCacheTimer->SetInterval(0.5);
130 m_MembersCacheTimer->OnTimerExpired.connect(boost::bind(&ServiceGroup::RefreshMembersCache));
133 m_MembersCacheTimer->Start();
134 m_MembersCacheNeedsUpdate = true;
138 * @threadsafety Always.
140 void ServiceGroup::RefreshMembersCache(void)
143 boost::mutex::scoped_lock lock(m_Mutex);
145 assert(m_MembersCacheNeedsUpdate);
146 m_MembersCacheTimer->Stop();
147 m_MembersCacheNeedsUpdate = false;
150 map<String, vector<Service::WeakPtr> > newMembersCache;
152 BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
153 const Service::Ptr& service = static_pointer_cast<Service>(object);
155 Dictionary::Ptr dict;
156 dict = service->GetGroups();
159 ObjectLock mlock(dict);
161 BOOST_FOREACH(tie(tuples::ignore, servicegroup), dict) {
162 newMembersCache[servicegroup].push_back(service);
167 boost::mutex::scoped_lock lock(m_Mutex);
168 m_MembersCache.swap(newMembersCache);