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 return static_cast<Service::Ptr>(service)->GetHost();
157 Value ServicesTable::ShortNameAccessor(const Value& row)
159 return static_cast<Service::Ptr>(row)->GetShortName();
162 Value ServicesTable::DisplayNameAccessor(const Value& row)
164 return static_cast<Service::Ptr>(row)->GetDisplayName();
167 Value ServicesTable::CheckCommandAccessor(const Value& row)
169 CheckCommand::Ptr checkcommand = static_cast<Service::Ptr>(row)->GetCheckCommand();
172 return checkcommand->GetName(); /* this is the name without '!' args */
177 Value ServicesTable::CheckCommandExpandedAccessor(const Value& row)
179 Service::Ptr service = static_cast<Service::Ptr>(row);
181 CheckCommand::Ptr commandObj = service->GetCheckCommand();
186 Value raw_command = commandObj->GetCommandLine();
188 std::vector<MacroResolver::Ptr> resolvers;
189 resolvers.push_back(service);
190 resolvers.push_back(service->GetHost());
191 resolvers.push_back(commandObj);
192 resolvers.push_back(IcingaApplication::GetInstance());
194 Value commandLine = MacroProcessor::ResolveMacros(raw_command, resolvers, Dictionary::Ptr(), Utility::EscapeShellCmd);
197 if (commandLine.IsObjectType<Array>()) {
198 Array::Ptr args = commandLine;
200 ObjectLock olock(args);
202 BOOST_FOREACH(arg, args) {
203 // This is obviously incorrect for non-trivial cases.
204 String argitem = " \"" + arg + "\"";
205 boost::algorithm::replace_all(argitem, "\n", "\\n");
208 } else if (!commandLine.IsEmpty()) {
209 String args = Convert::ToString(commandLine);
210 boost::algorithm::replace_all(args, "\n", "\\n");
219 Value ServicesTable::EventHandlerAccessor(const Value& row)
221 EventCommand::Ptr eventcommand = static_cast<Service::Ptr>(row)->GetEventCommand();
224 return eventcommand->GetName();
229 Value ServicesTable::PluginOutputAccessor(const Value& row)
231 return static_cast<Service::Ptr>(row)->GetLastCheckOutput();
234 Value ServicesTable::LongPluginOutputAccessor(const Value& row)
236 return static_cast<Service::Ptr>(row)->GetLastCheckLongOutput();
239 Value ServicesTable::PerfDataAccessor(const Value& row)
241 return static_cast<Service::Ptr>(row)->GetLastCheckPerfData();
244 Value ServicesTable::NotificationPeriodAccessor(const Value& row)
246 BOOST_FOREACH(const Notification::Ptr& notification, static_cast<Service::Ptr>(row)->GetNotifications()) {
247 ObjectLock olock(notification);
249 TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod();
251 /* XXX first notification wins */
253 return timeperiod->GetName();
259 Value ServicesTable::CheckPeriodAccessor(const Value& row)
261 TimePeriod::Ptr timeperiod = static_cast<Service::Ptr>(row)->GetCheckPeriod();
266 return timeperiod->GetName();
269 Value ServicesTable::NotesAccessor(const Value& row)
271 Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
276 return custom->Get("notes");
279 Value ServicesTable::NotesExpandedAccessor(const Value& row)
281 Service::Ptr service = static_cast<Service::Ptr>(row);
282 Dictionary::Ptr custom = service->GetCustom();
287 std::vector<MacroResolver::Ptr> resolvers;
288 resolvers.push_back(service);
289 resolvers.push_back(service->GetHost());
290 resolvers.push_back(IcingaApplication::GetInstance());
292 Value value = custom->Get("notes");
295 Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
297 return value_expanded;
300 Value ServicesTable::NotesUrlAccessor(const Value& row)
302 Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
307 return custom->Get("notes_url");
310 Value ServicesTable::NotesUrlExpandedAccessor(const Value& row)
312 Service::Ptr service = static_cast<Service::Ptr>(row);
313 Dictionary::Ptr custom = service->GetCustom();
318 std::vector<MacroResolver::Ptr> resolvers;
319 resolvers.push_back(service);
320 resolvers.push_back(service->GetHost());
321 resolvers.push_back(IcingaApplication::GetInstance());
323 Value value = custom->Get("notes_url");
326 Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
328 return value_expanded;
331 Value ServicesTable::ActionUrlAccessor(const Value& row)
333 Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
338 return custom->Get("action_url");
341 Value ServicesTable::ActionUrlExpandedAccessor(const Value& row)
343 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("action_url");
357 Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
359 return value_expanded;
362 Value ServicesTable::IconImageAccessor(const Value& row)
364 Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
369 return custom->Get("icon_image");
372 Value ServicesTable::IconImageExpandedAccessor(const Value& row)
374 Service::Ptr service = static_cast<Service::Ptr>(row);
375 Dictionary::Ptr custom = service->GetCustom();
380 std::vector<MacroResolver::Ptr> resolvers;
381 resolvers.push_back(service);
382 resolvers.push_back(service->GetHost());
383 resolvers.push_back(IcingaApplication::GetInstance());
385 Value value = custom->Get("icon_image");
388 Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
390 return value_expanded;
393 Value ServicesTable::IconImageAltAccessor(const Value& row)
395 Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
400 return custom->Get("icon_image_alt");
403 Value ServicesTable::MaxCheckAttemptsAccessor(const Value& row)
405 return static_cast<Service::Ptr>(row)->GetMaxCheckAttempts();
408 Value ServicesTable::CurrentAttemptAccessor(const Value& row)
410 return static_cast<Service::Ptr>(row)->GetCheckAttempt();
413 Value ServicesTable::StateAccessor(const Value& row)
415 return static_cast<Service::Ptr>(row)->GetState();
418 Value ServicesTable::HasBeenCheckedAccessor(const Value& row)
420 return (static_cast<Service::Ptr>(row)->HasBeenChecked() ? 1 : 0);
423 Value ServicesTable::LastStateAccessor(const Value& row)
425 return static_cast<Service::Ptr>(row)->GetLastState();
428 Value ServicesTable::LastHardStateAccessor(const Value& row)
430 return static_cast<Service::Ptr>(row)->GetLastHardState();
433 Value ServicesTable::StateTypeAccessor(const Value& row)
435 return static_cast<Service::Ptr>(row)->GetStateType();
438 Value ServicesTable::CheckTypeAccessor(const Value& row)
440 return (static_cast<Service::Ptr>(row)->GetEnableActiveChecks() ? 0 : 1);
443 Value ServicesTable::AcknowledgedAccessor(const Value& row)
445 Service::Ptr service = static_cast<Service::Ptr>(row);
447 /* important: lock acknowledgements */
448 ObjectLock olock(service);
450 return (service->IsAcknowledged() ? 1 : 0);
453 Value ServicesTable::AcknowledgementTypeAccessor(const Value& row)
455 Service::Ptr service = static_cast<Service::Ptr>(row);
457 /* important: lock acknowledgements */
458 ObjectLock olock(service);
460 return static_cast<int>(service->GetAcknowledgement());
463 Value ServicesTable::NoMoreNotificationsAccessor(const Value& row)
465 Service::Ptr service = static_cast<Service::Ptr>(row);
467 /* XXX take the smallest notification_interval */
468 double notification_interval = -1;
469 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
470 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
471 notification_interval = notification->GetNotificationInterval();
474 if (notification_interval == 0 && !service->GetVolatile())
480 Value ServicesTable::LastTimeOkAccessor(const Value& row)
482 return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastStateOK());
485 Value ServicesTable::LastTimeWarningAccessor(const Value& row)
487 return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastStateWarning());
490 Value ServicesTable::LastTimeCriticalAccessor(const Value& row)
492 return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastStateCritical());
495 Value ServicesTable::LastTimeUnknownAccessor(const Value& row)
497 return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastStateUnknown());
500 Value ServicesTable::LastCheckAccessor(const Value& row)
502 return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastCheck());
505 Value ServicesTable::NextCheckAccessor(const Value& row)
507 return static_cast<int>(static_cast<Service::Ptr>(row)->GetNextCheck());
510 Value ServicesTable::LastNotificationAccessor(const Value& row)
512 Service::Ptr service = static_cast<Service::Ptr>(row);
514 /* XXX Service -> Notifications, latest wins */
515 double last_notification = 0;
516 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
517 if (notification->GetLastNotification() > last_notification)
518 last_notification = notification->GetLastNotification();
521 return static_cast<int>(last_notification);
524 Value ServicesTable::NextNotificationAccessor(const Value& row)
526 Service::Ptr service = static_cast<Service::Ptr>(row);
528 /* XXX Service -> Notifications, latest wins */
529 double next_notification = 0;
530 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
531 if (notification->GetNextNotification() < next_notification)
532 next_notification = notification->GetNextNotification();
535 return static_cast<int>(next_notification);
538 Value ServicesTable::CurrentNotificationNumberAccessor(const Value& row)
540 Service::Ptr service = static_cast<Service::Ptr>(row);
542 /* XXX Service -> Notifications, biggest wins */
543 int notification_number = 0;
544 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
545 if (notification->GetNotificationNumber() > notification_number)
546 notification_number = notification->GetNotificationNumber();
549 return notification_number;
552 Value ServicesTable::LastStateChangeAccessor(const Value& row)
554 return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastStateChange());
557 Value ServicesTable::LastHardStateChangeAccessor(const Value& row)
559 return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastHardStateChange());
562 Value ServicesTable::ScheduledDowntimeDepthAccessor(const Value& row)
564 return static_cast<Service::Ptr>(row)->GetDowntimeDepth();
567 Value ServicesTable::IsFlappingAccessor(const Value& row)
569 return static_cast<Service::Ptr>(row)->IsFlapping();
572 Value ServicesTable::ChecksEnabledAccessor(const Value& row)
574 return (static_cast<Service::Ptr>(row)->GetEnableActiveChecks() ? 1 : 0);
577 Value ServicesTable::AcceptPassiveChecksAccessor(const Value& row)
579 return (static_cast<Service::Ptr>(row)->GetEnablePassiveChecks() ? 1 : 0);
582 Value ServicesTable::EventHandlerEnabledAccessor(const Value& row)
584 EventCommand::Ptr eventcommand = static_cast<Service::Ptr>(row)->GetEventCommand();
592 Value ServicesTable::NotificationsEnabledAccessor(const Value& row)
594 return (static_cast<Service::Ptr>(row)->GetEnableNotifications() ? 1 : 0);
597 Value ServicesTable::ActiveChecksEnabledAccessor(const Value& row)
599 return (static_cast<Service::Ptr>(row)->GetEnableActiveChecks() ? 1 : 0);
602 Value ServicesTable::CheckOptionsAccessor(const Value& row)
604 /* TODO - forcexec, freshness, orphan, none */
608 Value ServicesTable::FlapDetectionEnabledAccessor(const Value& row)
610 return (static_cast<Service::Ptr>(row)->GetEnableFlapping() ? 1 : 0);
613 Value ServicesTable::CheckFreshnessAccessor(const Value& row)
619 Value ServicesTable::ModifiedAttributesAccessor(const Value& row)
621 return static_cast<Service::Ptr>(row)->GetModifiedAttributes();
624 Value ServicesTable::ModifiedAttributesListAccessor(const Value& row)
630 Value ServicesTable::StalenessAccessor(const Value& row)
632 Service::Ptr service = static_cast<Service::Ptr>(row);
634 if (service->HasBeenChecked() && service->GetLastCheck() > 0)
635 return (Utility::GetTime() - service->GetLastCheck()) / (service->GetCheckInterval() * 3600);
640 Value ServicesTable::CheckIntervalAccessor(const Value& row)
642 return (static_cast<Service::Ptr>(row)->GetCheckInterval() / 60.0);
645 Value ServicesTable::RetryIntervalAccessor(const Value& row)
647 return (static_cast<Service::Ptr>(row)->GetRetryInterval() / 60.0);
650 Value ServicesTable::NotificationIntervalAccessor(const Value& row)
652 Service::Ptr service = static_cast<Service::Ptr>(row);
654 /* XXX take the smallest notification_interval */
655 double notification_interval = -1;
656 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
657 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
658 notification_interval = notification->GetNotificationInterval();
661 if (notification_interval == -1)
662 notification_interval = 60;
664 return (notification_interval / 60.0);
667 Value ServicesTable::LowFlapThresholdAccessor(const Value& row)
669 return static_cast<Service::Ptr>(row)->GetFlappingThreshold();
672 Value ServicesTable::HighFlapThresholdAccessor(const Value& row)
674 return static_cast<Service::Ptr>(row)->GetFlappingThreshold();
677 Value ServicesTable::LatencyAccessor(const Value& row)
679 return (Service::CalculateLatency(static_cast<Service::Ptr>(row)->GetLastCheckResult()));
682 Value ServicesTable::ExecutionTimeAccessor(const Value& row)
684 return (Service::CalculateExecutionTime(static_cast<Service::Ptr>(row)->GetLastCheckResult()));
687 Value ServicesTable::PercentStateChangeAccessor(const Value& row)
689 return static_cast<Service::Ptr>(row)->GetFlappingCurrent();
692 Value ServicesTable::InCheckPeriodAccessor(const Value& row)
694 TimePeriod::Ptr timeperiod = static_cast<Service::Ptr>(row)->GetCheckPeriod();
696 /* none set means always checked */
700 return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
703 Value ServicesTable::InNotificationPeriodAccessor(const Value& row)
705 BOOST_FOREACH(const Notification::Ptr& notification, static_cast<Service::Ptr>(row)->GetNotifications()) {
706 ObjectLock olock(notification);
708 TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod();
710 /* XXX first notification wins */
712 return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
715 /* none set means always notified */
719 Value ServicesTable::ContactsAccessor(const Value& row)
721 Array::Ptr contact_names = boost::make_shared<Array>();
723 BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetServiceNotificationUsers(static_cast<Service::Ptr>(row))) {
724 contact_names->Add(user->GetName());
727 return contact_names;
730 Value ServicesTable::DowntimesAccessor(const Value& row)
732 Dictionary::Ptr downtimes = static_cast<Service::Ptr>(row)->GetDowntimes();
734 Array::Ptr ids = boost::make_shared<Array>();
736 ObjectLock olock(downtimes);
739 Dictionary::Ptr downtime;
740 BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
745 if (Service::IsDowntimeExpired(downtime))
748 ids->Add(downtime->Get("legacy_id"));
754 Value ServicesTable::DowntimesWithInfoAccessor(const Value& row)
756 Dictionary::Ptr downtimes = static_cast<Service::Ptr>(row)->GetDowntimes();
758 Array::Ptr ids = boost::make_shared<Array>();
760 ObjectLock olock(downtimes);
763 Dictionary::Ptr downtime;
764 BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
769 if (Service::IsDowntimeExpired(downtime))
772 Array::Ptr downtime_info = boost::make_shared<Array>();
773 downtime_info->Add(downtime->Get("legacy_id"));
774 downtime_info->Add(downtime->Get("author"));
775 downtime_info->Add(downtime->Get("comment"));
776 ids->Add(downtime_info);
782 Value ServicesTable::CommentsAccessor(const Value& row)
784 Dictionary::Ptr comments = static_cast<Service::Ptr>(row)->GetComments();
786 Array::Ptr ids = boost::make_shared<Array>();
788 ObjectLock olock(comments);
791 Dictionary::Ptr comment;
792 BOOST_FOREACH(boost::tie(id, comment), comments) {
797 if (Service::IsCommentExpired(comment))
800 ids->Add(comment->Get("legacy_id"));
806 Value ServicesTable::CommentsWithInfoAccessor(const Value& row)
808 Dictionary::Ptr comments = static_cast<Service::Ptr>(row)->GetComments();
810 Array::Ptr ids = boost::make_shared<Array>();
812 ObjectLock olock(comments);
815 Dictionary::Ptr comment;
816 BOOST_FOREACH(boost::tie(id, comment), comments) {
821 if (Service::IsCommentExpired(comment))
824 Array::Ptr comment_info = boost::make_shared<Array>();
825 comment_info->Add(comment->Get("legacy_id"));
826 comment_info->Add(comment->Get("author"));
827 comment_info->Add(comment->Get("text"));
828 ids->Add(comment_info);
834 Value ServicesTable::CommentsWithExtraInfoAccessor(const Value& row)
836 Dictionary::Ptr comments = static_cast<Service::Ptr>(row)->GetComments();
838 Array::Ptr ids = boost::make_shared<Array>();
840 ObjectLock olock(comments);
843 Dictionary::Ptr comment;
844 BOOST_FOREACH(boost::tie(id, comment), comments) {
849 if (Service::IsCommentExpired(comment))
852 Array::Ptr comment_info = boost::make_shared<Array>();
853 comment_info->Add(comment->Get("legacy_id"));
854 comment_info->Add(comment->Get("author"));
855 comment_info->Add(comment->Get("text"));
856 comment_info->Add(comment->Get("entry_type"));
857 comment_info->Add(static_cast<int>(comment->Get("entry_time")));
858 ids->Add(comment_info);
864 Value ServicesTable::CustomVariableNamesAccessor(const Value& row)
866 Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
871 Array::Ptr cv = boost::make_shared<Array>();
873 ObjectLock olock(custom);
876 BOOST_FOREACH(boost::tie(key, value), custom) {
877 if (key == "notes" ||
878 key == "action_url" ||
879 key == "notes_url" ||
880 key == "icon_image" ||
881 key == "icon_image_alt" ||
882 key == "statusmap_image" ||
892 Value ServicesTable::CustomVariableValuesAccessor(const Value& row)
894 Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
899 Array::Ptr cv = boost::make_shared<Array>();
901 ObjectLock olock(custom);
904 BOOST_FOREACH(boost::tie(key, value), custom) {
905 if (key == "notes" ||
906 key == "action_url" ||
907 key == "notes_url" ||
908 key == "icon_image" ||
909 key == "icon_image_alt" ||
910 key == "statusmap_image" ||
920 Value ServicesTable::CustomVariablesAccessor(const Value& row)
922 Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
927 Array::Ptr cv = boost::make_shared<Array>();
929 ObjectLock olock(custom);
932 BOOST_FOREACH(boost::tie(key, value), custom) {
933 if (key == "notes" ||
934 key == "action_url" ||
935 key == "notes_url" ||
936 key == "icon_image" ||
937 key == "icon_image_alt" ||
938 key == "statusmap_image" ||
942 Array::Ptr key_val = boost::make_shared<Array>();
951 Value ServicesTable::GroupsAccessor(const Value& row)
953 Array::Ptr groups = static_cast<Service::Ptr>(row)->GetGroups();
961 Value ServicesTable::ContactGroupsAccessor(const Value& row)
963 Array::Ptr contactgroup_names = boost::make_shared<Array>();
965 BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetServiceNotificationUserGroups(static_cast<Service::Ptr>(row))) {
966 contactgroup_names->Add(usergroup->GetName());
969 return contactgroup_names;