1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
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"
9 using namespace icinga;
11 HostGroupsTable::HostGroupsTable()
16 void HostGroupsTable::AddColumns(Table *table, const String& prefix,
17 const Column::ObjectAccessor& objectAccessor)
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));
46 String HostGroupsTable::GetName() const
51 String HostGroupsTable::GetPrefix() const
56 void HostGroupsTable::FetchRows(const AddRowFunction& addRowFn)
58 for (const HostGroup::Ptr& hg : ConfigType::GetObjectsByType<HostGroup>()) {
59 if (!addRowFn(hg, LivestatusGroupByNone, Empty))
64 Value HostGroupsTable::NameAccessor(const Value& row)
66 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
74 Value HostGroupsTable::AliasAccessor(const Value& row)
76 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
81 return hg->GetDisplayName();
84 Value HostGroupsTable::NotesAccessor(const Value& row)
86 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
91 return hg->GetNotes();
94 Value HostGroupsTable::NotesUrlAccessor(const Value& row)
96 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
101 return hg->GetNotesUrl();
104 Value HostGroupsTable::ActionUrlAccessor(const Value& row)
106 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
111 return hg->GetActionUrl();
114 Value HostGroupsTable::MembersAccessor(const Value& row)
116 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
123 for (const Host::Ptr& host : hg->GetMembers()) {
124 members.push_back(host->GetName());
127 return new Array(std::move(members));
130 Value HostGroupsTable::MembersWithStateAccessor(const Value& row)
132 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
139 for (const Host::Ptr& host : hg->GetMembers()) {
140 members.push_back(new Array({
146 return new Array(std::move(members));
149 Value HostGroupsTable::WorstHostStateAccessor(const Value& row)
151 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
156 int worst_host = HostUp;
158 for (const Host::Ptr& host : hg->GetMembers()) {
159 if (host->GetState() > worst_host)
160 worst_host = host->GetState();
166 Value HostGroupsTable::NumHostsAccessor(const Value& row)
168 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
173 return hg->GetMembers().size();
176 Value HostGroupsTable::NumHostsPendingAccessor(const Value& row)
178 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
185 for (const Host::Ptr& host : hg->GetMembers()) {
187 if (!host->GetLastCheckResult())
194 Value HostGroupsTable::NumHostsUpAccessor(const Value& row)
196 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
203 for (const Host::Ptr& host : hg->GetMembers()) {
204 if (host->GetState() == HostUp)
211 Value HostGroupsTable::NumHostsDownAccessor(const Value& row)
213 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
220 for (const Host::Ptr& host : hg->GetMembers()) {
221 if (host->GetState() == HostDown)
228 Value HostGroupsTable::NumHostsUnreachAccessor(const Value& row)
230 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
237 for (const Host::Ptr& host : hg->GetMembers()) {
238 if (!host->IsReachable())
245 Value HostGroupsTable::NumServicesAccessor(const Value& row)
247 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
252 int num_services = 0;
254 if (hg->GetMembers().size() == 0)
257 for (const Host::Ptr& host : hg->GetMembers()) {
258 num_services += host->GetServices().size();
264 Value HostGroupsTable::WorstServiceStateAccessor(const Value& row)
266 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
271 Value worst_service = ServiceOK;
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();
280 return worst_service;
283 Value HostGroupsTable::NumServicesPendingAccessor(const Value& row)
285 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
290 int num_services = 0;
292 for (const Host::Ptr& host : hg->GetMembers()) {
293 for (const Service::Ptr& service : host->GetServices()) {
294 if (!service->GetLastCheckResult())
302 Value HostGroupsTable::NumServicesOkAccessor(const Value& row)
304 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
309 int num_services = 0;
311 for (const Host::Ptr& host : hg->GetMembers()) {
312 for (const Service::Ptr& service : host->GetServices()) {
313 if (service->GetState() == ServiceOK)
321 Value HostGroupsTable::NumServicesWarnAccessor(const Value& row)
323 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
328 int num_services = 0;
330 for (const Host::Ptr& host : hg->GetMembers()) {
331 for (const Service::Ptr& service : host->GetServices()) {
332 if (service->GetState() == ServiceWarning)
340 Value HostGroupsTable::NumServicesCritAccessor(const Value& row)
342 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
347 int num_services = 0;
349 for (const Host::Ptr& host : hg->GetMembers()) {
350 for (const Service::Ptr& service : host->GetServices()) {
351 if (service->GetState() == ServiceCritical)
359 Value HostGroupsTable::NumServicesUnknownAccessor(const Value& row)
361 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
366 int num_services = 0;
368 for (const Host::Ptr& host : hg->GetMembers()) {
369 for (const Service::Ptr& service : host->GetServices()) {
370 if (service->GetState() == ServiceUnknown)
378 Value HostGroupsTable::WorstServiceHardStateAccessor(const Value& row)
380 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
385 Value worst_service = ServiceOK;
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();
396 return worst_service;
399 Value HostGroupsTable::NumServicesHardOkAccessor(const Value& row)
401 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
406 int num_services = 0;
408 for (const Host::Ptr& host : hg->GetMembers()) {
409 for (const Service::Ptr& service : host->GetServices()) {
410 if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceOK)
418 Value HostGroupsTable::NumServicesHardWarnAccessor(const Value& row)
420 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
425 int num_services = 0;
427 for (const Host::Ptr& host : hg->GetMembers()) {
428 for (const Service::Ptr& service : host->GetServices()) {
429 if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceWarning)
437 Value HostGroupsTable::NumServicesHardCritAccessor(const Value& row)
439 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
444 int num_services = 0;
446 for (const Host::Ptr& host : hg->GetMembers()) {
447 for (const Service::Ptr& service : host->GetServices()) {
448 if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceCritical)
456 Value HostGroupsTable::NumServicesHardUnknownAccessor(const Value& row)
458 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
463 int num_services = 0;
465 for (const Host::Ptr& host : hg->GetMembers()) {
466 for (const Service::Ptr& service : host->GetServices()) {
467 if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceUnknown)