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"
21 #include "icinga/servicegroup.h"
22 #include "base/dynamictype.h"
23 #include "base/objectlock.h"
24 #include "base/logger_fwd.h"
25 #include "base/timer.h"
26 #include "base/utility.h"
27 #include <boost/smart_ptr/make_shared.hpp>
28 #include <boost/foreach.hpp>
30 using namespace icinga;
32 static boost::mutex l_Mutex;
33 static std::map<String, std::vector<Service::WeakPtr> > l_MembersCache;
34 static bool l_MembersCacheNeedsUpdate = false;
35 static Timer::Ptr l_MembersCacheTimer;
36 boost::signals2::signal<void (void)> ServiceGroup::OnMembersChanged;
38 REGISTER_TYPE(ServiceGroup);
40 ServiceGroup::ServiceGroup(const Dictionary::Ptr& serializedUpdate)
41 : DynamicObject(serializedUpdate)
43 RegisterAttribute("display_name", Attribute_Config, &m_DisplayName);
46 ServiceGroup::~ServiceGroup(void)
48 InvalidateMembersCache();
51 void ServiceGroup::OnRegistrationCompleted(void)
55 InvalidateMembersCache();
58 String ServiceGroup::GetDisplayName(void) const
60 if (!m_DisplayName.Get().IsEmpty())
66 ServiceGroup::Ptr ServiceGroup::GetByName(const String& name)
68 DynamicObject::Ptr configObject = DynamicObject::GetObject("ServiceGroup", name);
71 BOOST_THROW_EXCEPTION(std::invalid_argument("ServiceGroup '" + name + "' does not exist."));
73 return dynamic_pointer_cast<ServiceGroup>(configObject);
76 std::set<Service::Ptr> ServiceGroup::GetMembers(void) const
78 std::set<Service::Ptr> services;
81 boost::mutex::scoped_lock lock(l_Mutex);
83 BOOST_FOREACH(const Service::WeakPtr& wservice, l_MembersCache[GetName()]) {
84 Service::Ptr service = wservice.lock();
89 services.insert(service);
96 void ServiceGroup::InvalidateMembersCache(void)
98 boost::mutex::scoped_lock lock(l_Mutex);
100 if (l_MembersCacheNeedsUpdate)
101 return; /* Someone else has already requested a refresh. */
103 if (!l_MembersCacheTimer) {
104 l_MembersCacheTimer = boost::make_shared<Timer>();
105 l_MembersCacheTimer->SetInterval(0.5);
106 l_MembersCacheTimer->OnTimerExpired.connect(boost::bind(&ServiceGroup::RefreshMembersCache));
107 l_MembersCacheTimer->Start();
110 l_MembersCacheNeedsUpdate = true;
113 void ServiceGroup::RefreshMembersCache(void)
116 boost::mutex::scoped_lock lock(l_Mutex);
118 if (!l_MembersCacheNeedsUpdate)
121 l_MembersCacheNeedsUpdate = false;
124 Log(LogDebug, "icinga", "Updating ServiceGroup members cache.");
126 std::map<String, std::vector<Service::WeakPtr> > newMembersCache;
128 BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
129 const Service::Ptr& service = static_pointer_cast<Service>(object);
131 Array::Ptr groups = service->GetGroups();
134 ObjectLock mlock(groups);
135 BOOST_FOREACH(const Value& group, groups) {
136 newMembersCache[group].push_back(service);
142 boost::mutex::scoped_lock lock(l_Mutex);
143 l_MembersCache.swap(newMembersCache);