]> granicus.if.org Git - icinga2/blob - lib/livestatus/hostgroupstable.cpp
Merge pull request #6509 from gunnarbeutner/feature/real-constants
[icinga2] / lib / livestatus / hostgroupstable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
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/configtype.hpp"
25
26 using namespace icinga;
27
28 HostGroupsTable::HostGroupsTable()
29 {
30         AddColumns(this);
31 }
32
33 void HostGroupsTable::AddColumns(Table *table, const String& prefix,
34         const Column::ObjectAccessor& objectAccessor)
35 {
36         table->AddColumn(prefix + "name", Column(&HostGroupsTable::NameAccessor, objectAccessor));
37         table->AddColumn(prefix + "alias", Column(&HostGroupsTable::AliasAccessor, objectAccessor));
38         table->AddColumn(prefix + "notes", Column(&HostGroupsTable::NotesAccessor, objectAccessor));
39         table->AddColumn(prefix + "notes_url", Column(&HostGroupsTable::NotesUrlAccessor, objectAccessor));
40         table->AddColumn(prefix + "action_url", Column(&HostGroupsTable::ActionUrlAccessor, objectAccessor));
41         table->AddColumn(prefix + "members", Column(&HostGroupsTable::MembersAccessor, objectAccessor));
42         table->AddColumn(prefix + "members_with_state", Column(&HostGroupsTable::MembersWithStateAccessor, objectAccessor));
43         table->AddColumn(prefix + "worst_host_state", Column(&HostGroupsTable::WorstHostStateAccessor, objectAccessor));
44         table->AddColumn(prefix + "num_hosts", Column(&HostGroupsTable::NumHostsAccessor, objectAccessor));
45         table->AddColumn(prefix + "num_hosts_pending", Column(&HostGroupsTable::NumHostsPendingAccessor, objectAccessor));
46         table->AddColumn(prefix + "num_hosts_up", Column(&HostGroupsTable::NumHostsUpAccessor, objectAccessor));
47         table->AddColumn(prefix + "num_hosts_down", Column(&HostGroupsTable::NumHostsDownAccessor, objectAccessor));
48         table->AddColumn(prefix + "num_hosts_unreach", Column(&HostGroupsTable::NumHostsUnreachAccessor, objectAccessor));
49         table->AddColumn(prefix + "num_services", Column(&HostGroupsTable::NumServicesAccessor, objectAccessor));
50         table->AddColumn(prefix + "worst_service_state", Column(&HostGroupsTable::WorstServiceStateAccessor, objectAccessor));
51         table->AddColumn(prefix + "num_services_pending", Column(&HostGroupsTable::NumServicesPendingAccessor, objectAccessor));
52         table->AddColumn(prefix + "num_services_ok", Column(&HostGroupsTable::NumServicesOkAccessor, objectAccessor));
53         table->AddColumn(prefix + "num_services_warn", Column(&HostGroupsTable::NumServicesWarnAccessor, objectAccessor));
54         table->AddColumn(prefix + "num_services_crit", Column(&HostGroupsTable::NumServicesCritAccessor, objectAccessor));
55         table->AddColumn(prefix + "num_services_unknown", Column(&HostGroupsTable::NumServicesUnknownAccessor, objectAccessor));
56         table->AddColumn(prefix + "worst_service_hard_state", Column(&HostGroupsTable::WorstServiceHardStateAccessor, objectAccessor));
57         table->AddColumn(prefix + "num_services_hard_ok", Column(&HostGroupsTable::NumServicesHardOkAccessor, objectAccessor));
58         table->AddColumn(prefix + "num_services_hard_warn", Column(&HostGroupsTable::NumServicesHardWarnAccessor, objectAccessor));
59         table->AddColumn(prefix + "num_services_hard_crit", Column(&HostGroupsTable::NumServicesHardCritAccessor, objectAccessor));
60         table->AddColumn(prefix + "num_services_hard_unknown", Column(&HostGroupsTable::NumServicesHardUnknownAccessor, objectAccessor));
61 }
62
63 String HostGroupsTable::GetName() const
64 {
65         return "hostgroups";
66 }
67
68 String HostGroupsTable::GetPrefix() const
69 {
70         return "hostgroup";
71 }
72
73 void HostGroupsTable::FetchRows(const AddRowFunction& addRowFn)
74 {
75         for (const HostGroup::Ptr& hg : ConfigType::GetObjectsByType<HostGroup>()) {
76                 if (!addRowFn(hg, LivestatusGroupByNone, Empty))
77                         return;
78         }
79 }
80
81 Value HostGroupsTable::NameAccessor(const Value& row)
82 {
83         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
84
85         if (!hg)
86                 return Empty;
87
88         return hg->GetName();
89 }
90
91 Value HostGroupsTable::AliasAccessor(const Value& row)
92 {
93         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
94
95         if (!hg)
96                 return Empty;
97
98         return hg->GetDisplayName();
99 }
100
101 Value HostGroupsTable::NotesAccessor(const Value& row)
102 {
103         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
104
105         if (!hg)
106                 return Empty;
107
108         return hg->GetNotes();
109 }
110
111 Value HostGroupsTable::NotesUrlAccessor(const Value& row)
112 {
113         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
114
115         if (!hg)
116                 return Empty;
117
118         return hg->GetNotesUrl();
119 }
120
121 Value HostGroupsTable::ActionUrlAccessor(const Value& row)
122 {
123         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
124
125         if (!hg)
126                 return Empty;
127
128         return hg->GetActionUrl();
129 }
130
131 Value HostGroupsTable::MembersAccessor(const Value& row)
132 {
133         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
134
135         if (!hg)
136                 return Empty;
137
138         ArrayData members;
139
140         for (const Host::Ptr& host : hg->GetMembers()) {
141                 members.push_back(host->GetName());
142         }
143
144         return new Array(std::move(members));
145 }
146
147 Value HostGroupsTable::MembersWithStateAccessor(const Value& row)
148 {
149         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
150
151         if (!hg)
152                 return Empty;
153
154         ArrayData members;
155
156         for (const Host::Ptr& host : hg->GetMembers()) {
157                 members.push_back(new Array({
158                         host->GetName(),
159                         host->GetState()
160                 }));
161         }
162
163         return new Array(std::move(members));
164 }
165
166 Value HostGroupsTable::WorstHostStateAccessor(const Value& row)
167 {
168         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
169
170         if (!hg)
171                 return Empty;
172
173         int worst_host = HostUp;
174
175         for (const Host::Ptr& host : hg->GetMembers()) {
176                 if (host->GetState() > worst_host)
177                         worst_host = host->GetState();
178         }
179
180         return worst_host;
181 }
182
183 Value HostGroupsTable::NumHostsAccessor(const Value& row)
184 {
185         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
186
187         if (!hg)
188                 return Empty;
189
190         return hg->GetMembers().size();
191 }
192
193 Value HostGroupsTable::NumHostsPendingAccessor(const Value& row)
194 {
195         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
196
197         if (!hg)
198                 return Empty;
199
200         int num_hosts = 0;
201
202         for (const Host::Ptr& host : hg->GetMembers()) {
203                 /* no checkresult */
204                 if (!host->GetLastCheckResult())
205                         num_hosts++;
206         }
207
208         return num_hosts;
209 }
210
211 Value HostGroupsTable::NumHostsUpAccessor(const Value& row)
212 {
213         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
214
215         if (!hg)
216                 return Empty;
217
218         int num_hosts = 0;
219
220         for (const Host::Ptr& host : hg->GetMembers()) {
221                 if (host->GetState() == HostUp)
222                         num_hosts++;
223         }
224
225         return num_hosts;
226 }
227
228 Value HostGroupsTable::NumHostsDownAccessor(const Value& row)
229 {
230         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
231
232         if (!hg)
233                 return Empty;
234
235         int num_hosts = 0;
236
237         for (const Host::Ptr& host : hg->GetMembers()) {
238                 if (host->GetState() == HostDown)
239                         num_hosts++;
240         }
241
242         return num_hosts;
243 }
244
245 Value HostGroupsTable::NumHostsUnreachAccessor(const Value& row)
246 {
247         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
248
249         if (!hg)
250                 return Empty;
251
252         int num_hosts = 0;
253
254         for (const Host::Ptr& host : hg->GetMembers()) {
255                 if (!host->IsReachable())
256                         num_hosts++;
257         }
258
259         return num_hosts;
260 }
261
262 Value HostGroupsTable::NumServicesAccessor(const Value& row)
263 {
264         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
265
266         if (!hg)
267                 return Empty;
268
269         int num_services = 0;
270
271         if (hg->GetMembers().size() == 0)
272                 return 0;
273
274         for (const Host::Ptr& host : hg->GetMembers()) {
275                 num_services += host->GetServices().size();
276         }
277
278         return num_services;
279 }
280
281 Value HostGroupsTable::WorstServiceStateAccessor(const Value& row)
282 {
283         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
284
285         if (!hg)
286                 return Empty;
287
288         Value worst_service = ServiceOK;
289
290         for (const Host::Ptr& host : hg->GetMembers()) {
291                 for (const Service::Ptr& service : host->GetServices()) {
292                         if (service->GetState() > worst_service)
293                                 worst_service = service->GetState();
294                 }
295         }
296
297         return worst_service;
298 }
299
300 Value HostGroupsTable::NumServicesPendingAccessor(const Value& row)
301 {
302         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
303
304         if (!hg)
305                 return Empty;
306
307         int num_services = 0;
308
309         for (const Host::Ptr& host : hg->GetMembers()) {
310                 for (const Service::Ptr& service : host->GetServices()) {
311                         if (!service->GetLastCheckResult())
312                                 num_services++;
313                 }
314         }
315
316         return num_services;
317 }
318
319 Value HostGroupsTable::NumServicesOkAccessor(const Value& row)
320 {
321         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
322
323         if (!hg)
324                 return Empty;
325
326         int num_services = 0;
327
328         for (const Host::Ptr& host : hg->GetMembers()) {
329                 for (const Service::Ptr& service : host->GetServices()) {
330                         if (service->GetState() == ServiceOK)
331                                 num_services++;
332                 }
333         }
334
335         return num_services;
336 }
337
338 Value HostGroupsTable::NumServicesWarnAccessor(const Value& row)
339 {
340         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
341
342         if (!hg)
343                 return Empty;
344
345         int num_services = 0;
346
347         for (const Host::Ptr& host : hg->GetMembers()) {
348                 for (const Service::Ptr& service : host->GetServices()) {
349                         if (service->GetState() == ServiceWarning)
350                                 num_services++;
351                 }
352         }
353
354         return num_services;
355 }
356
357 Value HostGroupsTable::NumServicesCritAccessor(const Value& row)
358 {
359         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
360
361         if (!hg)
362                 return Empty;
363
364         int num_services = 0;
365
366         for (const Host::Ptr& host : hg->GetMembers()) {
367                 for (const Service::Ptr& service : host->GetServices()) {
368                         if (service->GetState() == ServiceCritical)
369                                 num_services++;
370                 }
371         }
372
373         return num_services;
374 }
375
376 Value HostGroupsTable::NumServicesUnknownAccessor(const Value& row)
377 {
378         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
379
380         if (!hg)
381                 return Empty;
382
383         int num_services = 0;
384
385         for (const Host::Ptr& host : hg->GetMembers()) {
386                 for (const Service::Ptr& service : host->GetServices()) {
387                         if (service->GetState() == ServiceUnknown)
388                                 num_services++;
389                 }
390         }
391
392         return num_services;
393 }
394
395 Value HostGroupsTable::WorstServiceHardStateAccessor(const Value& row)
396 {
397         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
398
399         if (!hg)
400                 return Empty;
401
402         Value worst_service = ServiceOK;
403
404         for (const Host::Ptr& host : hg->GetMembers()) {
405                 for (const Service::Ptr& service : host->GetServices()) {
406                         if (service->GetStateType() == StateTypeHard) {
407                                 if (service->GetState() > worst_service)
408                                         worst_service = service->GetState();
409                         }
410                 }
411         }
412
413         return worst_service;
414 }
415
416 Value HostGroupsTable::NumServicesHardOkAccessor(const Value& row)
417 {
418         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
419
420         if (!hg)
421                 return Empty;
422
423         int num_services = 0;
424
425         for (const Host::Ptr& host : hg->GetMembers()) {
426                 for (const Service::Ptr& service : host->GetServices()) {
427                         if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceOK)
428                                 num_services++;
429                 }
430         }
431
432         return num_services;
433 }
434
435 Value HostGroupsTable::NumServicesHardWarnAccessor(const Value& row)
436 {
437         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
438
439         if (!hg)
440                 return Empty;
441
442         int num_services = 0;
443
444         for (const Host::Ptr& host : hg->GetMembers()) {
445                 for (const Service::Ptr& service : host->GetServices()) {
446                         if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceWarning)
447                                 num_services++;
448                 }
449         }
450
451         return num_services;
452 }
453
454 Value HostGroupsTable::NumServicesHardCritAccessor(const Value& row)
455 {
456         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
457
458         if (!hg)
459                 return Empty;
460
461         int num_services = 0;
462
463         for (const Host::Ptr& host : hg->GetMembers()) {
464                 for (const Service::Ptr& service : host->GetServices()) {
465                         if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceCritical)
466                                 num_services++;
467                 }
468         }
469
470         return num_services;
471 }
472
473 Value HostGroupsTable::NumServicesHardUnknownAccessor(const Value& row)
474 {
475         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
476
477         if (!hg)
478                 return Empty;
479
480         int num_services = 0;
481
482         for (const Host::Ptr& host : hg->GetMembers()) {
483                 for (const Service::Ptr& service : host->GetServices()) {
484                         if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceUnknown)
485                                 num_services++;
486                 }
487         }
488
489         return num_services;
490 }