]> granicus.if.org Git - icinga2/blob - lib/livestatus/servicestable.cpp
Drop dead code in Livestatus feature
[icinga2] / lib / livestatus / servicestable.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/servicestable.hpp"
21 #include "livestatus/hoststable.hpp"
22 #include "livestatus/servicegroupstable.hpp"
23 #include "livestatus/hostgroupstable.hpp"
24 #include "livestatus/endpointstable.hpp"
25 #include "icinga/service.hpp"
26 #include "icinga/servicegroup.hpp"
27 #include "icinga/hostgroup.hpp"
28 #include "icinga/checkcommand.hpp"
29 #include "icinga/eventcommand.hpp"
30 #include "icinga/timeperiod.hpp"
31 #include "icinga/macroprocessor.hpp"
32 #include "icinga/icingaapplication.hpp"
33 #include "icinga/compatutility.hpp"
34 #include "base/configtype.hpp"
35 #include "base/objectlock.hpp"
36 #include "base/json.hpp"
37 #include "base/convert.hpp"
38 #include "base/utility.hpp"
39 #include <boost/tuple/tuple.hpp>
40 #include <boost/algorithm/string/replace.hpp>
41
42 using namespace icinga;
43
44 ServicesTable::ServicesTable(LivestatusGroupByType type)
45         : Table(type)
46 {
47         AddColumns(this);
48 }
49
50
51 void ServicesTable::AddColumns(Table *table, const String& prefix,
52         const Column::ObjectAccessor& objectAccessor)
53 {
54         table->AddColumn(prefix + "description", Column(&ServicesTable::ShortNameAccessor, objectAccessor));
55         table->AddColumn(prefix + "service_description", Column(&ServicesTable::ShortNameAccessor, objectAccessor)); //ugly compatibility hack
56         table->AddColumn(prefix + "display_name", Column(&ServicesTable::DisplayNameAccessor, objectAccessor));
57         table->AddColumn(prefix + "check_command", Column(&ServicesTable::CheckCommandAccessor, objectAccessor));
58         table->AddColumn(prefix + "check_command_expanded", Column(&ServicesTable::CheckCommandExpandedAccessor, objectAccessor));
59         table->AddColumn(prefix + "event_handler", Column(&ServicesTable::EventHandlerAccessor, objectAccessor));
60         table->AddColumn(prefix + "plugin_output", Column(&ServicesTable::PluginOutputAccessor, objectAccessor));
61         table->AddColumn(prefix + "long_plugin_output", Column(&ServicesTable::LongPluginOutputAccessor, objectAccessor));
62         table->AddColumn(prefix + "perf_data", Column(&ServicesTable::PerfDataAccessor, objectAccessor));
63         table->AddColumn(prefix + "notification_period", Column(&Table::EmptyStringAccessor, objectAccessor));
64         table->AddColumn(prefix + "check_period", Column(&ServicesTable::CheckPeriodAccessor, objectAccessor));
65         table->AddColumn(prefix + "notes", Column(&ServicesTable::NotesAccessor, objectAccessor));
66         table->AddColumn(prefix + "notes_expanded", Column(&ServicesTable::NotesExpandedAccessor, objectAccessor));
67         table->AddColumn(prefix + "notes_url", Column(&ServicesTable::NotesUrlAccessor, objectAccessor));
68         table->AddColumn(prefix + "notes_url_expanded", Column(&ServicesTable::NotesUrlExpandedAccessor, objectAccessor));
69         table->AddColumn(prefix + "action_url", Column(&ServicesTable::ActionUrlAccessor, objectAccessor));
70         table->AddColumn(prefix + "action_url_expanded", Column(&ServicesTable::ActionUrlExpandedAccessor, objectAccessor));
71         table->AddColumn(prefix + "icon_image", Column(&ServicesTable::IconImageAccessor, objectAccessor));
72         table->AddColumn(prefix + "icon_image_expanded", Column(&ServicesTable::IconImageExpandedAccessor, objectAccessor));
73         table->AddColumn(prefix + "icon_image_alt", Column(&ServicesTable::IconImageAltAccessor, objectAccessor));
74         table->AddColumn(prefix + "initial_state", Column(&Table::EmptyStringAccessor, objectAccessor));
75         table->AddColumn(prefix + "max_check_attempts", Column(&ServicesTable::MaxCheckAttemptsAccessor, objectAccessor));
76         table->AddColumn(prefix + "current_attempt", Column(&ServicesTable::CurrentAttemptAccessor, objectAccessor));
77         table->AddColumn(prefix + "state", Column(&ServicesTable::StateAccessor, objectAccessor));
78         table->AddColumn(prefix + "has_been_checked", Column(&ServicesTable::HasBeenCheckedAccessor, objectAccessor));
79         table->AddColumn(prefix + "last_state", Column(&ServicesTable::LastStateAccessor, objectAccessor));
80         table->AddColumn(prefix + "last_hard_state", Column(&ServicesTable::LastHardStateAccessor, objectAccessor));
81         table->AddColumn(prefix + "state_type", Column(&ServicesTable::StateTypeAccessor, objectAccessor));
82         table->AddColumn(prefix + "check_type", Column(&ServicesTable::CheckTypeAccessor, objectAccessor));
83         table->AddColumn(prefix + "acknowledged", Column(&ServicesTable::AcknowledgedAccessor, objectAccessor));
84         table->AddColumn(prefix + "acknowledgement_type", Column(&ServicesTable::AcknowledgementTypeAccessor, objectAccessor));
85         table->AddColumn(prefix + "no_more_notifications", Column(&ServicesTable::NoMoreNotificationsAccessor, objectAccessor));
86         table->AddColumn(prefix + "last_time_ok", Column(&ServicesTable::LastTimeOkAccessor, objectAccessor));
87         table->AddColumn(prefix + "last_time_warning", Column(&ServicesTable::LastTimeWarningAccessor, objectAccessor));
88         table->AddColumn(prefix + "last_time_critical", Column(&ServicesTable::LastTimeCriticalAccessor, objectAccessor));
89         table->AddColumn(prefix + "last_time_unknown", Column(&ServicesTable::LastTimeUnknownAccessor, objectAccessor));
90         table->AddColumn(prefix + "last_check", Column(&ServicesTable::LastCheckAccessor, objectAccessor));
91         table->AddColumn(prefix + "next_check", Column(&ServicesTable::NextCheckAccessor, objectAccessor));
92         table->AddColumn(prefix + "last_notification", Column(&ServicesTable::LastNotificationAccessor, objectAccessor));
93         table->AddColumn(prefix + "next_notification", Column(&ServicesTable::NextNotificationAccessor, objectAccessor));
94         table->AddColumn(prefix + "current_notification_number", Column(&ServicesTable::CurrentNotificationNumberAccessor, objectAccessor));
95         table->AddColumn(prefix + "last_state_change", Column(&ServicesTable::LastStateChangeAccessor, objectAccessor));
96         table->AddColumn(prefix + "last_hard_state_change", Column(&ServicesTable::LastHardStateChangeAccessor, objectAccessor));
97         table->AddColumn(prefix + "scheduled_downtime_depth", Column(&ServicesTable::ScheduledDowntimeDepthAccessor, objectAccessor));
98         table->AddColumn(prefix + "is_flapping", Column(&ServicesTable::IsFlappingAccessor, objectAccessor));
99         table->AddColumn(prefix + "checks_enabled", Column(&ServicesTable::ChecksEnabledAccessor, objectAccessor));
100         table->AddColumn(prefix + "accept_passive_checks", Column(&ServicesTable::AcceptPassiveChecksAccessor, objectAccessor));
101         table->AddColumn(prefix + "event_handler_enabled", Column(&ServicesTable::EventHandlerEnabledAccessor, objectAccessor));
102         table->AddColumn(prefix + "notifications_enabled", Column(&ServicesTable::NotificationsEnabledAccessor, objectAccessor));
103         table->AddColumn(prefix + "process_performance_data", Column(&ServicesTable::ProcessPerformanceDataAccessor, objectAccessor));
104         table->AddColumn(prefix + "is_executing", Column(&Table::ZeroAccessor, objectAccessor));
105         table->AddColumn(prefix + "active_checks_enabled", Column(&ServicesTable::ActiveChecksEnabledAccessor, objectAccessor));
106         table->AddColumn(prefix + "check_options", Column(&Table::EmptyStringAccessor, objectAccessor));
107         table->AddColumn(prefix + "flap_detection_enabled", Column(&ServicesTable::FlapDetectionEnabledAccessor, objectAccessor));
108         table->AddColumn(prefix + "check_freshness", Column(&ServicesTable::CheckFreshnessAccessor, objectAccessor));
109         table->AddColumn(prefix + "obsess_over_service", Column(&Table::ZeroAccessor, objectAccessor));
110         table->AddColumn(prefix + "modified_attributes", Column(&Table::ZeroAccessor, objectAccessor));
111         table->AddColumn(prefix + "modified_attributes_list", Column(&Table::ZeroAccessor, objectAccessor));
112         table->AddColumn(prefix + "pnpgraph_present", Column(&Table::ZeroAccessor, objectAccessor));
113         table->AddColumn(prefix + "staleness", Column(&ServicesTable::StalenessAccessor, objectAccessor));
114         table->AddColumn(prefix + "check_interval", Column(&ServicesTable::CheckIntervalAccessor, objectAccessor));
115         table->AddColumn(prefix + "retry_interval", Column(&ServicesTable::RetryIntervalAccessor, objectAccessor));
116         table->AddColumn(prefix + "notification_interval", Column(&ServicesTable::NotificationIntervalAccessor, objectAccessor));
117         table->AddColumn(prefix + "first_notification_delay", Column(&Table::EmptyStringAccessor, objectAccessor));
118         table->AddColumn(prefix + "low_flap_threshold", Column(&ServicesTable::LowFlapThresholdAccessor, objectAccessor));
119         table->AddColumn(prefix + "high_flap_threshold", Column(&ServicesTable::HighFlapThresholdAccessor, objectAccessor));
120         table->AddColumn(prefix + "latency", Column(&ServicesTable::LatencyAccessor, objectAccessor));
121         table->AddColumn(prefix + "execution_time", Column(&ServicesTable::ExecutionTimeAccessor, objectAccessor));
122         table->AddColumn(prefix + "percent_state_change", Column(&ServicesTable::PercentStateChangeAccessor, objectAccessor));
123         table->AddColumn(prefix + "in_check_period", Column(&ServicesTable::InCheckPeriodAccessor, objectAccessor));
124         table->AddColumn(prefix + "in_notification_period", Column(&ServicesTable::InNotificationPeriodAccessor, objectAccessor));
125         table->AddColumn(prefix + "contacts", Column(&ServicesTable::ContactsAccessor, objectAccessor));
126         table->AddColumn(prefix + "downtimes", Column(&ServicesTable::DowntimesAccessor, objectAccessor));
127         table->AddColumn(prefix + "downtimes_with_info", Column(&ServicesTable::DowntimesWithInfoAccessor, objectAccessor));
128         table->AddColumn(prefix + "comments", Column(&ServicesTable::CommentsAccessor, objectAccessor));
129         table->AddColumn(prefix + "comments_with_info", Column(&ServicesTable::CommentsWithInfoAccessor, objectAccessor));
130         table->AddColumn(prefix + "comments_with_extra_info", Column(&ServicesTable::CommentsWithExtraInfoAccessor, objectAccessor));
131         table->AddColumn(prefix + "custom_variable_names", Column(&ServicesTable::CustomVariableNamesAccessor, objectAccessor));
132         table->AddColumn(prefix + "custom_variable_values", Column(&ServicesTable::CustomVariableValuesAccessor, objectAccessor));
133         table->AddColumn(prefix + "custom_variables", Column(&ServicesTable::CustomVariablesAccessor, objectAccessor));
134         table->AddColumn(prefix + "groups", Column(&ServicesTable::GroupsAccessor, objectAccessor));
135         table->AddColumn(prefix + "contact_groups", Column(&ServicesTable::ContactGroupsAccessor, objectAccessor));
136         table->AddColumn(prefix + "check_source", Column(&ServicesTable::CheckSourceAccessor, objectAccessor));
137         table->AddColumn(prefix + "is_reachable", Column(&ServicesTable::IsReachableAccessor, objectAccessor));
138         table->AddColumn(prefix + "cv_is_json", Column(&ServicesTable::CVIsJsonAccessor, objectAccessor));
139         table->AddColumn(prefix + "original_attributes", Column(&ServicesTable::OriginalAttributesAccessor, objectAccessor));
140
141         HostsTable::AddColumns(table, "host_", std::bind(&ServicesTable::HostAccessor, _1, objectAccessor));
142
143         /* add additional group by values received through the object accessor */
144         if (table->GetGroupByType() == LivestatusGroupByServiceGroup) {
145                 /* _1 = row, _2 = groupByType, _3 = groupByObject */
146                 Log(LogDebug, "Livestatus")
147                         << "Processing services group by servicegroup table.";
148                 ServiceGroupsTable::AddColumns(table, "servicegroup_", std::bind(&ServicesTable::ServiceGroupAccessor, _1, _2, _3));
149         } else if (table->GetGroupByType() == LivestatusGroupByHostGroup) {
150                 /* _1 = row, _2 = groupByType, _3 = groupByObject */
151                 Log(LogDebug, "Livestatus")
152                         << "Processing services group by hostgroup table.";
153                 HostGroupsTable::AddColumns(table, "hostgroup_", std::bind(&ServicesTable::HostGroupAccessor, _1, _2, _3));
154         }
155 }
156
157 String ServicesTable::GetName() const
158 {
159         return "services";
160 }
161
162 String ServicesTable::GetPrefix() const
163 {
164         return "service";
165 }
166
167 void ServicesTable::FetchRows(const AddRowFunction& addRowFn)
168 {
169         if (GetGroupByType() == LivestatusGroupByServiceGroup) {
170                 for (const ServiceGroup::Ptr& sg : ConfigType::GetObjectsByType<ServiceGroup>()) {
171                         for (const Service::Ptr& service : sg->GetMembers()) {
172                                 /* the caller must know which groupby type and value are set for this row */
173                                 if (!addRowFn(service, LivestatusGroupByServiceGroup, sg))
174                                         return;
175                         }
176                 }
177         } else if (GetGroupByType() == LivestatusGroupByHostGroup) {
178                 for (const HostGroup::Ptr& hg : ConfigType::GetObjectsByType<HostGroup>()) {
179                         ObjectLock ylock(hg);
180                         for (const Host::Ptr& host : hg->GetMembers()) {
181                                 ObjectLock ylock(host);
182                                 for (const Service::Ptr& service : host->GetServices()) {
183                                         /* the caller must know which groupby type and value are set for this row */
184                                         if (!addRowFn(service, LivestatusGroupByHostGroup, hg))
185                                                 return;
186                                 }
187                         }
188                 }
189         } else {
190                 for (const Service::Ptr& service : ConfigType::GetObjectsByType<Service>()) {
191                         if (!addRowFn(service, LivestatusGroupByNone, Empty))
192                                 return;
193                 }
194         }
195 }
196
197 Object::Ptr ServicesTable::HostAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor)
198 {
199         Value service;
200
201         if (parentObjectAccessor)
202                 service = parentObjectAccessor(row, LivestatusGroupByNone, Empty);
203         else
204                 service = row;
205
206         Service::Ptr svc = static_cast<Service::Ptr>(service);
207
208         if (!svc)
209                 return nullptr;
210
211         return svc->GetHost();
212 }
213
214 Object::Ptr ServicesTable::ServiceGroupAccessor(const Value& row, LivestatusGroupByType groupByType, const Object::Ptr& groupByObject)
215 {
216         /* return the current group by value set from within FetchRows()
217          * this is the servicegroup object used for the table join inside
218          * in AddColumns()
219          */
220         if (groupByType == LivestatusGroupByServiceGroup)
221                 return groupByObject;
222
223         return nullptr;
224 }
225
226 Object::Ptr ServicesTable::HostGroupAccessor(const Value& row, LivestatusGroupByType groupByType, const Object::Ptr& groupByObject)
227 {
228         /* return the current group by value set from within FetchRows()
229          * this is the servicegroup object used for the table join inside
230          * in AddColumns()
231          */
232         if (groupByType == LivestatusGroupByHostGroup)
233                 return groupByObject;
234
235         return nullptr;
236 }
237
238 Value ServicesTable::ShortNameAccessor(const Value& row)
239 {
240         Service::Ptr service = static_cast<Service::Ptr>(row);
241
242         if (!service)
243                 return Empty;
244
245         return service->GetShortName();
246 }
247
248 Value ServicesTable::DisplayNameAccessor(const Value& row)
249 {
250         Service::Ptr service = static_cast<Service::Ptr>(row);
251
252         if (!service)
253                 return Empty;
254
255         return service->GetDisplayName();
256 }
257
258 Value ServicesTable::CheckCommandAccessor(const Value& row)
259 {
260         Service::Ptr service = static_cast<Service::Ptr>(row);
261
262         if (!service)
263                 return Empty;
264
265         CheckCommand::Ptr checkcommand = service->GetCheckCommand();
266
267         if (checkcommand)
268                 return CompatUtility::GetCommandName(checkcommand) + "!" + CompatUtility::GetCheckableCommandArgs(service);
269
270         return Empty;
271 }
272
273 Value ServicesTable::CheckCommandExpandedAccessor(const Value& row)
274 {
275         Service::Ptr service = static_cast<Service::Ptr>(row);
276
277         if (!service)
278                 return Empty;
279
280         CheckCommand::Ptr checkcommand = service->GetCheckCommand();
281
282         if (checkcommand)
283                 return CompatUtility::GetCommandName(checkcommand) + "!" + CompatUtility::GetCheckableCommandArgs(service);
284
285         return Empty;
286 }
287
288 Value ServicesTable::EventHandlerAccessor(const Value& row)
289 {
290         Service::Ptr service = static_cast<Service::Ptr>(row);
291
292         if (!service)
293                 return Empty;
294
295         EventCommand::Ptr eventcommand = service->GetEventCommand();
296
297         if (eventcommand)
298                 return CompatUtility::GetCommandName(eventcommand);
299
300         return Empty;
301 }
302
303 Value ServicesTable::PluginOutputAccessor(const Value& row)
304 {
305         Service::Ptr service = static_cast<Service::Ptr>(row);
306
307         if (!service)
308                 return Empty;
309
310         String output;
311         CheckResult::Ptr cr = service->GetLastCheckResult();
312
313         if (cr)
314                 output = CompatUtility::GetCheckResultOutput(cr);
315
316         return output;
317 }
318
319 Value ServicesTable::LongPluginOutputAccessor(const Value& row)
320 {
321         Service::Ptr service = static_cast<Service::Ptr>(row);
322
323         if (!service)
324                 return Empty;
325
326         String long_output;
327         CheckResult::Ptr cr = service->GetLastCheckResult();
328
329         if (cr)
330                 long_output = CompatUtility::GetCheckResultLongOutput(cr);
331
332         return long_output;
333 }
334
335 Value ServicesTable::PerfDataAccessor(const Value& row)
336 {
337         Service::Ptr service = static_cast<Service::Ptr>(row);
338
339         if (!service)
340                 return Empty;
341
342         String perfdata;
343         CheckResult::Ptr cr = service->GetLastCheckResult();
344
345         if (cr)
346                 perfdata = CompatUtility::GetCheckResultPerfdata(cr);
347
348         return perfdata;
349 }
350
351 Value ServicesTable::CheckPeriodAccessor(const Value& row)
352 {
353         Service::Ptr service = static_cast<Service::Ptr>(row);
354
355         if (!service)
356                 return Empty;
357
358         return CompatUtility::GetCheckableCheckPeriod(service);
359 }
360
361 Value ServicesTable::NotesAccessor(const Value& row)
362 {
363         Service::Ptr service = static_cast<Service::Ptr>(row);
364
365         if (!service)
366                 return Empty;
367
368         return service->GetNotes();
369 }
370
371 Value ServicesTable::NotesExpandedAccessor(const Value& row)
372 {
373         Service::Ptr service = static_cast<Service::Ptr>(row);
374
375         if (!service)
376                 return Empty;
377
378         MacroProcessor::ResolverList resolvers {
379                 { "service", service },
380                 { "host", service->GetHost() },
381                 { "icinga", IcingaApplication::GetInstance() }
382         };
383
384         return MacroProcessor::ResolveMacros(service->GetNotes(), resolvers);
385 }
386
387 Value ServicesTable::NotesUrlAccessor(const Value& row)
388 {
389         Service::Ptr service = static_cast<Service::Ptr>(row);
390
391         if (!service)
392                 return Empty;
393
394         return service->GetNotesUrl();
395 }
396
397 Value ServicesTable::NotesUrlExpandedAccessor(const Value& row)
398 {
399         Service::Ptr service = static_cast<Service::Ptr>(row);
400
401         if (!service)
402                 return Empty;
403
404         MacroProcessor::ResolverList resolvers {
405                 { "service", service },
406                 { "host", service->GetHost() },
407                 { "icinga", IcingaApplication::GetInstance() }
408         };
409
410         return MacroProcessor::ResolveMacros(service->GetNotesUrl(), resolvers);
411 }
412
413 Value ServicesTable::ActionUrlAccessor(const Value& row)
414 {
415         Service::Ptr service = static_cast<Service::Ptr>(row);
416
417         if (!service)
418                 return Empty;
419
420         return service->GetActionUrl();
421 }
422
423 Value ServicesTable::ActionUrlExpandedAccessor(const Value& row)
424 {
425         Service::Ptr service = static_cast<Service::Ptr>(row);
426
427         if (!service)
428                 return Empty;
429
430         MacroProcessor::ResolverList resolvers {
431                 { "service", service },
432                 { "host", service->GetHost() },
433                 { "icinga", IcingaApplication::GetInstance() }
434         };
435
436         return MacroProcessor::ResolveMacros(service->GetActionUrl(), resolvers);
437 }
438
439 Value ServicesTable::IconImageAccessor(const Value& row)
440 {
441         Service::Ptr service = static_cast<Service::Ptr>(row);
442
443         if (!service)
444                 return Empty;
445
446         return service->GetIconImage();
447 }
448
449 Value ServicesTable::IconImageExpandedAccessor(const Value& row)
450 {
451         Service::Ptr service = static_cast<Service::Ptr>(row);
452
453         if (!service)
454                 return Empty;
455
456         MacroProcessor::ResolverList resolvers {
457                 { "service", service },
458                 { "host", service->GetHost() },
459                 { "icinga", IcingaApplication::GetInstance() }
460         };
461
462         return MacroProcessor::ResolveMacros(service->GetIconImage(), resolvers);
463 }
464
465 Value ServicesTable::IconImageAltAccessor(const Value& row)
466 {
467         Service::Ptr service = static_cast<Service::Ptr>(row);
468
469         if (!service)
470                 return Empty;
471
472         return service->GetIconImageAlt();
473 }
474
475 Value ServicesTable::MaxCheckAttemptsAccessor(const Value& row)
476 {
477         Service::Ptr service = static_cast<Service::Ptr>(row);
478
479         if (!service)
480                 return Empty;
481
482         return service->GetMaxCheckAttempts();
483 }
484
485 Value ServicesTable::CurrentAttemptAccessor(const Value& row)
486 {
487         Service::Ptr service = static_cast<Service::Ptr>(row);
488
489         if (!service)
490                 return Empty;
491
492         return service->GetCheckAttempt();
493 }
494
495 Value ServicesTable::StateAccessor(const Value& row)
496 {
497         Service::Ptr service = static_cast<Service::Ptr>(row);
498
499         if (!service)
500                 return Empty;
501
502         return service->GetState();
503 }
504
505 Value ServicesTable::HasBeenCheckedAccessor(const Value& row)
506 {
507         Service::Ptr service = static_cast<Service::Ptr>(row);
508
509         if (!service)
510                 return Empty;
511
512         return Convert::ToLong(service->HasBeenChecked());
513 }
514
515 Value ServicesTable::LastStateAccessor(const Value& row)
516 {
517         Service::Ptr service = static_cast<Service::Ptr>(row);
518
519         if (!service)
520                 return Empty;
521
522         return service->GetLastState();
523 }
524
525 Value ServicesTable::LastHardStateAccessor(const Value& row)
526 {
527         Service::Ptr service = static_cast<Service::Ptr>(row);
528
529         if (!service)
530                 return Empty;
531
532         return service->GetLastHardState();
533 }
534
535 Value ServicesTable::StateTypeAccessor(const Value& row)
536 {
537         Service::Ptr service = static_cast<Service::Ptr>(row);
538
539         if (!service)
540                 return Empty;
541
542         return service->GetStateType();
543 }
544
545 Value ServicesTable::CheckTypeAccessor(const Value& row)
546 {
547         Service::Ptr service = static_cast<Service::Ptr>(row);
548
549         if (!service)
550                 return Empty;
551
552         return (service->GetEnableActiveChecks() ? 0 : 1); /* 0 .. active, 1 .. passive */
553 }
554
555 Value ServicesTable::AcknowledgedAccessor(const Value& row)
556 {
557         Service::Ptr service = static_cast<Service::Ptr>(row);
558
559         if (!service)
560                 return Empty;
561
562         ObjectLock olock(service);
563         return CompatUtility::GetCheckableIsAcknowledged(service);
564 }
565
566 Value ServicesTable::AcknowledgementTypeAccessor(const Value& row)
567 {
568         Service::Ptr service = static_cast<Service::Ptr>(row);
569
570         if (!service)
571                 return Empty;
572
573         ObjectLock olock(service);
574         return service->GetAcknowledgement();
575 }
576
577 Value ServicesTable::NoMoreNotificationsAccessor(const Value& row)
578 {
579         Service::Ptr service = static_cast<Service::Ptr>(row);
580
581         if (!service)
582                 return Empty;
583
584         return CompatUtility::GetCheckableNoMoreNotifications(service);
585 }
586
587 Value ServicesTable::LastTimeOkAccessor(const Value& row)
588 {
589         Service::Ptr service = static_cast<Service::Ptr>(row);
590
591         if (!service)
592                 return Empty;
593
594         return static_cast<int>(service->GetLastStateOK());
595 }
596
597 Value ServicesTable::LastTimeWarningAccessor(const Value& row)
598 {
599         Service::Ptr service = static_cast<Service::Ptr>(row);
600
601         if (!service)
602                 return Empty;
603
604         return static_cast<int>(service->GetLastStateWarning());
605 }
606
607 Value ServicesTable::LastTimeCriticalAccessor(const Value& row)
608 {
609         Service::Ptr service = static_cast<Service::Ptr>(row);
610
611         if (!service)
612                 return Empty;
613
614         return static_cast<int>(service->GetLastStateCritical());
615 }
616
617 Value ServicesTable::LastTimeUnknownAccessor(const Value& row)
618 {
619         Service::Ptr service = static_cast<Service::Ptr>(row);
620
621         if (!service)
622                 return Empty;
623
624         return static_cast<int>(service->GetLastStateUnknown());
625 }
626
627 Value ServicesTable::LastCheckAccessor(const Value& row)
628 {
629         Service::Ptr service = static_cast<Service::Ptr>(row);
630
631         if (!service)
632                 return Empty;
633
634         return static_cast<int>(service->GetLastCheck());
635 }
636
637 Value ServicesTable::NextCheckAccessor(const Value& row)
638 {
639         Service::Ptr service = static_cast<Service::Ptr>(row);
640
641         if (!service)
642                 return Empty;
643
644         return static_cast<int>(service->GetNextCheck());
645 }
646
647 Value ServicesTable::LastNotificationAccessor(const Value& row)
648 {
649         Service::Ptr service = static_cast<Service::Ptr>(row);
650
651         if (!service)
652                 return Empty;
653
654         return CompatUtility::GetCheckableNotificationLastNotification(service);
655 }
656
657 Value ServicesTable::NextNotificationAccessor(const Value& row)
658 {
659         Service::Ptr service = static_cast<Service::Ptr>(row);
660
661         if (!service)
662                 return Empty;
663
664         return CompatUtility::GetCheckableNotificationNextNotification(service);
665 }
666
667 Value ServicesTable::CurrentNotificationNumberAccessor(const Value& row)
668 {
669         Service::Ptr service = static_cast<Service::Ptr>(row);
670
671         if (!service)
672                 return Empty;
673
674         return CompatUtility::GetCheckableNotificationNotificationNumber(service);
675 }
676
677 Value ServicesTable::LastStateChangeAccessor(const Value& row)
678 {
679         Service::Ptr service = static_cast<Service::Ptr>(row);
680
681         if (!service)
682                 return Empty;
683
684         return static_cast<int>(service->GetLastStateChange());
685 }
686
687 Value ServicesTable::LastHardStateChangeAccessor(const Value& row)
688 {
689         Service::Ptr service = static_cast<Service::Ptr>(row);
690
691         if (!service)
692                 return Empty;
693
694         return static_cast<int>(service->GetLastHardStateChange());
695 }
696
697 Value ServicesTable::ScheduledDowntimeDepthAccessor(const Value& row)
698 {
699         Service::Ptr service = static_cast<Service::Ptr>(row);
700
701         if (!service)
702                 return Empty;
703
704         return service->GetDowntimeDepth();
705 }
706
707 Value ServicesTable::IsFlappingAccessor(const Value& row)
708 {
709         Service::Ptr service = static_cast<Service::Ptr>(row);
710
711         if (!service)
712                 return Empty;
713
714         return service->IsFlapping();
715 }
716
717 Value ServicesTable::ChecksEnabledAccessor(const Value& row)
718 {
719         Service::Ptr service = static_cast<Service::Ptr>(row);
720
721         if (!service)
722                 return Empty;
723
724         return Convert::ToLong(service->GetEnableActiveChecks());
725 }
726
727 Value ServicesTable::AcceptPassiveChecksAccessor(const Value& row)
728 {
729         Service::Ptr service = static_cast<Service::Ptr>(row);
730
731         if (!service)
732                 return Empty;
733
734         return Convert::ToLong(service->GetEnablePassiveChecks());
735 }
736
737 Value ServicesTable::EventHandlerEnabledAccessor(const Value& row)
738 {
739         Service::Ptr service = static_cast<Service::Ptr>(row);
740
741         if (!service)
742                 return Empty;
743
744         return Convert::ToLong(service->GetEnableEventHandler());
745 }
746
747 Value ServicesTable::NotificationsEnabledAccessor(const Value& row)
748 {
749         Service::Ptr service = static_cast<Service::Ptr>(row);
750
751         if (!service)
752                 return Empty;
753
754         return Convert::ToLong(service->GetEnableNotifications());
755 }
756
757 Value ServicesTable::ProcessPerformanceDataAccessor(const Value& row)
758 {
759         Service::Ptr service = static_cast<Service::Ptr>(row);
760
761         if (!service)
762                 return Empty;
763
764         return Convert::ToLong(service->GetEnablePerfdata());
765 }
766
767 Value ServicesTable::ActiveChecksEnabledAccessor(const Value& row)
768 {
769         Service::Ptr service = static_cast<Service::Ptr>(row);
770
771         if (!service)
772                 return Empty;
773
774         return Convert::ToLong(service->GetEnableActiveChecks());
775 }
776
777 Value ServicesTable::FlapDetectionEnabledAccessor(const Value& row)
778 {
779         Service::Ptr service = static_cast<Service::Ptr>(row);
780
781         if (!service)
782                 return Empty;
783
784         return Convert::ToLong(service->GetEnableFlapping());
785 }
786
787 Value ServicesTable::CheckFreshnessAccessor(const Value& row)
788 {
789         Service::Ptr service = static_cast<Service::Ptr>(row);
790
791         if (!service)
792                 return Empty;
793
794         return CompatUtility::GetCheckableFreshnessChecksEnabled(service);
795 }
796
797 Value ServicesTable::StalenessAccessor(const Value& row)
798 {
799         Service::Ptr service = static_cast<Service::Ptr>(row);
800
801         if (!service)
802                 return Empty;
803
804         return CompatUtility::GetCheckableStaleness(service);
805 }
806
807 Value ServicesTable::CheckIntervalAccessor(const Value& row)
808 {
809         Service::Ptr service = static_cast<Service::Ptr>(row);
810
811         if (!service)
812                 return Empty;
813
814         return CompatUtility::GetCheckableCheckInterval(service);
815 }
816
817 Value ServicesTable::RetryIntervalAccessor(const Value& row)
818 {
819         Service::Ptr service = static_cast<Service::Ptr>(row);
820
821         if (!service)
822                 return Empty;
823
824         return CompatUtility::GetCheckableRetryInterval(service);
825 }
826
827 Value ServicesTable::NotificationIntervalAccessor(const Value& row)
828 {
829         Service::Ptr service = static_cast<Service::Ptr>(row);
830
831         if (!service)
832                 return Empty;
833
834         return CompatUtility::GetCheckableNotificationNotificationInterval(service);
835 }
836
837 Value ServicesTable::LowFlapThresholdAccessor(const Value& row)
838 {
839         Service::Ptr service = static_cast<Service::Ptr>(row);
840
841         if (!service)
842                 return Empty;
843
844         return CompatUtility::GetCheckableLowFlapThreshold(service);
845 }
846
847 Value ServicesTable::HighFlapThresholdAccessor(const Value& row)
848 {
849         Service::Ptr service = static_cast<Service::Ptr>(row);
850
851         if (!service)
852                 return Empty;
853
854         return CompatUtility::GetCheckableHighFlapThreshold(service);
855 }
856
857 Value ServicesTable::LatencyAccessor(const Value& row)
858 {
859         Service::Ptr service = static_cast<Service::Ptr>(row);
860
861         if (!service)
862                 return Empty;
863
864         CheckResult::Ptr cr = service->GetLastCheckResult();
865
866         if (!cr)
867                 return Empty;
868
869         return cr->CalculateLatency();
870 }
871
872 Value ServicesTable::ExecutionTimeAccessor(const Value& row)
873 {
874         Service::Ptr service = static_cast<Service::Ptr>(row);
875
876         if (!service)
877                 return Empty;
878
879         CheckResult::Ptr cr = service->GetLastCheckResult();
880
881         if (!cr)
882                 return Empty;
883
884         return cr->CalculateExecutionTime();
885 }
886
887 Value ServicesTable::PercentStateChangeAccessor(const Value& row)
888 {
889         Service::Ptr service = static_cast<Service::Ptr>(row);
890
891         if (!service)
892                 return Empty;
893
894         return service->GetFlappingCurrent();
895 }
896
897 Value ServicesTable::InCheckPeriodAccessor(const Value& row)
898 {
899         Service::Ptr service = static_cast<Service::Ptr>(row);
900
901         if (!service)
902                 return Empty;
903
904         return CompatUtility::GetCheckableInCheckPeriod(service);
905 }
906
907 Value ServicesTable::InNotificationPeriodAccessor(const Value& row)
908 {
909         Service::Ptr service = static_cast<Service::Ptr>(row);
910
911         if (!service)
912                 return Empty;
913
914         return CompatUtility::GetCheckableInNotificationPeriod(service);
915 }
916
917 Value ServicesTable::ContactsAccessor(const Value& row)
918 {
919         Service::Ptr service = static_cast<Service::Ptr>(row);
920
921         if (!service)
922                 return Empty;
923
924         Array::Ptr contact_names = new Array();
925
926         for (const User::Ptr& user : CompatUtility::GetCheckableNotificationUsers(service)) {
927                 contact_names->Add(user->GetName());
928         }
929
930         return contact_names;
931 }
932
933 Value ServicesTable::DowntimesAccessor(const Value& row)
934 {
935         Service::Ptr service = static_cast<Service::Ptr>(row);
936
937         if (!service)
938                 return Empty;
939
940         Array::Ptr results = new Array();
941
942         for (const Downtime::Ptr& downtime : service->GetDowntimes()) {
943                 if (downtime->IsExpired())
944                         continue;
945
946                 results->Add(downtime->GetLegacyId());
947         }
948
949         return results;
950 }
951
952 Value ServicesTable::DowntimesWithInfoAccessor(const Value& row)
953 {
954         Service::Ptr service = static_cast<Service::Ptr>(row);
955
956         if (!service)
957                 return Empty;
958
959         Array::Ptr results = new Array();
960
961         for (const Downtime::Ptr& downtime : service->GetDowntimes()) {
962                 if (downtime->IsExpired())
963                         continue;
964
965                 Array::Ptr downtime_info = new Array();
966                 downtime_info->Add(downtime->GetLegacyId());
967                 downtime_info->Add(downtime->GetAuthor());
968                 downtime_info->Add(downtime->GetComment());
969                 results->Add(downtime_info);
970         }
971
972         return results;
973 }
974
975 Value ServicesTable::CommentsAccessor(const Value& row)
976 {
977         Service::Ptr service = static_cast<Service::Ptr>(row);
978
979         if (!service)
980                 return Empty;
981
982         Array::Ptr results = new Array();
983
984         for (const Comment::Ptr& comment : service->GetComments()) {
985                 if (comment->IsExpired())
986                         continue;
987
988                 results->Add(comment->GetLegacyId());
989         }
990
991         return results;
992 }
993
994 Value ServicesTable::CommentsWithInfoAccessor(const Value& row)
995 {
996         Service::Ptr service = static_cast<Service::Ptr>(row);
997
998         if (!service)
999                 return Empty;
1000
1001         Array::Ptr results = new Array();
1002
1003         for (const Comment::Ptr& comment : service->GetComments()) {
1004                 if (comment->IsExpired())
1005                         continue;
1006
1007                 Array::Ptr comment_info = new Array();
1008                 comment_info->Add(comment->GetLegacyId());
1009                 comment_info->Add(comment->GetAuthor());
1010                 comment_info->Add(comment->GetText());
1011                 results->Add(comment_info);
1012         }
1013
1014         return results;
1015 }
1016
1017 Value ServicesTable::CommentsWithExtraInfoAccessor(const Value& row)
1018 {
1019         Service::Ptr service = static_cast<Service::Ptr>(row);
1020
1021         if (!service)
1022                 return Empty;
1023
1024         Array::Ptr results = new Array();
1025
1026         for (const Comment::Ptr& comment : service->GetComments()) {
1027                 if (comment->IsExpired())
1028                         continue;
1029
1030                 Array::Ptr comment_info = new Array();
1031                 comment_info->Add(comment->GetLegacyId());
1032                 comment_info->Add(comment->GetAuthor());
1033                 comment_info->Add(comment->GetText());
1034                 comment_info->Add(comment->GetEntryType());
1035                 comment_info->Add(static_cast<int>(comment->GetEntryTime()));
1036                 results->Add(comment_info);
1037         }
1038
1039         return results;
1040 }
1041
1042 Value ServicesTable::CustomVariableNamesAccessor(const Value& row)
1043 {
1044         Service::Ptr service = static_cast<Service::Ptr>(row);
1045
1046         if (!service)
1047                 return Empty;
1048
1049         Dictionary::Ptr vars;
1050
1051         {
1052                 ObjectLock olock(service);
1053                 vars = CompatUtility::GetCustomAttributeConfig(service);
1054         }
1055
1056         Array::Ptr cv = new Array();
1057
1058         if (!vars)
1059                 return cv;
1060
1061         ObjectLock olock(vars);
1062         for (const Dictionary::Pair& kv : vars) {
1063                 cv->Add(kv.first);
1064         }
1065
1066         return cv;
1067 }
1068
1069 Value ServicesTable::CustomVariableValuesAccessor(const Value& row)
1070 {
1071         Service::Ptr service = static_cast<Service::Ptr>(row);
1072
1073         if (!service)
1074                 return Empty;
1075
1076         Dictionary::Ptr vars;
1077
1078         {
1079                 ObjectLock olock(service);
1080                 vars = CompatUtility::GetCustomAttributeConfig(service);
1081         }
1082
1083         Array::Ptr cv = new Array();
1084
1085         if (!vars)
1086                 return cv;
1087
1088         ObjectLock olock(vars);
1089         for (const Dictionary::Pair& kv : vars) {
1090                 if (kv.second.IsObjectType<Array>() || kv.second.IsObjectType<Dictionary>())
1091                         cv->Add(JsonEncode(kv.second));
1092                 else
1093                         cv->Add(kv.second);
1094         }
1095
1096         return cv;
1097 }
1098
1099 Value ServicesTable::CustomVariablesAccessor(const Value& row)
1100 {
1101         Service::Ptr service = static_cast<Service::Ptr>(row);
1102
1103         if (!service)
1104                 return Empty;
1105
1106         Dictionary::Ptr vars;
1107
1108         {
1109                 ObjectLock olock(service);
1110                 vars = CompatUtility::GetCustomAttributeConfig(service);
1111         }
1112
1113         Array::Ptr cv = new Array();
1114
1115         if (!vars)
1116                 return cv;
1117
1118         ObjectLock olock(vars);
1119         for (const Dictionary::Pair& kv : vars) {
1120                 Array::Ptr key_val = new Array();
1121                 key_val->Add(kv.first);
1122
1123                 if (kv.second.IsObjectType<Array>() || kv.second.IsObjectType<Dictionary>())
1124                         key_val->Add(JsonEncode(kv.second));
1125                 else
1126                         key_val->Add(kv.second);
1127
1128                 cv->Add(key_val);
1129         }
1130
1131         return cv;
1132 }
1133
1134 Value ServicesTable::CVIsJsonAccessor(const Value& row)
1135 {
1136         Service::Ptr service = static_cast<Service::Ptr>(row);
1137
1138         if (!service)
1139                 return Empty;
1140
1141         Dictionary::Ptr vars;
1142
1143         {
1144                 ObjectLock olock(service);
1145                 vars = CompatUtility::GetCustomAttributeConfig(service);
1146         }
1147
1148         if (!vars)
1149                 return Empty;
1150
1151         bool cv_is_json = false;
1152
1153         ObjectLock olock(vars);
1154         for (const Dictionary::Pair& kv : vars) {
1155                 if (kv.second.IsObjectType<Array>() || kv.second.IsObjectType<Dictionary>())
1156                         cv_is_json = true;
1157         }
1158
1159         return cv_is_json;
1160 }
1161
1162 Value ServicesTable::GroupsAccessor(const Value& row)
1163 {
1164         Service::Ptr service = static_cast<Service::Ptr>(row);
1165
1166         if (!service)
1167                 return Empty;
1168
1169         Array::Ptr groups = service->GetGroups();
1170
1171         if (!groups)
1172                 return Empty;
1173
1174         return groups;
1175 }
1176
1177 Value ServicesTable::ContactGroupsAccessor(const Value& row)
1178 {
1179         Service::Ptr service = static_cast<Service::Ptr>(row);
1180
1181         if (!service)
1182                 return Empty;
1183
1184         Array::Ptr contactgroup_names = new Array();
1185
1186         for (const UserGroup::Ptr& usergroup : CompatUtility::GetCheckableNotificationUserGroups(service)) {
1187                 contactgroup_names->Add(usergroup->GetName());
1188         }
1189
1190         return contactgroup_names;
1191 }
1192
1193 Value ServicesTable::CheckSourceAccessor(const Value& row)
1194 {
1195         Service::Ptr service = static_cast<Service::Ptr>(row);
1196
1197         if (!service)
1198                 return Empty;
1199
1200         CheckResult::Ptr cr = service->GetLastCheckResult();
1201
1202         if (cr)
1203                 return cr->GetCheckSource();
1204
1205         return Empty;
1206 }
1207
1208 Value ServicesTable::IsReachableAccessor(const Value& row)
1209 {
1210         Service::Ptr service = static_cast<Service::Ptr>(row);
1211
1212         if (!service)
1213                 return Empty;
1214
1215         return service->IsReachable();
1216 }
1217
1218 Value ServicesTable::OriginalAttributesAccessor(const Value& row)
1219 {
1220         Service::Ptr service = static_cast<Service::Ptr>(row);
1221
1222         if (!service)
1223                 return Empty;
1224
1225         return JsonEncode(service->GetOriginalAttributes());
1226 }