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