]> granicus.if.org Git - icinga2/blob - components/livestatus/hostgroupstable.cpp
Remove the HostUnreachable state.
[icinga2] / components / livestatus / hostgroupstable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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>
26
27 using namespace icinga;
28
29 HostGroupsTable::HostGroupsTable(void)
30 {
31         AddColumns(this);
32 }
33
34 void HostGroupsTable::AddColumns(Table *table, const String& prefix,
35     const Column::ObjectAccessor& objectAccessor)
36 {
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));
62 }
63
64 String HostGroupsTable::GetName(void) const
65 {
66         return "hostgroups";
67 }
68
69 void HostGroupsTable::FetchRows(const AddRowFunction& addRowFn)
70 {
71         BOOST_FOREACH(const HostGroup::Ptr& hg, DynamicType::GetObjects<HostGroup>()) {
72                 addRowFn(hg);
73         }
74 }
75
76 Value HostGroupsTable::NameAccessor(const Value& row)
77 {
78         return static_cast<HostGroup::Ptr>(row)->GetName();
79 }
80
81 Value HostGroupsTable::AliasAccessor(const Value& row)
82 {
83         return static_cast<HostGroup::Ptr>(row)->GetDisplayName();
84 }
85
86 Value HostGroupsTable::NotesAccessor(const Value& row)
87 {
88         Dictionary::Ptr vars = static_cast<HostGroup::Ptr>(row)->GetVars();
89
90         if (!vars)
91                 return Empty;
92
93         return vars->Get("notes");
94 }
95
96 Value HostGroupsTable::NotesUrlAccessor(const Value& row)
97 {
98         Dictionary::Ptr vars = static_cast<HostGroup::Ptr>(row)->GetVars();
99
100         if (!vars)
101                 return Empty;
102
103         return vars->Get("notes_url");
104 }
105
106 Value HostGroupsTable::ActionUrlAccessor(const Value& row)
107 {
108         Dictionary::Ptr vars = static_cast<HostGroup::Ptr>(row)->GetVars();
109
110         if (!vars)
111                 return Empty;
112
113         return vars->Get("action_url");
114 }
115
116 Value HostGroupsTable::MembersAccessor(const Value& row)
117 {
118         Array::Ptr members = make_shared<Array>();
119
120         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
121                 members->Add(host->GetName());
122         }
123
124         return members;
125 }
126
127 Value HostGroupsTable::MembersWithStateAccessor(const Value& row)
128 {
129         Array::Ptr members = make_shared<Array>();
130
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);
136         }
137
138         return members;
139 }
140
141 Value HostGroupsTable::WorstHostStateAccessor(const Value& row)
142 {
143         int worst_host = HostUp;
144
145         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
146                 if (host->GetState() > worst_host)
147                         worst_host = host->GetState();
148         }
149
150         return worst_host;
151 }
152
153 Value HostGroupsTable::NumHostsAccessor(const Value& row)
154 {
155         return static_cast<long>(static_cast<HostGroup::Ptr>(row)->GetMembers().size());
156 }
157
158 Value HostGroupsTable::NumHostsPendingAccessor(const Value& row)
159 {
160         int num_hosts = 0;
161
162         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
163                 /* no checkresult */
164                 if (!host->GetLastCheckResult())
165                         num_hosts++;
166         }
167
168         return num_hosts;
169 }
170
171 Value HostGroupsTable::NumHostsUpAccessor(const Value& row)
172 {
173         int num_hosts = 0;
174
175         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
176                 if (host->GetState() == HostUp)
177                         num_hosts++;
178         }
179
180         return num_hosts;
181 }
182
183 Value HostGroupsTable::NumHostsDownAccessor(const Value& row)
184 {
185         int num_hosts = 0;
186
187         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
188                 if (host->GetState() == HostDown)
189                         num_hosts++;
190         }
191
192         return num_hosts;
193 }
194
195 Value HostGroupsTable::NumHostsUnreachAccessor(const Value& row)
196 {
197         int num_hosts = 0;
198
199         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
200                 if (!host->IsReachable())
201                         num_hosts++;
202         }
203
204         return num_hosts;
205 }
206
207 Value HostGroupsTable::NumServicesAccessor(const Value& row)
208 {
209         int num_services = 0;
210         HostGroup::Ptr hg = static_cast<HostGroup::Ptr>(row);
211
212         if (hg->GetMembers().size() == 0)
213                 return 0;
214
215         BOOST_FOREACH(const Host::Ptr& host, hg->GetMembers()) {
216                 num_services += host->GetServices().size();
217         }
218
219         return num_services;
220 }
221
222 Value HostGroupsTable::WorstServicesStateAccessor(const Value& row)
223 {
224         Value worst_service = StateOK;
225
226         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
227                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
228                         if (service->GetState() > worst_service)
229                                 worst_service = service->GetState();
230                 }
231         }
232
233         return worst_service;
234 }
235
236 Value HostGroupsTable::NumServicesPendingAccessor(const Value& row)
237 {
238         int num_services = 0;
239
240         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
241                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
242                         if (!service->GetLastCheckResult())
243                                 num_services++;
244                 }
245         }
246
247         return num_services;
248 }
249
250 Value HostGroupsTable::NumServicesOkAccessor(const Value& row)
251 {
252         int num_services = 0;
253
254         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
255                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
256                         if (service->GetState() == StateOK)
257                                 num_services++;
258                 }
259         }
260
261         return num_services;
262 }
263
264 Value HostGroupsTable::NumServicesWarnAccessor(const Value& row)
265 {
266         int num_services = 0;
267
268         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
269                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
270                         if (service->GetState() == StateWarning)
271                                 num_services++;
272                 }
273         }
274
275         return num_services;
276 }
277
278 Value HostGroupsTable::NumServicesCritAccessor(const Value& row)
279 {
280         int num_services = 0;
281
282         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
283                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
284                         if (service->GetState() == StateCritical)
285                                 num_services++;
286                 }
287         }
288
289         return num_services;
290 }
291
292 Value HostGroupsTable::NumServicesUnknownAccessor(const Value& row)
293 {
294         int num_services = 0;
295
296         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
297                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
298                         if (service->GetState() == StateUnknown)
299                                 num_services++;
300                 }
301         }
302
303         return num_services;
304 }
305
306 Value HostGroupsTable::WorstServiceHardStateAccessor(const Value& row)
307 {
308         Value worst_service = StateOK;
309
310         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
311                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
312                         if (service->GetStateType() == StateTypeHard) {
313                                 if (service->GetState() > worst_service)
314                                         worst_service = service->GetState();
315                         }
316                 }
317         }
318
319         return worst_service;
320 }
321
322 Value HostGroupsTable::NumServicesHardOkAccessor(const Value& row)
323 {
324         int num_services = 0;
325
326         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
327                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
328                         if (service->GetStateType() == StateTypeHard && service->GetState() == StateOK)
329                                 num_services++;
330                 }
331         }
332
333         return num_services;
334 }
335
336 Value HostGroupsTable::NumServicesHardWarnAccessor(const Value& row)
337 {
338         int num_services = 0;
339
340         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
341                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
342                         if (service->GetStateType() == StateTypeHard && service->GetState() == StateWarning)
343                                 num_services++;
344                 }
345         }
346
347         return num_services;
348 }
349
350 Value HostGroupsTable::NumServicesHardCritAccessor(const Value& row)
351 {
352         int num_services = 0;
353
354         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
355                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
356                         if (service->GetStateType() == StateTypeHard && service->GetState() == StateCritical)
357                                 num_services++;
358                 }
359         }
360
361         return num_services;
362 }
363
364 Value HostGroupsTable::NumServicesHardUnknownAccessor(const Value& row)
365 {
366         int num_services = 0;
367
368         BOOST_FOREACH(const Host::Ptr& host, static_cast<HostGroup::Ptr>(row)->GetMembers()) {
369                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
370                         if (service->GetStateType() == StateTypeHard && service->GetState() == StateUnknown)
371                                 num_services++;
372                 }
373         }
374
375         return num_services;
376 }