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<String> > 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 String& svc, m_MembersCache[GetName()]) {
77 if (!Service::Exists(svc))
80 Service::Ptr service = Service::GetByName(svc);
81 services.insert(service);
87 void ServiceGroup::InvalidateMembersCache(void)
89 m_MembersCacheValid = false;
90 m_MembersCache.clear();
93 void ServiceGroup::ValidateMembersCache(void)
95 if (m_MembersCacheValid)
98 m_MembersCache.clear();
100 DynamicObject::Ptr object;
101 BOOST_FOREACH(tie(tuples::ignore, object), DynamicType::GetByName("Service")->GetObjects()) {
102 const Service::Ptr& service = static_pointer_cast<Service>(object);
104 Dictionary::Ptr dict;
105 dict = service->GetGroups();
109 BOOST_FOREACH(tie(tuples::ignore, servicegroup), dict) {
110 if (!ServiceGroup::Exists(servicegroup))
111 Logger::Write(LogWarning, "icinga", "Service group '" + static_cast<String>(servicegroup) + "' used but not defined.");
113 m_MembersCache[servicegroup].push_back(service->GetName());
118 m_MembersCacheValid = true;