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 static AttributeDescription serviceGroupAttributes[] = {
28 { "alias", Attribute_Config },
29 { "notes_url", Attribute_Config },
30 { "action_url", Attribute_Config }
33 REGISTER_TYPE(ServiceGroup, serviceGroupAttributes);
35 String ServiceGroup::GetAlias(void) const
37 String value = Get("alias");
45 String ServiceGroup::GetNotesUrl(void) const
47 return Get("notes_url");
50 String ServiceGroup::GetActionUrl(void) const
52 return Get("action_url");
55 bool ServiceGroup::Exists(const String& name)
57 return (DynamicObject::GetObject("ServiceGroup", name));
60 ServiceGroup::Ptr ServiceGroup::GetByName(const String& name)
62 DynamicObject::Ptr configObject = DynamicObject::GetObject("ServiceGroup", name);
65 throw_exception(invalid_argument("ServiceGroup '" + name + "' does not exist."));
67 return dynamic_pointer_cast<ServiceGroup>(configObject);
70 set<Service::Ptr> ServiceGroup::GetMembers(void) const
72 set<Service::Ptr> services;
74 ValidateMembersCache();
76 BOOST_FOREACH(const Service::WeakPtr& svc, m_MembersCache[GetName()]) {
77 Service::Ptr service = svc.lock();
82 services.insert(service);
88 void ServiceGroup::InvalidateMembersCache(void)
90 m_MembersCacheValid = false;
91 m_MembersCache.clear();
94 void ServiceGroup::ValidateMembersCache(void)
96 if (m_MembersCacheValid)
99 m_MembersCache.clear();
101 DynamicObject::Ptr object;
102 BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Service")->GetObjects()) {
103 const Service::Ptr& service = static_pointer_cast<Service>(object);
105 Dictionary::Ptr dict;
106 dict = service->GetGroups();
110 BOOST_FOREACH(tie(tuples::ignore, servicegroup), dict) {
111 if (!ServiceGroup::Exists(servicegroup))
112 Logger::Write(LogWarning, "icinga", "Service group '" + static_cast<String>(servicegroup) + "' used but not defined.");
114 m_MembersCache[servicegroup].push_back(service);
119 m_MembersCacheValid = true;