]> granicus.if.org Git - icinga2/blob - lib/livestatus/hostgroupstable.cpp
Implement support for the 'Limit' column in Livestatus
[icinga2] / lib / livestatus / hostgroupstable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 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                 if (!addRowFn(hg, LivestatusGroupByNone, Empty))
78                         return;
79         }
80 }
81
82 Value HostGroupsTable::NameAccessor(const Value& row)
83 {
84         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
85
86         if (!hg)
87                 return Empty;
88
89         return hg->GetName();
90 }
91
92 Value HostGroupsTable::AliasAccessor(const Value& row)
93 {
94         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
95
96         if (!hg)
97                 return Empty;
98
99         return hg->GetDisplayName();
100 }
101
102 Value HostGroupsTable::NotesAccessor(const Value& row)
103 {
104         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
105
106         if (!hg)
107                 return Empty;
108
109         return hg->GetNotes();
110 }
111
112 Value HostGroupsTable::NotesUrlAccessor(const Value& row)
113 {
114         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
115
116         if (!hg)
117                 return Empty;
118
119         return hg->GetNotesUrl();
120 }
121
122 Value HostGroupsTable::ActionUrlAccessor(const Value& row)
123 {
124         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
125
126         if (!hg)
127                 return Empty;
128
129         return hg->GetActionUrl();
130 }
131
132 Value HostGroupsTable::MembersAccessor(const Value& row)
133 {
134         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
135
136         if (!hg)
137                 return Empty;
138
139         Array::Ptr members = new Array();
140
141         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
142                 members->Add(host->GetName());
143         }
144
145         return members;
146 }
147
148 Value HostGroupsTable::MembersWithStateAccessor(const Value& row)
149 {
150         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
151
152         if (!hg)
153                 return Empty;
154
155         Array::Ptr members = new Array();
156
157         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
158                 Array::Ptr member_state = new Array();
159                 member_state->Add(host->GetName());
160                 member_state->Add(host->GetState());
161                 members->Add(member_state);
162         }
163
164         return members;
165 }
166
167 Value HostGroupsTable::WorstHostStateAccessor(const Value& row)
168 {
169         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
170
171         if (!hg)
172                 return Empty;
173
174         int worst_host = HostUp;
175
176         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
177                 if (host->GetState() > worst_host)
178                         worst_host = host->GetState();
179         }
180
181         return worst_host;
182 }
183
184 Value HostGroupsTable::NumHostsAccessor(const Value& row)
185 {
186         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
187
188         if (!hg)
189                 return Empty;
190
191         return hg->GetMembers().size();
192 }
193
194 Value HostGroupsTable::NumHostsPendingAccessor(const Value& row)
195 {
196         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
197
198         if (!hg)
199                 return Empty;
200
201         int num_hosts = 0;
202
203         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
204                 /* no checkresult */
205                 if (!host->GetLastCheckResult())
206                         num_hosts++;
207         }
208
209         return num_hosts;
210 }
211
212 Value HostGroupsTable::NumHostsUpAccessor(const Value& row)
213 {
214         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
215
216         if (!hg)
217                 return Empty;
218
219         int num_hosts = 0;
220
221         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
222                 if (host->GetState() == HostUp)
223                         num_hosts++;
224         }
225
226         return num_hosts;
227 }
228
229 Value HostGroupsTable::NumHostsDownAccessor(const Value& row)
230 {
231         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
232
233         if (!hg)
234                 return Empty;
235
236         int num_hosts = 0;
237
238         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
239                 if (host->GetState() == HostDown)
240                         num_hosts++;
241         }
242
243         return num_hosts;
244 }
245
246 Value HostGroupsTable::NumHostsUnreachAccessor(const Value& row)
247 {
248         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
249
250         if (!hg)
251                 return Empty;
252
253         int num_hosts = 0;
254
255         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
256                 if (!host->IsReachable())
257                         num_hosts++;
258         }
259
260         return num_hosts;
261 }
262
263 Value HostGroupsTable::NumServicesAccessor(const Value& row)
264 {
265         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
266
267         if (!hg)
268                 return Empty;
269
270         int num_services = 0;
271
272         if (hg->GetMembers().size() == 0)
273                 return 0;
274
275         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
276                 num_services += host->GetServices().size();
277         }
278
279         return num_services;
280 }
281
282 Value HostGroupsTable::WorstServicesStateAccessor(const Value& row)
283 {
284         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
285
286         if (!hg)
287                 return Empty;
288
289         Value worst_service = ServiceOK;
290
291         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
292                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
293                         if (service->GetState() > worst_service)
294                                 worst_service = service->GetState();
295                 }
296         }
297
298         return worst_service;
299 }
300
301 Value HostGroupsTable::NumServicesPendingAccessor(const Value& row)
302 {
303         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
304
305         if (!hg)
306                 return Empty;
307
308         int num_services = 0;
309
310         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
311                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
312                         if (!service->GetLastCheckResult())
313                                 num_services++;
314                 }
315         }
316
317         return num_services;
318 }
319
320 Value HostGroupsTable::NumServicesOkAccessor(const Value& row)
321 {
322         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
323
324         if (!hg)
325                 return Empty;
326
327         int num_services = 0;
328
329         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
330                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
331                         if (service->GetState() == ServiceOK)
332                                 num_services++;
333                 }
334         }
335
336         return num_services;
337 }
338
339 Value HostGroupsTable::NumServicesWarnAccessor(const Value& row)
340 {
341         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
342
343         if (!hg)
344                 return Empty;
345
346         int num_services = 0;
347
348         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
349                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
350                         if (service->GetState() == ServiceWarning)
351                                 num_services++;
352                 }
353         }
354
355         return num_services;
356 }
357
358 Value HostGroupsTable::NumServicesCritAccessor(const Value& row)
359 {
360         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
361
362         if (!hg)
363                 return Empty;
364
365         int num_services = 0;
366
367         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
368                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
369                         if (service->GetState() == ServiceCritical)
370                                 num_services++;
371                 }
372         }
373
374         return num_services;
375 }
376
377 Value HostGroupsTable::NumServicesUnknownAccessor(const Value& row)
378 {
379         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
380
381         if (!hg)
382                 return Empty;
383
384         int num_services = 0;
385
386         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
387                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
388                         if (service->GetState() == ServiceUnknown)
389                                 num_services++;
390                 }
391         }
392
393         return num_services;
394 }
395
396 Value HostGroupsTable::WorstServiceHardStateAccessor(const Value& row)
397 {
398         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
399
400         if (!hg)
401                 return Empty;
402
403         Value worst_service = ServiceOK;
404
405         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
406                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
407                         if (service->GetStateType() == StateTypeHard) {
408                                 if (service->GetState() > worst_service)
409                                         worst_service = service->GetState();
410                         }
411                 }
412         }
413
414         return worst_service;
415 }
416
417 Value HostGroupsTable::NumServicesHardOkAccessor(const Value& row)
418 {
419         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
420
421         if (!hg)
422                 return Empty;
423
424         int num_services = 0;
425
426         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
427                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
428                         if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceOK)
429                                 num_services++;
430                 }
431         }
432
433         return num_services;
434 }
435
436 Value HostGroupsTable::NumServicesHardWarnAccessor(const Value& row)
437 {
438         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
439
440         if (!hg)
441                 return Empty;
442
443         int num_services = 0;
444
445         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
446                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
447                         if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceWarning)
448                                 num_services++;
449                 }
450         }
451
452         return num_services;
453 }
454
455 Value HostGroupsTable::NumServicesHardCritAccessor(const Value& row)
456 {
457         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
458
459         if (!hg)
460                 return Empty;
461
462         int num_services = 0;
463
464         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
465                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
466                         if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceCritical)
467                                 num_services++;
468                 }
469         }
470
471         return num_services;
472 }
473
474 Value HostGroupsTable::NumServicesHardUnknownAccessor(const Value& row)
475 {
476         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
477
478         if (!hg)
479                 return Empty;
480
481         int num_services = 0;
482
483         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
484                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
485                         if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceUnknown)
486                                 num_services++;
487                 }
488         }
489
490         return num_services;
491 }