1 /******************************************************************************
3 * Copyright (C) 2012 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;
28 using namespace livestatus;
30 HostGroupsTable::HostGroupsTable(void)
35 void HostGroupsTable::AddColumns(Table *table, const String& prefix,
36 const Column::ObjectAccessor& objectAccessor)
38 table->AddColumn(prefix + "name", Column(&HostGroupsTable::NameAccessor, objectAccessor));
39 table->AddColumn(prefix + "alias", Column(&HostGroupsTable::AliasAccessor, objectAccessor));
40 table->AddColumn(prefix + "notes", Column(&HostGroupsTable::NotesAccessor, objectAccessor));
41 table->AddColumn(prefix + "notes_url", Column(&HostGroupsTable::NotesUrlAccessor, objectAccessor));
42 table->AddColumn(prefix + "action_url", Column(&HostGroupsTable::ActionUrlAccessor, objectAccessor));
43 table->AddColumn(prefix + "members", Column(&HostGroupsTable::MembersAccessor, objectAccessor));
44 table->AddColumn(prefix + "members_with_state", Column(&HostGroupsTable::MembersWithStateAccessor, objectAccessor));
45 table->AddColumn(prefix + "worst_host_state", Column(&HostGroupsTable::WorstHostStateAccessor, objectAccessor));
46 table->AddColumn(prefix + "num_hosts", Column(&HostGroupsTable::NumHostsAccessor, objectAccessor));
47 table->AddColumn(prefix + "num_hosts_pending", Column(&HostGroupsTable::NumHostsPendingAccessor, objectAccessor));
48 table->AddColumn(prefix + "num_hosts_up", Column(&HostGroupsTable::NumHostsUpAccessor, objectAccessor));
49 table->AddColumn(prefix + "num_hosts_down", Column(&HostGroupsTable::NumHostsDownAccessor, objectAccessor));
50 table->AddColumn(prefix + "num_hosts_unreach", Column(&HostGroupsTable::NumHostsUnreachAccessor, objectAccessor));
51 table->AddColumn(prefix + "num_services", Column(&HostGroupsTable::NumServicesAccessor, objectAccessor));
52 table->AddColumn(prefix + "worst_services_state", Column(&HostGroupsTable::WorstServicesStateAccessor, objectAccessor));
53 table->AddColumn(prefix + "num_services_pending", Column(&HostGroupsTable::NumServicesPendingAccessor, objectAccessor));
54 table->AddColumn(prefix + "num_services_ok", Column(&HostGroupsTable::NumServicesOkAccessor, objectAccessor));
55 table->AddColumn(prefix + "num_services_warn", Column(&HostGroupsTable::NumServicesWarnAccessor, objectAccessor));
56 table->AddColumn(prefix + "num_services_crit", Column(&HostGroupsTable::NumServicesCritAccessor, objectAccessor));
57 table->AddColumn(prefix + "num_services_unknown", Column(&HostGroupsTable::NumServicesUnknownAccessor, objectAccessor));
58 table->AddColumn(prefix + "worst_service_hard_state", Column(&HostGroupsTable::WorstServiceHardStateAccessor, objectAccessor));
59 table->AddColumn(prefix + "num_services_hard_ok", Column(&HostGroupsTable::NumServicesHardOkAccessor, objectAccessor));
60 table->AddColumn(prefix + "num_services_hard_warn", Column(&HostGroupsTable::NumServicesHardWarnAccessor, objectAccessor));
61 table->AddColumn(prefix + "num_services_hard_crit", Column(&HostGroupsTable::NumServicesHardCritAccessor, objectAccessor));
62 table->AddColumn(prefix + "num_services_hard_unknown", Column(&HostGroupsTable::NumServicesHardUnknownAccessor, objectAccessor));
65 String HostGroupsTable::GetName(void) const
70 void HostGroupsTable::FetchRows(const AddRowFunction& addRowFn)
72 BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("HostGroup")) {
77 Value HostGroupsTable::NameAccessor(const Value& row)
79 return static_cast<HostGroup::Ptr>(row)->GetName();
82 Value HostGroupsTable::AliasAccessor(const Value& row)
84 return static_cast<HostGroup::Ptr>(row)->GetDisplayName();
87 Value HostGroupsTable::NotesAccessor(const Value& row)
89 Dictionary::Ptr custom = static_cast<HostGroup::Ptr>(row)->GetCustom();
94 return custom->Get("notes");
97 Value HostGroupsTable::NotesUrlAccessor(const Value& row)
99 Dictionary::Ptr custom = static_cast<HostGroup::Ptr>(row)->GetCustom();
104 return custom->Get("notes_url");
107 Value HostGroupsTable::ActionUrlAccessor(const Value& row)
109 Dictionary::Ptr custom = static_cast<HostGroup::Ptr>(row)->GetCustom();
114 return custom->Get("action_url");
117 Value HostGroupsTable::MembersAccessor(const Value& row)
119 Array::Ptr members = boost::make_shared<Array>();
121 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
122 members->Add(host->GetName());
128 Value HostGroupsTable::MembersWithStateAccessor(const Value& row)
134 Value HostGroupsTable::WorstHostStateAccessor(const Value& row)
136 int worst_host = HostUp;
138 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
139 if (host->GetState() > worst_host)
140 worst_host = host->GetState();
146 Value HostGroupsTable::NumHostsAccessor(const Value& row)
148 return static_cast<HostGroup::Ptr>(row)->GetMembers().size();
151 Value HostGroupsTable::NumHostsPendingAccessor(const Value& row)
155 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
156 Service::Ptr hc = host->GetHostCheckService();
158 /* no hostcheck service or no checkresult */
159 if (!hc || !hc->GetLastCheckResult())
166 Value HostGroupsTable::NumHostsUpAccessor(const Value& row)
170 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
171 if (host->GetState() == HostUp)
178 Value HostGroupsTable::NumHostsDownAccessor(const Value& row)
182 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
183 if (host->GetState() == HostDown)
190 Value HostGroupsTable::NumHostsUnreachAccessor(const Value& row)
194 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
195 if (host->GetState() == HostUnreachable)
202 Value HostGroupsTable::NumServicesAccessor(const Value& row)
204 int num_services = 0;
205 HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
207 if (hg->GetMembers().size() == 0)
210 BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
211 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
219 Value HostGroupsTable::WorstServicesStateAccessor(const Value& row)
221 Value worst_service = StateOK;
223 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
224 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
225 if (service->GetState() > worst_service)
226 worst_service = service->GetState();
230 return worst_service;
233 Value HostGroupsTable::NumServicesPendingAccessor(const Value& row)
235 int num_services = 0;
237 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
238 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
239 if (!service->GetLastCheckResult())
247 Value HostGroupsTable::NumServicesOkAccessor(const Value& row)
249 int num_services = 0;
251 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
252 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
253 if (service->GetState() == StateOK)
261 Value HostGroupsTable::NumServicesWarnAccessor(const Value& row)
263 int num_services = 0;
265 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
266 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
267 if (service->GetState() == StateWarning)
275 Value HostGroupsTable::NumServicesCritAccessor(const Value& row)
277 int num_services = 0;
279 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
280 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
281 if (service->GetState() == StateCritical)
289 Value HostGroupsTable::NumServicesUnknownAccessor(const Value& row)
291 int num_services = 0;
293 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
294 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
295 if (service->GetState() == StateUnknown)
303 Value HostGroupsTable::WorstServiceHardStateAccessor(const Value& row)
305 Value worst_service = StateOK;
307 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
308 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
309 if (service->GetStateType() == StateTypeHard) {
310 if (service->GetState() > worst_service)
311 worst_service = service->GetState();
316 return worst_service;
319 Value HostGroupsTable::NumServicesHardOkAccessor(const Value& row)
321 int num_services = 0;
323 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
324 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
325 if (service->GetStateType() == StateTypeHard && service->GetState() == StateOK)
333 Value HostGroupsTable::NumServicesHardWarnAccessor(const Value& row)
335 int num_services = 0;
337 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
338 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
339 if (service->GetStateType() == StateTypeHard && service->GetState() == StateWarning)
347 Value HostGroupsTable::NumServicesHardCritAccessor(const Value& row)
349 int num_services = 0;
351 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
352 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
353 if (service->GetStateType() == StateTypeHard && service->GetState() == StateCritical)
361 Value HostGroupsTable::NumServicesHardUnknownAccessor(const Value& row)
363 int num_services = 0;
365 BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
366 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
367 if (service->GetStateType() == StateTypeHard && service->GetState() == StateUnknown)