1 /******************************************************************************
3 * Copyright (C) 2012-present Icinga Development Team (http://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.h"
21 #include "icinga/hostgroup.h"
22 #include "icinga/host.h"
23 #include "icinga/service.h"
24 #include "base/dynamictype.h"
25 #include <boost/foreach.hpp>
27 using namespace icinga;
29 HostGroupsTable::HostGroupsTable(void)
34 void HostGroupsTable::AddColumns(Table *table, const String& prefix,
35 const Column::ObjectAccessor& objectAccessor)
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));
64 String HostGroupsTable::GetName(void) const
69 void HostGroupsTable::FetchRows(const AddRowFunction& addRowFn)
71 BOOST_FOREACH(const HostGroup::Ptr& hg, DynamicType::GetObjects<HostGroup>()) {
76 Value HostGroupsTable::NameAccessor(const Value& row)
78 return static_cast<HostGroup::Ptr>(row)->GetName();
81 Value HostGroupsTable::AliasAccessor(const Value& row)
83 return static_cast<HostGroup::Ptr>(row)->GetDisplayName();
86 Value HostGroupsTable::NotesAccessor(const Value& row)
88 Dictionary::Ptr custom = static_cast<HostGroup::Ptr>(row)->GetCustom();
93 return custom->Get("notes");
96 Value HostGroupsTable::NotesUrlAccessor(const Value& row)
98 Dictionary::Ptr custom = static_cast<HostGroup::Ptr>(row)->GetCustom();
103 return custom->Get("notes_url");
106 Value HostGroupsTable::ActionUrlAccessor(const Value& row)
108 Dictionary::Ptr custom = static_cast<HostGroup::Ptr>(row)->GetCustom();
113 return custom->Get("action_url");
116 Value HostGroupsTable::MembersAccessor(const Value& row)
118 Array::Ptr members = make_shared<Array>();
120 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
121 members->Add(host->GetName());
127 Value HostGroupsTable::MembersWithStateAccessor(const Value& row)
129 Array::Ptr members = make_shared<Array>();
131 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
132 Array::Ptr member_state = make_shared<Array>();
133 member_state->Add(host->GetName());
134 member_state->Add(host->GetState());
135 members->Add(member_state);
141 Value HostGroupsTable::WorstHostStateAccessor(const Value& row)
143 int worst_host = HostUp;
145 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
146 if (host->GetState() > worst_host)
147 worst_host = host->GetState();
153 Value HostGroupsTable::NumHostsAccessor(const Value& row)
155 return static_cast<long>(static_cast<HostGroup::Ptr>(row)->GetMembers().size());
158 Value HostGroupsTable::NumHostsPendingAccessor(const Value& row)
162 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
163 Service::Ptr hc = host->GetCheckService();
165 /* no hostcheck service or no checkresult */
166 if (!hc || !hc->GetLastCheckResult())
173 Value HostGroupsTable::NumHostsUpAccessor(const Value& row)
177 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
178 if (host->GetState() == HostUp)
185 Value HostGroupsTable::NumHostsDownAccessor(const Value& row)
189 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
190 if (host->GetState() == HostDown)
197 Value HostGroupsTable::NumHostsUnreachAccessor(const Value& row)
201 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
202 if (host->GetState() == HostUnreachable)
209 Value HostGroupsTable::NumServicesAccessor(const Value& row)
211 int num_services = 0;
212 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
214 if (hg->GetMembers().size() == 0)
217 BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
218 num_services += host->GetServices().size();
224 Value HostGroupsTable::WorstServicesStateAccessor(const Value& row)
226 Value worst_service = StateOK;
228 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
229 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
230 if (service->GetState() > worst_service)
231 worst_service = service->GetState();
235 return worst_service;
238 Value HostGroupsTable::NumServicesPendingAccessor(const Value& row)
240 int num_services = 0;
242 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
243 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
244 if (!service->GetLastCheckResult())
252 Value HostGroupsTable::NumServicesOkAccessor(const Value& row)
254 int num_services = 0;
256 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
257 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
258 if (service->GetState() == StateOK)
266 Value HostGroupsTable::NumServicesWarnAccessor(const Value& row)
268 int num_services = 0;
270 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
271 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
272 if (service->GetState() == StateWarning)
280 Value HostGroupsTable::NumServicesCritAccessor(const Value& row)
282 int num_services = 0;
284 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
285 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
286 if (service->GetState() == StateCritical)
294 Value HostGroupsTable::NumServicesUnknownAccessor(const Value& row)
296 int num_services = 0;
298 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
299 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
300 if (service->GetState() == StateUnknown)
308 Value HostGroupsTable::WorstServiceHardStateAccessor(const Value& row)
310 Value worst_service = StateOK;
312 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
313 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
314 if (service->GetStateType() == StateTypeHard) {
315 if (service->GetState() > worst_service)
316 worst_service = service->GetState();
321 return worst_service;
324 Value HostGroupsTable::NumServicesHardOkAccessor(const Value& row)
326 int num_services = 0;
328 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
329 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
330 if (service->GetStateType() == StateTypeHard && service->GetState() == StateOK)
338 Value HostGroupsTable::NumServicesHardWarnAccessor(const Value& row)
340 int num_services = 0;
342 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
343 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
344 if (service->GetStateType() == StateTypeHard && service->GetState() == StateWarning)
352 Value HostGroupsTable::NumServicesHardCritAccessor(const Value& row)
354 int num_services = 0;
356 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
357 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
358 if (service->GetStateType() == StateTypeHard && service->GetState() == StateCritical)
366 Value HostGroupsTable::NumServicesHardUnknownAccessor(const Value& row)
368 int num_services = 0;
370 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
371 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
372 if (service->GetStateType() == StateTypeHard && service->GetState() == StateUnknown)