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);
265 Dictionary::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 Dictionary::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 Dictionary::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");
379 Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
381 return value_expanded;
384 Value ServicesTable::NotesUrlAccessor(const Value& row)
386 Service::Ptr service = static_cast<Service::Ptr>(row);
391 Dictionary::Ptr custom = service->GetCustom();
396 return custom->Get("notes_url");
399 Value ServicesTable::NotesUrlExpandedAccessor(const Value& row)
401 Service::Ptr service = static_cast<Service::Ptr>(row);
406 Dictionary::Ptr custom = service->GetCustom();
411 std::vector<MacroResolver::Ptr> resolvers;
412 resolvers.push_back(service);
413 resolvers.push_back(service->GetHost());
414 resolvers.push_back(IcingaApplication::GetInstance());
416 Value value = custom->Get("notes_url");
419 Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
421 return value_expanded;
424 Value ServicesTable::ActionUrlAccessor(const Value& row)
426 Service::Ptr service = static_cast<Service::Ptr>(row);
431 Dictionary::Ptr custom = service->GetCustom();
436 return custom->Get("action_url");
439 Value ServicesTable::ActionUrlExpandedAccessor(const Value& row)
441 Service::Ptr service = static_cast<Service::Ptr>(row);
446 Dictionary::Ptr custom = service->GetCustom();
451 std::vector<MacroResolver::Ptr> resolvers;
452 resolvers.push_back(service);
453 resolvers.push_back(service->GetHost());
454 resolvers.push_back(IcingaApplication::GetInstance());
456 Value value = custom->Get("action_url");
459 Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
461 return value_expanded;
464 Value ServicesTable::IconImageAccessor(const Value& row)
466 Service::Ptr service = static_cast<Service::Ptr>(row);
471 Dictionary::Ptr custom = service->GetCustom();
476 return custom->Get("icon_image");
479 Value ServicesTable::IconImageExpandedAccessor(const Value& row)
481 Service::Ptr service = static_cast<Service::Ptr>(row);
486 Dictionary::Ptr custom = service->GetCustom();
491 std::vector<MacroResolver::Ptr> resolvers;
492 resolvers.push_back(service);
493 resolvers.push_back(service->GetHost());
494 resolvers.push_back(IcingaApplication::GetInstance());
496 Value value = custom->Get("icon_image");
499 Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
501 return value_expanded;
504 Value ServicesTable::IconImageAltAccessor(const Value& row)
506 Service::Ptr service = static_cast<Service::Ptr>(row);
511 Dictionary::Ptr custom = service->GetCustom();
516 return custom->Get("icon_image_alt");
519 Value ServicesTable::MaxCheckAttemptsAccessor(const Value& row)
521 Service::Ptr service = static_cast<Service::Ptr>(row);
526 return service->GetMaxCheckAttempts();
529 Value ServicesTable::CurrentAttemptAccessor(const Value& row)
531 Service::Ptr service = static_cast<Service::Ptr>(row);
536 return service->GetCheckAttempt();
539 Value ServicesTable::StateAccessor(const Value& row)
541 Service::Ptr service = static_cast<Service::Ptr>(row);
546 return service->GetState();
549 Value ServicesTable::HasBeenCheckedAccessor(const Value& row)
551 Service::Ptr service = static_cast<Service::Ptr>(row);
556 return (service->HasBeenChecked() ? 1 : 0);
559 Value ServicesTable::LastStateAccessor(const Value& row)
561 Service::Ptr service = static_cast<Service::Ptr>(row);
566 return service->GetLastState();
569 Value ServicesTable::LastHardStateAccessor(const Value& row)
571 Service::Ptr service = static_cast<Service::Ptr>(row);
576 return service->GetLastHardState();
579 Value ServicesTable::StateTypeAccessor(const Value& row)
581 Service::Ptr service = static_cast<Service::Ptr>(row);
586 return service->GetStateType();
589 Value ServicesTable::CheckTypeAccessor(const Value& row)
591 Service::Ptr service = static_cast<Service::Ptr>(row);
596 return (service->GetEnableActiveChecks() ? 0 : 1);
599 Value ServicesTable::AcknowledgedAccessor(const Value& row)
601 Service::Ptr service = static_cast<Service::Ptr>(row);
606 /* important: lock acknowledgements */
607 ObjectLock olock(service);
609 return (service->IsAcknowledged() ? 1 : 0);
612 Value ServicesTable::AcknowledgementTypeAccessor(const Value& row)
614 Service::Ptr service = static_cast<Service::Ptr>(row);
619 /* important: lock acknowledgements */
620 ObjectLock olock(service);
622 return static_cast<int>(service->GetAcknowledgement());
625 Value ServicesTable::NoMoreNotificationsAccessor(const Value& row)
627 Service::Ptr service = static_cast<Service::Ptr>(row);
632 /* XXX take the smallest notification_interval */
633 double notification_interval = -1;
634 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
635 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
636 notification_interval = notification->GetNotificationInterval();
639 if (notification_interval == 0 && !service->GetVolatile())
645 Value ServicesTable::LastTimeOkAccessor(const Value& row)
647 Service::Ptr service = static_cast<Service::Ptr>(row);
652 return static_cast<int>(service->GetLastStateOK());
655 Value ServicesTable::LastTimeWarningAccessor(const Value& row)
657 Service::Ptr service = static_cast<Service::Ptr>(row);
662 return static_cast<int>(service->GetLastStateWarning());
665 Value ServicesTable::LastTimeCriticalAccessor(const Value& row)
667 Service::Ptr service = static_cast<Service::Ptr>(row);
672 return static_cast<int>(service->GetLastStateCritical());
675 Value ServicesTable::LastTimeUnknownAccessor(const Value& row)
677 Service::Ptr service = static_cast<Service::Ptr>(row);
682 return static_cast<int>(service->GetLastStateUnknown());
685 Value ServicesTable::LastCheckAccessor(const Value& row)
687 Service::Ptr service = static_cast<Service::Ptr>(row);
692 return static_cast<int>(service->GetLastCheck());
695 Value ServicesTable::NextCheckAccessor(const Value& row)
697 Service::Ptr service = static_cast<Service::Ptr>(row);
702 return static_cast<int>(service->GetNextCheck());
705 Value ServicesTable::LastNotificationAccessor(const Value& row)
707 Service::Ptr service = static_cast<Service::Ptr>(row);
712 /* XXX Service -> Notifications, latest wins */
713 double last_notification = 0;
714 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
715 if (notification->GetLastNotification() > last_notification)
716 last_notification = notification->GetLastNotification();
719 return static_cast<int>(last_notification);
722 Value ServicesTable::NextNotificationAccessor(const Value& row)
724 Service::Ptr service = static_cast<Service::Ptr>(row);
729 /* XXX Service -> Notifications, latest wins */
730 double next_notification = 0;
731 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
732 if (notification->GetNextNotification() < next_notification)
733 next_notification = notification->GetNextNotification();
736 return static_cast<int>(next_notification);
739 Value ServicesTable::CurrentNotificationNumberAccessor(const Value& row)
741 Service::Ptr service = static_cast<Service::Ptr>(row);
746 /* XXX Service -> Notifications, biggest wins */
747 int notification_number = 0;
748 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
749 if (notification->GetNotificationNumber() > notification_number)
750 notification_number = notification->GetNotificationNumber();
753 return notification_number;
756 Value ServicesTable::LastStateChangeAccessor(const Value& row)
758 Service::Ptr service = static_cast<Service::Ptr>(row);
763 return static_cast<int>(service->GetLastStateChange());
766 Value ServicesTable::LastHardStateChangeAccessor(const Value& row)
768 Service::Ptr service = static_cast<Service::Ptr>(row);
773 return static_cast<int>(service->GetLastHardStateChange());
776 Value ServicesTable::ScheduledDowntimeDepthAccessor(const Value& row)
778 Service::Ptr service = static_cast<Service::Ptr>(row);
783 return service->GetDowntimeDepth();
786 Value ServicesTable::IsFlappingAccessor(const Value& row)
788 Service::Ptr service = static_cast<Service::Ptr>(row);
793 return service->IsFlapping();
796 Value ServicesTable::ChecksEnabledAccessor(const Value& row)
798 Service::Ptr service = static_cast<Service::Ptr>(row);
803 return (service->GetEnableActiveChecks() ? 1 : 0);
806 Value ServicesTable::AcceptPassiveChecksAccessor(const Value& row)
808 Service::Ptr service = static_cast<Service::Ptr>(row);
813 return (service->GetEnablePassiveChecks() ? 1 : 0);
816 Value ServicesTable::EventHandlerEnabledAccessor(const Value& row)
818 Service::Ptr service = static_cast<Service::Ptr>(row);
823 EventCommand::Ptr eventcommand = service->GetEventCommand();
831 Value ServicesTable::NotificationsEnabledAccessor(const Value& row)
833 Service::Ptr service = static_cast<Service::Ptr>(row);
838 return (service->GetEnableNotifications() ? 1 : 0);
841 Value ServicesTable::ActiveChecksEnabledAccessor(const Value& row)
843 Service::Ptr service = static_cast<Service::Ptr>(row);
848 return (service->GetEnableActiveChecks() ? 1 : 0);
851 Value ServicesTable::CheckOptionsAccessor(const Value& row)
853 /* TODO - forcexec, freshness, orphan, none */
857 Value ServicesTable::FlapDetectionEnabledAccessor(const Value& row)
859 Service::Ptr service = static_cast<Service::Ptr>(row);
864 return (service->GetEnableFlapping() ? 1 : 0);
867 Value ServicesTable::CheckFreshnessAccessor(const Value& row)
873 Value ServicesTable::ModifiedAttributesAccessor(const Value& row)
875 Service::Ptr service = static_cast<Service::Ptr>(row);
880 return service->GetModifiedAttributes();
883 Value ServicesTable::ModifiedAttributesListAccessor(const Value& row)
889 Value ServicesTable::StalenessAccessor(const Value& row)
891 Service::Ptr service = static_cast<Service::Ptr>(row);
896 if (service->HasBeenChecked() && service->GetLastCheck() > 0)
897 return (Utility::GetTime() - service->GetLastCheck()) / (service->GetCheckInterval() * 3600);
902 Value ServicesTable::CheckIntervalAccessor(const Value& row)
904 Service::Ptr service = static_cast<Service::Ptr>(row);
909 return (service->GetCheckInterval() / 60.0);
912 Value ServicesTable::RetryIntervalAccessor(const Value& row)
914 Service::Ptr service = static_cast<Service::Ptr>(row);
919 return (service->GetRetryInterval() / 60.0);
922 Value ServicesTable::NotificationIntervalAccessor(const Value& row)
924 Service::Ptr service = static_cast<Service::Ptr>(row);
929 /* XXX take the smallest notification_interval */
930 double notification_interval = -1;
931 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
932 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
933 notification_interval = notification->GetNotificationInterval();
936 if (notification_interval == -1)
937 notification_interval = 60;
939 return (notification_interval / 60.0);
942 Value ServicesTable::LowFlapThresholdAccessor(const Value& row)
944 Service::Ptr service = static_cast<Service::Ptr>(row);
949 return service->GetFlappingThreshold();
952 Value ServicesTable::HighFlapThresholdAccessor(const Value& row)
954 Service::Ptr service = static_cast<Service::Ptr>(row);
959 return service->GetFlappingThreshold();
962 Value ServicesTable::LatencyAccessor(const Value& row)
964 Service::Ptr service = static_cast<Service::Ptr>(row);
969 return (Service::CalculateLatency(service->GetLastCheckResult()));
972 Value ServicesTable::ExecutionTimeAccessor(const Value& row)
974 Service::Ptr service = static_cast<Service::Ptr>(row);
979 return (Service::CalculateExecutionTime(service->GetLastCheckResult()));
982 Value ServicesTable::PercentStateChangeAccessor(const Value& row)
984 Service::Ptr service = static_cast<Service::Ptr>(row);
989 return service->GetFlappingCurrent();
992 Value ServicesTable::InCheckPeriodAccessor(const Value& row)
994 Service::Ptr service = static_cast<Service::Ptr>(row);
999 TimePeriod::Ptr timeperiod = service->GetCheckPeriod();
1001 /* none set means always checked */
1005 return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
1008 Value ServicesTable::InNotificationPeriodAccessor(const Value& row)
1010 Service::Ptr service = static_cast<Service::Ptr>(row);
1015 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
1016 ObjectLock olock(notification);
1018 TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod();
1020 /* XXX first notification wins */
1022 return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
1025 /* none set means always notified */
1029 Value ServicesTable::ContactsAccessor(const Value& row)
1031 Service::Ptr service = static_cast<Service::Ptr>(row);
1036 Array::Ptr contact_names = boost::make_shared<Array>();
1038 BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetServiceNotificationUsers(service)) {
1039 contact_names->Add(user->GetName());
1042 return contact_names;
1045 Value ServicesTable::DowntimesAccessor(const Value& row)
1047 Service::Ptr service = static_cast<Service::Ptr>(row);
1052 Dictionary::Ptr downtimes = service->GetDowntimes();
1054 Array::Ptr ids = boost::make_shared<Array>();
1056 ObjectLock olock(downtimes);
1059 Dictionary::Ptr downtime;
1060 BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
1065 if (Service::IsDowntimeExpired(downtime))
1068 ids->Add(downtime->Get("legacy_id"));
1074 Value ServicesTable::DowntimesWithInfoAccessor(const Value& row)
1076 Service::Ptr service = static_cast<Service::Ptr>(row);
1081 Dictionary::Ptr downtimes = service->GetDowntimes();
1083 Array::Ptr ids = boost::make_shared<Array>();
1085 ObjectLock olock(downtimes);
1088 Dictionary::Ptr downtime;
1089 BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
1094 if (Service::IsDowntimeExpired(downtime))
1097 Array::Ptr downtime_info = boost::make_shared<Array>();
1098 downtime_info->Add(downtime->Get("legacy_id"));
1099 downtime_info->Add(downtime->Get("author"));
1100 downtime_info->Add(downtime->Get("comment"));
1101 ids->Add(downtime_info);
1107 Value ServicesTable::CommentsAccessor(const Value& row)
1109 Service::Ptr service = static_cast<Service::Ptr>(row);
1114 Dictionary::Ptr comments = service->GetComments();
1116 Array::Ptr ids = boost::make_shared<Array>();
1118 ObjectLock olock(comments);
1121 Dictionary::Ptr comment;
1122 BOOST_FOREACH(boost::tie(id, comment), comments) {
1127 if (Service::IsCommentExpired(comment))
1130 ids->Add(comment->Get("legacy_id"));
1136 Value ServicesTable::CommentsWithInfoAccessor(const Value& row)
1138 Service::Ptr service = static_cast<Service::Ptr>(row);
1143 Dictionary::Ptr comments = service->GetComments();
1145 Array::Ptr ids = boost::make_shared<Array>();
1147 ObjectLock olock(comments);
1150 Dictionary::Ptr comment;
1151 BOOST_FOREACH(boost::tie(id, comment), comments) {
1156 if (Service::IsCommentExpired(comment))
1159 Array::Ptr comment_info = boost::make_shared<Array>();
1160 comment_info->Add(comment->Get("legacy_id"));
1161 comment_info->Add(comment->Get("author"));
1162 comment_info->Add(comment->Get("text"));
1163 ids->Add(comment_info);
1169 Value ServicesTable::CommentsWithExtraInfoAccessor(const Value& row)
1171 Service::Ptr service = static_cast<Service::Ptr>(row);
1176 Dictionary::Ptr comments = service->GetComments();
1178 Array::Ptr ids = boost::make_shared<Array>();
1180 ObjectLock olock(comments);
1183 Dictionary::Ptr comment;
1184 BOOST_FOREACH(boost::tie(id, comment), comments) {
1189 if (Service::IsCommentExpired(comment))
1192 Array::Ptr comment_info = boost::make_shared<Array>();
1193 comment_info->Add(comment->Get("legacy_id"));
1194 comment_info->Add(comment->Get("author"));
1195 comment_info->Add(comment->Get("text"));
1196 comment_info->Add(comment->Get("entry_type"));
1197 comment_info->Add(static_cast<int>(comment->Get("entry_time")));
1198 ids->Add(comment_info);
1204 Value ServicesTable::CustomVariableNamesAccessor(const Value& row)
1206 Service::Ptr service = static_cast<Service::Ptr>(row);
1211 Dictionary::Ptr customvars;
1214 ObjectLock olock(service);
1215 customvars = CompatUtility::GetCustomVariableConfig(service);
1221 Array::Ptr cv = boost::make_shared<Array>();
1225 BOOST_FOREACH(boost::tie(key, value), customvars) {
1232 Value ServicesTable::CustomVariableValuesAccessor(const Value& row)
1234 Service::Ptr service = static_cast<Service::Ptr>(row);
1239 Dictionary::Ptr customvars;
1242 ObjectLock olock(service);
1243 customvars = CompatUtility::GetCustomVariableConfig(service);
1249 Array::Ptr cv = boost::make_shared<Array>();
1253 BOOST_FOREACH(boost::tie(key, value), customvars) {
1260 Value ServicesTable::CustomVariablesAccessor(const Value& row)
1262 Service::Ptr service = static_cast<Service::Ptr>(row);
1267 Dictionary::Ptr customvars;
1270 ObjectLock olock(service);
1271 customvars = CompatUtility::GetCustomVariableConfig(service);
1277 Array::Ptr cv = boost::make_shared<Array>();
1281 BOOST_FOREACH(boost::tie(key, value), customvars) {
1282 Array::Ptr key_val = boost::make_shared<Array>();
1284 key_val->Add(value);
1291 Value ServicesTable::GroupsAccessor(const Value& row)
1293 Service::Ptr service = static_cast<Service::Ptr>(row);
1298 Array::Ptr groups = service->GetGroups();
1306 Value ServicesTable::ContactGroupsAccessor(const Value& row)
1308 Service::Ptr service = static_cast<Service::Ptr>(row);
1313 Array::Ptr contactgroup_names = boost::make_shared<Array>();
1315 BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetServiceNotificationUserGroups(service)) {
1316 contactgroup_names->Add(usergroup->GetName());
1319 return contactgroup_names;