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 commandObj = service->GetCheckCommand();
208 Value raw_command = commandObj->GetCommandLine();
210 std::vector<MacroResolver::Ptr> resolvers;
211 resolvers.push_back(service);
212 resolvers.push_back(service->GetHost());
213 resolvers.push_back(commandObj);
214 resolvers.push_back(IcingaApplication::GetInstance());
216 Value commandLine = MacroProcessor::ResolveMacros(raw_command, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd);
219 if (commandLine.IsObjectType<Array>()) {
220 Array::Ptr args = commandLine;
222 ObjectLock olock(args);
224 BOOST_FOREACH(arg, args) {
225 // This is obviously incorrect for non-trivial cases.
226 String argitem = " \"" + arg + "\"";
227 boost::algorithm::replace_all(argitem, "\n", "\\n");
230 } else if (!commandLine.IsEmpty()) {
231 String args = Convert::ToString(commandLine);
232 boost::algorithm::replace_all(args, "\n", "\\n");
241 Value ServicesTable::EventHandlerAccessor(const Value& row)
243 Service::Ptr service = static_cast<Service::Ptr>(row);
248 EventCommand::Ptr eventcommand = service->GetEventCommand();
251 return eventcommand->GetName();
256 Value ServicesTable::PluginOutputAccessor(const Value& row)
258 Service::Ptr service = static_cast<Service::Ptr>(row);
264 CheckResult::Ptr cr = service->GetLastCheckResult();
267 std::pair<String, String> output_bag = CompatUtility::GetCheckResultOutput(cr);
268 output = output_bag.first;
274 Value ServicesTable::LongPluginOutputAccessor(const Value& row)
276 Service::Ptr service = static_cast<Service::Ptr>(row);
282 CheckResult::Ptr cr = service->GetLastCheckResult();
285 std::pair<String, String> output_bag = CompatUtility::GetCheckResultOutput(cr);
286 long_output = output_bag.second;
292 Value ServicesTable::PerfDataAccessor(const Value& row)
294 Service::Ptr service = static_cast<Service::Ptr>(row);
300 CheckResult::Ptr cr = service->GetLastCheckResult();
303 perfdata = CompatUtility::GetCheckResultPerfdata(cr);
308 Value ServicesTable::NotificationPeriodAccessor(const Value& row)
310 Service::Ptr service = static_cast<Service::Ptr>(row);
315 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
316 ObjectLock olock(notification);
318 TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod();
320 /* XXX first notification wins */
322 return timeperiod->GetName();
328 Value ServicesTable::CheckPeriodAccessor(const Value& row)
330 Service::Ptr service = static_cast<Service::Ptr>(row);
335 TimePeriod::Ptr timeperiod = service->GetCheckPeriod();
340 return timeperiod->GetName();
343 Value ServicesTable::NotesAccessor(const Value& row)
345 Service::Ptr service = static_cast<Service::Ptr>(row);
350 Dictionary::Ptr custom = service->GetCustom();
355 return custom->Get("notes");
358 Value ServicesTable::NotesExpandedAccessor(const Value& row)
360 Service::Ptr service = static_cast<Service::Ptr>(row);
365 Dictionary::Ptr custom = service->GetCustom();
370 std::vector<MacroResolver::Ptr> resolvers;
371 resolvers.push_back(service);
372 resolvers.push_back(service->GetHost());
373 resolvers.push_back(IcingaApplication::GetInstance());
375 Value value = custom->Get("notes");
377 return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd);
380 Value ServicesTable::NotesUrlAccessor(const Value& row)
382 Service::Ptr service = static_cast<Service::Ptr>(row);
387 Dictionary::Ptr custom = service->GetCustom();
392 return custom->Get("notes_url");
395 Value ServicesTable::NotesUrlExpandedAccessor(const Value& row)
397 Service::Ptr service = static_cast<Service::Ptr>(row);
402 Dictionary::Ptr custom = service->GetCustom();
407 std::vector<MacroResolver::Ptr> resolvers;
408 resolvers.push_back(service);
409 resolvers.push_back(service->GetHost());
410 resolvers.push_back(IcingaApplication::GetInstance());
412 Value value = custom->Get("notes_url");
414 return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd);
417 Value ServicesTable::ActionUrlAccessor(const Value& row)
419 Service::Ptr service = static_cast<Service::Ptr>(row);
424 Dictionary::Ptr custom = service->GetCustom();
429 return custom->Get("action_url");
432 Value ServicesTable::ActionUrlExpandedAccessor(const Value& row)
434 Service::Ptr service = static_cast<Service::Ptr>(row);
439 Dictionary::Ptr custom = service->GetCustom();
444 std::vector<MacroResolver::Ptr> resolvers;
445 resolvers.push_back(service);
446 resolvers.push_back(service->GetHost());
447 resolvers.push_back(IcingaApplication::GetInstance());
449 Value value = custom->Get("action_url");
451 return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd);
454 Value ServicesTable::IconImageAccessor(const Value& row)
456 Service::Ptr service = static_cast<Service::Ptr>(row);
461 Dictionary::Ptr custom = service->GetCustom();
466 return custom->Get("icon_image");
469 Value ServicesTable::IconImageExpandedAccessor(const Value& row)
471 Service::Ptr service = static_cast<Service::Ptr>(row);
476 Dictionary::Ptr custom = service->GetCustom();
481 std::vector<MacroResolver::Ptr> resolvers;
482 resolvers.push_back(service);
483 resolvers.push_back(service->GetHost());
484 resolvers.push_back(IcingaApplication::GetInstance());
486 Value value = custom->Get("icon_image");
488 return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd);
491 Value ServicesTable::IconImageAltAccessor(const Value& row)
493 Service::Ptr service = static_cast<Service::Ptr>(row);
498 Dictionary::Ptr custom = service->GetCustom();
503 return custom->Get("icon_image_alt");
506 Value ServicesTable::MaxCheckAttemptsAccessor(const Value& row)
508 Service::Ptr service = static_cast<Service::Ptr>(row);
513 return service->GetMaxCheckAttempts();
516 Value ServicesTable::CurrentAttemptAccessor(const Value& row)
518 Service::Ptr service = static_cast<Service::Ptr>(row);
523 return service->GetCheckAttempt();
526 Value ServicesTable::StateAccessor(const Value& row)
528 Service::Ptr service = static_cast<Service::Ptr>(row);
533 return service->GetState();
536 Value ServicesTable::HasBeenCheckedAccessor(const Value& row)
538 Service::Ptr service = static_cast<Service::Ptr>(row);
543 return (service->HasBeenChecked() ? 1 : 0);
546 Value ServicesTable::LastStateAccessor(const Value& row)
548 Service::Ptr service = static_cast<Service::Ptr>(row);
553 return service->GetLastState();
556 Value ServicesTable::LastHardStateAccessor(const Value& row)
558 Service::Ptr service = static_cast<Service::Ptr>(row);
563 return service->GetLastHardState();
566 Value ServicesTable::StateTypeAccessor(const Value& row)
568 Service::Ptr service = static_cast<Service::Ptr>(row);
573 return service->GetStateType();
576 Value ServicesTable::CheckTypeAccessor(const Value& row)
578 Service::Ptr service = static_cast<Service::Ptr>(row);
583 return (service->GetEnableActiveChecks() ? 0 : 1);
586 Value ServicesTable::AcknowledgedAccessor(const Value& row)
588 Service::Ptr service = static_cast<Service::Ptr>(row);
593 /* important: lock acknowledgements */
594 ObjectLock olock(service);
596 return (service->IsAcknowledged() ? 1 : 0);
599 Value ServicesTable::AcknowledgementTypeAccessor(const Value& row)
601 Service::Ptr service = static_cast<Service::Ptr>(row);
606 /* important: lock acknowledgements */
607 ObjectLock olock(service);
609 return static_cast<int>(service->GetAcknowledgement());
612 Value ServicesTable::NoMoreNotificationsAccessor(const Value& row)
614 Service::Ptr service = static_cast<Service::Ptr>(row);
619 /* XXX take the smallest notification_interval */
620 double notification_interval = -1;
621 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
622 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
623 notification_interval = notification->GetNotificationInterval();
626 if (notification_interval == 0 && !service->GetVolatile())
632 Value ServicesTable::LastTimeOkAccessor(const Value& row)
634 Service::Ptr service = static_cast<Service::Ptr>(row);
639 return static_cast<int>(service->GetLastStateOK());
642 Value ServicesTable::LastTimeWarningAccessor(const Value& row)
644 Service::Ptr service = static_cast<Service::Ptr>(row);
649 return static_cast<int>(service->GetLastStateWarning());
652 Value ServicesTable::LastTimeCriticalAccessor(const Value& row)
654 Service::Ptr service = static_cast<Service::Ptr>(row);
659 return static_cast<int>(service->GetLastStateCritical());
662 Value ServicesTable::LastTimeUnknownAccessor(const Value& row)
664 Service::Ptr service = static_cast<Service::Ptr>(row);
669 return static_cast<int>(service->GetLastStateUnknown());
672 Value ServicesTable::LastCheckAccessor(const Value& row)
674 Service::Ptr service = static_cast<Service::Ptr>(row);
679 return static_cast<int>(service->GetLastCheck());
682 Value ServicesTable::NextCheckAccessor(const Value& row)
684 Service::Ptr service = static_cast<Service::Ptr>(row);
689 return static_cast<int>(service->GetNextCheck());
692 Value ServicesTable::LastNotificationAccessor(const Value& row)
694 Service::Ptr service = static_cast<Service::Ptr>(row);
699 /* XXX Service -> Notifications, latest wins */
700 double last_notification = 0;
701 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
702 if (notification->GetLastNotification() > last_notification)
703 last_notification = notification->GetLastNotification();
706 return static_cast<int>(last_notification);
709 Value ServicesTable::NextNotificationAccessor(const Value& row)
711 Service::Ptr service = static_cast<Service::Ptr>(row);
716 /* XXX Service -> Notifications, latest wins */
717 double next_notification = 0;
718 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
719 if (notification->GetNextNotification() < next_notification)
720 next_notification = notification->GetNextNotification();
723 return static_cast<int>(next_notification);
726 Value ServicesTable::CurrentNotificationNumberAccessor(const Value& row)
728 Service::Ptr service = static_cast<Service::Ptr>(row);
733 /* XXX Service -> Notifications, biggest wins */
734 int notification_number = 0;
735 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
736 if (notification->GetNotificationNumber() > notification_number)
737 notification_number = notification->GetNotificationNumber();
740 return notification_number;
743 Value ServicesTable::LastStateChangeAccessor(const Value& row)
745 Service::Ptr service = static_cast<Service::Ptr>(row);
750 return static_cast<int>(service->GetLastStateChange());
753 Value ServicesTable::LastHardStateChangeAccessor(const Value& row)
755 Service::Ptr service = static_cast<Service::Ptr>(row);
760 return static_cast<int>(service->GetLastHardStateChange());
763 Value ServicesTable::ScheduledDowntimeDepthAccessor(const Value& row)
765 Service::Ptr service = static_cast<Service::Ptr>(row);
770 return service->GetDowntimeDepth();
773 Value ServicesTable::IsFlappingAccessor(const Value& row)
775 Service::Ptr service = static_cast<Service::Ptr>(row);
780 return service->IsFlapping();
783 Value ServicesTable::ChecksEnabledAccessor(const Value& row)
785 Service::Ptr service = static_cast<Service::Ptr>(row);
790 return (service->GetEnableActiveChecks() ? 1 : 0);
793 Value ServicesTable::AcceptPassiveChecksAccessor(const Value& row)
795 Service::Ptr service = static_cast<Service::Ptr>(row);
800 return (service->GetEnablePassiveChecks() ? 1 : 0);
803 Value ServicesTable::EventHandlerEnabledAccessor(const Value& row)
805 Service::Ptr service = static_cast<Service::Ptr>(row);
810 EventCommand::Ptr eventcommand = service->GetEventCommand();
818 Value ServicesTable::NotificationsEnabledAccessor(const Value& row)
820 Service::Ptr service = static_cast<Service::Ptr>(row);
825 return (service->GetEnableNotifications() ? 1 : 0);
828 Value ServicesTable::ActiveChecksEnabledAccessor(const Value& row)
830 Service::Ptr service = static_cast<Service::Ptr>(row);
835 return (service->GetEnableActiveChecks() ? 1 : 0);
838 Value ServicesTable::CheckOptionsAccessor(const Value& row)
840 /* TODO - forcexec, freshness, orphan, none */
844 Value ServicesTable::FlapDetectionEnabledAccessor(const Value& row)
846 Service::Ptr service = static_cast<Service::Ptr>(row);
851 return (service->GetEnableFlapping() ? 1 : 0);
854 Value ServicesTable::CheckFreshnessAccessor(const Value& row)
860 Value ServicesTable::ModifiedAttributesAccessor(const Value& row)
862 Service::Ptr service = static_cast<Service::Ptr>(row);
867 return service->GetModifiedAttributes();
870 Value ServicesTable::ModifiedAttributesListAccessor(const Value& row)
876 Value ServicesTable::StalenessAccessor(const Value& row)
878 Service::Ptr service = static_cast<Service::Ptr>(row);
883 if (service->HasBeenChecked() && service->GetLastCheck() > 0)
884 return (Utility::GetTime() - service->GetLastCheck()) / (service->GetCheckInterval() * 3600);
889 Value ServicesTable::CheckIntervalAccessor(const Value& row)
891 Service::Ptr service = static_cast<Service::Ptr>(row);
896 return (service->GetCheckInterval() / 60.0);
899 Value ServicesTable::RetryIntervalAccessor(const Value& row)
901 Service::Ptr service = static_cast<Service::Ptr>(row);
906 return (service->GetRetryInterval() / 60.0);
909 Value ServicesTable::NotificationIntervalAccessor(const Value& row)
911 Service::Ptr service = static_cast<Service::Ptr>(row);
916 /* XXX take the smallest notification_interval */
917 double notification_interval = -1;
918 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
919 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
920 notification_interval = notification->GetNotificationInterval();
923 if (notification_interval == -1)
924 notification_interval = 60;
926 return (notification_interval / 60.0);
929 Value ServicesTable::LowFlapThresholdAccessor(const Value& row)
931 Service::Ptr service = static_cast<Service::Ptr>(row);
936 return service->GetFlappingThreshold();
939 Value ServicesTable::HighFlapThresholdAccessor(const Value& row)
941 Service::Ptr service = static_cast<Service::Ptr>(row);
946 return service->GetFlappingThreshold();
949 Value ServicesTable::LatencyAccessor(const Value& row)
951 Service::Ptr service = static_cast<Service::Ptr>(row);
956 return (Service::CalculateLatency(service->GetLastCheckResult()));
959 Value ServicesTable::ExecutionTimeAccessor(const Value& row)
961 Service::Ptr service = static_cast<Service::Ptr>(row);
966 return (Service::CalculateExecutionTime(service->GetLastCheckResult()));
969 Value ServicesTable::PercentStateChangeAccessor(const Value& row)
971 Service::Ptr service = static_cast<Service::Ptr>(row);
976 return service->GetFlappingCurrent();
979 Value ServicesTable::InCheckPeriodAccessor(const Value& row)
981 Service::Ptr service = static_cast<Service::Ptr>(row);
986 TimePeriod::Ptr timeperiod = service->GetCheckPeriod();
988 /* none set means always checked */
992 return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
995 Value ServicesTable::InNotificationPeriodAccessor(const Value& row)
997 Service::Ptr service = static_cast<Service::Ptr>(row);
1002 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
1003 ObjectLock olock(notification);
1005 TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod();
1007 /* XXX first notification wins */
1009 return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
1012 /* none set means always notified */
1016 Value ServicesTable::ContactsAccessor(const Value& row)
1018 Service::Ptr service = static_cast<Service::Ptr>(row);
1023 Array::Ptr contact_names = make_shared<Array>();
1025 BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetServiceNotificationUsers(service)) {
1026 contact_names->Add(user->GetName());
1029 return contact_names;
1032 Value ServicesTable::DowntimesAccessor(const Value& row)
1034 Service::Ptr service = static_cast<Service::Ptr>(row);
1039 Dictionary::Ptr downtimes = service->GetDowntimes();
1041 Array::Ptr ids = make_shared<Array>();
1043 ObjectLock olock(downtimes);
1046 Downtime::Ptr downtime;
1047 BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
1052 if (downtime->IsExpired())
1055 ids->Add(downtime->GetLegacyId());
1061 Value ServicesTable::DowntimesWithInfoAccessor(const Value& row)
1063 Service::Ptr service = static_cast<Service::Ptr>(row);
1068 Dictionary::Ptr downtimes = service->GetDowntimes();
1070 Array::Ptr ids = make_shared<Array>();
1072 ObjectLock olock(downtimes);
1075 Downtime::Ptr downtime;
1076 BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
1081 if (downtime->IsExpired())
1084 Array::Ptr downtime_info = make_shared<Array>();
1085 downtime_info->Add(downtime->GetLegacyId());
1086 downtime_info->Add(downtime->GetAuthor());
1087 downtime_info->Add(downtime->GetComment());
1088 ids->Add(downtime_info);
1094 Value ServicesTable::CommentsAccessor(const Value& row)
1096 Service::Ptr service = static_cast<Service::Ptr>(row);
1101 Dictionary::Ptr comments = service->GetComments();
1103 Array::Ptr ids = make_shared<Array>();
1105 ObjectLock olock(comments);
1108 Comment::Ptr comment;
1109 BOOST_FOREACH(boost::tie(id, comment), comments) {
1114 if (comment->IsExpired())
1117 ids->Add(comment->GetLegacyId());
1123 Value ServicesTable::CommentsWithInfoAccessor(const Value& row)
1125 Service::Ptr service = static_cast<Service::Ptr>(row);
1130 Dictionary::Ptr comments = service->GetComments();
1132 Array::Ptr ids = make_shared<Array>();
1134 ObjectLock olock(comments);
1137 Comment::Ptr comment;
1138 BOOST_FOREACH(boost::tie(id, comment), comments) {
1143 if (comment->IsExpired())
1146 Array::Ptr comment_info = make_shared<Array>();
1147 comment_info->Add(comment->GetLegacyId());
1148 comment_info->Add(comment->GetAuthor());
1149 comment_info->Add(comment->GetText());
1150 ids->Add(comment_info);
1156 Value ServicesTable::CommentsWithExtraInfoAccessor(const Value& row)
1158 Service::Ptr service = static_cast<Service::Ptr>(row);
1163 Dictionary::Ptr comments = service->GetComments();
1165 Array::Ptr ids = make_shared<Array>();
1167 ObjectLock olock(comments);
1170 Comment::Ptr comment;
1171 BOOST_FOREACH(boost::tie(id, comment), comments) {
1176 if (comment->IsExpired())
1179 Array::Ptr comment_info = make_shared<Array>();
1180 comment_info->Add(comment->GetLegacyId());
1181 comment_info->Add(comment->GetAuthor());
1182 comment_info->Add(comment->GetText());
1183 comment_info->Add(comment->GetEntryType());
1184 comment_info->Add(static_cast<int>(comment->GetEntryTime()));
1185 ids->Add(comment_info);
1191 Value ServicesTable::CustomVariableNamesAccessor(const Value& row)
1193 Service::Ptr service = static_cast<Service::Ptr>(row);
1198 Dictionary::Ptr customvars;
1201 ObjectLock olock(service);
1202 customvars = CompatUtility::GetCustomVariableConfig(service);
1208 Array::Ptr cv = make_shared<Array>();
1212 BOOST_FOREACH(boost::tie(key, value), customvars) {
1219 Value ServicesTable::CustomVariableValuesAccessor(const Value& row)
1221 Service::Ptr service = static_cast<Service::Ptr>(row);
1226 Dictionary::Ptr customvars;
1229 ObjectLock olock(service);
1230 customvars = CompatUtility::GetCustomVariableConfig(service);
1236 Array::Ptr cv = make_shared<Array>();
1240 BOOST_FOREACH(boost::tie(key, value), customvars) {
1247 Value ServicesTable::CustomVariablesAccessor(const Value& row)
1249 Service::Ptr service = static_cast<Service::Ptr>(row);
1254 Dictionary::Ptr customvars;
1257 ObjectLock olock(service);
1258 customvars = CompatUtility::GetCustomVariableConfig(service);
1264 Array::Ptr cv = make_shared<Array>();
1268 BOOST_FOREACH(boost::tie(key, value), customvars) {
1269 Array::Ptr key_val = make_shared<Array>();
1271 key_val->Add(value);
1278 Value ServicesTable::GroupsAccessor(const Value& row)
1280 Service::Ptr service = static_cast<Service::Ptr>(row);
1285 Array::Ptr groups = service->GetGroups();
1293 Value ServicesTable::ContactGroupsAccessor(const Value& row)
1295 Service::Ptr service = static_cast<Service::Ptr>(row);
1300 Array::Ptr contactgroup_names = make_shared<Array>();
1302 BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetServiceNotificationUserGroups(service)) {
1303 contactgroup_names->Add(usergroup->GetName());
1306 return contactgroup_names;