1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
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. *
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. *
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 ******************************************************************************/
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"
26 using namespace icinga;
28 HostGroupsTable::HostGroupsTable(void)
33 void HostGroupsTable::AddColumns(Table *table, const String& prefix,
34 const Column::ObjectAccessor& objectAccessor)
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_services_state", Column(&HostGroupsTable::WorstServicesStateAccessor, 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));
63 String HostGroupsTable::GetName(void) const
68 String HostGroupsTable::GetPrefix(void) const
73 void HostGroupsTable::FetchRows(const AddRowFunction& addRowFn)
75 for (const HostGroup::Ptr& hg : ConfigType::GetObjectsByType<HostGroup>()) {
76 if (!addRowFn(hg, LivestatusGroupByNone, Empty))
81 Value HostGroupsTable::NameAccessor(const Value& row)
83 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
91 Value HostGroupsTable::AliasAccessor(const Value& row)
93 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
98 return hg->GetDisplayName();
101 Value HostGroupsTable::NotesAccessor(const Value& row)
103 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
108 return hg->GetNotes();
111 Value HostGroupsTable::NotesUrlAccessor(const Value& row)
113 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
118 return hg->GetNotesUrl();
121 Value HostGroupsTable::ActionUrlAccessor(const Value& row)
123 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
128 return hg->GetActionUrl();
131 Value HostGroupsTable::MembersAccessor(const Value& row)
133 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
138 Array::Ptr members = new Array();
140 for (const Host::Ptr& host : hg->GetMembers()) {
141 members->Add(host->GetName());
147 Value HostGroupsTable::MembersWithStateAccessor(const Value& row)
149 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
154 Array::Ptr members = new Array();
156 for (const Host::Ptr& host : hg->GetMembers()) {
157 Array::Ptr member_state = new Array();
158 member_state->Add(host->GetName());
159 member_state->Add(host->GetState());
160 members->Add(member_state);
166 Value HostGroupsTable::WorstHostStateAccessor(const Value& row)
168 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
173 int worst_host = HostUp;
175 for (const Host::Ptr& host : hg->GetMembers()) {
176 if (host->GetState() > worst_host)
177 worst_host = host->GetState();
183 Value HostGroupsTable::NumHostsAccessor(const Value& row)
185 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
190 return hg->GetMembers().size();
193 Value HostGroupsTable::NumHostsPendingAccessor(const Value& row)
195 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
202 for (const Host::Ptr& host : hg->GetMembers()) {
204 if (!host->GetLastCheckResult())
211 Value HostGroupsTable::NumHostsUpAccessor(const Value& row)
213 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
220 for (const Host::Ptr& host : hg->GetMembers()) {
221 if (host->GetState() == HostUp)
228 Value HostGroupsTable::NumHostsDownAccessor(const Value& row)
230 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
237 for (const Host::Ptr& host : hg->GetMembers()) {
238 if (host->GetState() == HostDown)
245 Value HostGroupsTable::NumHostsUnreachAccessor(const Value& row)
247 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
254 for (const Host::Ptr& host : hg->GetMembers()) {
255 if (!host->IsReachable())
262 Value HostGroupsTable::NumServicesAccessor(const Value& row)
264 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
269 int num_services = 0;
271 if (hg->GetMembers().size() == 0)
274 for (const Host::Ptr& host : hg->GetMembers()) {
275 num_services += host->GetServices().size();
281 Value HostGroupsTable::WorstServicesStateAccessor(const Value& row)
283 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
288 Value worst_service = ServiceOK;
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();
297 return worst_service;
300 Value HostGroupsTable::NumServicesPendingAccessor(const Value& row)
302 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
307 int num_services = 0;
309 for (const Host::Ptr& host : hg->GetMembers()) {
310 for (const Service::Ptr& service : host->GetServices()) {
311 if (!service->GetLastCheckResult())
319 Value HostGroupsTable::NumServicesOkAccessor(const Value& row)
321 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
326 int num_services = 0;
328 for (const Host::Ptr& host : hg->GetMembers()) {
329 for (const Service::Ptr& service : host->GetServices()) {
330 if (service->GetState() == ServiceOK)
338 Value HostGroupsTable::NumServicesWarnAccessor(const Value& row)
340 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
345 int num_services = 0;
347 for (const Host::Ptr& host : hg->GetMembers()) {
348 for (const Service::Ptr& service : host->GetServices()) {
349 if (service->GetState() == ServiceWarning)
357 Value HostGroupsTable::NumServicesCritAccessor(const Value& row)
359 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
364 int num_services = 0;
366 for (const Host::Ptr& host : hg->GetMembers()) {
367 for (const Service::Ptr& service : host->GetServices()) {
368 if (service->GetState() == ServiceCritical)
376 Value HostGroupsTable::NumServicesUnknownAccessor(const Value& row)
378 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
383 int num_services = 0;
385 for (const Host::Ptr& host : hg->GetMembers()) {
386 for (const Service::Ptr& service : host->GetServices()) {
387 if (service->GetState() == ServiceUnknown)
395 Value HostGroupsTable::WorstServiceHardStateAccessor(const Value& row)
397 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
402 Value worst_service = ServiceOK;
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();
413 return worst_service;
416 Value HostGroupsTable::NumServicesHardOkAccessor(const Value& row)
418 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
423 int num_services = 0;
425 for (const Host::Ptr& host : hg->GetMembers()) {
426 for (const Service::Ptr& service : host->GetServices()) {
427 if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceOK)
435 Value HostGroupsTable::NumServicesHardWarnAccessor(const Value& row)
437 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
442 int num_services = 0;
444 for (const Host::Ptr& host : hg->GetMembers()) {
445 for (const Service::Ptr& service : host->GetServices()) {
446 if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceWarning)
454 Value HostGroupsTable::NumServicesHardCritAccessor(const Value& row)
456 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
461 int num_services = 0;
463 for (const Host::Ptr& host : hg->GetMembers()) {
464 for (const Service::Ptr& service : host->GetServices()) {
465 if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceCritical)
473 Value HostGroupsTable::NumServicesHardUnknownAccessor(const Value& row)
475 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
480 int num_services = 0;
482 for (const Host::Ptr& host : hg->GetMembers()) {
483 for (const Service::Ptr& service : host->GetServices()) {
484 if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceUnknown)