1 /******************************************************************************
3 * Copyright (C) 2012 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 "base/dynamictype.h"
27 #include "base/objectlock.h"
28 #include <boost/foreach.hpp>
30 using namespace icinga;
31 using namespace livestatus;
33 ServicesTable::ServicesTable(void)
38 void ServicesTable::AddColumns(Table *table, const String& prefix,
39 const Column::ObjectAccessor& objectAccessor)
41 table->AddColumn(prefix + "description", Column(&ServicesTable::ShortNameAccessor, objectAccessor));
42 table->AddColumn(prefix + "display_name", Column(&ServicesTable::DisplayNameAccessor, objectAccessor));
43 table->AddColumn(prefix + "check_command", Column(&ServicesTable::CheckCommandAccessor, objectAccessor));
44 table->AddColumn(prefix + "check_command_expanded", Column(&ServicesTable::CheckCommandExpandedAccessor, objectAccessor));
45 table->AddColumn(prefix + "event_handler", Column(&ServicesTable::EventHandlerAccessor, objectAccessor));
46 table->AddColumn(prefix + "plugin_output", Column(&ServicesTable::PluginOutputAccessor, objectAccessor));
47 table->AddColumn(prefix + "long_plugin_output", Column(&ServicesTable::LongPluginOutputAccessor, objectAccessor));
48 table->AddColumn(prefix + "perf_data", Column(&ServicesTable::PerfDataAccessor, objectAccessor));
49 table->AddColumn(prefix + "notification_period", Column(&ServicesTable::NotificationPeriodAccessor, objectAccessor));
50 table->AddColumn(prefix + "check_period", Column(&ServicesTable::CheckPeriodAccessor, objectAccessor));
51 table->AddColumn(prefix + "notes", Column(&ServicesTable::NotesAccessor, objectAccessor));
52 table->AddColumn(prefix + "notes_expanded", Column(&ServicesTable::NotesExpandedAccessor, objectAccessor));
53 table->AddColumn(prefix + "notes_url", Column(&ServicesTable::NotesUrlAccessor, objectAccessor));
54 table->AddColumn(prefix + "notes_url_expanded", Column(&ServicesTable::NotesUrlExpandedAccessor, objectAccessor));
55 table->AddColumn(prefix + "action_url", Column(&ServicesTable::ActionUrlAccessor, objectAccessor));
56 table->AddColumn(prefix + "action_url_expanded", Column(&ServicesTable::ActionUrlExpandedAccessor, objectAccessor));
57 table->AddColumn(prefix + "icon_image", Column(&ServicesTable::IconImageAccessor, objectAccessor));
58 table->AddColumn(prefix + "icon_image_expanded", Column(&ServicesTable::IconImageExpandedAccessor, objectAccessor));
59 table->AddColumn(prefix + "icon_image_alt", Column(&ServicesTable::IconImageAltAccessor, objectAccessor));
60 table->AddColumn(prefix + "initial_state", Column(&ServicesTable::InitialStateAccessor, objectAccessor));
61 table->AddColumn(prefix + "max_check_attempts", Column(&ServicesTable::MaxCheckAttemptsAccessor, objectAccessor));
62 table->AddColumn(prefix + "current_attempt", Column(&ServicesTable::CurrentAttemptAccessor, objectAccessor));
63 table->AddColumn(prefix + "state", Column(&ServicesTable::StateAccessor, objectAccessor));
64 table->AddColumn(prefix + "has_been_checked", Column(&ServicesTable::HasBeenCheckedAccessor, objectAccessor));
65 table->AddColumn(prefix + "last_state", Column(&ServicesTable::LastStateAccessor, objectAccessor));
66 table->AddColumn(prefix + "last_hard_state", Column(&ServicesTable::LastHardStateAccessor, objectAccessor));
67 table->AddColumn(prefix + "state_type", Column(&ServicesTable::StateTypeAccessor, objectAccessor));
68 table->AddColumn(prefix + "check_type", Column(&ServicesTable::CheckTypeAccessor, objectAccessor));
69 table->AddColumn(prefix + "acknowledged", Column(&ServicesTable::AcknowledgedAccessor, objectAccessor));
70 table->AddColumn(prefix + "acknowledgement_type", Column(&ServicesTable::AcknowledgementTypeAccessor, objectAccessor));
71 table->AddColumn(prefix + "no_more_notifications", Column(&ServicesTable::NoMoreNotificationsAccessor, objectAccessor));
72 table->AddColumn(prefix + "last_time_ok", Column(&ServicesTable::LastTimeOkAccessor, objectAccessor));
73 table->AddColumn(prefix + "last_time_warning", Column(&ServicesTable::LastTimeWarningAccessor, objectAccessor));
74 table->AddColumn(prefix + "last_time_critical", Column(&ServicesTable::LastTimeCriticalAccessor, objectAccessor));
75 table->AddColumn(prefix + "last_time_unknown", Column(&ServicesTable::LastTimeUnknownAccessor, objectAccessor));
76 table->AddColumn(prefix + "last_check", Column(&ServicesTable::LastCheckAccessor, objectAccessor));
77 table->AddColumn(prefix + "next_check", Column(&ServicesTable::NextCheckAccessor, objectAccessor));
78 table->AddColumn(prefix + "last_notification", Column(&ServicesTable::LastNotificationAccessor, objectAccessor));
79 table->AddColumn(prefix + "next_notification", Column(&ServicesTable::NextNotificationAccessor, objectAccessor));
80 table->AddColumn(prefix + "current_notification_number", Column(&ServicesTable::CurrentNotificationNumberAccessor, objectAccessor));
81 table->AddColumn(prefix + "last_state_change", Column(&ServicesTable::LastStateChangeAccessor, objectAccessor));
82 table->AddColumn(prefix + "last_hard_state_change", Column(&ServicesTable::LastHardStateChangeAccessor, objectAccessor));
83 table->AddColumn(prefix + "scheduled_downtime_depth", Column(&ServicesTable::ScheduledDowntimeDepthAccessor, objectAccessor));
84 table->AddColumn(prefix + "is_flapping", Column(&ServicesTable::IsFlappingAccessor, objectAccessor));
85 table->AddColumn(prefix + "checks_enabled", Column(&ServicesTable::ChecksEnabledAccessor, objectAccessor));
86 table->AddColumn(prefix + "accept_passive_checks", Column(&ServicesTable::AcceptPassiveChecksAccessor, objectAccessor));
87 table->AddColumn(prefix + "event_handler_enabled", Column(&ServicesTable::EventHandlerEnabledAccessor, objectAccessor));
88 table->AddColumn(prefix + "notifications_enabled", Column(&ServicesTable::NotificationsEnabledAccessor, objectAccessor));
89 table->AddColumn(prefix + "process_performance_data", Column(&ServicesTable::ProcessPerformanceDataAccessor, objectAccessor));
90 table->AddColumn(prefix + "is_executing", Column(&ServicesTable::IsExecutingAccessor, objectAccessor));
91 table->AddColumn(prefix + "active_checks_enabled", Column(&ServicesTable::ActiveChecksEnabledAccessor, objectAccessor));
92 table->AddColumn(prefix + "check_options", Column(&ServicesTable::CheckOptionsAccessor, objectAccessor));
93 table->AddColumn(prefix + "flap_detection_enabled", Column(&ServicesTable::FlapDetectionEnabledAccessor, objectAccessor));
94 table->AddColumn(prefix + "check_freshness", Column(&ServicesTable::CheckFreshnessAccessor, objectAccessor));
95 table->AddColumn(prefix + "obsess_over_service", Column(&ServicesTable::ObsessOverServiceAccessor, objectAccessor));
96 table->AddColumn(prefix + "modified_attributes", Column(&ServicesTable::ModifiedAttributesAccessor, objectAccessor));
97 table->AddColumn(prefix + "modified_attributes_list", Column(&ServicesTable::ModifiedAttributesListAccessor, objectAccessor));
98 table->AddColumn(prefix + "pnpgraph_present", Column(&ServicesTable::PnpgraphPresentAccessor, objectAccessor));
99 table->AddColumn(prefix + "check_interval", Column(&ServicesTable::CheckIntervalAccessor, objectAccessor));
100 table->AddColumn(prefix + "retry_interval", Column(&ServicesTable::RetryIntervalAccessor, objectAccessor));
101 table->AddColumn(prefix + "notification_interval", Column(&ServicesTable::NotificationIntervalAccessor, objectAccessor));
102 table->AddColumn(prefix + "first_notification_delay", Column(&ServicesTable::FirstNotificationDelayAccessor, objectAccessor));
103 table->AddColumn(prefix + "low_flap_threshold", Column(&ServicesTable::LowFlapThresholdAccessor, objectAccessor));
104 table->AddColumn(prefix + "high_flap_threshold", Column(&ServicesTable::HighFlapThresholdAccessor, objectAccessor));
105 table->AddColumn(prefix + "latency", Column(&ServicesTable::LatencyAccessor, objectAccessor));
106 table->AddColumn(prefix + "execution_time", Column(&ServicesTable::ExecutionTimeAccessor, objectAccessor));
107 table->AddColumn(prefix + "percent_state_change", Column(&ServicesTable::PercentStateChangeAccessor, objectAccessor));
108 table->AddColumn(prefix + "in_check_period", Column(&ServicesTable::InCheckPeriodAccessor, objectAccessor));
109 table->AddColumn(prefix + "in_notification_period", Column(&ServicesTable::InNotificationPeriodAccessor, objectAccessor));
110 table->AddColumn(prefix + "contacts", Column(&ServicesTable::ContactsAccessor, objectAccessor));
111 table->AddColumn(prefix + "downtimes", Column(&ServicesTable::DowntimesAccessor, objectAccessor));
112 table->AddColumn(prefix + "downtimes_with_info", Column(&ServicesTable::DowntimesWithInfoAccessor, objectAccessor));
113 table->AddColumn(prefix + "comments", Column(&ServicesTable::CommentsAccessor, objectAccessor));
114 table->AddColumn(prefix + "comments_with_info", Column(&ServicesTable::CommentsWithInfoAccessor, objectAccessor));
115 table->AddColumn(prefix + "comments_with_extra_info", Column(&ServicesTable::CommentsWithExtraInfoAccessor, objectAccessor));
116 table->AddColumn(prefix + "custom_variable_names", Column(&ServicesTable::CustomVariableNamesAccessor, objectAccessor));
117 table->AddColumn(prefix + "custom_variable_values", Column(&ServicesTable::CustomVariableValuesAccessor, objectAccessor));
118 table->AddColumn(prefix + "custom_variables", Column(&ServicesTable::CustomVariablesAccessor, objectAccessor));
119 table->AddColumn(prefix + "groups", Column(&ServicesTable::GroupsAccessor, objectAccessor));
120 table->AddColumn(prefix + "contact_groups", Column(&ServicesTable::ContactGroupsAccessor, objectAccessor));
122 HostsTable::AddColumns(table, "host_", boost::bind(&ServicesTable::HostAccessor, _1, objectAccessor));
125 String ServicesTable::GetName(void) const
130 void ServicesTable::FetchRows(const AddRowFunction& addRowFn)
132 BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Service")) {
137 Object::Ptr ServicesTable::HostAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor)
141 if (parentObjectAccessor)
142 service = parentObjectAccessor(row);
146 return static_cast<Service::Ptr>(service)->GetHost();
149 Value ServicesTable::ShortNameAccessor(const Value& row)
151 return static_cast<Service::Ptr>(row)->GetShortName();
154 Value ServicesTable::DisplayNameAccessor(const Value& row)
156 return static_cast<Service::Ptr>(row)->GetDisplayName();
159 Value ServicesTable::CheckCommandAccessor(const Value& row)
161 CheckCommand::Ptr checkcommand = static_cast<Service::Ptr>(row)->GetCheckCommand();
164 return checkcommand->GetName(); /* this is the name without '!' args */
169 Value ServicesTable::CheckCommandExpandedAccessor(const Value& row)
171 CheckCommand::Ptr checkcommand = static_cast<Service::Ptr>(row)->GetCheckCommand();
174 return checkcommand->GetName(); /* this is the name without '!' args */
179 Value ServicesTable::EventHandlerAccessor(const Value& row)
181 EventCommand::Ptr eventcommand = static_cast<Service::Ptr>(row)->GetEventCommand();
184 return eventcommand->GetName();
189 Value ServicesTable::PluginOutputAccessor(const Value& row)
191 return static_cast<Service::Ptr>(row)->GetLastCheckOutput();
194 Value ServicesTable::LongPluginOutputAccessor(const Value& row)
196 return static_cast<Service::Ptr>(row)->GetLastCheckLongOutput();
199 Value ServicesTable::PerfDataAccessor(const Value& row)
201 return static_cast<Service::Ptr>(row)->GetLastCheckPerfData();
204 Value ServicesTable::NotificationPeriodAccessor(const Value& row)
210 Value ServicesTable::CheckPeriodAccessor(const Value& row)
212 TimePeriod::Ptr timeperiod = static_cast<Service::Ptr>(row)->GetCheckPeriod();
217 return timeperiod->GetName();
220 Value ServicesTable::NotesAccessor(const Value& row)
222 Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
227 return custom->Get("notes");
230 Value ServicesTable::NotesExpandedAccessor(const Value& row)
236 Value ServicesTable::NotesUrlAccessor(const Value& row)
238 Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
243 return custom->Get("notes_url");
246 Value ServicesTable::NotesUrlExpandedAccessor(const Value& row)
252 Value ServicesTable::ActionUrlAccessor(const Value& row)
254 Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
259 return custom->Get("action_url");
262 Value ServicesTable::ActionUrlExpandedAccessor(const Value& row)
268 Value ServicesTable::IconImageAccessor(const Value& row)
270 Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
275 return custom->Get("icon_image");
278 Value ServicesTable::IconImageExpandedAccessor(const Value& row)
284 Value ServicesTable::IconImageAltAccessor(const Value& row)
286 Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
291 return custom->Get("icon_image_alt");
294 Value ServicesTable::InitialStateAccessor(const Value& row)
300 Value ServicesTable::MaxCheckAttemptsAccessor(const Value& row)
302 return static_cast<Service::Ptr>(row)->GetMaxCheckAttempts();
305 Value ServicesTable::CurrentAttemptAccessor(const Value& row)
307 return static_cast<Service::Ptr>(row)->GetCurrentCheckAttempt();
310 Value ServicesTable::StateAccessor(const Value& row)
312 return static_cast<Service::Ptr>(row)->GetState();
315 Value ServicesTable::HasBeenCheckedAccessor(const Value& row)
317 return (static_cast<Service::Ptr>(row)->HasBeenChecked() ? 1 : 0);
320 Value ServicesTable::LastStateAccessor(const Value& row)
322 return static_cast<Service::Ptr>(row)->GetLastState();
325 Value ServicesTable::LastHardStateAccessor(const Value& row)
327 return static_cast<Service::Ptr>(row)->GetLastHardState();
330 Value ServicesTable::StateTypeAccessor(const Value& row)
332 return static_cast<Service::Ptr>(row)->GetStateType();
335 Value ServicesTable::CheckTypeAccessor(const Value& row)
337 return (static_cast<Service::Ptr>(row)->GetEnableActiveChecks() ? 0 : 1);
340 Value ServicesTable::AcknowledgedAccessor(const Value& row)
342 Service::Ptr service = static_cast<Service::Ptr>(row);
344 /* important: lock acknowledgements */
345 ObjectLock olock(service);
347 return (service->IsAcknowledged() ? 1 : 0);
350 Value ServicesTable::AcknowledgementTypeAccessor(const Value& row)
352 Service::Ptr service = static_cast<Service::Ptr>(row);
354 /* important: lock acknowledgements */
355 ObjectLock olock(service);
357 return static_cast<int>(service->GetAcknowledgement());
360 Value ServicesTable::NoMoreNotificationsAccessor(const Value& row)
362 /* TODO: notification_interval == 0, volatile == false */
366 Value ServicesTable::LastTimeOkAccessor(const Value& row)
372 Value ServicesTable::LastTimeWarningAccessor(const Value& row)
378 Value ServicesTable::LastTimeCriticalAccessor(const Value& row)
384 Value ServicesTable::LastTimeUnknownAccessor(const Value& row)
390 Value ServicesTable::LastCheckAccessor(const Value& row)
392 return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastCheck());
395 Value ServicesTable::NextCheckAccessor(const Value& row)
397 return static_cast<int>(static_cast<Service::Ptr>(row)->GetNextCheck());
400 Value ServicesTable::LastNotificationAccessor(const Value& row)
402 /* TODO Host->Service->GetNotifications->(loop)->GetLastNotification() */
406 Value ServicesTable::NextNotificationAccessor(const Value& row)
408 /* TODO Host->Service->GetNotifications->(loop)->GetLastNotification() */
412 Value ServicesTable::CurrentNotificationNumberAccessor(const Value& row)
414 /* TODO Host->Service->GetNotifications->(loop) new attribute */
418 Value ServicesTable::LastStateChangeAccessor(const Value& row)
420 return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastStateChange());
423 Value ServicesTable::LastHardStateChangeAccessor(const Value& row)
425 return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastHardStateChange());
428 Value ServicesTable::ScheduledDowntimeDepthAccessor(const Value& row)
430 return static_cast<Service::Ptr>(row)->GetDowntimeDepth();
433 Value ServicesTable::IsFlappingAccessor(const Value& row)
435 return static_cast<Service::Ptr>(row)->IsFlapping();
438 Value ServicesTable::ChecksEnabledAccessor(const Value& row)
440 return (static_cast<Service::Ptr>(row)->GetEnableActiveChecks() ? 1 : 0);
443 Value ServicesTable::AcceptPassiveChecksAccessor(const Value& row)
445 return (static_cast<Service::Ptr>(row)->GetEnablePassiveChecks() ? 1 : 0);
448 Value ServicesTable::EventHandlerEnabledAccessor(const Value& row)
450 /* TODO always enabled*/
454 Value ServicesTable::NotificationsEnabledAccessor(const Value& row)
456 return (static_cast<Service::Ptr>(row)->GetEnableNotifications() ? 1 : 0);
459 Value ServicesTable::ProcessPerformanceDataAccessor(const Value& row)
461 /* TODO always enabled */
465 Value ServicesTable::IsExecutingAccessor(const Value& row)
467 /* TODO does that make sense with Icinga2? */
471 Value ServicesTable::ActiveChecksEnabledAccessor(const Value& row)
473 return (static_cast<Service::Ptr>(row)->GetEnableActiveChecks() ? 1 : 0);
476 Value ServicesTable::CheckOptionsAccessor(const Value& row)
478 /* TODO - forcexec, freshness, orphan, none */
482 Value ServicesTable::FlapDetectionEnabledAccessor(const Value& row)
484 return (static_cast<Service::Ptr>(row)->GetEnableFlapping() ? 1 : 0);
487 Value ServicesTable::CheckFreshnessAccessor(const Value& row)
493 Value ServicesTable::ObsessOverServiceAccessor(const Value& row)
499 Value ServicesTable::ModifiedAttributesAccessor(const Value& row)
505 Value ServicesTable::ModifiedAttributesListAccessor(const Value& row)
511 Value ServicesTable::PnpgraphPresentAccessor(const Value& row)
517 Value ServicesTable::CheckIntervalAccessor(const Value& row)
519 return (static_cast<Service::Ptr>(row)->GetCheckInterval() / 60.0);
522 Value ServicesTable::RetryIntervalAccessor(const Value& row)
524 return (static_cast<Service::Ptr>(row)->GetRetryInterval() / 60.0);
527 Value ServicesTable::NotificationIntervalAccessor(const Value& row)
529 /* TODO Host->Services->GetNotifications->(loop)->GetNotificationInterval() */
533 Value ServicesTable::FirstNotificationDelayAccessor(const Value& row)
539 Value ServicesTable::LowFlapThresholdAccessor(const Value& row)
545 Value ServicesTable::HighFlapThresholdAccessor(const Value& row)
551 Value ServicesTable::LatencyAccessor(const Value& row)
553 return (Service::CalculateLatency(static_cast<Service::Ptr>(row)->GetLastCheckResult()));
556 Value ServicesTable::ExecutionTimeAccessor(const Value& row)
558 return (Service::CalculateExecutionTime(static_cast<Service::Ptr>(row)->GetLastCheckResult()));
561 Value ServicesTable::PercentStateChangeAccessor(const Value& row)
563 return static_cast<Service::Ptr>(row)->GetFlappingCurrent();
566 Value ServicesTable::InCheckPeriodAccessor(const Value& row)
572 Value ServicesTable::InNotificationPeriodAccessor(const Value& row)
578 Value ServicesTable::ContactsAccessor(const Value& row)
580 /* TODO - host->service->notifications->users */
582 std::set<User::Ptr> allUsers;
583 std::set<User::Ptr> users;
585 BOOST_FOREACH(const Notification::Ptr& notification, static_cast<Service::Ptr>(row)->GetNotifications()) {
586 ObjectLock olock(notification);
588 users = notification->GetUsers();
590 std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
592 BOOST_FOREACH(const UserGroup::Ptr& ug, notification->GetGroups()) {
593 std::set<User::Ptr> members = ug->GetMembers();
594 std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
601 Value ServicesTable::DowntimesAccessor(const Value& row)
607 Value ServicesTable::DowntimesWithInfoAccessor(const Value& row)
613 Value ServicesTable::CommentsAccessor(const Value& row)
619 Value ServicesTable::CommentsWithInfoAccessor(const Value& row)
625 Value ServicesTable::CommentsWithExtraInfoAccessor(const Value& row)
631 Value ServicesTable::CustomVariableNamesAccessor(const Value& row)
637 Value ServicesTable::CustomVariableValuesAccessor(const Value& row)
643 Value ServicesTable::CustomVariablesAccessor(const Value& row)
646 Service::Ptr svc = static_cast<Service::Ptr>(row);
648 Dictionary::Ptr custom = svc->Get("custom");
653 Dictionary::Ptr customvars = custom->ShallowClone();
654 customvars->Remove("notes");
655 customvars->Remove("action_url");
656 customvars->Remove("notes_url");
657 customvars->Remove("icon_image");
658 customvars->Remove("icon_image_alt");
659 customvars->Remove("statusmap_image");
660 customvars->Remove("2d_coords");
668 Value ServicesTable::GroupsAccessor(const Value& row)
674 Value ServicesTable::ContactGroupsAccessor(const Value& row)