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_MembersCacheValid = true;
28 REGISTER_TYPE(ServiceGroup);
30 ServiceGroup::ServiceGroup(const Dictionary::Ptr& properties)
31 : DynamicObject(properties)
33 RegisterAttribute("display_name", Attribute_Config, &m_DisplayName);
34 RegisterAttribute("notes_url", Attribute_Config, &m_NotesUrl);
35 RegisterAttribute("action_url", Attribute_Config, &m_ActionUrl);
38 ServiceGroup::~ServiceGroup(void)
40 InvalidateMembersCache();
44 * @threadsafety Always.
46 void ServiceGroup::OnRegistrationCompleted(void)
50 InvalidateMembersCache();
54 * @threadsafety Always.
56 String ServiceGroup::GetDisplayName(void) const
58 if (!m_DisplayName.Get().IsEmpty())
65 * @threadsafety Always.
67 String ServiceGroup::GetNotesUrl(void) const
73 * @threadsafety Always.
75 String ServiceGroup::GetActionUrl(void) const
81 * @threadsafety Always.
83 ServiceGroup::Ptr ServiceGroup::GetByName(const String& name)
85 DynamicObject::Ptr configObject = DynamicObject::GetObject("ServiceGroup", name);
88 BOOST_THROW_EXCEPTION(invalid_argument("ServiceGroup '" + name + "' does not exist."));
90 return dynamic_pointer_cast<ServiceGroup>(configObject);
94 * @threadsafety Always.
96 set<Service::Ptr> ServiceGroup::GetMembers(void) const
98 set<Service::Ptr> services;
101 boost::mutex::scoped_lock lock(m_Mutex);
103 BOOST_FOREACH(const Service::WeakPtr& wservice, m_MembersCache[GetName()]) {
104 Service::Ptr service = wservice.lock();
109 services.insert(service);
117 * @threadsafety Always.
119 void ServiceGroup::InvalidateMembersCache(void)
121 boost::mutex::scoped_lock lock(m_Mutex);
123 if (m_MembersCacheValid)
124 Utility::QueueAsyncCallback(boost::bind(&ServiceGroup::RefreshMembersCache));
126 m_MembersCacheValid = false;
130 * @threadsafety Always.
132 void ServiceGroup::RefreshMembersCache(void)
135 boost::mutex::scoped_lock lock(m_Mutex);
137 if (m_MembersCacheValid)
140 m_MembersCacheValid = true;
143 map<String, vector<Service::WeakPtr> > newMembersCache;
145 BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
146 const Service::Ptr& service = static_pointer_cast<Service>(object);
148 Dictionary::Ptr dict;
149 dict = service->GetGroups();
152 ObjectLock mlock(dict);
154 BOOST_FOREACH(tie(tuples::ignore, servicegroup), dict) {
155 newMembersCache[servicegroup].push_back(service);
160 boost::mutex::scoped_lock lock(m_Mutex);
161 m_MembersCache.swap(newMembersCache);