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, CheckResult::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);
265 CheckResult::Ptr cr = service->GetLastCheckResult();
268 Dictionary::Ptr output_bag = CompatUtility::GetCheckResultOutput(cr);
269 output = output_bag->Get("output");
275 Value ServicesTable::LongPluginOutputAccessor(const Value& row)
277 Service::Ptr service = static_cast<Service::Ptr>(row);
283 CheckResult::Ptr cr = service->GetLastCheckResult();
286 Dictionary::Ptr output_bag = CompatUtility::GetCheckResultOutput(cr);
287 long_output = output_bag->Get("long_output");
293 Value ServicesTable::PerfDataAccessor(const Value& row)
295 Service::Ptr service = static_cast<Service::Ptr>(row);
301 CheckResult::Ptr cr = service->GetLastCheckResult();
304 perfdata = CompatUtility::GetCheckResultPerfdata(cr);
309 Value ServicesTable::NotificationPeriodAccessor(const Value& row)
311 Service::Ptr service = static_cast<Service::Ptr>(row);
316 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
317 ObjectLock olock(notification);
319 TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod();
321 /* XXX first notification wins */
323 return timeperiod->GetName();
329 Value ServicesTable::CheckPeriodAccessor(const Value& row)
331 Service::Ptr service = static_cast<Service::Ptr>(row);
336 TimePeriod::Ptr timeperiod = service->GetCheckPeriod();
341 return timeperiod->GetName();
344 Value ServicesTable::NotesAccessor(const Value& row)
346 Service::Ptr service = static_cast<Service::Ptr>(row);
351 Dictionary::Ptr custom = service->GetCustom();
356 return custom->Get("notes");
359 Value ServicesTable::NotesExpandedAccessor(const Value& row)
361 Service::Ptr service = static_cast<Service::Ptr>(row);
366 Dictionary::Ptr custom = service->GetCustom();
371 std::vector<MacroResolver::Ptr> resolvers;
372 resolvers.push_back(service);
373 resolvers.push_back(service->GetHost());
374 resolvers.push_back(IcingaApplication::GetInstance());
376 Value value = custom->Get("notes");
378 return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd);
381 Value ServicesTable::NotesUrlAccessor(const Value& row)
383 Service::Ptr service = static_cast<Service::Ptr>(row);
388 Dictionary::Ptr custom = service->GetCustom();
393 return custom->Get("notes_url");
396 Value ServicesTable::NotesUrlExpandedAccessor(const Value& row)
398 Service::Ptr service = static_cast<Service::Ptr>(row);
403 Dictionary::Ptr custom = service->GetCustom();
408 std::vector<MacroResolver::Ptr> resolvers;
409 resolvers.push_back(service);
410 resolvers.push_back(service->GetHost());
411 resolvers.push_back(IcingaApplication::GetInstance());
413 Value value = custom->Get("notes_url");
415 return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd);
418 Value ServicesTable::ActionUrlAccessor(const Value& row)
420 Service::Ptr service = static_cast<Service::Ptr>(row);
425 Dictionary::Ptr custom = service->GetCustom();
430 return custom->Get("action_url");
433 Value ServicesTable::ActionUrlExpandedAccessor(const Value& row)
435 Service::Ptr service = static_cast<Service::Ptr>(row);
440 Dictionary::Ptr custom = service->GetCustom();
445 std::vector<MacroResolver::Ptr> resolvers;
446 resolvers.push_back(service);
447 resolvers.push_back(service->GetHost());
448 resolvers.push_back(IcingaApplication::GetInstance());
450 Value value = custom->Get("action_url");
452 return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd);
455 Value ServicesTable::IconImageAccessor(const Value& row)
457 Service::Ptr service = static_cast<Service::Ptr>(row);
462 Dictionary::Ptr custom = service->GetCustom();
467 return custom->Get("icon_image");
470 Value ServicesTable::IconImageExpandedAccessor(const Value& row)
472 Service::Ptr service = static_cast<Service::Ptr>(row);
477 Dictionary::Ptr custom = service->GetCustom();
482 std::vector<MacroResolver::Ptr> resolvers;
483 resolvers.push_back(service);
484 resolvers.push_back(service->GetHost());
485 resolvers.push_back(IcingaApplication::GetInstance());
487 Value value = custom->Get("icon_image");
489 return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd);
492 Value ServicesTable::IconImageAltAccessor(const Value& row)
494 Service::Ptr service = static_cast<Service::Ptr>(row);
499 Dictionary::Ptr custom = service->GetCustom();
504 return custom->Get("icon_image_alt");
507 Value ServicesTable::MaxCheckAttemptsAccessor(const Value& row)
509 Service::Ptr service = static_cast<Service::Ptr>(row);
514 return service->GetMaxCheckAttempts();
517 Value ServicesTable::CurrentAttemptAccessor(const Value& row)
519 Service::Ptr service = static_cast<Service::Ptr>(row);
524 return service->GetCheckAttempt();
527 Value ServicesTable::StateAccessor(const Value& row)
529 Service::Ptr service = static_cast<Service::Ptr>(row);
534 return service->GetState();
537 Value ServicesTable::HasBeenCheckedAccessor(const Value& row)
539 Service::Ptr service = static_cast<Service::Ptr>(row);
544 return (service->HasBeenChecked() ? 1 : 0);
547 Value ServicesTable::LastStateAccessor(const Value& row)
549 Service::Ptr service = static_cast<Service::Ptr>(row);
554 return service->GetLastState();
557 Value ServicesTable::LastHardStateAccessor(const Value& row)
559 Service::Ptr service = static_cast<Service::Ptr>(row);
564 return service->GetLastHardState();
567 Value ServicesTable::StateTypeAccessor(const Value& row)
569 Service::Ptr service = static_cast<Service::Ptr>(row);
574 return service->GetStateType();
577 Value ServicesTable::CheckTypeAccessor(const Value& row)
579 Service::Ptr service = static_cast<Service::Ptr>(row);
584 return (service->GetEnableActiveChecks() ? 0 : 1);
587 Value ServicesTable::AcknowledgedAccessor(const Value& row)
589 Service::Ptr service = static_cast<Service::Ptr>(row);
594 /* important: lock acknowledgements */
595 ObjectLock olock(service);
597 return (service->IsAcknowledged() ? 1 : 0);
600 Value ServicesTable::AcknowledgementTypeAccessor(const Value& row)
602 Service::Ptr service = static_cast<Service::Ptr>(row);
607 /* important: lock acknowledgements */
608 ObjectLock olock(service);
610 return static_cast<int>(service->GetAcknowledgement());
613 Value ServicesTable::NoMoreNotificationsAccessor(const Value& row)
615 Service::Ptr service = static_cast<Service::Ptr>(row);
620 /* XXX take the smallest notification_interval */
621 double notification_interval = -1;
622 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
623 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
624 notification_interval = notification->GetNotificationInterval();
627 if (notification_interval == 0 && !service->GetVolatile())
633 Value ServicesTable::LastTimeOkAccessor(const Value& row)
635 Service::Ptr service = static_cast<Service::Ptr>(row);
640 return static_cast<int>(service->GetLastStateOK());
643 Value ServicesTable::LastTimeWarningAccessor(const Value& row)
645 Service::Ptr service = static_cast<Service::Ptr>(row);
650 return static_cast<int>(service->GetLastStateWarning());
653 Value ServicesTable::LastTimeCriticalAccessor(const Value& row)
655 Service::Ptr service = static_cast<Service::Ptr>(row);
660 return static_cast<int>(service->GetLastStateCritical());
663 Value ServicesTable::LastTimeUnknownAccessor(const Value& row)
665 Service::Ptr service = static_cast<Service::Ptr>(row);
670 return static_cast<int>(service->GetLastStateUnknown());
673 Value ServicesTable::LastCheckAccessor(const Value& row)
675 Service::Ptr service = static_cast<Service::Ptr>(row);
680 return static_cast<int>(service->GetLastCheck());
683 Value ServicesTable::NextCheckAccessor(const Value& row)
685 Service::Ptr service = static_cast<Service::Ptr>(row);
690 return static_cast<int>(service->GetNextCheck());
693 Value ServicesTable::LastNotificationAccessor(const Value& row)
695 Service::Ptr service = static_cast<Service::Ptr>(row);
700 /* XXX Service -> Notifications, latest wins */
701 double last_notification = 0;
702 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
703 if (notification->GetLastNotification() > last_notification)
704 last_notification = notification->GetLastNotification();
707 return static_cast<int>(last_notification);
710 Value ServicesTable::NextNotificationAccessor(const Value& row)
712 Service::Ptr service = static_cast<Service::Ptr>(row);
717 /* XXX Service -> Notifications, latest wins */
718 double next_notification = 0;
719 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
720 if (notification->GetNextNotification() < next_notification)
721 next_notification = notification->GetNextNotification();
724 return static_cast<int>(next_notification);
727 Value ServicesTable::CurrentNotificationNumberAccessor(const Value& row)
729 Service::Ptr service = static_cast<Service::Ptr>(row);
734 /* XXX Service -> Notifications, biggest wins */
735 int notification_number = 0;
736 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
737 if (notification->GetNotificationNumber() > notification_number)
738 notification_number = notification->GetNotificationNumber();
741 return notification_number;
744 Value ServicesTable::LastStateChangeAccessor(const Value& row)
746 Service::Ptr service = static_cast<Service::Ptr>(row);
751 return static_cast<int>(service->GetLastStateChange());
754 Value ServicesTable::LastHardStateChangeAccessor(const Value& row)
756 Service::Ptr service = static_cast<Service::Ptr>(row);
761 return static_cast<int>(service->GetLastHardStateChange());
764 Value ServicesTable::ScheduledDowntimeDepthAccessor(const Value& row)
766 Service::Ptr service = static_cast<Service::Ptr>(row);
771 return service->GetDowntimeDepth();
774 Value ServicesTable::IsFlappingAccessor(const Value& row)
776 Service::Ptr service = static_cast<Service::Ptr>(row);
781 return service->IsFlapping();
784 Value ServicesTable::ChecksEnabledAccessor(const Value& row)
786 Service::Ptr service = static_cast<Service::Ptr>(row);
791 return (service->GetEnableActiveChecks() ? 1 : 0);
794 Value ServicesTable::AcceptPassiveChecksAccessor(const Value& row)
796 Service::Ptr service = static_cast<Service::Ptr>(row);
801 return (service->GetEnablePassiveChecks() ? 1 : 0);
804 Value ServicesTable::EventHandlerEnabledAccessor(const Value& row)
806 Service::Ptr service = static_cast<Service::Ptr>(row);
811 EventCommand::Ptr eventcommand = service->GetEventCommand();
819 Value ServicesTable::NotificationsEnabledAccessor(const Value& row)
821 Service::Ptr service = static_cast<Service::Ptr>(row);
826 return (service->GetEnableNotifications() ? 1 : 0);
829 Value ServicesTable::ActiveChecksEnabledAccessor(const Value& row)
831 Service::Ptr service = static_cast<Service::Ptr>(row);
836 return (service->GetEnableActiveChecks() ? 1 : 0);
839 Value ServicesTable::CheckOptionsAccessor(const Value& row)
841 /* TODO - forcexec, freshness, orphan, none */
845 Value ServicesTable::FlapDetectionEnabledAccessor(const Value& row)
847 Service::Ptr service = static_cast<Service::Ptr>(row);
852 return (service->GetEnableFlapping() ? 1 : 0);
855 Value ServicesTable::CheckFreshnessAccessor(const Value& row)
861 Value ServicesTable::ModifiedAttributesAccessor(const Value& row)
863 Service::Ptr service = static_cast<Service::Ptr>(row);
868 return service->GetModifiedAttributes();
871 Value ServicesTable::ModifiedAttributesListAccessor(const Value& row)
877 Value ServicesTable::StalenessAccessor(const Value& row)
879 Service::Ptr service = static_cast<Service::Ptr>(row);
884 if (service->HasBeenChecked() && service->GetLastCheck() > 0)
885 return (Utility::GetTime() - service->GetLastCheck()) / (service->GetCheckInterval() * 3600);
890 Value ServicesTable::CheckIntervalAccessor(const Value& row)
892 Service::Ptr service = static_cast<Service::Ptr>(row);
897 return (service->GetCheckInterval() / 60.0);
900 Value ServicesTable::RetryIntervalAccessor(const Value& row)
902 Service::Ptr service = static_cast<Service::Ptr>(row);
907 return (service->GetRetryInterval() / 60.0);
910 Value ServicesTable::NotificationIntervalAccessor(const Value& row)
912 Service::Ptr service = static_cast<Service::Ptr>(row);
917 /* XXX take the smallest notification_interval */
918 double notification_interval = -1;
919 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
920 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
921 notification_interval = notification->GetNotificationInterval();
924 if (notification_interval == -1)
925 notification_interval = 60;
927 return (notification_interval / 60.0);
930 Value ServicesTable::LowFlapThresholdAccessor(const Value& row)
932 Service::Ptr service = static_cast<Service::Ptr>(row);
937 return service->GetFlappingThreshold();
940 Value ServicesTable::HighFlapThresholdAccessor(const Value& row)
942 Service::Ptr service = static_cast<Service::Ptr>(row);
947 return service->GetFlappingThreshold();
950 Value ServicesTable::LatencyAccessor(const Value& row)
952 Service::Ptr service = static_cast<Service::Ptr>(row);
957 return (Service::CalculateLatency(service->GetLastCheckResult()));
960 Value ServicesTable::ExecutionTimeAccessor(const Value& row)
962 Service::Ptr service = static_cast<Service::Ptr>(row);
967 return (Service::CalculateExecutionTime(service->GetLastCheckResult()));
970 Value ServicesTable::PercentStateChangeAccessor(const Value& row)
972 Service::Ptr service = static_cast<Service::Ptr>(row);
977 return service->GetFlappingCurrent();
980 Value ServicesTable::InCheckPeriodAccessor(const Value& row)
982 Service::Ptr service = static_cast<Service::Ptr>(row);
987 TimePeriod::Ptr timeperiod = service->GetCheckPeriod();
989 /* none set means always checked */
993 return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
996 Value ServicesTable::InNotificationPeriodAccessor(const Value& row)
998 Service::Ptr service = static_cast<Service::Ptr>(row);
1003 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
1004 ObjectLock olock(notification);
1006 TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod();
1008 /* XXX first notification wins */
1010 return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
1013 /* none set means always notified */
1017 Value ServicesTable::ContactsAccessor(const Value& row)
1019 Service::Ptr service = static_cast<Service::Ptr>(row);
1024 Array::Ptr contact_names = make_shared<Array>();
1026 BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetServiceNotificationUsers(service)) {
1027 contact_names->Add(user->GetName());
1030 return contact_names;
1033 Value ServicesTable::DowntimesAccessor(const Value& row)
1035 Service::Ptr service = static_cast<Service::Ptr>(row);
1040 Dictionary::Ptr downtimes = service->GetDowntimes();
1042 Array::Ptr ids = make_shared<Array>();
1044 ObjectLock olock(downtimes);
1047 Downtime::Ptr downtime;
1048 BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
1053 if (Service::IsDowntimeExpired(downtime))
1056 ids->Add(downtime->GetLegacyId());
1062 Value ServicesTable::DowntimesWithInfoAccessor(const Value& row)
1064 Service::Ptr service = static_cast<Service::Ptr>(row);
1069 Dictionary::Ptr downtimes = service->GetDowntimes();
1071 Array::Ptr ids = make_shared<Array>();
1073 ObjectLock olock(downtimes);
1076 Downtime::Ptr downtime;
1077 BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
1082 if (Service::IsDowntimeExpired(downtime))
1085 Array::Ptr downtime_info = make_shared<Array>();
1086 downtime_info->Add(downtime->GetLegacyId());
1087 downtime_info->Add(downtime->GetAuthor());
1088 downtime_info->Add(downtime->GetComment());
1089 ids->Add(downtime_info);
1095 Value ServicesTable::CommentsAccessor(const Value& row)
1097 Service::Ptr service = static_cast<Service::Ptr>(row);
1102 Dictionary::Ptr comments = service->GetComments();
1104 Array::Ptr ids = make_shared<Array>();
1106 ObjectLock olock(comments);
1109 Comment::Ptr comment;
1110 BOOST_FOREACH(boost::tie(id, comment), comments) {
1115 if (Service::IsCommentExpired(comment))
1118 ids->Add(comment->GetLegacyId());
1124 Value ServicesTable::CommentsWithInfoAccessor(const Value& row)
1126 Service::Ptr service = static_cast<Service::Ptr>(row);
1131 Dictionary::Ptr comments = service->GetComments();
1133 Array::Ptr ids = make_shared<Array>();
1135 ObjectLock olock(comments);
1138 Comment::Ptr comment;
1139 BOOST_FOREACH(boost::tie(id, comment), comments) {
1144 if (Service::IsCommentExpired(comment))
1147 Array::Ptr comment_info = make_shared<Array>();
1148 comment_info->Add(comment->GetLegacyId());
1149 comment_info->Add(comment->GetAuthor());
1150 comment_info->Add(comment->GetText());
1151 ids->Add(comment_info);
1157 Value ServicesTable::CommentsWithExtraInfoAccessor(const Value& row)
1159 Service::Ptr service = static_cast<Service::Ptr>(row);
1164 Dictionary::Ptr comments = service->GetComments();
1166 Array::Ptr ids = make_shared<Array>();
1168 ObjectLock olock(comments);
1171 Comment::Ptr comment;
1172 BOOST_FOREACH(boost::tie(id, comment), comments) {
1177 if (Service::IsCommentExpired(comment))
1180 Array::Ptr comment_info = make_shared<Array>();
1181 comment_info->Add(comment->GetLegacyId());
1182 comment_info->Add(comment->GetAuthor());
1183 comment_info->Add(comment->GetText());
1184 comment_info->Add(comment->GetEntryType());
1185 comment_info->Add(static_cast<int>(comment->GetEntryTime()));
1186 ids->Add(comment_info);
1192 Value ServicesTable::CustomVariableNamesAccessor(const Value& row)
1194 Service::Ptr service = static_cast<Service::Ptr>(row);
1199 Dictionary::Ptr customvars;
1202 ObjectLock olock(service);
1203 customvars = CompatUtility::GetCustomVariableConfig(service);
1209 Array::Ptr cv = make_shared<Array>();
1213 BOOST_FOREACH(boost::tie(key, value), customvars) {
1220 Value ServicesTable::CustomVariableValuesAccessor(const Value& row)
1222 Service::Ptr service = static_cast<Service::Ptr>(row);
1227 Dictionary::Ptr customvars;
1230 ObjectLock olock(service);
1231 customvars = CompatUtility::GetCustomVariableConfig(service);
1237 Array::Ptr cv = make_shared<Array>();
1241 BOOST_FOREACH(boost::tie(key, value), customvars) {
1248 Value ServicesTable::CustomVariablesAccessor(const Value& row)
1250 Service::Ptr service = static_cast<Service::Ptr>(row);
1255 Dictionary::Ptr customvars;
1258 ObjectLock olock(service);
1259 customvars = CompatUtility::GetCustomVariableConfig(service);
1265 Array::Ptr cv = make_shared<Array>();
1269 BOOST_FOREACH(boost::tie(key, value), customvars) {
1270 Array::Ptr key_val = make_shared<Array>();
1272 key_val->Add(value);
1279 Value ServicesTable::GroupsAccessor(const Value& row)
1281 Service::Ptr service = static_cast<Service::Ptr>(row);
1286 Array::Ptr groups = service->GetGroups();
1294 Value ServicesTable::ContactGroupsAccessor(const Value& row)
1296 Service::Ptr service = static_cast<Service::Ptr>(row);
1301 Array::Ptr contactgroup_names = make_shared<Array>();
1303 BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetServiceNotificationUserGroups(service)) {
1304 contactgroup_names->Add(usergroup->GetName());
1307 return contactgroup_names;