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 map<String, vector<Service::WeakPtr> > ServiceGroup::m_MembersCache;
25 bool ServiceGroup::m_MembersCacheValid;
27 REGISTER_TYPE(ServiceGroup, NULL);
29 ServiceGroup::ServiceGroup(const Dictionary::Ptr& properties)
30 : DynamicObject(properties)
33 String ServiceGroup::GetDisplayName(void) const
35 String value = Get("display_name");
43 String ServiceGroup::GetNotesUrl(void) const
45 return Get("notes_url");
48 String ServiceGroup::GetActionUrl(void) const
50 return Get("action_url");
54 * @threadsafety Always.
56 bool ServiceGroup::Exists(const String& name)
58 return (DynamicObject::GetObject("ServiceGroup", name));
62 * @threadsafety Always.
64 ServiceGroup::Ptr ServiceGroup::GetByName(const String& name)
66 DynamicObject::Ptr configObject = DynamicObject::GetObject("ServiceGroup", name);
69 BOOST_THROW_EXCEPTION(invalid_argument("ServiceGroup '" + name + "' does not exist."));
71 return dynamic_pointer_cast<ServiceGroup>(configObject);
74 set<Service::Ptr> ServiceGroup::GetMembers(void) const
76 set<Service::Ptr> services;
78 ValidateMembersCache();
80 BOOST_FOREACH(const Service::WeakPtr& svc, m_MembersCache[GetName()]) {
81 Service::Ptr service = svc.lock();
86 services.insert(service);
92 void ServiceGroup::InvalidateMembersCache(void)
94 m_MembersCacheValid = false;
95 m_MembersCache.clear();
98 void ServiceGroup::ValidateMembersCache(void)
100 if (m_MembersCacheValid)
103 m_MembersCache.clear();
105 DynamicObject::Ptr object;
106 BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Service")->GetObjects()) {
107 const Service::Ptr& service = static_pointer_cast<Service>(object);
109 Dictionary::Ptr dict;
110 dict = service->GetGroups();
114 BOOST_FOREACH(tie(tuples::ignore, servicegroup), dict) {
115 if (!ServiceGroup::Exists(servicegroup))
116 Logger::Write(LogWarning, "icinga", "Service group '" + static_cast<String>(servicegroup) + "' used but not defined.");
118 m_MembersCache[servicegroup].push_back(service);
123 m_MembersCacheValid = true;