]> granicus.if.org Git - icinga2/blob - lib/livestatus/hostgroupstable.cpp
Replace boost::shared_ptr with boost::intrusive_ptr
[icinga2] / lib / livestatus / hostgroupstable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 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 "livestatus/hostgroupstable.hpp"
21 #include "icinga/hostgroup.hpp"
22 #include "icinga/host.hpp"
23 #include "icinga/service.hpp"
24 #include "base/dynamictype.hpp"
25 #include <boost/foreach.hpp>
26
27 using namespace icinga;
28
29 HostGroupsTable::HostGroupsTable(void)
30 {
31         AddColumns(this);
32 }
33
34 void HostGroupsTable::AddColumns(Table *table, const String& prefix,
35     const Column::ObjectAccessor& objectAccessor)
36 {
37         table->AddColumn(prefix + "name", Column(&HostGroupsTable::NameAccessor, objectAccessor));
38         table->AddColumn(prefix + "alias", Column(&HostGroupsTable::AliasAccessor, objectAccessor));
39         table->AddColumn(prefix + "notes", Column(&HostGroupsTable::NotesAccessor, objectAccessor));
40         table->AddColumn(prefix + "notes_url", Column(&HostGroupsTable::NotesUrlAccessor, objectAccessor));
41         table->AddColumn(prefix + "action_url", Column(&HostGroupsTable::ActionUrlAccessor, objectAccessor));
42         table->AddColumn(prefix + "members", Column(&HostGroupsTable::MembersAccessor, objectAccessor));
43         table->AddColumn(prefix + "members_with_state", Column(&HostGroupsTable::MembersWithStateAccessor, objectAccessor));
44         table->AddColumn(prefix + "worst_host_state", Column(&HostGroupsTable::WorstHostStateAccessor, objectAccessor));
45         table->AddColumn(prefix + "num_hosts", Column(&HostGroupsTable::NumHostsAccessor, objectAccessor));
46         table->AddColumn(prefix + "num_hosts_pending", Column(&HostGroupsTable::NumHostsPendingAccessor, objectAccessor));
47         table->AddColumn(prefix + "num_hosts_up", Column(&HostGroupsTable::NumHostsUpAccessor, objectAccessor));
48         table->AddColumn(prefix + "num_hosts_down", Column(&HostGroupsTable::NumHostsDownAccessor, objectAccessor));
49         table->AddColumn(prefix + "num_hosts_unreach", Column(&HostGroupsTable::NumHostsUnreachAccessor, objectAccessor));
50         table->AddColumn(prefix + "num_services", Column(&HostGroupsTable::NumServicesAccessor, objectAccessor));
51         table->AddColumn(prefix + "worst_services_state", Column(&HostGroupsTable::WorstServicesStateAccessor, objectAccessor));
52         table->AddColumn(prefix + "num_services_pending", Column(&HostGroupsTable::NumServicesPendingAccessor, objectAccessor));
53         table->AddColumn(prefix + "num_services_ok", Column(&HostGroupsTable::NumServicesOkAccessor, objectAccessor));
54         table->AddColumn(prefix + "num_services_warn", Column(&HostGroupsTable::NumServicesWarnAccessor, objectAccessor));
55         table->AddColumn(prefix + "num_services_crit", Column(&HostGroupsTable::NumServicesCritAccessor, objectAccessor));
56         table->AddColumn(prefix + "num_services_unknown", Column(&HostGroupsTable::NumServicesUnknownAccessor, objectAccessor));
57         table->AddColumn(prefix + "worst_service_hard_state", Column(&HostGroupsTable::WorstServiceHardStateAccessor, objectAccessor));
58         table->AddColumn(prefix + "num_services_hard_ok", Column(&HostGroupsTable::NumServicesHardOkAccessor, objectAccessor));
59         table->AddColumn(prefix + "num_services_hard_warn", Column(&HostGroupsTable::NumServicesHardWarnAccessor, objectAccessor));
60         table->AddColumn(prefix + "num_services_hard_crit", Column(&HostGroupsTable::NumServicesHardCritAccessor, objectAccessor));
61         table->AddColumn(prefix + "num_services_hard_unknown", Column(&HostGroupsTable::NumServicesHardUnknownAccessor, objectAccessor));
62 }
63
64 String HostGroupsTable::GetName(void) const
65 {
66         return "hostgroups";
67 }
68
69 String HostGroupsTable::GetPrefix(void) const
70 {
71         return "hostgroup";
72 }
73
74 void HostGroupsTable::FetchRows(const AddRowFunction& addRowFn)
75 {
76         BOOST_FOREACH(const HostGroup::Ptr& hg, DynamicType::GetObjectsByType<HostGroup>()) {
77                 addRowFn(hg);
78         }
79 }
80
81 Value HostGroupsTable::NameAccessor(const Value& row)
82 {
83         return static_cast<HostGroup::Ptr>(row)->GetName();
84 }
85
86 Value HostGroupsTable::AliasAccessor(const Value& row)
87 {
88         return static_cast<HostGroup::Ptr>(row)->GetDisplayName();
89 }
90
91 Value HostGroupsTable::NotesAccessor(const Value& row)
92 {
93         return static_cast<HostGroup::Ptr>(row)->GetNotes();
94 }
95
96 Value HostGroupsTable::NotesUrlAccessor(const Value& row)
97 {
98         return static_cast<HostGroup::Ptr>(row)->GetNotesUrl();
99 }
100
101 Value HostGroupsTable::ActionUrlAccessor(const Value& row)
102 {
103         return static_cast<HostGroup::Ptr>(row)->GetActionUrl();
104 }
105
106 Value HostGroupsTable::MembersAccessor(const Value& row)
107 {
108         Array::Ptr members = new Array();
109
110         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
111                 members->Add(host->GetName());
112         }
113
114         return members;
115 }
116
117 Value HostGroupsTable::MembersWithStateAccessor(const Value& row)
118 {
119         Array::Ptr members = new Array();
120
121         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
122                 Array::Ptr member_state = new Array();
123                 member_state->Add(host->GetName());
124                 member_state->Add(host->GetState());
125                 members->Add(member_state);
126         }
127
128         return members;
129 }
130
131 Value HostGroupsTable::WorstHostStateAccessor(const Value& row)
132 {
133         int worst_host = HostUp;
134
135         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
136                 if (host->GetState() > worst_host)
137                         worst_host = host->GetState();
138         }
139
140         return worst_host;
141 }
142
143 Value HostGroupsTable::NumHostsAccessor(const Value& row)
144 {
145         return static_cast<long>(static_cast<HostGroup::Ptr>(row)->GetMembers().size());
146 }
147
148 Value HostGroupsTable::NumHostsPendingAccessor(const Value& row)
149 {
150         int num_hosts = 0;
151
152         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
153                 /* no checkresult */
154                 if (!host->GetLastCheckResult())
155                         num_hosts++;
156         }
157
158         return num_hosts;
159 }
160
161 Value HostGroupsTable::NumHostsUpAccessor(const Value& row)
162 {
163         int num_hosts = 0;
164
165         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
166                 if (host->GetState() == HostUp)
167                         num_hosts++;
168         }
169
170         return num_hosts;
171 }
172
173 Value HostGroupsTable::NumHostsDownAccessor(const Value& row)
174 {
175         int num_hosts = 0;
176
177         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
178                 if (host->GetState() == HostDown)
179                         num_hosts++;
180         }
181
182         return num_hosts;
183 }
184
185 Value HostGroupsTable::NumHostsUnreachAccessor(const Value& row)
186 {
187         int num_hosts = 0;
188
189         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
190                 if (!host->IsReachable())
191                         num_hosts++;
192         }
193
194         return num_hosts;
195 }
196
197 Value HostGroupsTable::NumServicesAccessor(const Value& row)
198 {
199         int num_services = 0;
200         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
201
202         if (hg->GetMembers().size() == 0)
203                 return 0;
204
205         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
206                 num_services += host->GetServices().size();
207         }
208
209         return num_services;
210 }
211
212 Value HostGroupsTable::WorstServicesStateAccessor(const Value& row)
213 {
214         Value worst_service = ServiceOK;
215
216         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
217                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
218                         if (service->GetState() > worst_service)
219                                 worst_service = service->GetState();
220                 }
221         }
222
223         return worst_service;
224 }
225
226 Value HostGroupsTable::NumServicesPendingAccessor(const Value& row)
227 {
228         int num_services = 0;
229
230         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
231                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
232                         if (!service->GetLastCheckResult())
233                                 num_services++;
234                 }
235         }
236
237         return num_services;
238 }
239
240 Value HostGroupsTable::NumServicesOkAccessor(const Value& row)
241 {
242         int num_services = 0;
243
244         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
245                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
246                         if (service->GetState() == ServiceOK)
247                                 num_services++;
248                 }
249         }
250
251         return num_services;
252 }
253
254 Value HostGroupsTable::NumServicesWarnAccessor(const Value& row)
255 {
256         int num_services = 0;
257
258         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
259                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
260                         if (service->GetState() == ServiceWarning)
261                                 num_services++;
262                 }
263         }
264
265         return num_services;
266 }
267
268 Value HostGroupsTable::NumServicesCritAccessor(const Value& row)
269 {
270         int num_services = 0;
271
272         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
273                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
274                         if (service->GetState() == ServiceCritical)
275                                 num_services++;
276                 }
277         }
278
279         return num_services;
280 }
281
282 Value HostGroupsTable::NumServicesUnknownAccessor(const Value& row)
283 {
284         int num_services = 0;
285
286         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
287                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
288                         if (service->GetState() == ServiceUnknown)
289                                 num_services++;
290                 }
291         }
292
293         return num_services;
294 }
295
296 Value HostGroupsTable::WorstServiceHardStateAccessor(const Value& row)
297 {
298         Value worst_service = ServiceOK;
299
300         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
301                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
302                         if (service->GetStateType() == StateTypeHard) {
303                                 if (service->GetState() > worst_service)
304                                         worst_service = service->GetState();
305                         }
306                 }
307         }
308
309         return worst_service;
310 }
311
312 Value HostGroupsTable::NumServicesHardOkAccessor(const Value& row)
313 {
314         int num_services = 0;
315
316         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
317                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
318                         if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceOK)
319                                 num_services++;
320                 }
321         }
322
323         return num_services;
324 }
325
326 Value HostGroupsTable::NumServicesHardWarnAccessor(const Value& row)
327 {
328         int num_services = 0;
329
330         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
331                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
332                         if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceWarning)
333                                 num_services++;
334                 }
335         }
336
337         return num_services;
338 }
339
340 Value HostGroupsTable::NumServicesHardCritAccessor(const Value& row)
341 {
342         int num_services = 0;
343
344         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
345                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
346                         if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceCritical)
347                                 num_services++;
348                 }
349         }
350
351         return num_services;
352 }
353
354 Value HostGroupsTable::NumServicesHardUnknownAccessor(const Value& row)
355 {
356         int num_services = 0;
357
358         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
359                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
360                         if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceUnknown)
361                                 num_services++;
362                 }
363         }
364
365         return num_services;
366 }