1 /******************************************************************************
3 * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
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. *
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. *
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 ******************************************************************************/
20 #include "livestatus/servicestable.h"
21 #include "livestatus/hoststable.h"
22 #include "icinga/service.h"
23 #include "icinga/checkcommand.h"
24 #include "icinga/eventcommand.h"
25 #include "icinga/timeperiod.h"
26 #include "icinga/macroprocessor.h"
27 #include "icinga/icingaapplication.h"
28 #include "icinga/compatutility.h"
29 #include "base/dynamictype.h"
30 #include "base/objectlock.h"
31 #include "base/convert.h"
32 #include "base/utility.h"
33 #include <boost/foreach.hpp>
34 #include <boost/tuple/tuple.hpp>
35 #include <boost/algorithm/string/replace.hpp>
37 using namespace icinga;
39 ServicesTable::ServicesTable(void)
44 void ServicesTable::AddColumns(Table *table, const String& prefix,
45 const Column::ObjectAccessor& objectAccessor)
47 table->AddColumn(prefix + "description", Column(&ServicesTable::ShortNameAccessor, objectAccessor));
48 table->AddColumn(prefix + "display_name", Column(&ServicesTable::DisplayNameAccessor, objectAccessor));
49 table->AddColumn(prefix + "check_command", Column(&ServicesTable::CheckCommandAccessor, objectAccessor));
50 table->AddColumn(prefix + "check_command_expanded", Column(&ServicesTable::CheckCommandExpandedAccessor, objectAccessor));
51 table->AddColumn(prefix + "event_handler", Column(&ServicesTable::EventHandlerAccessor, objectAccessor));
52 table->AddColumn(prefix + "plugin_output", Column(&ServicesTable::PluginOutputAccessor, objectAccessor));
53 table->AddColumn(prefix + "long_plugin_output", Column(&ServicesTable::LongPluginOutputAccessor, objectAccessor));
54 table->AddColumn(prefix + "perf_data", Column(&ServicesTable::PerfDataAccessor, objectAccessor));
55 table->AddColumn(prefix + "notification_period", Column(&ServicesTable::NotificationPeriodAccessor, objectAccessor));
56 table->AddColumn(prefix + "check_period", Column(&ServicesTable::CheckPeriodAccessor, objectAccessor));
57 table->AddColumn(prefix + "notes", Column(&ServicesTable::NotesAccessor, objectAccessor));
58 table->AddColumn(prefix + "notes_expanded", Column(&ServicesTable::NotesExpandedAccessor, objectAccessor));
59 table->AddColumn(prefix + "notes_url", Column(&ServicesTable::NotesUrlAccessor, objectAccessor));
60 table->AddColumn(prefix + "notes_url_expanded", Column(&ServicesTable::NotesUrlExpandedAccessor, objectAccessor));
61 table->AddColumn(prefix + "action_url", Column(&ServicesTable::ActionUrlAccessor, objectAccessor));
62 table->AddColumn(prefix + "action_url_expanded", Column(&ServicesTable::ActionUrlExpandedAccessor, objectAccessor));
63 table->AddColumn(prefix + "icon_image", Column(&ServicesTable::IconImageAccessor, objectAccessor));
64 table->AddColumn(prefix + "icon_image_expanded", Column(&ServicesTable::IconImageExpandedAccessor, objectAccessor));
65 table->AddColumn(prefix + "icon_image_alt", Column(&ServicesTable::IconImageAltAccessor, objectAccessor));
66 table->AddColumn(prefix + "initial_state", Column(&Table::EmptyStringAccessor, objectAccessor));
67 table->AddColumn(prefix + "max_check_attempts", Column(&ServicesTable::MaxCheckAttemptsAccessor, objectAccessor));
68 table->AddColumn(prefix + "current_attempt", Column(&ServicesTable::CurrentAttemptAccessor, objectAccessor));
69 table->AddColumn(prefix + "state", Column(&ServicesTable::StateAccessor, objectAccessor));
70 table->AddColumn(prefix + "has_been_checked", Column(&ServicesTable::HasBeenCheckedAccessor, objectAccessor));
71 table->AddColumn(prefix + "last_state", Column(&ServicesTable::LastStateAccessor, objectAccessor));
72 table->AddColumn(prefix + "last_hard_state", Column(&ServicesTable::LastHardStateAccessor, objectAccessor));
73 table->AddColumn(prefix + "state_type", Column(&ServicesTable::StateTypeAccessor, objectAccessor));
74 table->AddColumn(prefix + "check_type", Column(&ServicesTable::CheckTypeAccessor, objectAccessor));
75 table->AddColumn(prefix + "acknowledged", Column(&ServicesTable::AcknowledgedAccessor, objectAccessor));
76 table->AddColumn(prefix + "acknowledgement_type", Column(&ServicesTable::AcknowledgementTypeAccessor, objectAccessor));
77 table->AddColumn(prefix + "no_more_notifications", Column(&ServicesTable::NoMoreNotificationsAccessor, objectAccessor));
78 table->AddColumn(prefix + "last_time_ok", Column(&ServicesTable::LastTimeOkAccessor, objectAccessor));
79 table->AddColumn(prefix + "last_time_warning", Column(&ServicesTable::LastTimeWarningAccessor, objectAccessor));
80 table->AddColumn(prefix + "last_time_critical", Column(&ServicesTable::LastTimeCriticalAccessor, objectAccessor));
81 table->AddColumn(prefix + "last_time_unknown", Column(&ServicesTable::LastTimeUnknownAccessor, objectAccessor));
82 table->AddColumn(prefix + "last_check", Column(&ServicesTable::LastCheckAccessor, objectAccessor));
83 table->AddColumn(prefix + "next_check", Column(&ServicesTable::NextCheckAccessor, objectAccessor));
84 table->AddColumn(prefix + "last_notification", Column(&ServicesTable::LastNotificationAccessor, objectAccessor));
85 table->AddColumn(prefix + "next_notification", Column(&ServicesTable::NextNotificationAccessor, objectAccessor));
86 table->AddColumn(prefix + "current_notification_number", Column(&ServicesTable::CurrentNotificationNumberAccessor, objectAccessor));
87 table->AddColumn(prefix + "last_state_change", Column(&ServicesTable::LastStateChangeAccessor, objectAccessor));
88 table->AddColumn(prefix + "last_hard_state_change", Column(&ServicesTable::LastHardStateChangeAccessor, objectAccessor));
89 table->AddColumn(prefix + "scheduled_downtime_depth", Column(&ServicesTable::ScheduledDowntimeDepthAccessor, objectAccessor));
90 table->AddColumn(prefix + "is_flapping", Column(&ServicesTable::IsFlappingAccessor, objectAccessor));
91 table->AddColumn(prefix + "checks_enabled", Column(&ServicesTable::ChecksEnabledAccessor, objectAccessor));
92 table->AddColumn(prefix + "accept_passive_checks", Column(&ServicesTable::AcceptPassiveChecksAccessor, objectAccessor));
93 table->AddColumn(prefix + "event_handler_enabled", Column(&ServicesTable::EventHandlerEnabledAccessor, objectAccessor));
94 table->AddColumn(prefix + "notifications_enabled", Column(&ServicesTable::NotificationsEnabledAccessor, objectAccessor));
95 table->AddColumn(prefix + "process_performance_data", Column(&Table::OneAccessor, objectAccessor));
96 table->AddColumn(prefix + "is_executing", Column(&Table::ZeroAccessor, objectAccessor));
97 table->AddColumn(prefix + "active_checks_enabled", Column(&ServicesTable::ActiveChecksEnabledAccessor, objectAccessor));
98 table->AddColumn(prefix + "check_options", Column(&ServicesTable::CheckOptionsAccessor, objectAccessor));
99 table->AddColumn(prefix + "flap_detection_enabled", Column(&ServicesTable::FlapDetectionEnabledAccessor, objectAccessor));
100 table->AddColumn(prefix + "check_freshness", Column(&ServicesTable::CheckFreshnessAccessor, objectAccessor));
101 table->AddColumn(prefix + "obsess_over_service", Column(&Table::ZeroAccessor, objectAccessor));
102 table->AddColumn(prefix + "modified_attributes", Column(&ServicesTable::ModifiedAttributesAccessor, objectAccessor));
103 table->AddColumn(prefix + "modified_attributes_list", Column(&ServicesTable::ModifiedAttributesListAccessor, objectAccessor));
104 table->AddColumn(prefix + "pnpgraph_present", Column(&Table::ZeroAccessor, objectAccessor));
105 table->AddColumn(prefix + "staleness", Column(&ServicesTable::StalenessAccessor, objectAccessor));
106 table->AddColumn(prefix + "check_interval", Column(&ServicesTable::CheckIntervalAccessor, objectAccessor));
107 table->AddColumn(prefix + "retry_interval", Column(&ServicesTable::RetryIntervalAccessor, objectAccessor));
108 table->AddColumn(prefix + "notification_interval", Column(&ServicesTable::NotificationIntervalAccessor, objectAccessor));
109 table->AddColumn(prefix + "first_notification_delay", Column(&Table::EmptyStringAccessor, objectAccessor));
110 table->AddColumn(prefix + "low_flap_threshold", Column(&ServicesTable::LowFlapThresholdAccessor, objectAccessor));
111 table->AddColumn(prefix + "high_flap_threshold", Column(&ServicesTable::HighFlapThresholdAccessor, objectAccessor));
112 table->AddColumn(prefix + "latency", Column(&ServicesTable::LatencyAccessor, objectAccessor));
113 table->AddColumn(prefix + "execution_time", Column(&ServicesTable::ExecutionTimeAccessor, objectAccessor));
114 table->AddColumn(prefix + "percent_state_change", Column(&ServicesTable::PercentStateChangeAccessor, objectAccessor));
115 table->AddColumn(prefix + "in_check_period", Column(&ServicesTable::InCheckPeriodAccessor, objectAccessor));
116 table->AddColumn(prefix + "in_notification_period", Column(&ServicesTable::InNotificationPeriodAccessor, objectAccessor));
117 table->AddColumn(prefix + "contacts", Column(&ServicesTable::ContactsAccessor, objectAccessor));
118 table->AddColumn(prefix + "downtimes", Column(&ServicesTable::DowntimesAccessor, objectAccessor));
119 table->AddColumn(prefix + "downtimes_with_info", Column(&ServicesTable::DowntimesWithInfoAccessor, objectAccessor));
120 table->AddColumn(prefix + "comments", Column(&ServicesTable::CommentsAccessor, objectAccessor));
121 table->AddColumn(prefix + "comments_with_info", Column(&ServicesTable::CommentsWithInfoAccessor, objectAccessor));
122 table->AddColumn(prefix + "comments_with_extra_info", Column(&ServicesTable::CommentsWithExtraInfoAccessor, objectAccessor));
123 table->AddColumn(prefix + "custom_variable_names", Column(&ServicesTable::CustomVariableNamesAccessor, objectAccessor));
124 table->AddColumn(prefix + "custom_variable_values", Column(&ServicesTable::CustomVariableValuesAccessor, objectAccessor));
125 table->AddColumn(prefix + "custom_variables", Column(&ServicesTable::CustomVariablesAccessor, objectAccessor));
126 table->AddColumn(prefix + "groups", Column(&ServicesTable::GroupsAccessor, objectAccessor));
127 table->AddColumn(prefix + "contact_groups", Column(&ServicesTable::ContactGroupsAccessor, objectAccessor));
129 HostsTable::AddColumns(table, "host_", boost::bind(&ServicesTable::HostAccessor, _1, objectAccessor));
132 String ServicesTable::GetName(void) const
137 void ServicesTable::FetchRows(const AddRowFunction& addRowFn)
139 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
144 Object::Ptr ServicesTable::HostAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor)
148 if (parentObjectAccessor)
149 service = parentObjectAccessor(row);
153 Service::Ptr svc = static_cast<Service::Ptr>(service);
156 return Object::Ptr();
158 return svc->GetHost();
161 Value ServicesTable::ShortNameAccessor(const Value& row)
163 Service::Ptr service = static_cast<Service::Ptr>(row);
168 return service->GetShortName();
171 Value ServicesTable::DisplayNameAccessor(const Value& row)
173 Service::Ptr service = static_cast<Service::Ptr>(row);
178 return service->GetDisplayName();
181 Value ServicesTable::CheckCommandAccessor(const Value& row)
183 Service::Ptr service = static_cast<Service::Ptr>(row);
188 CheckCommand::Ptr checkcommand = service->GetCheckCommand();
191 return checkcommand->GetName(); /* this is the name without '!' args */
196 Value ServicesTable::CheckCommandExpandedAccessor(const Value& row)
198 Service::Ptr service = static_cast<Service::Ptr>(row);
203 CheckCommand::Ptr checkcommand = service->GetCheckCommand();
206 return checkcommand->GetName(); /* this is the name without '!' args */
211 Value ServicesTable::EventHandlerAccessor(const Value& row)
213 Service::Ptr service = static_cast<Service::Ptr>(row);
218 EventCommand::Ptr eventcommand = service->GetEventCommand();
221 return eventcommand->GetName();
226 Value ServicesTable::PluginOutputAccessor(const Value& row)
228 Service::Ptr service = static_cast<Service::Ptr>(row);
234 CheckResult::Ptr cr = service->GetLastCheckResult();
237 std::pair<String, String> output_bag = CompatUtility::GetCheckResultOutput(cr);
238 output = output_bag.first;
244 Value ServicesTable::LongPluginOutputAccessor(const Value& row)
246 Service::Ptr service = static_cast<Service::Ptr>(row);
252 CheckResult::Ptr cr = service->GetLastCheckResult();
255 std::pair<String, String> output_bag = CompatUtility::GetCheckResultOutput(cr);
256 long_output = output_bag.second;
262 Value ServicesTable::PerfDataAccessor(const Value& row)
264 Service::Ptr service = static_cast<Service::Ptr>(row);
270 CheckResult::Ptr cr = service->GetLastCheckResult();
273 perfdata = CompatUtility::GetCheckResultPerfdata(cr);
278 Value ServicesTable::NotificationPeriodAccessor(const Value& row)
280 Service::Ptr service = static_cast<Service::Ptr>(row);
285 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
286 ObjectLock olock(notification);
288 TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod();
290 /* XXX first notification wins */
292 return timeperiod->GetName();
298 Value ServicesTable::CheckPeriodAccessor(const Value& row)
300 Service::Ptr service = static_cast<Service::Ptr>(row);
305 TimePeriod::Ptr timeperiod = service->GetCheckPeriod();
310 return timeperiod->GetName();
313 Value ServicesTable::NotesAccessor(const Value& row)
315 Service::Ptr service = static_cast<Service::Ptr>(row);
320 Dictionary::Ptr custom = service->GetCustom();
325 return custom->Get("notes");
328 Value ServicesTable::NotesExpandedAccessor(const Value& row)
330 Service::Ptr service = static_cast<Service::Ptr>(row);
335 Dictionary::Ptr custom = service->GetCustom();
340 std::vector<MacroResolver::Ptr> resolvers;
341 resolvers.push_back(service);
342 resolvers.push_back(service->GetHost());
343 resolvers.push_back(IcingaApplication::GetInstance());
345 Value value = custom->Get("notes");
347 return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd);
350 Value ServicesTable::NotesUrlAccessor(const Value& row)
352 Service::Ptr service = static_cast<Service::Ptr>(row);
357 Dictionary::Ptr custom = service->GetCustom();
362 return custom->Get("notes_url");
365 Value ServicesTable::NotesUrlExpandedAccessor(const Value& row)
367 Service::Ptr service = static_cast<Service::Ptr>(row);
372 Dictionary::Ptr custom = service->GetCustom();
377 std::vector<MacroResolver::Ptr> resolvers;
378 resolvers.push_back(service);
379 resolvers.push_back(service->GetHost());
380 resolvers.push_back(IcingaApplication::GetInstance());
382 Value value = custom->Get("notes_url");
384 return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd);
387 Value ServicesTable::ActionUrlAccessor(const Value& row)
389 Service::Ptr service = static_cast<Service::Ptr>(row);
394 Dictionary::Ptr custom = service->GetCustom();
399 return custom->Get("action_url");
402 Value ServicesTable::ActionUrlExpandedAccessor(const Value& row)
404 Service::Ptr service = static_cast<Service::Ptr>(row);
409 Dictionary::Ptr custom = service->GetCustom();
414 std::vector<MacroResolver::Ptr> resolvers;
415 resolvers.push_back(service);
416 resolvers.push_back(service->GetHost());
417 resolvers.push_back(IcingaApplication::GetInstance());
419 Value value = custom->Get("action_url");
421 return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd);
424 Value ServicesTable::IconImageAccessor(const Value& row)
426 Service::Ptr service = static_cast<Service::Ptr>(row);
431 Dictionary::Ptr custom = service->GetCustom();
436 return custom->Get("icon_image");
439 Value ServicesTable::IconImageExpandedAccessor(const Value& row)
441 Service::Ptr service = static_cast<Service::Ptr>(row);
446 Dictionary::Ptr custom = service->GetCustom();
451 std::vector<MacroResolver::Ptr> resolvers;
452 resolvers.push_back(service);
453 resolvers.push_back(service->GetHost());
454 resolvers.push_back(IcingaApplication::GetInstance());
456 Value value = custom->Get("icon_image");
458 return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd);
461 Value ServicesTable::IconImageAltAccessor(const Value& row)
463 Service::Ptr service = static_cast<Service::Ptr>(row);
468 Dictionary::Ptr custom = service->GetCustom();
473 return custom->Get("icon_image_alt");
476 Value ServicesTable::MaxCheckAttemptsAccessor(const Value& row)
478 Service::Ptr service = static_cast<Service::Ptr>(row);
483 return service->GetMaxCheckAttempts();
486 Value ServicesTable::CurrentAttemptAccessor(const Value& row)
488 Service::Ptr service = static_cast<Service::Ptr>(row);
493 return service->GetCheckAttempt();
496 Value ServicesTable::StateAccessor(const Value& row)
498 Service::Ptr service = static_cast<Service::Ptr>(row);
503 return service->GetState();
506 Value ServicesTable::HasBeenCheckedAccessor(const Value& row)
508 Service::Ptr service = static_cast<Service::Ptr>(row);
513 return (service->HasBeenChecked() ? 1 : 0);
516 Value ServicesTable::LastStateAccessor(const Value& row)
518 Service::Ptr service = static_cast<Service::Ptr>(row);
523 return service->GetLastState();
526 Value ServicesTable::LastHardStateAccessor(const Value& row)
528 Service::Ptr service = static_cast<Service::Ptr>(row);
533 return service->GetLastHardState();
536 Value ServicesTable::StateTypeAccessor(const Value& row)
538 Service::Ptr service = static_cast<Service::Ptr>(row);
543 return service->GetStateType();
546 Value ServicesTable::CheckTypeAccessor(const Value& row)
548 Service::Ptr service = static_cast<Service::Ptr>(row);
553 return (service->GetEnableActiveChecks() ? 0 : 1);
556 Value ServicesTable::AcknowledgedAccessor(const Value& row)
558 Service::Ptr service = static_cast<Service::Ptr>(row);
563 /* important: lock acknowledgements */
564 ObjectLock olock(service);
566 return (service->IsAcknowledged() ? 1 : 0);
569 Value ServicesTable::AcknowledgementTypeAccessor(const Value& row)
571 Service::Ptr service = static_cast<Service::Ptr>(row);
576 /* important: lock acknowledgements */
577 ObjectLock olock(service);
579 return static_cast<int>(service->GetAcknowledgement());
582 Value ServicesTable::NoMoreNotificationsAccessor(const Value& row)
584 Service::Ptr service = static_cast<Service::Ptr>(row);
589 /* XXX take the smallest notification_interval */
590 double notification_interval = -1;
591 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
592 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
593 notification_interval = notification->GetNotificationInterval();
596 if (notification_interval == 0 && !service->GetVolatile())
602 Value ServicesTable::LastTimeOkAccessor(const Value& row)
604 Service::Ptr service = static_cast<Service::Ptr>(row);
609 return static_cast<int>(service->GetLastStateOK());
612 Value ServicesTable::LastTimeWarningAccessor(const Value& row)
614 Service::Ptr service = static_cast<Service::Ptr>(row);
619 return static_cast<int>(service->GetLastStateWarning());
622 Value ServicesTable::LastTimeCriticalAccessor(const Value& row)
624 Service::Ptr service = static_cast<Service::Ptr>(row);
629 return static_cast<int>(service->GetLastStateCritical());
632 Value ServicesTable::LastTimeUnknownAccessor(const Value& row)
634 Service::Ptr service = static_cast<Service::Ptr>(row);
639 return static_cast<int>(service->GetLastStateUnknown());
642 Value ServicesTable::LastCheckAccessor(const Value& row)
644 Service::Ptr service = static_cast<Service::Ptr>(row);
649 return static_cast<int>(service->GetLastCheck());
652 Value ServicesTable::NextCheckAccessor(const Value& row)
654 Service::Ptr service = static_cast<Service::Ptr>(row);
659 return static_cast<int>(service->GetNextCheck());
662 Value ServicesTable::LastNotificationAccessor(const Value& row)
664 Service::Ptr service = static_cast<Service::Ptr>(row);
669 /* XXX Service -> Notifications, latest wins */
670 double last_notification = 0;
671 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
672 if (notification->GetLastNotification() > last_notification)
673 last_notification = notification->GetLastNotification();
676 return static_cast<int>(last_notification);
679 Value ServicesTable::NextNotificationAccessor(const Value& row)
681 Service::Ptr service = static_cast<Service::Ptr>(row);
686 /* XXX Service -> Notifications, latest wins */
687 double next_notification = 0;
688 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
689 if (notification->GetNextNotification() < next_notification)
690 next_notification = notification->GetNextNotification();
693 return static_cast<int>(next_notification);
696 Value ServicesTable::CurrentNotificationNumberAccessor(const Value& row)
698 Service::Ptr service = static_cast<Service::Ptr>(row);
703 /* XXX Service -> Notifications, biggest wins */
704 int notification_number = 0;
705 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
706 if (notification->GetNotificationNumber() > notification_number)
707 notification_number = notification->GetNotificationNumber();
710 return notification_number;
713 Value ServicesTable::LastStateChangeAccessor(const Value& row)
715 Service::Ptr service = static_cast<Service::Ptr>(row);
720 return static_cast<int>(service->GetLastStateChange());
723 Value ServicesTable::LastHardStateChangeAccessor(const Value& row)
725 Service::Ptr service = static_cast<Service::Ptr>(row);
730 return static_cast<int>(service->GetLastHardStateChange());
733 Value ServicesTable::ScheduledDowntimeDepthAccessor(const Value& row)
735 Service::Ptr service = static_cast<Service::Ptr>(row);
740 return service->GetDowntimeDepth();
743 Value ServicesTable::IsFlappingAccessor(const Value& row)
745 Service::Ptr service = static_cast<Service::Ptr>(row);
750 return service->IsFlapping();
753 Value ServicesTable::ChecksEnabledAccessor(const Value& row)
755 Service::Ptr service = static_cast<Service::Ptr>(row);
760 return (service->GetEnableActiveChecks() ? 1 : 0);
763 Value ServicesTable::AcceptPassiveChecksAccessor(const Value& row)
765 Service::Ptr service = static_cast<Service::Ptr>(row);
770 return (service->GetEnablePassiveChecks() ? 1 : 0);
773 Value ServicesTable::EventHandlerEnabledAccessor(const Value& row)
775 Service::Ptr service = static_cast<Service::Ptr>(row);
780 EventCommand::Ptr eventcommand = service->GetEventCommand();
788 Value ServicesTable::NotificationsEnabledAccessor(const Value& row)
790 Service::Ptr service = static_cast<Service::Ptr>(row);
795 return (service->GetEnableNotifications() ? 1 : 0);
798 Value ServicesTable::ActiveChecksEnabledAccessor(const Value& row)
800 Service::Ptr service = static_cast<Service::Ptr>(row);
805 return (service->GetEnableActiveChecks() ? 1 : 0);
808 Value ServicesTable::CheckOptionsAccessor(const Value& row)
810 /* TODO - forcexec, freshness, orphan, none */
814 Value ServicesTable::FlapDetectionEnabledAccessor(const Value& row)
816 Service::Ptr service = static_cast<Service::Ptr>(row);
821 return (service->GetEnableFlapping() ? 1 : 0);
824 Value ServicesTable::CheckFreshnessAccessor(const Value& row)
830 Value ServicesTable::ModifiedAttributesAccessor(const Value& row)
832 Service::Ptr service = static_cast<Service::Ptr>(row);
837 return service->GetModifiedAttributes();
840 Value ServicesTable::ModifiedAttributesListAccessor(const Value& row)
846 Value ServicesTable::StalenessAccessor(const Value& row)
848 Service::Ptr service = static_cast<Service::Ptr>(row);
853 if (service->HasBeenChecked() && service->GetLastCheck() > 0)
854 return (Utility::GetTime() - service->GetLastCheck()) / (service->GetCheckInterval() * 3600);
859 Value ServicesTable::CheckIntervalAccessor(const Value& row)
861 Service::Ptr service = static_cast<Service::Ptr>(row);
866 return (service->GetCheckInterval() / 60.0);
869 Value ServicesTable::RetryIntervalAccessor(const Value& row)
871 Service::Ptr service = static_cast<Service::Ptr>(row);
876 return (service->GetRetryInterval() / 60.0);
879 Value ServicesTable::NotificationIntervalAccessor(const Value& row)
881 Service::Ptr service = static_cast<Service::Ptr>(row);
886 /* XXX take the smallest notification_interval */
887 double notification_interval = -1;
888 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
889 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
890 notification_interval = notification->GetNotificationInterval();
893 if (notification_interval == -1)
894 notification_interval = 60;
896 return (notification_interval / 60.0);
899 Value ServicesTable::LowFlapThresholdAccessor(const Value& row)
901 Service::Ptr service = static_cast<Service::Ptr>(row);
906 return service->GetFlappingThreshold();
909 Value ServicesTable::HighFlapThresholdAccessor(const Value& row)
911 Service::Ptr service = static_cast<Service::Ptr>(row);
916 return service->GetFlappingThreshold();
919 Value ServicesTable::LatencyAccessor(const Value& row)
921 Service::Ptr service = static_cast<Service::Ptr>(row);
926 return (Service::CalculateLatency(service->GetLastCheckResult()));
929 Value ServicesTable::ExecutionTimeAccessor(const Value& row)
931 Service::Ptr service = static_cast<Service::Ptr>(row);
936 return (Service::CalculateExecutionTime(service->GetLastCheckResult()));
939 Value ServicesTable::PercentStateChangeAccessor(const Value& row)
941 Service::Ptr service = static_cast<Service::Ptr>(row);
946 return service->GetFlappingCurrent();
949 Value ServicesTable::InCheckPeriodAccessor(const Value& row)
951 Service::Ptr service = static_cast<Service::Ptr>(row);
956 TimePeriod::Ptr timeperiod = service->GetCheckPeriod();
958 /* none set means always checked */
962 return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
965 Value ServicesTable::InNotificationPeriodAccessor(const Value& row)
967 Service::Ptr service = static_cast<Service::Ptr>(row);
972 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
973 ObjectLock olock(notification);
975 TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod();
977 /* XXX first notification wins */
979 return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
982 /* none set means always notified */
986 Value ServicesTable::ContactsAccessor(const Value& row)
988 Service::Ptr service = static_cast<Service::Ptr>(row);
993 Array::Ptr contact_names = make_shared<Array>();
995 BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetServiceNotificationUsers(service)) {
996 contact_names->Add(user->GetName());
999 return contact_names;
1002 Value ServicesTable::DowntimesAccessor(const Value& row)
1004 Service::Ptr service = static_cast<Service::Ptr>(row);
1009 Dictionary::Ptr downtimes = service->GetDowntimes();
1011 Array::Ptr ids = make_shared<Array>();
1013 ObjectLock olock(downtimes);
1016 Downtime::Ptr downtime;
1017 BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
1022 if (downtime->IsExpired())
1025 ids->Add(downtime->GetLegacyId());
1031 Value ServicesTable::DowntimesWithInfoAccessor(const Value& row)
1033 Service::Ptr service = static_cast<Service::Ptr>(row);
1038 Dictionary::Ptr downtimes = service->GetDowntimes();
1040 Array::Ptr ids = make_shared<Array>();
1042 ObjectLock olock(downtimes);
1045 Downtime::Ptr downtime;
1046 BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
1051 if (downtime->IsExpired())
1054 Array::Ptr downtime_info = make_shared<Array>();
1055 downtime_info->Add(downtime->GetLegacyId());
1056 downtime_info->Add(downtime->GetAuthor());
1057 downtime_info->Add(downtime->GetComment());
1058 ids->Add(downtime_info);
1064 Value ServicesTable::CommentsAccessor(const Value& row)
1066 Service::Ptr service = static_cast<Service::Ptr>(row);
1071 Dictionary::Ptr comments = service->GetComments();
1073 Array::Ptr ids = make_shared<Array>();
1075 ObjectLock olock(comments);
1078 Comment::Ptr comment;
1079 BOOST_FOREACH(boost::tie(id, comment), comments) {
1084 if (comment->IsExpired())
1087 ids->Add(comment->GetLegacyId());
1093 Value ServicesTable::CommentsWithInfoAccessor(const Value& row)
1095 Service::Ptr service = static_cast<Service::Ptr>(row);
1100 Dictionary::Ptr comments = service->GetComments();
1102 Array::Ptr ids = make_shared<Array>();
1104 ObjectLock olock(comments);
1107 Comment::Ptr comment;
1108 BOOST_FOREACH(boost::tie(id, comment), comments) {
1113 if (comment->IsExpired())
1116 Array::Ptr comment_info = make_shared<Array>();
1117 comment_info->Add(comment->GetLegacyId());
1118 comment_info->Add(comment->GetAuthor());
1119 comment_info->Add(comment->GetText());
1120 ids->Add(comment_info);
1126 Value ServicesTable::CommentsWithExtraInfoAccessor(const Value& row)
1128 Service::Ptr service = static_cast<Service::Ptr>(row);
1133 Dictionary::Ptr comments = service->GetComments();
1135 Array::Ptr ids = make_shared<Array>();
1137 ObjectLock olock(comments);
1140 Comment::Ptr comment;
1141 BOOST_FOREACH(boost::tie(id, comment), comments) {
1146 if (comment->IsExpired())
1149 Array::Ptr comment_info = make_shared<Array>();
1150 comment_info->Add(comment->GetLegacyId());
1151 comment_info->Add(comment->GetAuthor());
1152 comment_info->Add(comment->GetText());
1153 comment_info->Add(comment->GetEntryType());
1154 comment_info->Add(static_cast<int>(comment->GetEntryTime()));
1155 ids->Add(comment_info);
1161 Value ServicesTable::CustomVariableNamesAccessor(const Value& row)
1163 Service::Ptr service = static_cast<Service::Ptr>(row);
1168 Dictionary::Ptr customvars;
1171 ObjectLock olock(service);
1172 customvars = CompatUtility::GetCustomVariableConfig(service);
1178 Array::Ptr cv = make_shared<Array>();
1182 BOOST_FOREACH(boost::tie(key, value), customvars) {
1189 Value ServicesTable::CustomVariableValuesAccessor(const Value& row)
1191 Service::Ptr service = static_cast<Service::Ptr>(row);
1196 Dictionary::Ptr customvars;
1199 ObjectLock olock(service);
1200 customvars = CompatUtility::GetCustomVariableConfig(service);
1206 Array::Ptr cv = make_shared<Array>();
1210 BOOST_FOREACH(boost::tie(key, value), customvars) {
1217 Value ServicesTable::CustomVariablesAccessor(const Value& row)
1219 Service::Ptr service = static_cast<Service::Ptr>(row);
1224 Dictionary::Ptr customvars;
1227 ObjectLock olock(service);
1228 customvars = CompatUtility::GetCustomVariableConfig(service);
1234 Array::Ptr cv = make_shared<Array>();
1238 BOOST_FOREACH(boost::tie(key, value), customvars) {
1239 Array::Ptr key_val = make_shared<Array>();
1241 key_val->Add(value);
1248 Value ServicesTable::GroupsAccessor(const Value& row)
1250 Service::Ptr service = static_cast<Service::Ptr>(row);
1255 Array::Ptr groups = service->GetGroups();
1263 Value ServicesTable::ContactGroupsAccessor(const Value& row)
1265 Service::Ptr service = static_cast<Service::Ptr>(row);
1270 Array::Ptr contactgroup_names = make_shared<Array>();
1272 BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetServiceNotificationUserGroups(service)) {
1273 contactgroup_names->Add(usergroup->GetName());
1276 return contactgroup_names;