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;
28 REGISTER_TYPE(ServiceGroup, NULL);
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 String ServiceGroup::GetDisplayName(void) const
40 if (!m_DisplayName.Get().IsEmpty())
46 String ServiceGroup::GetNotesUrl(void) const
51 String ServiceGroup::GetActionUrl(void) const
57 * @threadsafety Always.
59 bool ServiceGroup::Exists(const String& name)
61 return (DynamicObject::GetObject("ServiceGroup", name));
65 * @threadsafety Always.
67 ServiceGroup::Ptr ServiceGroup::GetByName(const String& name)
69 DynamicObject::Ptr configObject = DynamicObject::GetObject("ServiceGroup", name);
72 BOOST_THROW_EXCEPTION(invalid_argument("ServiceGroup '" + name + "' does not exist."));
74 return dynamic_pointer_cast<ServiceGroup>(configObject);
77 set<Service::Ptr> ServiceGroup::GetMembers(const ServiceGroup::Ptr& self)
82 ObjectLock olock(self);
83 name = self->GetName();
86 set<Service::Ptr> services;
89 boost::mutex::scoped_lock lock(m_Mutex);
91 ValidateMembersCache();
93 BOOST_FOREACH(const Service::WeakPtr& svc, m_MembersCache[name]) {
94 Service::Ptr service = svc.lock();
99 services.insert(service);
106 void ServiceGroup::InvalidateMembersCache(void)
108 boost::mutex::scoped_lock lock(m_Mutex);
109 m_MembersCacheValid = false;
110 m_MembersCache.clear();
114 * @threadsafety Caller must hold m_Mutex.
116 void ServiceGroup::ValidateMembersCache(void)
118 if (m_MembersCacheValid)
121 m_MembersCache.clear();
123 BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
124 const Service::Ptr& service = static_pointer_cast<Service>(object);
125 ObjectLock olock(service);
127 Dictionary::Ptr dict;
128 dict = service->GetGroups();
131 ObjectLock mlock(dict);
133 BOOST_FOREACH(tie(tuples::ignore, servicegroup), dict) {
134 if (!ServiceGroup::Exists(servicegroup))
135 Logger::Write(LogWarning, "icinga", "Service group '" + static_cast<String>(servicegroup) + "' used but not defined.");
137 m_MembersCache[servicegroup].push_back(service);
142 m_MembersCacheValid = true;