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;
37 REGISTER_TYPE(ServiceGroup);
39 ServiceGroup::ServiceGroup(const Dictionary::Ptr& serializedUpdate)
40 : DynamicObject(serializedUpdate)
42 RegisterAttribute("display_name", Attribute_Config, &m_DisplayName);
45 ServiceGroup::~ServiceGroup(void)
47 InvalidateMembersCache();
50 void ServiceGroup::OnRegistrationCompleted(void)
54 InvalidateMembersCache();
57 String ServiceGroup::GetDisplayName(void) const
59 if (!m_DisplayName.Get().IsEmpty())
65 ServiceGroup::Ptr ServiceGroup::GetByName(const String& name)
67 DynamicObject::Ptr configObject = DynamicObject::GetObject("ServiceGroup", name);
70 BOOST_THROW_EXCEPTION(std::invalid_argument("ServiceGroup '" + name + "' does not exist."));
72 return dynamic_pointer_cast<ServiceGroup>(configObject);
75 std::set<Service::Ptr> ServiceGroup::GetMembers(void) const
77 std::set<Service::Ptr> services;
80 boost::mutex::scoped_lock lock(l_Mutex);
82 BOOST_FOREACH(const Service::WeakPtr& wservice, l_MembersCache[GetName()]) {
83 Service::Ptr service = wservice.lock();
88 services.insert(service);
95 void ServiceGroup::InvalidateMembersCache(void)
97 boost::mutex::scoped_lock lock(l_Mutex);
99 if (l_MembersCacheNeedsUpdate)
100 return; /* Someone else has already requested a refresh. */
102 if (!l_MembersCacheTimer) {
103 l_MembersCacheTimer = boost::make_shared<Timer>();
104 l_MembersCacheTimer->SetInterval(0.5);
105 l_MembersCacheTimer->OnTimerExpired.connect(boost::bind(&ServiceGroup::RefreshMembersCache));
106 l_MembersCacheTimer->Start();
109 l_MembersCacheNeedsUpdate = true;
112 void ServiceGroup::RefreshMembersCache(void)
115 boost::mutex::scoped_lock lock(l_Mutex);
117 if (!l_MembersCacheNeedsUpdate)
120 l_MembersCacheNeedsUpdate = false;
123 Log(LogDebug, "icinga", "Updating ServiceGroup members cache.");
125 std::map<String, std::vector<Service::WeakPtr> > newMembersCache;
127 BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
128 const Service::Ptr& service = static_pointer_cast<Service>(object);
130 Array::Ptr groups = service->GetGroups();
133 ObjectLock mlock(groups);
134 BOOST_FOREACH(const Value& group, groups) {
135 newMembersCache[group].push_back(service);
140 boost::mutex::scoped_lock lock(l_Mutex);
141 l_MembersCache.swap(newMembersCache);