]> granicus.if.org Git - icinga2/blob - lib/livestatus/servicestable.cpp
Merge pull request #5555 from Icinga/feature/ecc-certs
[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_", boost::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_", boost::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_", boost::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 Object::Ptr();
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 Object::Ptr();
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 Object::Ptr();
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         resolvers.push_back(std::make_pair("service", service));
380         resolvers.push_back(std::make_pair("host", service->GetHost()));
381         resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance()));
382
383         return MacroProcessor::ResolveMacros(service->GetNotes(), resolvers);
384 }
385
386 Value ServicesTable::NotesUrlAccessor(const Value& row)
387 {
388         Service::Ptr service = static_cast<Service::Ptr>(row);
389
390         if (!service)
391                 return Empty;
392
393         return service->GetNotesUrl();
394 }
395
396 Value ServicesTable::NotesUrlExpandedAccessor(const Value& row)
397 {
398         Service::Ptr service = static_cast<Service::Ptr>(row);
399
400         if (!service)
401                 return Empty;
402
403         MacroProcessor::ResolverList resolvers;
404         resolvers.push_back(std::make_pair("service", service));
405         resolvers.push_back(std::make_pair("host", service->GetHost()));
406         resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance()));
407
408         return MacroProcessor::ResolveMacros(service->GetNotesUrl(), resolvers);
409 }
410
411 Value ServicesTable::ActionUrlAccessor(const Value& row)
412 {
413         Service::Ptr service = static_cast<Service::Ptr>(row);
414
415         if (!service)
416                 return Empty;
417
418         return service->GetActionUrl();
419 }
420
421 Value ServicesTable::ActionUrlExpandedAccessor(const Value& row)
422 {
423         Service::Ptr service = static_cast<Service::Ptr>(row);
424
425         if (!service)
426                 return Empty;
427
428         MacroProcessor::ResolverList resolvers;
429         resolvers.push_back(std::make_pair("service", service));
430         resolvers.push_back(std::make_pair("host", service->GetHost()));
431         resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance()));
432
433         return MacroProcessor::ResolveMacros(service->GetActionUrl(), resolvers);
434 }
435
436 Value ServicesTable::IconImageAccessor(const Value& row)
437 {
438         Service::Ptr service = static_cast<Service::Ptr>(row);
439
440         if (!service)
441                 return Empty;
442
443         return service->GetIconImage();
444 }
445
446 Value ServicesTable::IconImageExpandedAccessor(const Value& row)
447 {
448         Service::Ptr service = static_cast<Service::Ptr>(row);
449
450         if (!service)
451                 return Empty;
452
453         MacroProcessor::ResolverList resolvers;
454         resolvers.push_back(std::make_pair("service", service));
455         resolvers.push_back(std::make_pair("host", service->GetHost()));
456         resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance()));
457
458         return MacroProcessor::ResolveMacros(service->GetIconImage(), resolvers);
459 }
460
461 Value ServicesTable::IconImageAltAccessor(const Value& row)
462 {
463         Service::Ptr service = static_cast<Service::Ptr>(row);
464
465         if (!service)
466                 return Empty;
467
468         return service->GetIconImageAlt();
469 }
470
471 Value ServicesTable::MaxCheckAttemptsAccessor(const Value& row)
472 {
473         Service::Ptr service = static_cast<Service::Ptr>(row);
474
475         if (!service)
476                 return Empty;
477
478         return service->GetMaxCheckAttempts();
479 }
480
481 Value ServicesTable::CurrentAttemptAccessor(const Value& row)
482 {
483         Service::Ptr service = static_cast<Service::Ptr>(row);
484
485         if (!service)
486                 return Empty;
487
488         return service->GetCheckAttempt();
489 }
490
491 Value ServicesTable::StateAccessor(const Value& row)
492 {
493         Service::Ptr service = static_cast<Service::Ptr>(row);
494
495         if (!service)
496                 return Empty;
497
498         return service->GetState();
499 }
500
501 Value ServicesTable::HasBeenCheckedAccessor(const Value& row)
502 {
503         Service::Ptr service = static_cast<Service::Ptr>(row);
504
505         if (!service)
506                 return Empty;
507
508         return CompatUtility::GetCheckableHasBeenChecked(service);
509 }
510
511 Value ServicesTable::LastStateAccessor(const Value& row)
512 {
513         Service::Ptr service = static_cast<Service::Ptr>(row);
514
515         if (!service)
516                 return Empty;
517
518         return service->GetLastState();
519 }
520
521 Value ServicesTable::LastHardStateAccessor(const Value& row)
522 {
523         Service::Ptr service = static_cast<Service::Ptr>(row);
524
525         if (!service)
526                 return Empty;
527
528         return service->GetLastHardState();
529 }
530
531 Value ServicesTable::StateTypeAccessor(const Value& row)
532 {
533         Service::Ptr service = static_cast<Service::Ptr>(row);
534
535         if (!service)
536                 return Empty;
537
538         return service->GetStateType();
539 }
540
541 Value ServicesTable::CheckTypeAccessor(const Value& row)
542 {
543         Service::Ptr service = static_cast<Service::Ptr>(row);
544
545         if (!service)
546                 return Empty;
547
548         return CompatUtility::GetCheckableCheckType(service);
549 }
550
551 Value ServicesTable::AcknowledgedAccessor(const Value& row)
552 {
553         Service::Ptr service = static_cast<Service::Ptr>(row);
554
555         if (!service)
556                 return Empty;
557
558         ObjectLock olock(service);
559         return CompatUtility::GetCheckableIsAcknowledged(service);
560 }
561
562 Value ServicesTable::AcknowledgementTypeAccessor(const Value& row)
563 {
564         Service::Ptr service = static_cast<Service::Ptr>(row);
565
566         if (!service)
567                 return Empty;
568
569         ObjectLock olock(service);
570         return CompatUtility::GetCheckableAcknowledgementType(service);
571 }
572
573 Value ServicesTable::NoMoreNotificationsAccessor(const Value& row)
574 {
575         Service::Ptr service = static_cast<Service::Ptr>(row);
576
577         if (!service)
578                 return Empty;
579
580         return CompatUtility::GetCheckableNoMoreNotifications(service);
581 }
582
583 Value ServicesTable::LastTimeOkAccessor(const Value& row)
584 {
585         Service::Ptr service = static_cast<Service::Ptr>(row);
586
587         if (!service)
588                 return Empty;
589
590         return static_cast<int>(service->GetLastStateOK());
591 }
592
593 Value ServicesTable::LastTimeWarningAccessor(const Value& row)
594 {
595         Service::Ptr service = static_cast<Service::Ptr>(row);
596
597         if (!service)
598                 return Empty;
599
600         return static_cast<int>(service->GetLastStateWarning());
601 }
602
603 Value ServicesTable::LastTimeCriticalAccessor(const Value& row)
604 {
605         Service::Ptr service = static_cast<Service::Ptr>(row);
606
607         if (!service)
608                 return Empty;
609
610         return static_cast<int>(service->GetLastStateCritical());
611 }
612
613 Value ServicesTable::LastTimeUnknownAccessor(const Value& row)
614 {
615         Service::Ptr service = static_cast<Service::Ptr>(row);
616
617         if (!service)
618                 return Empty;
619
620         return static_cast<int>(service->GetLastStateUnknown());
621 }
622
623 Value ServicesTable::LastCheckAccessor(const Value& row)
624 {
625         Service::Ptr service = static_cast<Service::Ptr>(row);
626
627         if (!service)
628                 return Empty;
629
630         return static_cast<int>(service->GetLastCheck());
631 }
632
633 Value ServicesTable::NextCheckAccessor(const Value& row)
634 {
635         Service::Ptr service = static_cast<Service::Ptr>(row);
636
637         if (!service)
638                 return Empty;
639
640         return static_cast<int>(service->GetNextCheck());
641 }
642
643 Value ServicesTable::LastNotificationAccessor(const Value& row)
644 {
645         Service::Ptr service = static_cast<Service::Ptr>(row);
646
647         if (!service)
648                 return Empty;
649
650         return CompatUtility::GetCheckableNotificationLastNotification(service);
651 }
652
653 Value ServicesTable::NextNotificationAccessor(const Value& row)
654 {
655         Service::Ptr service = static_cast<Service::Ptr>(row);
656
657         if (!service)
658                 return Empty;
659
660         return CompatUtility::GetCheckableNotificationNextNotification(service);
661 }
662
663 Value ServicesTable::CurrentNotificationNumberAccessor(const Value& row)
664 {
665         Service::Ptr service = static_cast<Service::Ptr>(row);
666
667         if (!service)
668                 return Empty;
669
670         return CompatUtility::GetCheckableNotificationNotificationNumber(service);
671 }
672
673 Value ServicesTable::LastStateChangeAccessor(const Value& row)
674 {
675         Service::Ptr service = static_cast<Service::Ptr>(row);
676
677         if (!service)
678                 return Empty;
679
680         return static_cast<int>(service->GetLastStateChange());
681 }
682
683 Value ServicesTable::LastHardStateChangeAccessor(const Value& row)
684 {
685         Service::Ptr service = static_cast<Service::Ptr>(row);
686
687         if (!service)
688                 return Empty;
689
690         return static_cast<int>(service->GetLastHardStateChange());
691 }
692
693 Value ServicesTable::ScheduledDowntimeDepthAccessor(const Value& row)
694 {
695         Service::Ptr service = static_cast<Service::Ptr>(row);
696
697         if (!service)
698                 return Empty;
699
700         return service->GetDowntimeDepth();
701 }
702
703 Value ServicesTable::IsFlappingAccessor(const Value& row)
704 {
705         Service::Ptr service = static_cast<Service::Ptr>(row);
706
707         if (!service)
708                 return Empty;
709
710         return service->IsFlapping();
711 }
712
713 Value ServicesTable::ChecksEnabledAccessor(const Value& row)
714 {
715         Service::Ptr service = static_cast<Service::Ptr>(row);
716
717         if (!service)
718                 return Empty;
719
720         return CompatUtility::GetCheckableActiveChecksEnabled(service);
721 }
722
723 Value ServicesTable::AcceptPassiveChecksAccessor(const Value& row)
724 {
725         Service::Ptr service = static_cast<Service::Ptr>(row);
726
727         if (!service)
728                 return Empty;
729
730         return CompatUtility::GetCheckablePassiveChecksEnabled(service);
731 }
732
733 Value ServicesTable::EventHandlerEnabledAccessor(const Value& row)
734 {
735         Service::Ptr service = static_cast<Service::Ptr>(row);
736
737         if (!service)
738                 return Empty;
739
740         return CompatUtility::GetCheckableEventHandlerEnabled(service);
741 }
742
743 Value ServicesTable::NotificationsEnabledAccessor(const Value& row)
744 {
745         Service::Ptr service = static_cast<Service::Ptr>(row);
746
747         if (!service)
748                 return Empty;
749
750         return CompatUtility::GetCheckableNotificationsEnabled(service);
751 }
752
753 Value ServicesTable::ProcessPerformanceDataAccessor(const Value& row)
754 {
755         Service::Ptr service = static_cast<Service::Ptr>(row);
756
757         if (!service)
758                 return Empty;
759
760         return CompatUtility::GetCheckableProcessPerformanceData(service);
761 }
762
763 Value ServicesTable::ActiveChecksEnabledAccessor(const Value& row)
764 {
765         Service::Ptr service = static_cast<Service::Ptr>(row);
766
767         if (!service)
768                 return Empty;
769
770         return CompatUtility::GetCheckableActiveChecksEnabled(service);
771 }
772
773 Value ServicesTable::CheckOptionsAccessor(const Value& row)
774 {
775         /* TODO - forcexec, freshness, orphan, none */
776         return Empty;
777 }
778
779 Value ServicesTable::FlapDetectionEnabledAccessor(const Value& row)
780 {
781         Service::Ptr service = static_cast<Service::Ptr>(row);
782
783         if (!service)
784                 return Empty;
785
786         return CompatUtility::GetCheckableFlapDetectionEnabled(service);
787 }
788
789 Value ServicesTable::CheckFreshnessAccessor(const Value& row)
790 {
791         Service::Ptr service = static_cast<Service::Ptr>(row);
792
793         if (!service)
794                 return Empty;
795
796         return CompatUtility::GetCheckableFreshnessChecksEnabled(service);
797 }
798
799 Value ServicesTable::StalenessAccessor(const Value& row)
800 {
801         Service::Ptr service = static_cast<Service::Ptr>(row);
802
803         if (!service)
804                 return Empty;
805
806         return CompatUtility::GetCheckableStaleness(service);
807 }
808
809 Value ServicesTable::CheckIntervalAccessor(const Value& row)
810 {
811         Service::Ptr service = static_cast<Service::Ptr>(row);
812
813         if (!service)
814                 return Empty;
815
816         return CompatUtility::GetCheckableCheckInterval(service);
817 }
818
819 Value ServicesTable::RetryIntervalAccessor(const Value& row)
820 {
821         Service::Ptr service = static_cast<Service::Ptr>(row);
822
823         if (!service)
824                 return Empty;
825
826         return CompatUtility::GetCheckableRetryInterval(service);
827 }
828
829 Value ServicesTable::NotificationIntervalAccessor(const Value& row)
830 {
831         Service::Ptr service = static_cast<Service::Ptr>(row);
832
833         if (!service)
834                 return Empty;
835
836         return CompatUtility::GetCheckableNotificationNotificationInterval(service);
837 }
838
839 Value ServicesTable::LowFlapThresholdAccessor(const Value& row)
840 {
841         Service::Ptr service = static_cast<Service::Ptr>(row);
842
843         if (!service)
844                 return Empty;
845
846         return CompatUtility::GetCheckableLowFlapThreshold(service);
847 }
848
849 Value ServicesTable::HighFlapThresholdAccessor(const Value& row)
850 {
851         Service::Ptr service = static_cast<Service::Ptr>(row);
852
853         if (!service)
854                 return Empty;
855
856         return CompatUtility::GetCheckableHighFlapThreshold(service);
857 }
858
859 Value ServicesTable::LatencyAccessor(const Value& row)
860 {
861         Service::Ptr service = static_cast<Service::Ptr>(row);
862
863         if (!service)
864                 return Empty;
865
866         CheckResult::Ptr cr = service->GetLastCheckResult();
867
868         if (!cr)
869                 return Empty;
870
871         return cr->CalculateLatency();
872 }
873
874 Value ServicesTable::ExecutionTimeAccessor(const Value& row)
875 {
876         Service::Ptr service = static_cast<Service::Ptr>(row);
877
878         if (!service)
879                 return Empty;
880
881         CheckResult::Ptr cr = service->GetLastCheckResult();
882
883         if (!cr)
884                 return Empty;
885
886         return cr->CalculateExecutionTime();
887 }
888
889 Value ServicesTable::PercentStateChangeAccessor(const Value& row)
890 {
891         Service::Ptr service = static_cast<Service::Ptr>(row);
892
893         if (!service)
894                 return Empty;
895
896         return CompatUtility::GetCheckablePercentStateChange(service);
897 }
898
899 Value ServicesTable::InCheckPeriodAccessor(const Value& row)
900 {
901         Service::Ptr service = static_cast<Service::Ptr>(row);
902
903         if (!service)
904                 return Empty;
905
906         return CompatUtility::GetCheckableInCheckPeriod(service);
907 }
908
909 Value ServicesTable::InNotificationPeriodAccessor(const Value& row)
910 {
911         Service::Ptr service = static_cast<Service::Ptr>(row);
912
913         if (!service)
914                 return Empty;
915
916         return CompatUtility::GetCheckableInNotificationPeriod(service);
917 }
918
919 Value ServicesTable::ContactsAccessor(const Value& row)
920 {
921         Service::Ptr service = static_cast<Service::Ptr>(row);
922
923         if (!service)
924                 return Empty;
925
926         Array::Ptr contact_names = new Array();
927
928         for (const User::Ptr& user : CompatUtility::GetCheckableNotificationUsers(service)) {
929                 contact_names->Add(user->GetName());
930         }
931
932         return contact_names;
933 }
934
935 Value ServicesTable::DowntimesAccessor(const Value& row)
936 {
937         Service::Ptr service = static_cast<Service::Ptr>(row);
938
939         if (!service)
940                 return Empty;
941
942         Array::Ptr results = new Array();
943
944         for (const Downtime::Ptr& downtime : service->GetDowntimes()) {
945                 if (downtime->IsExpired())
946                         continue;
947
948                 results->Add(downtime->GetLegacyId());
949         }
950
951         return results;
952 }
953
954 Value ServicesTable::DowntimesWithInfoAccessor(const Value& row)
955 {
956         Service::Ptr service = static_cast<Service::Ptr>(row);
957
958         if (!service)
959                 return Empty;
960
961         Array::Ptr results = new Array();
962
963         for (const Downtime::Ptr& downtime : service->GetDowntimes()) {
964                 if (downtime->IsExpired())
965                         continue;
966
967                 Array::Ptr downtime_info = new Array();
968                 downtime_info->Add(downtime->GetLegacyId());
969                 downtime_info->Add(downtime->GetAuthor());
970                 downtime_info->Add(downtime->GetComment());
971                 results->Add(downtime_info);
972         }
973
974         return results;
975 }
976
977 Value ServicesTable::CommentsAccessor(const Value& row)
978 {
979         Service::Ptr service = static_cast<Service::Ptr>(row);
980
981         if (!service)
982                 return Empty;
983
984         Array::Ptr results = new Array();
985
986         for (const Comment::Ptr& comment : service->GetComments()) {
987                 if (comment->IsExpired())
988                         continue;
989
990                 results->Add(comment->GetLegacyId());
991         }
992
993         return results;
994 }
995
996 Value ServicesTable::CommentsWithInfoAccessor(const Value& row)
997 {
998         Service::Ptr service = static_cast<Service::Ptr>(row);
999
1000         if (!service)
1001                 return Empty;
1002
1003         Array::Ptr results = new Array();
1004
1005         for (const Comment::Ptr& comment : service->GetComments()) {
1006                 if (comment->IsExpired())
1007                         continue;
1008
1009                 Array::Ptr comment_info = new Array();
1010                 comment_info->Add(comment->GetLegacyId());
1011                 comment_info->Add(comment->GetAuthor());
1012                 comment_info->Add(comment->GetText());
1013                 results->Add(comment_info);
1014         }
1015
1016         return results;
1017 }
1018
1019 Value ServicesTable::CommentsWithExtraInfoAccessor(const Value& row)
1020 {
1021         Service::Ptr service = static_cast<Service::Ptr>(row);
1022
1023         if (!service)
1024                 return Empty;
1025
1026         Array::Ptr results = new Array();
1027
1028         for (const Comment::Ptr& comment : service->GetComments()) {
1029                 if (comment->IsExpired())
1030                         continue;
1031
1032                 Array::Ptr comment_info = new Array();
1033                 comment_info->Add(comment->GetLegacyId());
1034                 comment_info->Add(comment->GetAuthor());
1035                 comment_info->Add(comment->GetText());
1036                 comment_info->Add(comment->GetEntryType());
1037                 comment_info->Add(static_cast<int>(comment->GetEntryTime()));
1038                 results->Add(comment_info);
1039         }
1040
1041         return results;
1042 }
1043
1044 Value ServicesTable::CustomVariableNamesAccessor(const Value& row)
1045 {
1046         Service::Ptr service = static_cast<Service::Ptr>(row);
1047
1048         if (!service)
1049                 return Empty;
1050
1051         Dictionary::Ptr vars;
1052
1053         {
1054                 ObjectLock olock(service);
1055                 vars = CompatUtility::GetCustomAttributeConfig(service);
1056         }
1057
1058         Array::Ptr cv = new Array();
1059
1060         if (!vars)
1061                 return cv;
1062
1063         ObjectLock olock(vars);
1064         for (const Dictionary::Pair& kv : vars) {
1065                 cv->Add(kv.first);
1066         }
1067
1068         return cv;
1069 }
1070
1071 Value ServicesTable::CustomVariableValuesAccessor(const Value& row)
1072 {
1073         Service::Ptr service = static_cast<Service::Ptr>(row);
1074
1075         if (!service)
1076                 return Empty;
1077
1078         Dictionary::Ptr vars;
1079
1080         {
1081                 ObjectLock olock(service);
1082                 vars = CompatUtility::GetCustomAttributeConfig(service);
1083         }
1084
1085         Array::Ptr cv = new Array();
1086
1087         if (!vars)
1088                 return cv;
1089
1090         ObjectLock olock(vars);
1091         for (const Dictionary::Pair& kv : vars) {
1092                 if (kv.second.IsObjectType<Array>() || kv.second.IsObjectType<Dictionary>())
1093                         cv->Add(JsonEncode(kv.second));
1094                 else
1095                         cv->Add(kv.second);
1096         }
1097
1098         return cv;
1099 }
1100
1101 Value ServicesTable::CustomVariablesAccessor(const Value& row)
1102 {
1103         Service::Ptr service = static_cast<Service::Ptr>(row);
1104
1105         if (!service)
1106                 return Empty;
1107
1108         Dictionary::Ptr vars;
1109
1110         {
1111                 ObjectLock olock(service);
1112                 vars = CompatUtility::GetCustomAttributeConfig(service);
1113         }
1114
1115         Array::Ptr cv = new Array();
1116
1117         if (!vars)
1118                 return cv;
1119
1120         ObjectLock olock(vars);
1121         for (const Dictionary::Pair& kv : vars) {
1122                 Array::Ptr key_val = new Array();
1123                 key_val->Add(kv.first);
1124
1125                 if (kv.second.IsObjectType<Array>() || kv.second.IsObjectType<Dictionary>())
1126                         key_val->Add(JsonEncode(kv.second));
1127                 else
1128                         key_val->Add(kv.second);
1129
1130                 cv->Add(key_val);
1131         }
1132
1133         return cv;
1134 }
1135
1136 Value ServicesTable::CVIsJsonAccessor(const Value& row)
1137 {
1138         Service::Ptr service = static_cast<Service::Ptr>(row);
1139
1140         if (!service)
1141                 return Empty;
1142
1143         Dictionary::Ptr vars;
1144
1145         {
1146                 ObjectLock olock(service);
1147                 vars = CompatUtility::GetCustomAttributeConfig(service);
1148         }
1149
1150         if (!vars)
1151                 return Empty;
1152
1153         bool cv_is_json = false;
1154
1155         ObjectLock olock(vars);
1156         for (const Dictionary::Pair& kv : vars) {
1157                 if (kv.second.IsObjectType<Array>() || kv.second.IsObjectType<Dictionary>())
1158                         cv_is_json = true;
1159         }
1160
1161         return cv_is_json;
1162 }
1163
1164 Value ServicesTable::GroupsAccessor(const Value& row)
1165 {
1166         Service::Ptr service = static_cast<Service::Ptr>(row);
1167
1168         if (!service)
1169                 return Empty;
1170
1171         Array::Ptr groups = service->GetGroups();
1172
1173         if (!groups)
1174                 return Empty;
1175
1176         return groups;
1177 }
1178
1179 Value ServicesTable::ContactGroupsAccessor(const Value& row)
1180 {
1181         Service::Ptr service = static_cast<Service::Ptr>(row);
1182
1183         if (!service)
1184                 return Empty;
1185
1186         Array::Ptr contactgroup_names = new Array();
1187
1188         for (const UserGroup::Ptr& usergroup : CompatUtility::GetCheckableNotificationUserGroups(service)) {
1189                 contactgroup_names->Add(usergroup->GetName());
1190         }
1191
1192         return contactgroup_names;
1193 }
1194
1195 Value ServicesTable::CheckSourceAccessor(const Value& row)
1196 {
1197         Service::Ptr service = static_cast<Service::Ptr>(row);
1198
1199         if (!service)
1200                 return Empty;
1201
1202         CheckResult::Ptr cr = service->GetLastCheckResult();
1203
1204         if (cr)
1205                 return cr->GetCheckSource();
1206
1207         return Empty;
1208 }
1209
1210 Value ServicesTable::IsReachableAccessor(const Value& row)
1211 {
1212         Service::Ptr service = static_cast<Service::Ptr>(row);
1213
1214         if (!service)
1215                 return Empty;
1216
1217         return service->IsReachable();
1218 }
1219
1220 Value ServicesTable::OriginalAttributesAccessor(const Value& row)
1221 {
1222         Service::Ptr service = static_cast<Service::Ptr>(row);
1223
1224         if (!service)
1225                 return Empty;
1226
1227         return JsonEncode(service->GetOriginalAttributes());
1228 }