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)
34 String ServiceGroup::GetDisplayName(void) const
36 String value = Get("display_name");
44 String ServiceGroup::GetNotesUrl(void) const
46 return Get("notes_url");
49 String ServiceGroup::GetActionUrl(void) const
51 return Get("action_url");
55 * @threadsafety Always.
57 bool ServiceGroup::Exists(const String& name)
59 return (DynamicObject::GetObject("ServiceGroup", name));
63 * @threadsafety Always.
65 ServiceGroup::Ptr ServiceGroup::GetByName(const String& name)
67 DynamicObject::Ptr configObject = DynamicObject::GetObject("ServiceGroup", name);
70 BOOST_THROW_EXCEPTION(invalid_argument("ServiceGroup '" + name + "' does not exist."));
72 return dynamic_pointer_cast<ServiceGroup>(configObject);
75 set<Service::Ptr> ServiceGroup::GetMembers(const ServiceGroup::Ptr& self)
80 ObjectLock olock(self);
81 name = self->GetName();
84 set<Service::Ptr> services;
87 boost::mutex::scoped_lock lock(m_Mutex);
89 ValidateMembersCache();
91 BOOST_FOREACH(const Service::WeakPtr& svc, m_MembersCache[name]) {
92 Service::Ptr service = svc.lock();
97 services.insert(service);
104 void ServiceGroup::InvalidateMembersCache(void)
106 boost::mutex::scoped_lock lock(m_Mutex);
107 m_MembersCacheValid = false;
108 m_MembersCache.clear();
112 * @threadsafety Caller must hold m_Mutex.
114 void ServiceGroup::ValidateMembersCache(void)
116 if (m_MembersCacheValid)
119 m_MembersCache.clear();
121 BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
122 const Service::Ptr& service = static_pointer_cast<Service>(object);
123 ObjectLock olock(service);
125 Dictionary::Ptr dict;
126 dict = service->GetGroups();
129 ObjectLock mlock(dict);
131 BOOST_FOREACH(tie(tuples::ignore, servicegroup), dict) {
132 if (!ServiceGroup::Exists(servicegroup))
133 Logger::Write(LogWarning, "icinga", "Service group '" + static_cast<String>(servicegroup) + "' used but not defined.");
135 m_MembersCache[servicegroup].push_back(service);
140 m_MembersCacheValid = true;