]> granicus.if.org Git - icinga2/blob - lib/livestatus/servicestable.cpp
Merge pull request #5811 from Icinga/feature/update-constants-conf-api-setup
[icinga2] / lib / livestatus / servicestable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 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(&ServicesTable::CheckOptionsAccessor, 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(void) const
158 {
159         return "services";
160 }
161
162 String ServicesTable::GetPrefix(void) 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 CompatUtility::GetCheckableHasBeenChecked(service);
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 CompatUtility::GetCheckableCheckType(service);
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 CompatUtility::GetCheckableAcknowledgementType(service);
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 CompatUtility::GetCheckableActiveChecksEnabled(service);
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 CompatUtility::GetCheckablePassiveChecksEnabled(service);
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 CompatUtility::GetCheckableEventHandlerEnabled(service);
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 CompatUtility::GetCheckableNotificationsEnabled(service);
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 CompatUtility::GetCheckableProcessPerformanceData(service);
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 CompatUtility::GetCheckableActiveChecksEnabled(service);
775 }
776
777 Value ServicesTable::CheckOptionsAccessor(const Value& row)
778 {
779         /* TODO - forcexec, freshness, orphan, none */
780         return Empty;
781 }
782
783 Value ServicesTable::FlapDetectionEnabledAccessor(const Value& row)
784 {
785         Service::Ptr service = static_cast<Service::Ptr>(row);
786
787         if (!service)
788                 return Empty;
789
790         return CompatUtility::GetCheckableFlapDetectionEnabled(service);
791 }
792
793 Value ServicesTable::CheckFreshnessAccessor(const Value& row)
794 {
795         Service::Ptr service = static_cast<Service::Ptr>(row);
796
797         if (!service)
798                 return Empty;
799
800         return CompatUtility::GetCheckableFreshnessChecksEnabled(service);
801 }
802
803 Value ServicesTable::StalenessAccessor(const Value& row)
804 {
805         Service::Ptr service = static_cast<Service::Ptr>(row);
806
807         if (!service)
808                 return Empty;
809
810         return CompatUtility::GetCheckableStaleness(service);
811 }
812
813 Value ServicesTable::CheckIntervalAccessor(const Value& row)
814 {
815         Service::Ptr service = static_cast<Service::Ptr>(row);
816
817         if (!service)
818                 return Empty;
819
820         return CompatUtility::GetCheckableCheckInterval(service);
821 }
822
823 Value ServicesTable::RetryIntervalAccessor(const Value& row)
824 {
825         Service::Ptr service = static_cast<Service::Ptr>(row);
826
827         if (!service)
828                 return Empty;
829
830         return CompatUtility::GetCheckableRetryInterval(service);
831 }
832
833 Value ServicesTable::NotificationIntervalAccessor(const Value& row)
834 {
835         Service::Ptr service = static_cast<Service::Ptr>(row);
836
837         if (!service)
838                 return Empty;
839
840         return CompatUtility::GetCheckableNotificationNotificationInterval(service);
841 }
842
843 Value ServicesTable::LowFlapThresholdAccessor(const Value& row)
844 {
845         Service::Ptr service = static_cast<Service::Ptr>(row);
846
847         if (!service)
848                 return Empty;
849
850         return CompatUtility::GetCheckableLowFlapThreshold(service);
851 }
852
853 Value ServicesTable::HighFlapThresholdAccessor(const Value& row)
854 {
855         Service::Ptr service = static_cast<Service::Ptr>(row);
856
857         if (!service)
858                 return Empty;
859
860         return CompatUtility::GetCheckableHighFlapThreshold(service);
861 }
862
863 Value ServicesTable::LatencyAccessor(const Value& row)
864 {
865         Service::Ptr service = static_cast<Service::Ptr>(row);
866
867         if (!service)
868                 return Empty;
869
870         CheckResult::Ptr cr = service->GetLastCheckResult();
871
872         if (!cr)
873                 return Empty;
874
875         return cr->CalculateLatency();
876 }
877
878 Value ServicesTable::ExecutionTimeAccessor(const Value& row)
879 {
880         Service::Ptr service = static_cast<Service::Ptr>(row);
881
882         if (!service)
883                 return Empty;
884
885         CheckResult::Ptr cr = service->GetLastCheckResult();
886
887         if (!cr)
888                 return Empty;
889
890         return cr->CalculateExecutionTime();
891 }
892
893 Value ServicesTable::PercentStateChangeAccessor(const Value& row)
894 {
895         Service::Ptr service = static_cast<Service::Ptr>(row);
896
897         if (!service)
898                 return Empty;
899
900         return CompatUtility::GetCheckablePercentStateChange(service);
901 }
902
903 Value ServicesTable::InCheckPeriodAccessor(const Value& row)
904 {
905         Service::Ptr service = static_cast<Service::Ptr>(row);
906
907         if (!service)
908                 return Empty;
909
910         return CompatUtility::GetCheckableInCheckPeriod(service);
911 }
912
913 Value ServicesTable::InNotificationPeriodAccessor(const Value& row)
914 {
915         Service::Ptr service = static_cast<Service::Ptr>(row);
916
917         if (!service)
918                 return Empty;
919
920         return CompatUtility::GetCheckableInNotificationPeriod(service);
921 }
922
923 Value ServicesTable::ContactsAccessor(const Value& row)
924 {
925         Service::Ptr service = static_cast<Service::Ptr>(row);
926
927         if (!service)
928                 return Empty;
929
930         Array::Ptr contact_names = new Array();
931
932         for (const User::Ptr& user : CompatUtility::GetCheckableNotificationUsers(service)) {
933                 contact_names->Add(user->GetName());
934         }
935
936         return contact_names;
937 }
938
939 Value ServicesTable::DowntimesAccessor(const Value& row)
940 {
941         Service::Ptr service = static_cast<Service::Ptr>(row);
942
943         if (!service)
944                 return Empty;
945
946         Array::Ptr results = new Array();
947
948         for (const Downtime::Ptr& downtime : service->GetDowntimes()) {
949                 if (downtime->IsExpired())
950                         continue;
951
952                 results->Add(downtime->GetLegacyId());
953         }
954
955         return results;
956 }
957
958 Value ServicesTable::DowntimesWithInfoAccessor(const Value& row)
959 {
960         Service::Ptr service = static_cast<Service::Ptr>(row);
961
962         if (!service)
963                 return Empty;
964
965         Array::Ptr results = new Array();
966
967         for (const Downtime::Ptr& downtime : service->GetDowntimes()) {
968                 if (downtime->IsExpired())
969                         continue;
970
971                 Array::Ptr downtime_info = new Array();
972                 downtime_info->Add(downtime->GetLegacyId());
973                 downtime_info->Add(downtime->GetAuthor());
974                 downtime_info->Add(downtime->GetComment());
975                 results->Add(downtime_info);
976         }
977
978         return results;
979 }
980
981 Value ServicesTable::CommentsAccessor(const Value& row)
982 {
983         Service::Ptr service = static_cast<Service::Ptr>(row);
984
985         if (!service)
986                 return Empty;
987
988         Array::Ptr results = new Array();
989
990         for (const Comment::Ptr& comment : service->GetComments()) {
991                 if (comment->IsExpired())
992                         continue;
993
994                 results->Add(comment->GetLegacyId());
995         }
996
997         return results;
998 }
999
1000 Value ServicesTable::CommentsWithInfoAccessor(const Value& row)
1001 {
1002         Service::Ptr service = static_cast<Service::Ptr>(row);
1003
1004         if (!service)
1005                 return Empty;
1006
1007         Array::Ptr results = new Array();
1008
1009         for (const Comment::Ptr& comment : service->GetComments()) {
1010                 if (comment->IsExpired())
1011                         continue;
1012
1013                 Array::Ptr comment_info = new Array();
1014                 comment_info->Add(comment->GetLegacyId());
1015                 comment_info->Add(comment->GetAuthor());
1016                 comment_info->Add(comment->GetText());
1017                 results->Add(comment_info);
1018         }
1019
1020         return results;
1021 }
1022
1023 Value ServicesTable::CommentsWithExtraInfoAccessor(const Value& row)
1024 {
1025         Service::Ptr service = static_cast<Service::Ptr>(row);
1026
1027         if (!service)
1028                 return Empty;
1029
1030         Array::Ptr results = new Array();
1031
1032         for (const Comment::Ptr& comment : service->GetComments()) {
1033                 if (comment->IsExpired())
1034                         continue;
1035
1036                 Array::Ptr comment_info = new Array();
1037                 comment_info->Add(comment->GetLegacyId());
1038                 comment_info->Add(comment->GetAuthor());
1039                 comment_info->Add(comment->GetText());
1040                 comment_info->Add(comment->GetEntryType());
1041                 comment_info->Add(static_cast<int>(comment->GetEntryTime()));
1042                 results->Add(comment_info);
1043         }
1044
1045         return results;
1046 }
1047
1048 Value ServicesTable::CustomVariableNamesAccessor(const Value& row)
1049 {
1050         Service::Ptr service = static_cast<Service::Ptr>(row);
1051
1052         if (!service)
1053                 return Empty;
1054
1055         Dictionary::Ptr vars;
1056
1057         {
1058                 ObjectLock olock(service);
1059                 vars = CompatUtility::GetCustomAttributeConfig(service);
1060         }
1061
1062         Array::Ptr cv = new Array();
1063
1064         if (!vars)
1065                 return cv;
1066
1067         ObjectLock olock(vars);
1068         for (const Dictionary::Pair& kv : vars) {
1069                 cv->Add(kv.first);
1070         }
1071
1072         return cv;
1073 }
1074
1075 Value ServicesTable::CustomVariableValuesAccessor(const Value& row)
1076 {
1077         Service::Ptr service = static_cast<Service::Ptr>(row);
1078
1079         if (!service)
1080                 return Empty;
1081
1082         Dictionary::Ptr vars;
1083
1084         {
1085                 ObjectLock olock(service);
1086                 vars = CompatUtility::GetCustomAttributeConfig(service);
1087         }
1088
1089         Array::Ptr cv = new Array();
1090
1091         if (!vars)
1092                 return cv;
1093
1094         ObjectLock olock(vars);
1095         for (const Dictionary::Pair& kv : vars) {
1096                 if (kv.second.IsObjectType<Array>() || kv.second.IsObjectType<Dictionary>())
1097                         cv->Add(JsonEncode(kv.second));
1098                 else
1099                         cv->Add(kv.second);
1100         }
1101
1102         return cv;
1103 }
1104
1105 Value ServicesTable::CustomVariablesAccessor(const Value& row)
1106 {
1107         Service::Ptr service = static_cast<Service::Ptr>(row);
1108
1109         if (!service)
1110                 return Empty;
1111
1112         Dictionary::Ptr vars;
1113
1114         {
1115                 ObjectLock olock(service);
1116                 vars = CompatUtility::GetCustomAttributeConfig(service);
1117         }
1118
1119         Array::Ptr cv = new Array();
1120
1121         if (!vars)
1122                 return cv;
1123
1124         ObjectLock olock(vars);
1125         for (const Dictionary::Pair& kv : vars) {
1126                 Array::Ptr key_val = new Array();
1127                 key_val->Add(kv.first);
1128
1129                 if (kv.second.IsObjectType<Array>() || kv.second.IsObjectType<Dictionary>())
1130                         key_val->Add(JsonEncode(kv.second));
1131                 else
1132                         key_val->Add(kv.second);
1133
1134                 cv->Add(key_val);
1135         }
1136
1137         return cv;
1138 }
1139
1140 Value ServicesTable::CVIsJsonAccessor(const Value& row)
1141 {
1142         Service::Ptr service = static_cast<Service::Ptr>(row);
1143
1144         if (!service)
1145                 return Empty;
1146
1147         Dictionary::Ptr vars;
1148
1149         {
1150                 ObjectLock olock(service);
1151                 vars = CompatUtility::GetCustomAttributeConfig(service);
1152         }
1153
1154         if (!vars)
1155                 return Empty;
1156
1157         bool cv_is_json = false;
1158
1159         ObjectLock olock(vars);
1160         for (const Dictionary::Pair& kv : vars) {
1161                 if (kv.second.IsObjectType<Array>() || kv.second.IsObjectType<Dictionary>())
1162                         cv_is_json = true;
1163         }
1164
1165         return cv_is_json;
1166 }
1167
1168 Value ServicesTable::GroupsAccessor(const Value& row)
1169 {
1170         Service::Ptr service = static_cast<Service::Ptr>(row);
1171
1172         if (!service)
1173                 return Empty;
1174
1175         Array::Ptr groups = service->GetGroups();
1176
1177         if (!groups)
1178                 return Empty;
1179
1180         return groups;
1181 }
1182
1183 Value ServicesTable::ContactGroupsAccessor(const Value& row)
1184 {
1185         Service::Ptr service = static_cast<Service::Ptr>(row);
1186
1187         if (!service)
1188                 return Empty;
1189
1190         Array::Ptr contactgroup_names = new Array();
1191
1192         for (const UserGroup::Ptr& usergroup : CompatUtility::GetCheckableNotificationUserGroups(service)) {
1193                 contactgroup_names->Add(usergroup->GetName());
1194         }
1195
1196         return contactgroup_names;
1197 }
1198
1199 Value ServicesTable::CheckSourceAccessor(const Value& row)
1200 {
1201         Service::Ptr service = static_cast<Service::Ptr>(row);
1202
1203         if (!service)
1204                 return Empty;
1205
1206         CheckResult::Ptr cr = service->GetLastCheckResult();
1207
1208         if (cr)
1209                 return cr->GetCheckSource();
1210
1211         return Empty;
1212 }
1213
1214 Value ServicesTable::IsReachableAccessor(const Value& row)
1215 {
1216         Service::Ptr service = static_cast<Service::Ptr>(row);
1217
1218         if (!service)
1219                 return Empty;
1220
1221         return service->IsReachable();
1222 }
1223
1224 Value ServicesTable::OriginalAttributesAccessor(const Value& row)
1225 {
1226         Service::Ptr service = static_cast<Service::Ptr>(row);
1227
1228         if (!service)
1229                 return Empty;
1230
1231         return JsonEncode(service->GetOriginalAttributes());
1232 }