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