1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "livestatus/servicestable.hpp"
4 #include "livestatus/hoststable.hpp"
5 #include "livestatus/servicegroupstable.hpp"
6 #include "livestatus/hostgroupstable.hpp"
7 #include "livestatus/endpointstable.hpp"
8 #include "icinga/service.hpp"
9 #include "icinga/servicegroup.hpp"
10 #include "icinga/hostgroup.hpp"
11 #include "icinga/checkcommand.hpp"
12 #include "icinga/eventcommand.hpp"
13 #include "icinga/timeperiod.hpp"
14 #include "icinga/macroprocessor.hpp"
15 #include "icinga/icingaapplication.hpp"
16 #include "icinga/compatutility.hpp"
17 #include "icinga/pluginutility.hpp"
18 #include "base/configtype.hpp"
19 #include "base/objectlock.hpp"
20 #include "base/json.hpp"
21 #include "base/convert.hpp"
22 #include "base/utility.hpp"
23 #include <boost/tuple/tuple.hpp>
24 #include <boost/algorithm/string/replace.hpp>
26 using namespace icinga;
28 ServicesTable::ServicesTable(LivestatusGroupByType type)
35 void ServicesTable::AddColumns(Table *table, const String& prefix,
36 const Column::ObjectAccessor& objectAccessor)
38 table->AddColumn(prefix + "description", Column(&ServicesTable::ShortNameAccessor, objectAccessor));
39 table->AddColumn(prefix + "service_description", Column(&ServicesTable::ShortNameAccessor, objectAccessor)); //ugly compatibility hack
40 table->AddColumn(prefix + "display_name", Column(&ServicesTable::DisplayNameAccessor, objectAccessor));
41 table->AddColumn(prefix + "check_command", Column(&ServicesTable::CheckCommandAccessor, objectAccessor));
42 table->AddColumn(prefix + "check_command_expanded", Column(&ServicesTable::CheckCommandExpandedAccessor, objectAccessor));
43 table->AddColumn(prefix + "event_handler", Column(&ServicesTable::EventHandlerAccessor, objectAccessor));
44 table->AddColumn(prefix + "plugin_output", Column(&ServicesTable::PluginOutputAccessor, objectAccessor));
45 table->AddColumn(prefix + "long_plugin_output", Column(&ServicesTable::LongPluginOutputAccessor, objectAccessor));
46 table->AddColumn(prefix + "perf_data", Column(&ServicesTable::PerfDataAccessor, objectAccessor));
47 table->AddColumn(prefix + "notification_period", Column(&Table::EmptyStringAccessor, objectAccessor));
48 table->AddColumn(prefix + "check_period", Column(&ServicesTable::CheckPeriodAccessor, objectAccessor));
49 table->AddColumn(prefix + "notes", Column(&ServicesTable::NotesAccessor, objectAccessor));
50 table->AddColumn(prefix + "notes_expanded", Column(&ServicesTable::NotesExpandedAccessor, objectAccessor));
51 table->AddColumn(prefix + "notes_url", Column(&ServicesTable::NotesUrlAccessor, objectAccessor));
52 table->AddColumn(prefix + "notes_url_expanded", Column(&ServicesTable::NotesUrlExpandedAccessor, objectAccessor));
53 table->AddColumn(prefix + "action_url", Column(&ServicesTable::ActionUrlAccessor, objectAccessor));
54 table->AddColumn(prefix + "action_url_expanded", Column(&ServicesTable::ActionUrlExpandedAccessor, objectAccessor));
55 table->AddColumn(prefix + "icon_image", Column(&ServicesTable::IconImageAccessor, objectAccessor));
56 table->AddColumn(prefix + "icon_image_expanded", Column(&ServicesTable::IconImageExpandedAccessor, objectAccessor));
57 table->AddColumn(prefix + "icon_image_alt", Column(&ServicesTable::IconImageAltAccessor, objectAccessor));
58 table->AddColumn(prefix + "initial_state", Column(&Table::EmptyStringAccessor, objectAccessor));
59 table->AddColumn(prefix + "max_check_attempts", Column(&ServicesTable::MaxCheckAttemptsAccessor, objectAccessor));
60 table->AddColumn(prefix + "current_attempt", Column(&ServicesTable::CurrentAttemptAccessor, objectAccessor));
61 table->AddColumn(prefix + "state", Column(&ServicesTable::StateAccessor, objectAccessor));
62 table->AddColumn(prefix + "has_been_checked", Column(&ServicesTable::HasBeenCheckedAccessor, objectAccessor));
63 table->AddColumn(prefix + "last_state", Column(&ServicesTable::LastStateAccessor, objectAccessor));
64 table->AddColumn(prefix + "last_hard_state", Column(&ServicesTable::LastHardStateAccessor, objectAccessor));
65 table->AddColumn(prefix + "state_type", Column(&ServicesTable::StateTypeAccessor, objectAccessor));
66 table->AddColumn(prefix + "check_type", Column(&ServicesTable::CheckTypeAccessor, objectAccessor));
67 table->AddColumn(prefix + "acknowledged", Column(&ServicesTable::AcknowledgedAccessor, objectAccessor));
68 table->AddColumn(prefix + "acknowledgement_type", Column(&ServicesTable::AcknowledgementTypeAccessor, objectAccessor));
69 table->AddColumn(prefix + "no_more_notifications", Column(&ServicesTable::NoMoreNotificationsAccessor, objectAccessor));
70 table->AddColumn(prefix + "last_time_ok", Column(&ServicesTable::LastTimeOkAccessor, objectAccessor));
71 table->AddColumn(prefix + "last_time_warning", Column(&ServicesTable::LastTimeWarningAccessor, objectAccessor));
72 table->AddColumn(prefix + "last_time_critical", Column(&ServicesTable::LastTimeCriticalAccessor, objectAccessor));
73 table->AddColumn(prefix + "last_time_unknown", Column(&ServicesTable::LastTimeUnknownAccessor, objectAccessor));
74 table->AddColumn(prefix + "last_check", Column(&ServicesTable::LastCheckAccessor, objectAccessor));
75 table->AddColumn(prefix + "next_check", Column(&ServicesTable::NextCheckAccessor, objectAccessor));
76 table->AddColumn(prefix + "last_notification", Column(&ServicesTable::LastNotificationAccessor, objectAccessor));
77 table->AddColumn(prefix + "next_notification", Column(&ServicesTable::NextNotificationAccessor, objectAccessor));
78 table->AddColumn(prefix + "current_notification_number", Column(&ServicesTable::CurrentNotificationNumberAccessor, objectAccessor));
79 table->AddColumn(prefix + "last_state_change", Column(&ServicesTable::LastStateChangeAccessor, objectAccessor));
80 table->AddColumn(prefix + "last_hard_state_change", Column(&ServicesTable::LastHardStateChangeAccessor, objectAccessor));
81 table->AddColumn(prefix + "scheduled_downtime_depth", Column(&ServicesTable::ScheduledDowntimeDepthAccessor, objectAccessor));
82 table->AddColumn(prefix + "is_flapping", Column(&ServicesTable::IsFlappingAccessor, objectAccessor));
83 table->AddColumn(prefix + "checks_enabled", Column(&ServicesTable::ChecksEnabledAccessor, objectAccessor));
84 table->AddColumn(prefix + "accept_passive_checks", Column(&ServicesTable::AcceptPassiveChecksAccessor, objectAccessor));
85 table->AddColumn(prefix + "event_handler_enabled", Column(&ServicesTable::EventHandlerEnabledAccessor, objectAccessor));
86 table->AddColumn(prefix + "notifications_enabled", Column(&ServicesTable::NotificationsEnabledAccessor, objectAccessor));
87 table->AddColumn(prefix + "process_performance_data", Column(&ServicesTable::ProcessPerformanceDataAccessor, objectAccessor));
88 table->AddColumn(prefix + "is_executing", Column(&Table::ZeroAccessor, objectAccessor));
89 table->AddColumn(prefix + "active_checks_enabled", Column(&ServicesTable::ActiveChecksEnabledAccessor, objectAccessor));
90 table->AddColumn(prefix + "check_options", Column(&Table::EmptyStringAccessor, objectAccessor));
91 table->AddColumn(prefix + "flap_detection_enabled", Column(&ServicesTable::FlapDetectionEnabledAccessor, objectAccessor));
92 table->AddColumn(prefix + "check_freshness", Column(&Table::OneAccessor, objectAccessor));
93 table->AddColumn(prefix + "obsess_over_service", Column(&Table::ZeroAccessor, objectAccessor));
94 table->AddColumn(prefix + "modified_attributes", Column(&Table::ZeroAccessor, objectAccessor));
95 table->AddColumn(prefix + "modified_attributes_list", Column(&Table::ZeroAccessor, objectAccessor));
96 table->AddColumn(prefix + "pnpgraph_present", Column(&Table::ZeroAccessor, objectAccessor));
97 table->AddColumn(prefix + "staleness", Column(&ServicesTable::StalenessAccessor, objectAccessor));
98 table->AddColumn(prefix + "check_interval", Column(&ServicesTable::CheckIntervalAccessor, objectAccessor));
99 table->AddColumn(prefix + "retry_interval", Column(&ServicesTable::RetryIntervalAccessor, objectAccessor));
100 table->AddColumn(prefix + "notification_interval", Column(&ServicesTable::NotificationIntervalAccessor, objectAccessor));
101 table->AddColumn(prefix + "first_notification_delay", Column(&Table::EmptyStringAccessor, objectAccessor));
102 table->AddColumn(prefix + "low_flap_threshold", Column(&ServicesTable::LowFlapThresholdAccessor, objectAccessor));
103 table->AddColumn(prefix + "high_flap_threshold", Column(&ServicesTable::HighFlapThresholdAccessor, objectAccessor));
104 table->AddColumn(prefix + "latency", Column(&ServicesTable::LatencyAccessor, objectAccessor));
105 table->AddColumn(prefix + "execution_time", Column(&ServicesTable::ExecutionTimeAccessor, objectAccessor));
106 table->AddColumn(prefix + "percent_state_change", Column(&ServicesTable::PercentStateChangeAccessor, objectAccessor));
107 table->AddColumn(prefix + "in_check_period", Column(&ServicesTable::InCheckPeriodAccessor, objectAccessor));
108 table->AddColumn(prefix + "in_notification_period", Column(&ServicesTable::InNotificationPeriodAccessor, objectAccessor));
109 table->AddColumn(prefix + "contacts", Column(&ServicesTable::ContactsAccessor, objectAccessor));
110 table->AddColumn(prefix + "downtimes", Column(&ServicesTable::DowntimesAccessor, objectAccessor));
111 table->AddColumn(prefix + "downtimes_with_info", Column(&ServicesTable::DowntimesWithInfoAccessor, objectAccessor));
112 table->AddColumn(prefix + "comments", Column(&ServicesTable::CommentsAccessor, objectAccessor));
113 table->AddColumn(prefix + "comments_with_info", Column(&ServicesTable::CommentsWithInfoAccessor, objectAccessor));
114 table->AddColumn(prefix + "comments_with_extra_info", Column(&ServicesTable::CommentsWithExtraInfoAccessor, objectAccessor));
115 table->AddColumn(prefix + "custom_variable_names", Column(&ServicesTable::CustomVariableNamesAccessor, objectAccessor));
116 table->AddColumn(prefix + "custom_variable_values", Column(&ServicesTable::CustomVariableValuesAccessor, objectAccessor));
117 table->AddColumn(prefix + "custom_variables", Column(&ServicesTable::CustomVariablesAccessor, objectAccessor));
118 table->AddColumn(prefix + "groups", Column(&ServicesTable::GroupsAccessor, objectAccessor));
119 table->AddColumn(prefix + "contact_groups", Column(&ServicesTable::ContactGroupsAccessor, objectAccessor));
120 table->AddColumn(prefix + "check_source", Column(&ServicesTable::CheckSourceAccessor, objectAccessor));
121 table->AddColumn(prefix + "is_reachable", Column(&ServicesTable::IsReachableAccessor, objectAccessor));
122 table->AddColumn(prefix + "cv_is_json", Column(&ServicesTable::CVIsJsonAccessor, objectAccessor));
123 table->AddColumn(prefix + "original_attributes", Column(&ServicesTable::OriginalAttributesAccessor, objectAccessor));
125 HostsTable::AddColumns(table, "host_", std::bind(&ServicesTable::HostAccessor, _1, objectAccessor));
127 /* add additional group by values received through the object accessor */
128 if (table->GetGroupByType() == LivestatusGroupByServiceGroup) {
129 /* _1 = row, _2 = groupByType, _3 = groupByObject */
130 Log(LogDebug, "Livestatus")
131 << "Processing services group by servicegroup table.";
132 ServiceGroupsTable::AddColumns(table, "servicegroup_", std::bind(&ServicesTable::ServiceGroupAccessor, _1, _2, _3));
133 } else if (table->GetGroupByType() == LivestatusGroupByHostGroup) {
134 /* _1 = row, _2 = groupByType, _3 = groupByObject */
135 Log(LogDebug, "Livestatus")
136 << "Processing services group by hostgroup table.";
137 HostGroupsTable::AddColumns(table, "hostgroup_", std::bind(&ServicesTable::HostGroupAccessor, _1, _2, _3));
141 String ServicesTable::GetName() const
146 String ServicesTable::GetPrefix() const
151 void ServicesTable::FetchRows(const AddRowFunction& addRowFn)
153 if (GetGroupByType() == LivestatusGroupByServiceGroup) {
154 for (const ServiceGroup::Ptr& sg : ConfigType::GetObjectsByType<ServiceGroup>()) {
155 for (const Service::Ptr& service : sg->GetMembers()) {
156 /* the caller must know which groupby type and value are set for this row */
157 if (!addRowFn(service, LivestatusGroupByServiceGroup, sg))
161 } else if (GetGroupByType() == LivestatusGroupByHostGroup) {
162 for (const HostGroup::Ptr& hg : ConfigType::GetObjectsByType<HostGroup>()) {
163 ObjectLock ylock(hg);
164 for (const Host::Ptr& host : hg->GetMembers()) {
165 ObjectLock ylock(host);
166 for (const Service::Ptr& service : host->GetServices()) {
167 /* the caller must know which groupby type and value are set for this row */
168 if (!addRowFn(service, LivestatusGroupByHostGroup, hg))
174 for (const Service::Ptr& service : ConfigType::GetObjectsByType<Service>()) {
175 if (!addRowFn(service, LivestatusGroupByNone, Empty))
181 Object::Ptr ServicesTable::HostAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor)
185 if (parentObjectAccessor)
186 service = parentObjectAccessor(row, LivestatusGroupByNone, Empty);
190 Service::Ptr svc = static_cast<Service::Ptr>(service);
195 return svc->GetHost();
198 Object::Ptr ServicesTable::ServiceGroupAccessor(const Value& row, LivestatusGroupByType groupByType, const Object::Ptr& groupByObject)
200 /* return the current group by value set from within FetchRows()
201 * this is the servicegroup object used for the table join inside
204 if (groupByType == LivestatusGroupByServiceGroup)
205 return groupByObject;
210 Object::Ptr ServicesTable::HostGroupAccessor(const Value& row, LivestatusGroupByType groupByType, const Object::Ptr& groupByObject)
212 /* return the current group by value set from within FetchRows()
213 * this is the servicegroup object used for the table join inside
216 if (groupByType == LivestatusGroupByHostGroup)
217 return groupByObject;
222 Value ServicesTable::ShortNameAccessor(const Value& row)
224 Service::Ptr service = static_cast<Service::Ptr>(row);
229 return service->GetShortName();
232 Value ServicesTable::DisplayNameAccessor(const Value& row)
234 Service::Ptr service = static_cast<Service::Ptr>(row);
239 return service->GetDisplayName();
242 Value ServicesTable::CheckCommandAccessor(const Value& row)
244 Service::Ptr service = static_cast<Service::Ptr>(row);
249 CheckCommand::Ptr checkcommand = service->GetCheckCommand();
252 return CompatUtility::GetCommandName(checkcommand) + "!" + CompatUtility::GetCheckableCommandArgs(service);
257 Value ServicesTable::CheckCommandExpandedAccessor(const Value& row)
259 Service::Ptr service = static_cast<Service::Ptr>(row);
264 CheckCommand::Ptr checkcommand = service->GetCheckCommand();
267 return CompatUtility::GetCommandName(checkcommand) + "!" + CompatUtility::GetCheckableCommandArgs(service);
272 Value ServicesTable::EventHandlerAccessor(const Value& row)
274 Service::Ptr service = static_cast<Service::Ptr>(row);
279 EventCommand::Ptr eventcommand = service->GetEventCommand();
282 return CompatUtility::GetCommandName(eventcommand);
287 Value ServicesTable::PluginOutputAccessor(const Value& row)
289 Service::Ptr service = static_cast<Service::Ptr>(row);
295 CheckResult::Ptr cr = service->GetLastCheckResult();
298 output = CompatUtility::GetCheckResultOutput(cr);
303 Value ServicesTable::LongPluginOutputAccessor(const Value& row)
305 Service::Ptr service = static_cast<Service::Ptr>(row);
311 CheckResult::Ptr cr = service->GetLastCheckResult();
314 long_output = CompatUtility::GetCheckResultLongOutput(cr);
319 Value ServicesTable::PerfDataAccessor(const Value& row)
321 Service::Ptr service = static_cast<Service::Ptr>(row);
327 CheckResult::Ptr cr = service->GetLastCheckResult();
332 return PluginUtility::FormatPerfdata(cr->GetPerformanceData());
335 Value ServicesTable::CheckPeriodAccessor(const Value& row)
337 Service::Ptr service = static_cast<Service::Ptr>(row);
342 TimePeriod::Ptr checkPeriod = service->GetCheckPeriod();
347 return checkPeriod->GetName();
350 Value ServicesTable::NotesAccessor(const Value& row)
352 Service::Ptr service = static_cast<Service::Ptr>(row);
357 return service->GetNotes();
360 Value ServicesTable::NotesExpandedAccessor(const Value& row)
362 Service::Ptr service = static_cast<Service::Ptr>(row);
367 MacroProcessor::ResolverList resolvers {
368 { "service", service },
369 { "host", service->GetHost() },
370 { "icinga", IcingaApplication::GetInstance() }
373 return MacroProcessor::ResolveMacros(service->GetNotes(), resolvers);
376 Value ServicesTable::NotesUrlAccessor(const Value& row)
378 Service::Ptr service = static_cast<Service::Ptr>(row);
383 return service->GetNotesUrl();
386 Value ServicesTable::NotesUrlExpandedAccessor(const Value& row)
388 Service::Ptr service = static_cast<Service::Ptr>(row);
393 MacroProcessor::ResolverList resolvers {
394 { "service", service },
395 { "host", service->GetHost() },
396 { "icinga", IcingaApplication::GetInstance() }
399 return MacroProcessor::ResolveMacros(service->GetNotesUrl(), resolvers);
402 Value ServicesTable::ActionUrlAccessor(const Value& row)
404 Service::Ptr service = static_cast<Service::Ptr>(row);
409 return service->GetActionUrl();
412 Value ServicesTable::ActionUrlExpandedAccessor(const Value& row)
414 Service::Ptr service = static_cast<Service::Ptr>(row);
419 MacroProcessor::ResolverList resolvers {
420 { "service", service },
421 { "host", service->GetHost() },
422 { "icinga", IcingaApplication::GetInstance() }
425 return MacroProcessor::ResolveMacros(service->GetActionUrl(), resolvers);
428 Value ServicesTable::IconImageAccessor(const Value& row)
430 Service::Ptr service = static_cast<Service::Ptr>(row);
435 return service->GetIconImage();
438 Value ServicesTable::IconImageExpandedAccessor(const Value& row)
440 Service::Ptr service = static_cast<Service::Ptr>(row);
445 MacroProcessor::ResolverList resolvers {
446 { "service", service },
447 { "host", service->GetHost() },
448 { "icinga", IcingaApplication::GetInstance() }
451 return MacroProcessor::ResolveMacros(service->GetIconImage(), resolvers);
454 Value ServicesTable::IconImageAltAccessor(const Value& row)
456 Service::Ptr service = static_cast<Service::Ptr>(row);
461 return service->GetIconImageAlt();
464 Value ServicesTable::MaxCheckAttemptsAccessor(const Value& row)
466 Service::Ptr service = static_cast<Service::Ptr>(row);
471 return service->GetMaxCheckAttempts();
474 Value ServicesTable::CurrentAttemptAccessor(const Value& row)
476 Service::Ptr service = static_cast<Service::Ptr>(row);
481 return service->GetCheckAttempt();
484 Value ServicesTable::StateAccessor(const Value& row)
486 Service::Ptr service = static_cast<Service::Ptr>(row);
491 return service->GetState();
494 Value ServicesTable::HasBeenCheckedAccessor(const Value& row)
496 Service::Ptr service = static_cast<Service::Ptr>(row);
501 return Convert::ToLong(service->HasBeenChecked());
504 Value ServicesTable::LastStateAccessor(const Value& row)
506 Service::Ptr service = static_cast<Service::Ptr>(row);
511 return service->GetLastState();
514 Value ServicesTable::LastHardStateAccessor(const Value& row)
516 Service::Ptr service = static_cast<Service::Ptr>(row);
521 return service->GetLastHardState();
524 Value ServicesTable::StateTypeAccessor(const Value& row)
526 Service::Ptr service = static_cast<Service::Ptr>(row);
531 return service->GetStateType();
534 Value ServicesTable::CheckTypeAccessor(const Value& row)
536 Service::Ptr service = static_cast<Service::Ptr>(row);
541 return (service->GetEnableActiveChecks() ? 0 : 1); /* 0 .. active, 1 .. passive */
544 Value ServicesTable::AcknowledgedAccessor(const Value& row)
546 Service::Ptr service = static_cast<Service::Ptr>(row);
551 ObjectLock olock(service);
552 return service->IsAcknowledged();
555 Value ServicesTable::AcknowledgementTypeAccessor(const Value& row)
557 Service::Ptr service = static_cast<Service::Ptr>(row);
562 ObjectLock olock(service);
563 return service->GetAcknowledgement();
566 Value ServicesTable::NoMoreNotificationsAccessor(const Value& row)
568 Service::Ptr service = static_cast<Service::Ptr>(row);
573 return (CompatUtility::GetCheckableNotificationNotificationInterval(service) == 0 && !service->GetVolatile()) ? 1 : 0;
576 Value ServicesTable::LastTimeOkAccessor(const Value& row)
578 Service::Ptr service = static_cast<Service::Ptr>(row);
583 return static_cast<int>(service->GetLastStateOK());
586 Value ServicesTable::LastTimeWarningAccessor(const Value& row)
588 Service::Ptr service = static_cast<Service::Ptr>(row);
593 return static_cast<int>(service->GetLastStateWarning());
596 Value ServicesTable::LastTimeCriticalAccessor(const Value& row)
598 Service::Ptr service = static_cast<Service::Ptr>(row);
603 return static_cast<int>(service->GetLastStateCritical());
606 Value ServicesTable::LastTimeUnknownAccessor(const Value& row)
608 Service::Ptr service = static_cast<Service::Ptr>(row);
613 return static_cast<int>(service->GetLastStateUnknown());
616 Value ServicesTable::LastCheckAccessor(const Value& row)
618 Service::Ptr service = static_cast<Service::Ptr>(row);
623 return static_cast<int>(service->GetLastCheck());
626 Value ServicesTable::NextCheckAccessor(const Value& row)
628 Service::Ptr service = static_cast<Service::Ptr>(row);
633 return static_cast<int>(service->GetNextCheck());
636 Value ServicesTable::LastNotificationAccessor(const Value& row)
638 Service::Ptr service = static_cast<Service::Ptr>(row);
643 return CompatUtility::GetCheckableNotificationLastNotification(service);
646 Value ServicesTable::NextNotificationAccessor(const Value& row)
648 Service::Ptr service = static_cast<Service::Ptr>(row);
653 return CompatUtility::GetCheckableNotificationNextNotification(service);
656 Value ServicesTable::CurrentNotificationNumberAccessor(const Value& row)
658 Service::Ptr service = static_cast<Service::Ptr>(row);
663 return CompatUtility::GetCheckableNotificationNotificationNumber(service);
666 Value ServicesTable::LastStateChangeAccessor(const Value& row)
668 Service::Ptr service = static_cast<Service::Ptr>(row);
673 return static_cast<int>(service->GetLastStateChange());
676 Value ServicesTable::LastHardStateChangeAccessor(const Value& row)
678 Service::Ptr service = static_cast<Service::Ptr>(row);
683 return static_cast<int>(service->GetLastHardStateChange());
686 Value ServicesTable::ScheduledDowntimeDepthAccessor(const Value& row)
688 Service::Ptr service = static_cast<Service::Ptr>(row);
693 return service->GetDowntimeDepth();
696 Value ServicesTable::IsFlappingAccessor(const Value& row)
698 Service::Ptr service = static_cast<Service::Ptr>(row);
703 return service->IsFlapping();
706 Value ServicesTable::ChecksEnabledAccessor(const Value& row)
708 Service::Ptr service = static_cast<Service::Ptr>(row);
713 return Convert::ToLong(service->GetEnableActiveChecks());
716 Value ServicesTable::AcceptPassiveChecksAccessor(const Value& row)
718 Service::Ptr service = static_cast<Service::Ptr>(row);
723 return Convert::ToLong(service->GetEnablePassiveChecks());
726 Value ServicesTable::EventHandlerEnabledAccessor(const Value& row)
728 Service::Ptr service = static_cast<Service::Ptr>(row);
733 return Convert::ToLong(service->GetEnableEventHandler());
736 Value ServicesTable::NotificationsEnabledAccessor(const Value& row)
738 Service::Ptr service = static_cast<Service::Ptr>(row);
743 return Convert::ToLong(service->GetEnableNotifications());
746 Value ServicesTable::ProcessPerformanceDataAccessor(const Value& row)
748 Service::Ptr service = static_cast<Service::Ptr>(row);
753 return Convert::ToLong(service->GetEnablePerfdata());
756 Value ServicesTable::ActiveChecksEnabledAccessor(const Value& row)
758 Service::Ptr service = static_cast<Service::Ptr>(row);
763 return Convert::ToLong(service->GetEnableActiveChecks());
766 Value ServicesTable::FlapDetectionEnabledAccessor(const Value& row)
768 Service::Ptr service = static_cast<Service::Ptr>(row);
773 return Convert::ToLong(service->GetEnableFlapping());
776 Value ServicesTable::StalenessAccessor(const Value& row)
778 Service::Ptr service = static_cast<Service::Ptr>(row);
783 if (service->HasBeenChecked() && service->GetLastCheck() > 0)
784 return (Utility::GetTime() - service->GetLastCheck()) / (service->GetCheckInterval() * 3600);
789 Value ServicesTable::CheckIntervalAccessor(const Value& row)
791 Service::Ptr service = static_cast<Service::Ptr>(row);
796 return service->GetCheckInterval() / 60.0;
799 Value ServicesTable::RetryIntervalAccessor(const Value& row)
801 Service::Ptr service = static_cast<Service::Ptr>(row);
806 return service->GetRetryInterval() / 60.0;
809 Value ServicesTable::NotificationIntervalAccessor(const Value& row)
811 Service::Ptr service = static_cast<Service::Ptr>(row);
816 return CompatUtility::GetCheckableNotificationNotificationInterval(service);
819 Value ServicesTable::LowFlapThresholdAccessor(const Value& row)
821 Service::Ptr service = static_cast<Service::Ptr>(row);
826 return service->GetFlappingThresholdLow();
829 Value ServicesTable::HighFlapThresholdAccessor(const Value& row)
831 Service::Ptr service = static_cast<Service::Ptr>(row);
836 return service->GetFlappingThresholdHigh();
839 Value ServicesTable::LatencyAccessor(const Value& row)
841 Service::Ptr service = static_cast<Service::Ptr>(row);
846 CheckResult::Ptr cr = service->GetLastCheckResult();
851 return cr->CalculateLatency();
854 Value ServicesTable::ExecutionTimeAccessor(const Value& row)
856 Service::Ptr service = static_cast<Service::Ptr>(row);
861 CheckResult::Ptr cr = service->GetLastCheckResult();
866 return cr->CalculateExecutionTime();
869 Value ServicesTable::PercentStateChangeAccessor(const Value& row)
871 Service::Ptr service = static_cast<Service::Ptr>(row);
876 return service->GetFlappingCurrent();
879 Value ServicesTable::InCheckPeriodAccessor(const Value& row)
881 Service::Ptr service = static_cast<Service::Ptr>(row);
886 TimePeriod::Ptr timeperiod = service->GetCheckPeriod();
888 /* none set means always checked */
892 return Convert::ToLong(timeperiod->IsInside(Utility::GetTime()));
895 Value ServicesTable::InNotificationPeriodAccessor(const Value& row)
897 Service::Ptr service = static_cast<Service::Ptr>(row);
902 for (const Notification::Ptr& notification : service->GetNotifications()) {
903 TimePeriod::Ptr timeperiod = notification->GetPeriod();
905 if (!timeperiod || timeperiod->IsInside(Utility::GetTime()))
912 Value ServicesTable::ContactsAccessor(const Value& row)
914 Service::Ptr service = static_cast<Service::Ptr>(row);
921 for (const User::Ptr& user : CompatUtility::GetCheckableNotificationUsers(service)) {
922 result.push_back(user->GetName());
925 return new Array(std::move(result));
928 Value ServicesTable::DowntimesAccessor(const Value& row)
930 Service::Ptr service = static_cast<Service::Ptr>(row);
937 for (const Downtime::Ptr& downtime : service->GetDowntimes()) {
938 if (downtime->IsExpired())
941 result.push_back(downtime->GetLegacyId());
944 return new Array(std::move(result));
947 Value ServicesTable::DowntimesWithInfoAccessor(const Value& row)
949 Service::Ptr service = static_cast<Service::Ptr>(row);
956 for (const Downtime::Ptr& downtime : service->GetDowntimes()) {
957 if (downtime->IsExpired())
960 result.push_back(new Array({
961 downtime->GetLegacyId(),
962 downtime->GetAuthor(),
963 downtime->GetComment()
967 return new Array(std::move(result));
970 Value ServicesTable::CommentsAccessor(const Value& row)
972 Service::Ptr service = static_cast<Service::Ptr>(row);
979 for (const Comment::Ptr& comment : service->GetComments()) {
980 if (comment->IsExpired())
983 result.push_back(comment->GetLegacyId());
986 return new Array(std::move(result));
989 Value ServicesTable::CommentsWithInfoAccessor(const Value& row)
991 Service::Ptr service = static_cast<Service::Ptr>(row);
998 for (const Comment::Ptr& comment : service->GetComments()) {
999 if (comment->IsExpired())
1002 result.push_back(new Array({
1003 comment->GetLegacyId(),
1004 comment->GetAuthor(),
1009 return new Array(std::move(result));
1012 Value ServicesTable::CommentsWithExtraInfoAccessor(const Value& row)
1014 Service::Ptr service = static_cast<Service::Ptr>(row);
1021 for (const Comment::Ptr& comment : service->GetComments()) {
1022 if (comment->IsExpired())
1025 result.push_back(new Array({
1026 comment->GetLegacyId(),
1027 comment->GetAuthor(),
1029 comment->GetEntryType(),
1030 static_cast<int>(comment->GetEntryTime())
1034 return new Array(std::move(result));
1037 Value ServicesTable::CustomVariableNamesAccessor(const Value& row)
1039 Service::Ptr service = static_cast<Service::Ptr>(row);
1044 Dictionary::Ptr vars = service->GetVars();
1049 ObjectLock olock(vars);
1050 for (const Dictionary::Pair& kv : vars) {
1051 result.push_back(kv.first);
1055 return new Array(std::move(result));
1058 Value ServicesTable::CustomVariableValuesAccessor(const Value& row)
1060 Service::Ptr service = static_cast<Service::Ptr>(row);
1065 Dictionary::Ptr vars = service->GetVars();
1070 ObjectLock olock(vars);
1071 for (const Dictionary::Pair& kv : vars) {
1072 if (kv.second.IsObjectType<Array>() || kv.second.IsObjectType<Dictionary>())
1073 result.push_back(JsonEncode(kv.second));
1075 result.push_back(kv.second);
1079 return new Array(std::move(result));
1082 Value ServicesTable::CustomVariablesAccessor(const Value& row)
1084 Service::Ptr service = static_cast<Service::Ptr>(row);
1089 Dictionary::Ptr vars = service->GetVars();
1094 ObjectLock olock(vars);
1095 for (const Dictionary::Pair& kv : vars) {
1098 if (kv.second.IsObjectType<Array>() || kv.second.IsObjectType<Dictionary>())
1099 val = JsonEncode(kv.second);
1103 result.push_back(new Array({
1110 return new Array(std::move(result));
1113 Value ServicesTable::CVIsJsonAccessor(const Value& row)
1115 Service::Ptr service = static_cast<Service::Ptr>(row);
1120 Dictionary::Ptr vars = service->GetVars();
1125 bool cv_is_json = false;
1127 ObjectLock olock(vars);
1128 for (const Dictionary::Pair& kv : vars) {
1129 if (kv.second.IsObjectType<Array>() || kv.second.IsObjectType<Dictionary>())
1136 Value ServicesTable::GroupsAccessor(const Value& row)
1138 Service::Ptr service = static_cast<Service::Ptr>(row);
1143 Array::Ptr groups = service->GetGroups();
1151 Value ServicesTable::ContactGroupsAccessor(const Value& row)
1153 Service::Ptr service = static_cast<Service::Ptr>(row);
1160 for (const UserGroup::Ptr& usergroup : CompatUtility::GetCheckableNotificationUserGroups(service)) {
1161 result.push_back(usergroup->GetName());
1164 return new Array(std::move(result));
1167 Value ServicesTable::CheckSourceAccessor(const Value& row)
1169 Service::Ptr service = static_cast<Service::Ptr>(row);
1174 CheckResult::Ptr cr = service->GetLastCheckResult();
1177 return cr->GetCheckSource();
1182 Value ServicesTable::IsReachableAccessor(const Value& row)
1184 Service::Ptr service = static_cast<Service::Ptr>(row);
1189 return service->IsReachable();
1192 Value ServicesTable::OriginalAttributesAccessor(const Value& row)
1194 Service::Ptr service = static_cast<Service::Ptr>(row);
1199 return JsonEncode(service->GetOriginalAttributes());