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;
38 using namespace livestatus;
40 ServicesTable::ServicesTable(void)
45 void ServicesTable::AddColumns(Table *table, const String& prefix,
46 const Column::ObjectAccessor& objectAccessor)
48 table->AddColumn(prefix + "description", Column(&ServicesTable::ShortNameAccessor, objectAccessor));
49 table->AddColumn(prefix + "display_name", Column(&ServicesTable::DisplayNameAccessor, objectAccessor));
50 table->AddColumn(prefix + "check_command", Column(&ServicesTable::CheckCommandAccessor, objectAccessor));
51 table->AddColumn(prefix + "check_command_expanded", Column(&ServicesTable::CheckCommandExpandedAccessor, objectAccessor));
52 table->AddColumn(prefix + "event_handler", Column(&ServicesTable::EventHandlerAccessor, objectAccessor));
53 table->AddColumn(prefix + "plugin_output", Column(&ServicesTable::PluginOutputAccessor, objectAccessor));
54 table->AddColumn(prefix + "long_plugin_output", Column(&ServicesTable::LongPluginOutputAccessor, objectAccessor));
55 table->AddColumn(prefix + "perf_data", Column(&ServicesTable::PerfDataAccessor, objectAccessor));
56 table->AddColumn(prefix + "notification_period", Column(&ServicesTable::NotificationPeriodAccessor, objectAccessor));
57 table->AddColumn(prefix + "check_period", Column(&ServicesTable::CheckPeriodAccessor, objectAccessor));
58 table->AddColumn(prefix + "notes", Column(&ServicesTable::NotesAccessor, objectAccessor));
59 table->AddColumn(prefix + "notes_expanded", Column(&ServicesTable::NotesExpandedAccessor, objectAccessor));
60 table->AddColumn(prefix + "notes_url", Column(&ServicesTable::NotesUrlAccessor, objectAccessor));
61 table->AddColumn(prefix + "notes_url_expanded", Column(&ServicesTable::NotesUrlExpandedAccessor, objectAccessor));
62 table->AddColumn(prefix + "action_url", Column(&ServicesTable::ActionUrlAccessor, objectAccessor));
63 table->AddColumn(prefix + "action_url_expanded", Column(&ServicesTable::ActionUrlExpandedAccessor, objectAccessor));
64 table->AddColumn(prefix + "icon_image", Column(&ServicesTable::IconImageAccessor, objectAccessor));
65 table->AddColumn(prefix + "icon_image_expanded", Column(&ServicesTable::IconImageExpandedAccessor, objectAccessor));
66 table->AddColumn(prefix + "icon_image_alt", Column(&ServicesTable::IconImageAltAccessor, objectAccessor));
67 table->AddColumn(prefix + "initial_state", Column(&Table::EmptyStringAccessor, objectAccessor));
68 table->AddColumn(prefix + "max_check_attempts", Column(&ServicesTable::MaxCheckAttemptsAccessor, objectAccessor));
69 table->AddColumn(prefix + "current_attempt", Column(&ServicesTable::CurrentAttemptAccessor, objectAccessor));
70 table->AddColumn(prefix + "state", Column(&ServicesTable::StateAccessor, objectAccessor));
71 table->AddColumn(prefix + "has_been_checked", Column(&ServicesTable::HasBeenCheckedAccessor, objectAccessor));
72 table->AddColumn(prefix + "last_state", Column(&ServicesTable::LastStateAccessor, objectAccessor));
73 table->AddColumn(prefix + "last_hard_state", Column(&ServicesTable::LastHardStateAccessor, objectAccessor));
74 table->AddColumn(prefix + "state_type", Column(&ServicesTable::StateTypeAccessor, objectAccessor));
75 table->AddColumn(prefix + "check_type", Column(&ServicesTable::CheckTypeAccessor, objectAccessor));
76 table->AddColumn(prefix + "acknowledged", Column(&ServicesTable::AcknowledgedAccessor, objectAccessor));
77 table->AddColumn(prefix + "acknowledgement_type", Column(&ServicesTable::AcknowledgementTypeAccessor, objectAccessor));
78 table->AddColumn(prefix + "no_more_notifications", Column(&ServicesTable::NoMoreNotificationsAccessor, objectAccessor));
79 table->AddColumn(prefix + "last_time_ok", Column(&ServicesTable::LastTimeOkAccessor, objectAccessor));
80 table->AddColumn(prefix + "last_time_warning", Column(&ServicesTable::LastTimeWarningAccessor, objectAccessor));
81 table->AddColumn(prefix + "last_time_critical", Column(&ServicesTable::LastTimeCriticalAccessor, objectAccessor));
82 table->AddColumn(prefix + "last_time_unknown", Column(&ServicesTable::LastTimeUnknownAccessor, objectAccessor));
83 table->AddColumn(prefix + "last_check", Column(&ServicesTable::LastCheckAccessor, objectAccessor));
84 table->AddColumn(prefix + "next_check", Column(&ServicesTable::NextCheckAccessor, objectAccessor));
85 table->AddColumn(prefix + "last_notification", Column(&ServicesTable::LastNotificationAccessor, objectAccessor));
86 table->AddColumn(prefix + "next_notification", Column(&ServicesTable::NextNotificationAccessor, objectAccessor));
87 table->AddColumn(prefix + "current_notification_number", Column(&ServicesTable::CurrentNotificationNumberAccessor, objectAccessor));
88 table->AddColumn(prefix + "last_state_change", Column(&ServicesTable::LastStateChangeAccessor, objectAccessor));
89 table->AddColumn(prefix + "last_hard_state_change", Column(&ServicesTable::LastHardStateChangeAccessor, objectAccessor));
90 table->AddColumn(prefix + "scheduled_downtime_depth", Column(&ServicesTable::ScheduledDowntimeDepthAccessor, objectAccessor));
91 table->AddColumn(prefix + "is_flapping", Column(&ServicesTable::IsFlappingAccessor, objectAccessor));
92 table->AddColumn(prefix + "checks_enabled", Column(&ServicesTable::ChecksEnabledAccessor, objectAccessor));
93 table->AddColumn(prefix + "accept_passive_checks", Column(&ServicesTable::AcceptPassiveChecksAccessor, objectAccessor));
94 table->AddColumn(prefix + "event_handler_enabled", Column(&ServicesTable::EventHandlerEnabledAccessor, objectAccessor));
95 table->AddColumn(prefix + "notifications_enabled", Column(&ServicesTable::NotificationsEnabledAccessor, objectAccessor));
96 table->AddColumn(prefix + "process_performance_data", Column(&Table::OneAccessor, objectAccessor));
97 table->AddColumn(prefix + "is_executing", Column(&Table::ZeroAccessor, objectAccessor));
98 table->AddColumn(prefix + "active_checks_enabled", Column(&ServicesTable::ActiveChecksEnabledAccessor, objectAccessor));
99 table->AddColumn(prefix + "check_options", Column(&ServicesTable::CheckOptionsAccessor, objectAccessor));
100 table->AddColumn(prefix + "flap_detection_enabled", Column(&ServicesTable::FlapDetectionEnabledAccessor, objectAccessor));
101 table->AddColumn(prefix + "check_freshness", Column(&ServicesTable::CheckFreshnessAccessor, objectAccessor));
102 table->AddColumn(prefix + "obsess_over_service", Column(&Table::ZeroAccessor, objectAccessor));
103 table->AddColumn(prefix + "modified_attributes", Column(&ServicesTable::ModifiedAttributesAccessor, objectAccessor));
104 table->AddColumn(prefix + "modified_attributes_list", Column(&ServicesTable::ModifiedAttributesListAccessor, objectAccessor));
105 table->AddColumn(prefix + "pnpgraph_present", Column(&Table::ZeroAccessor, objectAccessor));
106 table->AddColumn(prefix + "staleness", Column(&ServicesTable::StalenessAccessor, objectAccessor));
107 table->AddColumn(prefix + "check_interval", Column(&ServicesTable::CheckIntervalAccessor, objectAccessor));
108 table->AddColumn(prefix + "retry_interval", Column(&ServicesTable::RetryIntervalAccessor, objectAccessor));
109 table->AddColumn(prefix + "notification_interval", Column(&ServicesTable::NotificationIntervalAccessor, objectAccessor));
110 table->AddColumn(prefix + "first_notification_delay", Column(&Table::EmptyStringAccessor, objectAccessor));
111 table->AddColumn(prefix + "low_flap_threshold", Column(&ServicesTable::LowFlapThresholdAccessor, objectAccessor));
112 table->AddColumn(prefix + "high_flap_threshold", Column(&ServicesTable::HighFlapThresholdAccessor, objectAccessor));
113 table->AddColumn(prefix + "latency", Column(&ServicesTable::LatencyAccessor, objectAccessor));
114 table->AddColumn(prefix + "execution_time", Column(&ServicesTable::ExecutionTimeAccessor, objectAccessor));
115 table->AddColumn(prefix + "percent_state_change", Column(&ServicesTable::PercentStateChangeAccessor, objectAccessor));
116 table->AddColumn(prefix + "in_check_period", Column(&ServicesTable::InCheckPeriodAccessor, objectAccessor));
117 table->AddColumn(prefix + "in_notification_period", Column(&ServicesTable::InNotificationPeriodAccessor, objectAccessor));
118 table->AddColumn(prefix + "contacts", Column(&ServicesTable::ContactsAccessor, objectAccessor));
119 table->AddColumn(prefix + "downtimes", Column(&ServicesTable::DowntimesAccessor, objectAccessor));
120 table->AddColumn(prefix + "downtimes_with_info", Column(&ServicesTable::DowntimesWithInfoAccessor, objectAccessor));
121 table->AddColumn(prefix + "comments", Column(&ServicesTable::CommentsAccessor, objectAccessor));
122 table->AddColumn(prefix + "comments_with_info", Column(&ServicesTable::CommentsWithInfoAccessor, objectAccessor));
123 table->AddColumn(prefix + "comments_with_extra_info", Column(&ServicesTable::CommentsWithExtraInfoAccessor, objectAccessor));
124 table->AddColumn(prefix + "custom_variable_names", Column(&ServicesTable::CustomVariableNamesAccessor, objectAccessor));
125 table->AddColumn(prefix + "custom_variable_values", Column(&ServicesTable::CustomVariableValuesAccessor, objectAccessor));
126 table->AddColumn(prefix + "custom_variables", Column(&ServicesTable::CustomVariablesAccessor, objectAccessor));
127 table->AddColumn(prefix + "groups", Column(&ServicesTable::GroupsAccessor, objectAccessor));
128 table->AddColumn(prefix + "contact_groups", Column(&ServicesTable::ContactGroupsAccessor, objectAccessor));
130 HostsTable::AddColumns(table, "host_", boost::bind(&ServicesTable::HostAccessor, _1, objectAccessor));
133 String ServicesTable::GetName(void) const
138 void ServicesTable::FetchRows(const AddRowFunction& addRowFn)
140 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
145 Object::Ptr ServicesTable::HostAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor)
149 if (parentObjectAccessor)
150 service = parentObjectAccessor(row);
154 Service::Ptr svc = static_cast<Service::Ptr>(service);
157 return Object::Ptr();
159 return svc->GetHost();
162 Value ServicesTable::ShortNameAccessor(const Value& row)
164 Service::Ptr service = static_cast<Service::Ptr>(row);
169 return service->GetShortName();
172 Value ServicesTable::DisplayNameAccessor(const Value& row)
174 Service::Ptr service = static_cast<Service::Ptr>(row);
179 return service->GetDisplayName();
182 Value ServicesTable::CheckCommandAccessor(const Value& row)
184 Service::Ptr service = static_cast<Service::Ptr>(row);
189 CheckCommand::Ptr checkcommand = service->GetCheckCommand();
192 return checkcommand->GetName(); /* this is the name without '!' args */
197 Value ServicesTable::CheckCommandExpandedAccessor(const Value& row)
199 Service::Ptr service = static_cast<Service::Ptr>(row);
204 CheckCommand::Ptr commandObj = service->GetCheckCommand();
209 Value raw_command = commandObj->GetCommandLine();
211 std::vector<MacroResolver::Ptr> resolvers;
212 resolvers.push_back(service);
213 resolvers.push_back(service->GetHost());
214 resolvers.push_back(commandObj);
215 resolvers.push_back(IcingaApplication::GetInstance());
217 Value commandLine = MacroProcessor::ResolveMacros(raw_command, resolvers, Dictionary::Ptr(), Utility::EscapeShellCmd);
220 if (commandLine.IsObjectType<Array>()) {
221 Array::Ptr args = commandLine;
223 ObjectLock olock(args);
225 BOOST_FOREACH(arg, args) {
226 // This is obviously incorrect for non-trivial cases.
227 String argitem = " \"" + arg + "\"";
228 boost::algorithm::replace_all(argitem, "\n", "\\n");
231 } else if (!commandLine.IsEmpty()) {
232 String args = Convert::ToString(commandLine);
233 boost::algorithm::replace_all(args, "\n", "\\n");
242 Value ServicesTable::EventHandlerAccessor(const Value& row)
244 Service::Ptr service = static_cast<Service::Ptr>(row);
249 EventCommand::Ptr eventcommand = service->GetEventCommand();
252 return eventcommand->GetName();
257 Value ServicesTable::PluginOutputAccessor(const Value& row)
259 Service::Ptr service = static_cast<Service::Ptr>(row);
264 return service->GetLastCheckOutput();
267 Value ServicesTable::LongPluginOutputAccessor(const Value& row)
269 Service::Ptr service = static_cast<Service::Ptr>(row);
274 return service->GetLastCheckLongOutput();
277 Value ServicesTable::PerfDataAccessor(const Value& row)
279 Service::Ptr service = static_cast<Service::Ptr>(row);
284 return service->GetLastCheckPerfData();
287 Value ServicesTable::NotificationPeriodAccessor(const Value& row)
289 Service::Ptr service = static_cast<Service::Ptr>(row);
294 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
295 ObjectLock olock(notification);
297 TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod();
299 /* XXX first notification wins */
301 return timeperiod->GetName();
307 Value ServicesTable::CheckPeriodAccessor(const Value& row)
309 Service::Ptr service = static_cast<Service::Ptr>(row);
314 TimePeriod::Ptr timeperiod = service->GetCheckPeriod();
319 return timeperiod->GetName();
322 Value ServicesTable::NotesAccessor(const Value& row)
324 Service::Ptr service = static_cast<Service::Ptr>(row);
329 Dictionary::Ptr custom = service->GetCustom();
334 return custom->Get("notes");
337 Value ServicesTable::NotesExpandedAccessor(const Value& row)
339 Service::Ptr service = static_cast<Service::Ptr>(row);
344 Dictionary::Ptr custom = service->GetCustom();
349 std::vector<MacroResolver::Ptr> resolvers;
350 resolvers.push_back(service);
351 resolvers.push_back(service->GetHost());
352 resolvers.push_back(IcingaApplication::GetInstance());
354 Value value = custom->Get("notes");
357 Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
359 return value_expanded;
362 Value ServicesTable::NotesUrlAccessor(const Value& row)
364 Service::Ptr service = static_cast<Service::Ptr>(row);
369 Dictionary::Ptr custom = service->GetCustom();
374 return custom->Get("notes_url");
377 Value ServicesTable::NotesUrlExpandedAccessor(const Value& row)
379 Service::Ptr service = static_cast<Service::Ptr>(row);
384 Dictionary::Ptr custom = service->GetCustom();
389 std::vector<MacroResolver::Ptr> resolvers;
390 resolvers.push_back(service);
391 resolvers.push_back(service->GetHost());
392 resolvers.push_back(IcingaApplication::GetInstance());
394 Value value = custom->Get("notes_url");
397 Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
399 return value_expanded;
402 Value ServicesTable::ActionUrlAccessor(const Value& row)
404 Service::Ptr service = static_cast<Service::Ptr>(row);
409 Dictionary::Ptr custom = service->GetCustom();
414 return custom->Get("action_url");
417 Value ServicesTable::ActionUrlExpandedAccessor(const Value& row)
419 Service::Ptr service = static_cast<Service::Ptr>(row);
424 Dictionary::Ptr custom = service->GetCustom();
429 std::vector<MacroResolver::Ptr> resolvers;
430 resolvers.push_back(service);
431 resolvers.push_back(service->GetHost());
432 resolvers.push_back(IcingaApplication::GetInstance());
434 Value value = custom->Get("action_url");
437 Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
439 return value_expanded;
442 Value ServicesTable::IconImageAccessor(const Value& row)
444 Service::Ptr service = static_cast<Service::Ptr>(row);
449 Dictionary::Ptr custom = service->GetCustom();
454 return custom->Get("icon_image");
457 Value ServicesTable::IconImageExpandedAccessor(const Value& row)
459 Service::Ptr service = static_cast<Service::Ptr>(row);
464 Dictionary::Ptr custom = service->GetCustom();
469 std::vector<MacroResolver::Ptr> resolvers;
470 resolvers.push_back(service);
471 resolvers.push_back(service->GetHost());
472 resolvers.push_back(IcingaApplication::GetInstance());
474 Value value = custom->Get("icon_image");
477 Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
479 return value_expanded;
482 Value ServicesTable::IconImageAltAccessor(const Value& row)
484 Service::Ptr service = static_cast<Service::Ptr>(row);
489 Dictionary::Ptr custom = service->GetCustom();
494 return custom->Get("icon_image_alt");
497 Value ServicesTable::MaxCheckAttemptsAccessor(const Value& row)
499 Service::Ptr service = static_cast<Service::Ptr>(row);
504 return service->GetMaxCheckAttempts();
507 Value ServicesTable::CurrentAttemptAccessor(const Value& row)
509 Service::Ptr service = static_cast<Service::Ptr>(row);
514 return service->GetCheckAttempt();
517 Value ServicesTable::StateAccessor(const Value& row)
519 Service::Ptr service = static_cast<Service::Ptr>(row);
524 return service->GetState();
527 Value ServicesTable::HasBeenCheckedAccessor(const Value& row)
529 Service::Ptr service = static_cast<Service::Ptr>(row);
534 return (service->HasBeenChecked() ? 1 : 0);
537 Value ServicesTable::LastStateAccessor(const Value& row)
539 Service::Ptr service = static_cast<Service::Ptr>(row);
544 return service->GetLastState();
547 Value ServicesTable::LastHardStateAccessor(const Value& row)
549 Service::Ptr service = static_cast<Service::Ptr>(row);
554 return service->GetLastHardState();
557 Value ServicesTable::StateTypeAccessor(const Value& row)
559 Service::Ptr service = static_cast<Service::Ptr>(row);
564 return service->GetStateType();
567 Value ServicesTable::CheckTypeAccessor(const Value& row)
569 Service::Ptr service = static_cast<Service::Ptr>(row);
574 return (service->GetEnableActiveChecks() ? 0 : 1);
577 Value ServicesTable::AcknowledgedAccessor(const Value& row)
579 Service::Ptr service = static_cast<Service::Ptr>(row);
584 /* important: lock acknowledgements */
585 ObjectLock olock(service);
587 return (service->IsAcknowledged() ? 1 : 0);
590 Value ServicesTable::AcknowledgementTypeAccessor(const Value& row)
592 Service::Ptr service = static_cast<Service::Ptr>(row);
597 /* important: lock acknowledgements */
598 ObjectLock olock(service);
600 return static_cast<int>(service->GetAcknowledgement());
603 Value ServicesTable::NoMoreNotificationsAccessor(const Value& row)
605 Service::Ptr service = static_cast<Service::Ptr>(row);
610 /* XXX take the smallest notification_interval */
611 double notification_interval = -1;
612 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
613 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
614 notification_interval = notification->GetNotificationInterval();
617 if (notification_interval == 0 && !service->GetVolatile())
623 Value ServicesTable::LastTimeOkAccessor(const Value& row)
625 Service::Ptr service = static_cast<Service::Ptr>(row);
630 return static_cast<int>(service->GetLastStateOK());
633 Value ServicesTable::LastTimeWarningAccessor(const Value& row)
635 Service::Ptr service = static_cast<Service::Ptr>(row);
640 return static_cast<int>(service->GetLastStateWarning());
643 Value ServicesTable::LastTimeCriticalAccessor(const Value& row)
645 Service::Ptr service = static_cast<Service::Ptr>(row);
650 return static_cast<int>(service->GetLastStateCritical());
653 Value ServicesTable::LastTimeUnknownAccessor(const Value& row)
655 Service::Ptr service = static_cast<Service::Ptr>(row);
660 return static_cast<int>(service->GetLastStateUnknown());
663 Value ServicesTable::LastCheckAccessor(const Value& row)
665 Service::Ptr service = static_cast<Service::Ptr>(row);
670 return static_cast<int>(service->GetLastCheck());
673 Value ServicesTable::NextCheckAccessor(const Value& row)
675 Service::Ptr service = static_cast<Service::Ptr>(row);
680 return static_cast<int>(service->GetNextCheck());
683 Value ServicesTable::LastNotificationAccessor(const Value& row)
685 Service::Ptr service = static_cast<Service::Ptr>(row);
690 /* XXX Service -> Notifications, latest wins */
691 double last_notification = 0;
692 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
693 if (notification->GetLastNotification() > last_notification)
694 last_notification = notification->GetLastNotification();
697 return static_cast<int>(last_notification);
700 Value ServicesTable::NextNotificationAccessor(const Value& row)
702 Service::Ptr service = static_cast<Service::Ptr>(row);
707 /* XXX Service -> Notifications, latest wins */
708 double next_notification = 0;
709 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
710 if (notification->GetNextNotification() < next_notification)
711 next_notification = notification->GetNextNotification();
714 return static_cast<int>(next_notification);
717 Value ServicesTable::CurrentNotificationNumberAccessor(const Value& row)
719 Service::Ptr service = static_cast<Service::Ptr>(row);
724 /* XXX Service -> Notifications, biggest wins */
725 int notification_number = 0;
726 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
727 if (notification->GetNotificationNumber() > notification_number)
728 notification_number = notification->GetNotificationNumber();
731 return notification_number;
734 Value ServicesTable::LastStateChangeAccessor(const Value& row)
736 Service::Ptr service = static_cast<Service::Ptr>(row);
741 return static_cast<int>(service->GetLastStateChange());
744 Value ServicesTable::LastHardStateChangeAccessor(const Value& row)
746 Service::Ptr service = static_cast<Service::Ptr>(row);
751 return static_cast<int>(service->GetLastHardStateChange());
754 Value ServicesTable::ScheduledDowntimeDepthAccessor(const Value& row)
756 Service::Ptr service = static_cast<Service::Ptr>(row);
761 return service->GetDowntimeDepth();
764 Value ServicesTable::IsFlappingAccessor(const Value& row)
766 Service::Ptr service = static_cast<Service::Ptr>(row);
771 return service->IsFlapping();
774 Value ServicesTable::ChecksEnabledAccessor(const Value& row)
776 Service::Ptr service = static_cast<Service::Ptr>(row);
781 return (service->GetEnableActiveChecks() ? 1 : 0);
784 Value ServicesTable::AcceptPassiveChecksAccessor(const Value& row)
786 Service::Ptr service = static_cast<Service::Ptr>(row);
791 return (service->GetEnablePassiveChecks() ? 1 : 0);
794 Value ServicesTable::EventHandlerEnabledAccessor(const Value& row)
796 Service::Ptr service = static_cast<Service::Ptr>(row);
801 EventCommand::Ptr eventcommand = service->GetEventCommand();
809 Value ServicesTable::NotificationsEnabledAccessor(const Value& row)
811 Service::Ptr service = static_cast<Service::Ptr>(row);
816 return (service->GetEnableNotifications() ? 1 : 0);
819 Value ServicesTable::ActiveChecksEnabledAccessor(const Value& row)
821 Service::Ptr service = static_cast<Service::Ptr>(row);
826 return (service->GetEnableActiveChecks() ? 1 : 0);
829 Value ServicesTable::CheckOptionsAccessor(const Value& row)
831 /* TODO - forcexec, freshness, orphan, none */
835 Value ServicesTable::FlapDetectionEnabledAccessor(const Value& row)
837 Service::Ptr service = static_cast<Service::Ptr>(row);
842 return (service->GetEnableFlapping() ? 1 : 0);
845 Value ServicesTable::CheckFreshnessAccessor(const Value& row)
851 Value ServicesTable::ModifiedAttributesAccessor(const Value& row)
853 Service::Ptr service = static_cast<Service::Ptr>(row);
858 return service->GetModifiedAttributes();
861 Value ServicesTable::ModifiedAttributesListAccessor(const Value& row)
867 Value ServicesTable::StalenessAccessor(const Value& row)
869 Service::Ptr service = static_cast<Service::Ptr>(row);
874 if (service->HasBeenChecked() && service->GetLastCheck() > 0)
875 return (Utility::GetTime() - service->GetLastCheck()) / (service->GetCheckInterval() * 3600);
880 Value ServicesTable::CheckIntervalAccessor(const Value& row)
882 Service::Ptr service = static_cast<Service::Ptr>(row);
887 return (service->GetCheckInterval() / 60.0);
890 Value ServicesTable::RetryIntervalAccessor(const Value& row)
892 Service::Ptr service = static_cast<Service::Ptr>(row);
897 return (service->GetRetryInterval() / 60.0);
900 Value ServicesTable::NotificationIntervalAccessor(const Value& row)
902 Service::Ptr service = static_cast<Service::Ptr>(row);
907 /* XXX take the smallest notification_interval */
908 double notification_interval = -1;
909 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
910 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
911 notification_interval = notification->GetNotificationInterval();
914 if (notification_interval == -1)
915 notification_interval = 60;
917 return (notification_interval / 60.0);
920 Value ServicesTable::LowFlapThresholdAccessor(const Value& row)
922 Service::Ptr service = static_cast<Service::Ptr>(row);
927 return service->GetFlappingThreshold();
930 Value ServicesTable::HighFlapThresholdAccessor(const Value& row)
932 Service::Ptr service = static_cast<Service::Ptr>(row);
937 return service->GetFlappingThreshold();
940 Value ServicesTable::LatencyAccessor(const Value& row)
942 Service::Ptr service = static_cast<Service::Ptr>(row);
947 return (Service::CalculateLatency(service->GetLastCheckResult()));
950 Value ServicesTable::ExecutionTimeAccessor(const Value& row)
952 Service::Ptr service = static_cast<Service::Ptr>(row);
957 return (Service::CalculateExecutionTime(service->GetLastCheckResult()));
960 Value ServicesTable::PercentStateChangeAccessor(const Value& row)
962 Service::Ptr service = static_cast<Service::Ptr>(row);
967 return service->GetFlappingCurrent();
970 Value ServicesTable::InCheckPeriodAccessor(const Value& row)
972 Service::Ptr service = static_cast<Service::Ptr>(row);
977 TimePeriod::Ptr timeperiod = service->GetCheckPeriod();
979 /* none set means always checked */
983 return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
986 Value ServicesTable::InNotificationPeriodAccessor(const Value& row)
988 Service::Ptr service = static_cast<Service::Ptr>(row);
993 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
994 ObjectLock olock(notification);
996 TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod();
998 /* XXX first notification wins */
1000 return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
1003 /* none set means always notified */
1007 Value ServicesTable::ContactsAccessor(const Value& row)
1009 Service::Ptr service = static_cast<Service::Ptr>(row);
1014 Array::Ptr contact_names = boost::make_shared<Array>();
1016 BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetServiceNotificationUsers(service)) {
1017 contact_names->Add(user->GetName());
1020 return contact_names;
1023 Value ServicesTable::DowntimesAccessor(const Value& row)
1025 Service::Ptr service = static_cast<Service::Ptr>(row);
1030 Dictionary::Ptr downtimes = service->GetDowntimes();
1032 Array::Ptr ids = boost::make_shared<Array>();
1034 ObjectLock olock(downtimes);
1037 Dictionary::Ptr downtime;
1038 BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
1043 if (Service::IsDowntimeExpired(downtime))
1046 ids->Add(downtime->Get("legacy_id"));
1052 Value ServicesTable::DowntimesWithInfoAccessor(const Value& row)
1054 Service::Ptr service = static_cast<Service::Ptr>(row);
1059 Dictionary::Ptr downtimes = service->GetDowntimes();
1061 Array::Ptr ids = boost::make_shared<Array>();
1063 ObjectLock olock(downtimes);
1066 Dictionary::Ptr downtime;
1067 BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
1072 if (Service::IsDowntimeExpired(downtime))
1075 Array::Ptr downtime_info = boost::make_shared<Array>();
1076 downtime_info->Add(downtime->Get("legacy_id"));
1077 downtime_info->Add(downtime->Get("author"));
1078 downtime_info->Add(downtime->Get("comment"));
1079 ids->Add(downtime_info);
1085 Value ServicesTable::CommentsAccessor(const Value& row)
1087 Service::Ptr service = static_cast<Service::Ptr>(row);
1092 Dictionary::Ptr comments = service->GetComments();
1094 Array::Ptr ids = boost::make_shared<Array>();
1096 ObjectLock olock(comments);
1099 Dictionary::Ptr comment;
1100 BOOST_FOREACH(boost::tie(id, comment), comments) {
1105 if (Service::IsCommentExpired(comment))
1108 ids->Add(comment->Get("legacy_id"));
1114 Value ServicesTable::CommentsWithInfoAccessor(const Value& row)
1116 Service::Ptr service = static_cast<Service::Ptr>(row);
1121 Dictionary::Ptr comments = service->GetComments();
1123 Array::Ptr ids = boost::make_shared<Array>();
1125 ObjectLock olock(comments);
1128 Dictionary::Ptr comment;
1129 BOOST_FOREACH(boost::tie(id, comment), comments) {
1134 if (Service::IsCommentExpired(comment))
1137 Array::Ptr comment_info = boost::make_shared<Array>();
1138 comment_info->Add(comment->Get("legacy_id"));
1139 comment_info->Add(comment->Get("author"));
1140 comment_info->Add(comment->Get("text"));
1141 ids->Add(comment_info);
1147 Value ServicesTable::CommentsWithExtraInfoAccessor(const Value& row)
1149 Service::Ptr service = static_cast<Service::Ptr>(row);
1154 Dictionary::Ptr comments = service->GetComments();
1156 Array::Ptr ids = boost::make_shared<Array>();
1158 ObjectLock olock(comments);
1161 Dictionary::Ptr comment;
1162 BOOST_FOREACH(boost::tie(id, comment), comments) {
1167 if (Service::IsCommentExpired(comment))
1170 Array::Ptr comment_info = boost::make_shared<Array>();
1171 comment_info->Add(comment->Get("legacy_id"));
1172 comment_info->Add(comment->Get("author"));
1173 comment_info->Add(comment->Get("text"));
1174 comment_info->Add(comment->Get("entry_type"));
1175 comment_info->Add(static_cast<int>(comment->Get("entry_time")));
1176 ids->Add(comment_info);
1182 Value ServicesTable::CustomVariableNamesAccessor(const Value& row)
1184 Service::Ptr service = static_cast<Service::Ptr>(row);
1189 Dictionary::Ptr custom = service->GetCustom();
1194 Array::Ptr cv = boost::make_shared<Array>();
1196 ObjectLock olock(custom);
1199 BOOST_FOREACH(boost::tie(key, value), custom) {
1200 if (key == "notes" ||
1201 key == "action_url" ||
1202 key == "notes_url" ||
1203 key == "icon_image" ||
1204 key == "icon_image_alt" ||
1205 key == "statusmap_image" ||
1215 Value ServicesTable::CustomVariableValuesAccessor(const Value& row)
1217 Service::Ptr service = static_cast<Service::Ptr>(row);
1222 Dictionary::Ptr custom = service->GetCustom();
1227 Array::Ptr cv = boost::make_shared<Array>();
1229 ObjectLock olock(custom);
1232 BOOST_FOREACH(boost::tie(key, value), custom) {
1233 if (key == "notes" ||
1234 key == "action_url" ||
1235 key == "notes_url" ||
1236 key == "icon_image" ||
1237 key == "icon_image_alt" ||
1238 key == "statusmap_image" ||
1248 Value ServicesTable::CustomVariablesAccessor(const Value& row)
1250 Service::Ptr service = static_cast<Service::Ptr>(row);
1255 Dictionary::Ptr custom = service->GetCustom();
1260 Array::Ptr cv = boost::make_shared<Array>();
1262 ObjectLock olock(custom);
1265 BOOST_FOREACH(boost::tie(key, value), custom) {
1266 if (key == "notes" ||
1267 key == "action_url" ||
1268 key == "notes_url" ||
1269 key == "icon_image" ||
1270 key == "icon_image_alt" ||
1271 key == "statusmap_image" ||
1275 Array::Ptr key_val = boost::make_shared<Array>();
1277 key_val->Add(value);
1284 Value ServicesTable::GroupsAccessor(const Value& row)
1286 Service::Ptr service = static_cast<Service::Ptr>(row);
1291 Array::Ptr groups = service->GetGroups();
1299 Value ServicesTable::ContactGroupsAccessor(const Value& row)
1301 Service::Ptr service = static_cast<Service::Ptr>(row);
1306 Array::Ptr contactgroup_names = boost::make_shared<Array>();
1308 BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetServiceNotificationUserGroups(service)) {
1309 contactgroup_names->Add(usergroup->GetName());
1312 return contactgroup_names;