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