]> granicus.if.org Git - icinga2/blob - lib/livestatus/servicegroupstable.cpp
Remove more redundant wrappers from CompatUtility class
[icinga2] / lib / livestatus / servicegroupstable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
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/servicegroupstable.hpp"
21 #include "icinga/servicegroup.hpp"
22 #include "base/configtype.hpp"
23
24 using namespace icinga;
25
26 ServiceGroupsTable::ServiceGroupsTable()
27 {
28         AddColumns(this);
29 }
30
31 void ServiceGroupsTable::AddColumns(Table *table, const String& prefix,
32         const Column::ObjectAccessor& objectAccessor)
33 {
34         table->AddColumn(prefix + "name", Column(&ServiceGroupsTable::NameAccessor, objectAccessor));
35         table->AddColumn(prefix + "alias", Column(&ServiceGroupsTable::AliasAccessor, objectAccessor));
36         table->AddColumn(prefix + "notes", Column(&ServiceGroupsTable::NotesAccessor, objectAccessor));
37         table->AddColumn(prefix + "notes_url", Column(&ServiceGroupsTable::NotesUrlAccessor, objectAccessor));
38         table->AddColumn(prefix + "action_url", Column(&ServiceGroupsTable::ActionUrlAccessor, objectAccessor));
39         table->AddColumn(prefix + "members", Column(&ServiceGroupsTable::MembersAccessor, objectAccessor));
40         table->AddColumn(prefix + "members_with_state", Column(&ServiceGroupsTable::MembersWithStateAccessor, objectAccessor));
41         table->AddColumn(prefix + "worst_service_state", Column(&ServiceGroupsTable::WorstServiceStateAccessor, objectAccessor));
42         table->AddColumn(prefix + "num_services", Column(&ServiceGroupsTable::NumServicesAccessor, objectAccessor));
43         table->AddColumn(prefix + "num_services_ok", Column(&ServiceGroupsTable::NumServicesOkAccessor, objectAccessor));
44         table->AddColumn(prefix + "num_services_warn", Column(&ServiceGroupsTable::NumServicesWarnAccessor, objectAccessor));
45         table->AddColumn(prefix + "num_services_crit", Column(&ServiceGroupsTable::NumServicesCritAccessor, objectAccessor));
46         table->AddColumn(prefix + "num_services_unknown", Column(&ServiceGroupsTable::NumServicesUnknownAccessor, objectAccessor));
47         table->AddColumn(prefix + "num_services_pending", Column(&ServiceGroupsTable::NumServicesPendingAccessor, objectAccessor));
48         table->AddColumn(prefix + "num_services_hard_ok", Column(&ServiceGroupsTable::NumServicesHardOkAccessor, objectAccessor));
49         table->AddColumn(prefix + "num_services_hard_warn", Column(&ServiceGroupsTable::NumServicesHardWarnAccessor, objectAccessor));
50         table->AddColumn(prefix + "num_services_hard_crit", Column(&ServiceGroupsTable::NumServicesHardCritAccessor, objectAccessor));
51         table->AddColumn(prefix + "num_services_hard_unknown", Column(&ServiceGroupsTable::NumServicesHardUnknownAccessor, objectAccessor));
52 }
53
54 String ServiceGroupsTable::GetName() const
55 {
56         return "servicegroups";
57 }
58
59 String ServiceGroupsTable::GetPrefix() const
60 {
61         return "servicegroup";
62 }
63
64 void ServiceGroupsTable::FetchRows(const AddRowFunction& addRowFn)
65 {
66         for (const ServiceGroup::Ptr& sg : ConfigType::GetObjectsByType<ServiceGroup>()) {
67                 if (!addRowFn(sg, LivestatusGroupByNone, Empty))
68                         return;
69         }
70 }
71
72 Value ServiceGroupsTable::NameAccessor(const Value& row)
73 {
74         ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
75
76         if (!sg)
77                 return Empty;
78
79         return sg->GetName();
80 }
81
82 Value ServiceGroupsTable::AliasAccessor(const Value& row)
83 {
84         ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
85
86         if (!sg)
87                 return Empty;
88
89         return sg->GetDisplayName();
90 }
91
92 Value ServiceGroupsTable::NotesAccessor(const Value& row)
93 {
94         ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
95
96         if (!sg)
97                 return Empty;
98
99         return sg->GetNotes();
100 }
101
102 Value ServiceGroupsTable::NotesUrlAccessor(const Value& row)
103 {
104         ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
105
106         if (!sg)
107                 return Empty;
108
109         return sg->GetNotesUrl();
110 }
111
112 Value ServiceGroupsTable::ActionUrlAccessor(const Value& row)
113 {
114         ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
115
116         if (!sg)
117                 return Empty;
118
119         return sg->GetActionUrl();
120 }
121
122 Value ServiceGroupsTable::MembersAccessor(const Value& row)
123 {
124         ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
125
126         if (!sg)
127                 return Empty;
128
129         Array::Ptr members = new Array();
130
131         for (const Service::Ptr& service : sg->GetMembers()) {
132                 Array::Ptr host_svc = new Array();
133                 host_svc->Add(service->GetHost()->GetName());
134                 host_svc->Add(service->GetShortName());
135                 members->Add(host_svc);
136         }
137
138         return members;
139 }
140
141 Value ServiceGroupsTable::MembersWithStateAccessor(const Value& row)
142 {
143         ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
144
145         if (!sg)
146                 return Empty;
147
148         Array::Ptr members = new Array();
149
150         for (const Service::Ptr& service : sg->GetMembers()) {
151                 Array::Ptr host_svc = new Array();
152                 host_svc->Add(service->GetHost()->GetName());
153                 host_svc->Add(service->GetShortName());
154                 host_svc->Add(service->GetHost()->GetState());
155                 host_svc->Add(service->GetState());
156                 members->Add(host_svc);
157         }
158
159         return members;
160 }
161
162 Value ServiceGroupsTable::WorstServiceStateAccessor(const Value& row)
163 {
164         ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
165
166         if (!sg)
167                 return Empty;
168
169         Value worst_service = ServiceOK;
170
171         for (const Service::Ptr& service : sg->GetMembers()) {
172                 if (service->GetState() > worst_service)
173                         worst_service = service->GetState();
174         }
175
176         return worst_service;
177 }
178
179 Value ServiceGroupsTable::NumServicesAccessor(const Value& row)
180 {
181         ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
182
183         if (!sg)
184                 return Empty;
185
186         return sg->GetMembers().size();
187 }
188
189 Value ServiceGroupsTable::NumServicesOkAccessor(const Value& row)
190 {
191         ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
192
193         if (!sg)
194                 return Empty;
195
196         int num_services = 0;
197
198         for (const Service::Ptr& service : sg->GetMembers()) {
199                 if (service->GetState() == ServiceOK)
200                         num_services++;
201         }
202
203         return num_services;
204 }
205
206 Value ServiceGroupsTable::NumServicesWarnAccessor(const Value& row)
207 {
208         ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
209
210         if (!sg)
211                 return Empty;
212
213         int num_services = 0;
214
215         for (const Service::Ptr& service : sg->GetMembers()) {
216                 if (service->GetState() == ServiceWarning)
217                         num_services++;
218         }
219
220         return num_services;
221 }
222
223 Value ServiceGroupsTable::NumServicesCritAccessor(const Value& row)
224 {
225         ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
226
227         if (!sg)
228                 return Empty;
229
230         int num_services = 0;
231
232         for (const Service::Ptr& service : sg->GetMembers()) {
233                 if (service->GetState() == ServiceCritical)
234                         num_services++;
235         }
236
237         return num_services;
238 }
239
240 Value ServiceGroupsTable::NumServicesUnknownAccessor(const Value& row)
241 {
242         ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
243
244         if (!sg)
245                 return Empty;
246
247         int num_services = 0;
248
249         for (const Service::Ptr& service : sg->GetMembers()) {
250                 if (service->GetState() == ServiceUnknown)
251                         num_services++;
252         }
253
254         return num_services;
255 }
256
257 Value ServiceGroupsTable::NumServicesPendingAccessor(const Value& row)
258 {
259         ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
260
261         if (!sg)
262                 return Empty;
263
264         int num_services = 0;
265
266         for (const Service::Ptr& service : sg->GetMembers()) {
267                 if (!service->GetLastCheckResult())
268                         num_services++;
269         }
270
271         return num_services;
272 }
273
274 Value ServiceGroupsTable::NumServicesHardOkAccessor(const Value& row)
275 {
276         ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
277
278         if (!sg)
279                 return Empty;
280
281         int num_services = 0;
282
283         for (const Service::Ptr& service : sg->GetMembers()) {
284                 if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceOK)
285                         num_services++;
286         }
287
288         return num_services;
289 }
290
291 Value ServiceGroupsTable::NumServicesHardWarnAccessor(const Value& row)
292 {
293         ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
294
295         if (!sg)
296                 return Empty;
297
298         int num_services = 0;
299
300         for (const Service::Ptr& service : sg->GetMembers()) {
301                 if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceWarning)
302                         num_services++;
303         }
304
305         return num_services;
306 }
307
308 Value ServiceGroupsTable::NumServicesHardCritAccessor(const Value& row)
309 {
310         ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
311
312         if (!sg)
313                 return Empty;
314
315         int num_services = 0;
316
317         for (const Service::Ptr& service : sg->GetMembers()) {
318                 if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceCritical)
319                         num_services++;
320         }
321
322         return num_services;
323 }
324
325 Value ServiceGroupsTable::NumServicesHardUnknownAccessor(const Value& row)
326 {
327         ServiceGroup::Ptr sg = static_cast<ServiceGroup::Ptr>(row);
328
329         if (!sg)
330                 return Empty;
331
332         int num_services = 0;
333
334         for (const Service::Ptr& service : sg->GetMembers()) {
335                 if (service->GetStateType() == StateTypeHard && service->GetState() == ServiceUnknown)
336                         num_services++;
337         }
338
339         return num_services;
340 }