]> granicus.if.org Git - icinga2/blob - lib/icinga/servicegroup.cpp
ido: Implement servicegroup members.
[icinga2] / lib / icinga / servicegroup.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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>
29
30 using namespace icinga;
31
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;
37
38 REGISTER_TYPE(ServiceGroup);
39
40 ServiceGroup::ServiceGroup(const Dictionary::Ptr& serializedUpdate)
41         : DynamicObject(serializedUpdate)
42 {
43         RegisterAttribute("display_name", Attribute_Config, &m_DisplayName);
44 }
45
46 ServiceGroup::~ServiceGroup(void)
47 {
48         InvalidateMembersCache();
49 }
50
51 void ServiceGroup::OnRegistrationCompleted(void)
52 {
53         ASSERT(!OwnsLock());
54
55         InvalidateMembersCache();
56 }
57
58 String ServiceGroup::GetDisplayName(void) const
59 {
60         if (!m_DisplayName.Get().IsEmpty())
61                 return m_DisplayName;
62         else
63                 return GetName();
64 }
65
66 ServiceGroup::Ptr ServiceGroup::GetByName(const String& name)
67 {
68         DynamicObject::Ptr configObject = DynamicObject::GetObject("ServiceGroup", name);
69
70         if (!configObject)
71                 BOOST_THROW_EXCEPTION(std::invalid_argument("ServiceGroup '" + name + "' does not exist."));
72
73         return dynamic_pointer_cast<ServiceGroup>(configObject);
74 }
75
76 std::set<Service::Ptr> ServiceGroup::GetMembers(void) const
77 {
78         std::set<Service::Ptr> services;
79
80         {
81                 boost::mutex::scoped_lock lock(l_Mutex);
82
83                 BOOST_FOREACH(const Service::WeakPtr& wservice, l_MembersCache[GetName()]) {
84                         Service::Ptr service = wservice.lock();
85
86                         if (!service)
87                                 continue;
88
89                         services.insert(service);
90                 }
91         }
92
93         return services;
94 }
95
96 void ServiceGroup::InvalidateMembersCache(void)
97 {
98         boost::mutex::scoped_lock lock(l_Mutex);
99
100         if (l_MembersCacheNeedsUpdate)
101                 return; /* Someone else has already requested a refresh. */
102
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();
108         }
109
110         l_MembersCacheNeedsUpdate = true;
111 }
112
113 void ServiceGroup::RefreshMembersCache(void)
114 {
115         {
116                 boost::mutex::scoped_lock lock(l_Mutex);
117
118                 if (!l_MembersCacheNeedsUpdate)
119                         return;
120
121                 l_MembersCacheNeedsUpdate = false;
122         }
123
124         Log(LogDebug, "icinga", "Updating ServiceGroup members cache.");
125
126         std::map<String, std::vector<Service::WeakPtr> > newMembersCache;
127
128         BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
129                 const Service::Ptr& service = static_pointer_cast<Service>(object);
130
131                 Array::Ptr groups = service->GetGroups();
132
133                 if (groups) {
134                         ObjectLock mlock(groups);
135                         BOOST_FOREACH(const Value& group, groups) {
136                                 newMembersCache[group].push_back(service);
137                         }
138                 }
139         }
140
141         {
142                 boost::mutex::scoped_lock lock(l_Mutex);
143                 l_MembersCache.swap(newMembersCache);
144         }
145
146         OnMembersChanged();
147 }