]> granicus.if.org Git - icinga2/blob - components/livestatus/servicestable.cpp
Merge branch 'feature/mkclass-4963' into next
[icinga2] / components / livestatus / servicestable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/)   *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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>
36
37 using namespace icinga;
38 using namespace livestatus;
39
40 ServicesTable::ServicesTable(void)
41 {
42         AddColumns(this);
43 }
44
45 void ServicesTable::AddColumns(Table *table, const String& prefix,
46     const Column::ObjectAccessor& objectAccessor)
47 {
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));
129
130         HostsTable::AddColumns(table, "host_", boost::bind(&ServicesTable::HostAccessor, _1, objectAccessor));
131 }
132
133 String ServicesTable::GetName(void) const
134 {
135         return "services";
136 }
137
138 void ServicesTable::FetchRows(const AddRowFunction& addRowFn)
139 {
140         BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
141                 addRowFn(service);
142         }
143 }
144
145 Object::Ptr ServicesTable::HostAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor)
146 {
147         Value service;
148
149         if (parentObjectAccessor)
150                 service = parentObjectAccessor(row);
151         else
152                 service = row;
153
154         return static_cast<Service::Ptr>(service)->GetHost();
155 }
156
157 Value ServicesTable::ShortNameAccessor(const Value& row)
158 {
159         return static_cast<Service::Ptr>(row)->GetShortName();
160 }
161
162 Value ServicesTable::DisplayNameAccessor(const Value& row)
163 {
164         return static_cast<Service::Ptr>(row)->GetDisplayName();
165 }
166
167 Value ServicesTable::CheckCommandAccessor(const Value& row)
168 {
169         CheckCommand::Ptr checkcommand = static_cast<Service::Ptr>(row)->GetCheckCommand();
170
171         if (checkcommand)
172                 return checkcommand->GetName(); /* this is the name without '!' args */
173
174         return Empty;
175 }
176
177 Value ServicesTable::CheckCommandExpandedAccessor(const Value& row)
178 {
179         Service::Ptr service = static_cast<Service::Ptr>(row);
180
181         CheckCommand::Ptr commandObj = service->GetCheckCommand();
182
183         if (!commandObj)
184                 return Empty;
185
186         Value raw_command = commandObj->GetCommandLine();
187
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());
193
194         Value commandLine = MacroProcessor::ResolveMacros(raw_command, resolvers, Dictionary::Ptr(), Utility::EscapeShellCmd);
195
196         String buf;
197         if (commandLine.IsObjectType<Array>()) {
198                 Array::Ptr args = commandLine;
199
200                 ObjectLock olock(args);
201                 String arg;
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");
206                         buf += argitem;
207                 }
208         } else if (!commandLine.IsEmpty()) {
209                 String args = Convert::ToString(commandLine);
210                 boost::algorithm::replace_all(args, "\n", "\\n");
211                 buf += args;
212         } else {
213                 buf += "<internal>";
214         }
215
216         return buf;
217 }
218
219 Value ServicesTable::EventHandlerAccessor(const Value& row)
220 {
221         EventCommand::Ptr eventcommand = static_cast<Service::Ptr>(row)->GetEventCommand();
222
223         if (eventcommand)
224                 return eventcommand->GetName();
225
226         return Empty;
227 }
228
229 Value ServicesTable::PluginOutputAccessor(const Value& row)
230 {
231         return static_cast<Service::Ptr>(row)->GetLastCheckOutput();
232 }
233
234 Value ServicesTable::LongPluginOutputAccessor(const Value& row)
235 {
236         return static_cast<Service::Ptr>(row)->GetLastCheckLongOutput();
237 }
238
239 Value ServicesTable::PerfDataAccessor(const Value& row)
240 {
241         return static_cast<Service::Ptr>(row)->GetLastCheckPerfData();
242 }
243
244 Value ServicesTable::NotificationPeriodAccessor(const Value& row)
245 {
246         BOOST_FOREACH(const Notification::Ptr& notification, static_cast<Service::Ptr>(row)->GetNotifications()) {
247                 ObjectLock olock(notification);
248
249                 TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod();
250
251                 /* XXX first notification wins */
252                 if (timeperiod)
253                         return timeperiod->GetName();
254         }
255
256         return Empty;
257 }
258
259 Value ServicesTable::CheckPeriodAccessor(const Value& row)
260 {
261         TimePeriod::Ptr timeperiod = static_cast<Service::Ptr>(row)->GetCheckPeriod();
262
263         if (!timeperiod)
264                 return Empty;
265
266         return timeperiod->GetName();
267 }
268
269 Value ServicesTable::NotesAccessor(const Value& row)
270 {
271         Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
272
273         if (!custom)
274                 return Empty;
275
276         return custom->Get("notes");
277 }
278
279 Value ServicesTable::NotesExpandedAccessor(const Value& row)
280 {
281         Service::Ptr service = static_cast<Service::Ptr>(row);
282         Dictionary::Ptr custom = service->GetCustom();
283
284         if (!custom)
285                 return Empty;
286
287         std::vector<MacroResolver::Ptr> resolvers;
288         resolvers.push_back(service);
289         resolvers.push_back(service->GetHost());
290         resolvers.push_back(IcingaApplication::GetInstance());
291
292         Value value = custom->Get("notes");
293
294         Dictionary::Ptr cr;
295         Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
296
297         return value_expanded;
298 }
299
300 Value ServicesTable::NotesUrlAccessor(const Value& row)
301 {
302         Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
303
304         if (!custom)
305                 return Empty;
306
307         return custom->Get("notes_url");
308 }
309
310 Value ServicesTable::NotesUrlExpandedAccessor(const Value& row)
311 {
312         Service::Ptr service = static_cast<Service::Ptr>(row);
313         Dictionary::Ptr custom = service->GetCustom();
314
315         if (!custom)
316                 return Empty;
317
318         std::vector<MacroResolver::Ptr> resolvers;
319         resolvers.push_back(service);
320         resolvers.push_back(service->GetHost());
321         resolvers.push_back(IcingaApplication::GetInstance());
322
323         Value value = custom->Get("notes_url");
324
325         Dictionary::Ptr cr;
326         Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
327
328         return value_expanded;
329 }
330
331 Value ServicesTable::ActionUrlAccessor(const Value& row)
332 {
333         Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
334
335         if (!custom)
336                 return Empty;
337
338         return custom->Get("action_url");
339 }
340
341 Value ServicesTable::ActionUrlExpandedAccessor(const Value& row)
342 {
343         Service::Ptr service = static_cast<Service::Ptr>(row);
344         Dictionary::Ptr custom = service->GetCustom();
345
346         if (!custom)
347                 return Empty;
348
349         std::vector<MacroResolver::Ptr> resolvers;
350         resolvers.push_back(service);
351         resolvers.push_back(service->GetHost());
352         resolvers.push_back(IcingaApplication::GetInstance());
353
354         Value value = custom->Get("action_url");
355
356         Dictionary::Ptr cr;
357         Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
358
359         return value_expanded;
360 }
361
362 Value ServicesTable::IconImageAccessor(const Value& row)
363 {
364         Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
365
366         if (!custom)
367                 return Empty;
368
369         return custom->Get("icon_image");
370 }
371
372 Value ServicesTable::IconImageExpandedAccessor(const Value& row)
373 {
374         Service::Ptr service = static_cast<Service::Ptr>(row);
375         Dictionary::Ptr custom = service->GetCustom();
376
377         if (!custom)
378                 return Empty;
379
380         std::vector<MacroResolver::Ptr> resolvers;
381         resolvers.push_back(service);
382         resolvers.push_back(service->GetHost());
383         resolvers.push_back(IcingaApplication::GetInstance());
384
385         Value value = custom->Get("icon_image");
386
387         Dictionary::Ptr cr;
388         Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
389
390         return value_expanded;
391 }
392
393 Value ServicesTable::IconImageAltAccessor(const Value& row)
394 {
395         Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
396
397         if (!custom)
398                 return Empty;
399
400         return custom->Get("icon_image_alt");
401 }
402
403 Value ServicesTable::MaxCheckAttemptsAccessor(const Value& row)
404 {
405         return static_cast<Service::Ptr>(row)->GetMaxCheckAttempts();
406 }
407
408 Value ServicesTable::CurrentAttemptAccessor(const Value& row)
409 {
410         return static_cast<Service::Ptr>(row)->GetCheckAttempt();
411 }
412
413 Value ServicesTable::StateAccessor(const Value& row)
414 {
415         return static_cast<Service::Ptr>(row)->GetState();
416 }
417
418 Value ServicesTable::HasBeenCheckedAccessor(const Value& row)
419 {
420         return (static_cast<Service::Ptr>(row)->HasBeenChecked() ? 1 : 0);
421 }
422
423 Value ServicesTable::LastStateAccessor(const Value& row)
424 {
425         return static_cast<Service::Ptr>(row)->GetLastState();
426 }
427
428 Value ServicesTable::LastHardStateAccessor(const Value& row)
429 {
430         return static_cast<Service::Ptr>(row)->GetLastHardState();
431 }
432
433 Value ServicesTable::StateTypeAccessor(const Value& row)
434 {
435         return static_cast<Service::Ptr>(row)->GetStateType();
436 }
437
438 Value ServicesTable::CheckTypeAccessor(const Value& row)
439 {
440         return (static_cast<Service::Ptr>(row)->GetEnableActiveChecks() ? 0 : 1);
441 }
442
443 Value ServicesTable::AcknowledgedAccessor(const Value& row)
444 {
445         Service::Ptr service = static_cast<Service::Ptr>(row);
446
447         /* important: lock acknowledgements */
448         ObjectLock olock(service);
449
450         return (service->IsAcknowledged() ? 1 : 0);
451 }
452
453 Value ServicesTable::AcknowledgementTypeAccessor(const Value& row)
454 {
455         Service::Ptr service = static_cast<Service::Ptr>(row);
456
457         /* important: lock acknowledgements */
458         ObjectLock olock(service);
459
460         return static_cast<int>(service->GetAcknowledgement());
461 }
462
463 Value ServicesTable::NoMoreNotificationsAccessor(const Value& row)
464 {
465         Service::Ptr service = static_cast<Service::Ptr>(row);
466
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();
472         }
473
474         if (notification_interval == 0 && !service->GetVolatile())
475                 return 1;
476
477         return 0;
478 }
479
480 Value ServicesTable::LastTimeOkAccessor(const Value& row)
481 {
482         return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastStateOK());
483 }
484
485 Value ServicesTable::LastTimeWarningAccessor(const Value& row)
486 {
487         return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastStateWarning());
488 }
489
490 Value ServicesTable::LastTimeCriticalAccessor(const Value& row)
491 {
492         return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastStateCritical());
493 }
494
495 Value ServicesTable::LastTimeUnknownAccessor(const Value& row)
496 {
497         return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastStateUnknown());
498 }
499
500 Value ServicesTable::LastCheckAccessor(const Value& row)
501 {
502         return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastCheck());
503 }
504
505 Value ServicesTable::NextCheckAccessor(const Value& row)
506 {
507         return static_cast<int>(static_cast<Service::Ptr>(row)->GetNextCheck());
508 }
509
510 Value ServicesTable::LastNotificationAccessor(const Value& row)
511 {
512         Service::Ptr service = static_cast<Service::Ptr>(row);
513
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();
519         }
520
521         return static_cast<int>(last_notification);
522 }
523
524 Value ServicesTable::NextNotificationAccessor(const Value& row)
525 {
526         Service::Ptr service = static_cast<Service::Ptr>(row);
527
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();
533         }
534
535         return static_cast<int>(next_notification);
536 }
537
538 Value ServicesTable::CurrentNotificationNumberAccessor(const Value& row)
539 {
540         Service::Ptr service = static_cast<Service::Ptr>(row);
541
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();
547         }
548
549         return notification_number;
550 }
551
552 Value ServicesTable::LastStateChangeAccessor(const Value& row)
553 {
554         return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastStateChange());
555 }
556
557 Value ServicesTable::LastHardStateChangeAccessor(const Value& row)
558 {
559         return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastHardStateChange());
560 }
561
562 Value ServicesTable::ScheduledDowntimeDepthAccessor(const Value& row)
563 {
564         return static_cast<Service::Ptr>(row)->GetDowntimeDepth();
565 }
566
567 Value ServicesTable::IsFlappingAccessor(const Value& row)
568 {
569         return static_cast<Service::Ptr>(row)->IsFlapping();
570 }
571
572 Value ServicesTable::ChecksEnabledAccessor(const Value& row)
573 {
574         return (static_cast<Service::Ptr>(row)->GetEnableActiveChecks() ? 1 : 0);
575 }
576
577 Value ServicesTable::AcceptPassiveChecksAccessor(const Value& row)
578 {
579         return (static_cast<Service::Ptr>(row)->GetEnablePassiveChecks() ? 1 : 0);
580 }
581
582 Value ServicesTable::EventHandlerEnabledAccessor(const Value& row)
583 {
584         EventCommand::Ptr eventcommand = static_cast<Service::Ptr>(row)->GetEventCommand();
585
586         if (eventcommand)
587                 return 1;
588
589         return 0;
590 }
591
592 Value ServicesTable::NotificationsEnabledAccessor(const Value& row)
593 {
594         return (static_cast<Service::Ptr>(row)->GetEnableNotifications() ? 1 : 0);
595 }
596
597 Value ServicesTable::ActiveChecksEnabledAccessor(const Value& row)
598 {
599         return (static_cast<Service::Ptr>(row)->GetEnableActiveChecks() ? 1 : 0);
600 }
601
602 Value ServicesTable::CheckOptionsAccessor(const Value& row)
603 {
604         /* TODO - forcexec, freshness, orphan, none */
605         return Empty;
606 }
607
608 Value ServicesTable::FlapDetectionEnabledAccessor(const Value& row)
609 {
610         return (static_cast<Service::Ptr>(row)->GetEnableFlapping() ? 1 : 0);
611 }
612
613 Value ServicesTable::CheckFreshnessAccessor(const Value& row)
614 {
615         /* always enabled */
616         return 1;
617 }
618
619 Value ServicesTable::ModifiedAttributesAccessor(const Value& row)
620 {
621         return static_cast<Service::Ptr>(row)->GetModifiedAttributes();
622 }
623
624 Value ServicesTable::ModifiedAttributesListAccessor(const Value& row)
625 {
626         /* not supported */
627         return Empty;
628 }
629
630 Value ServicesTable::StalenessAccessor(const Value& row)
631 {
632         Service::Ptr service = static_cast<Service::Ptr>(row);
633
634         if (service->HasBeenChecked() && service->GetLastCheck() > 0)
635                 return (Utility::GetTime() - service->GetLastCheck()) / (service->GetCheckInterval() * 3600);
636
637         return Empty;
638 }
639
640 Value ServicesTable::CheckIntervalAccessor(const Value& row)
641 {
642         return (static_cast<Service::Ptr>(row)->GetCheckInterval() / 60.0);
643 }
644
645 Value ServicesTable::RetryIntervalAccessor(const Value& row)
646 {
647         return (static_cast<Service::Ptr>(row)->GetRetryInterval() / 60.0);
648 }
649
650 Value ServicesTable::NotificationIntervalAccessor(const Value& row)
651 {
652         Service::Ptr service = static_cast<Service::Ptr>(row);
653
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();
659         }
660
661         if (notification_interval == -1)
662                 notification_interval = 60;
663
664         return (notification_interval / 60.0);
665 }
666
667 Value ServicesTable::LowFlapThresholdAccessor(const Value& row)
668 {
669         return static_cast<Service::Ptr>(row)->GetFlappingThreshold();
670 }
671
672 Value ServicesTable::HighFlapThresholdAccessor(const Value& row)
673 {
674         return static_cast<Service::Ptr>(row)->GetFlappingThreshold();
675 }
676
677 Value ServicesTable::LatencyAccessor(const Value& row)
678 {
679         return (Service::CalculateLatency(static_cast<Service::Ptr>(row)->GetLastCheckResult()));
680 }
681
682 Value ServicesTable::ExecutionTimeAccessor(const Value& row)
683 {
684         return (Service::CalculateExecutionTime(static_cast<Service::Ptr>(row)->GetLastCheckResult()));
685 }
686
687 Value ServicesTable::PercentStateChangeAccessor(const Value& row)
688 {
689         return static_cast<Service::Ptr>(row)->GetFlappingCurrent();
690 }
691
692 Value ServicesTable::InCheckPeriodAccessor(const Value& row)
693 {
694         TimePeriod::Ptr timeperiod = static_cast<Service::Ptr>(row)->GetCheckPeriod();
695
696         /* none set means always checked */
697         if (!timeperiod)
698                 return 1;
699
700         return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
701 }
702
703 Value ServicesTable::InNotificationPeriodAccessor(const Value& row)
704 {
705         BOOST_FOREACH(const Notification::Ptr& notification, static_cast<Service::Ptr>(row)->GetNotifications()) {
706                 ObjectLock olock(notification);
707
708                 TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod();
709
710                 /* XXX first notification wins */
711                 if (timeperiod)
712                         return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
713         }
714
715         /* none set means always notified */
716         return 1;
717 }
718
719 Value ServicesTable::ContactsAccessor(const Value& row)
720 {
721         Array::Ptr contact_names = boost::make_shared<Array>();
722
723         BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetServiceNotificationUsers(static_cast<Service::Ptr>(row))) {
724                 contact_names->Add(user->GetName());
725         }
726
727         return contact_names;
728 }
729
730 Value ServicesTable::DowntimesAccessor(const Value& row)
731 {
732         Dictionary::Ptr downtimes = static_cast<Service::Ptr>(row)->GetDowntimes();
733
734         Array::Ptr ids = boost::make_shared<Array>();
735
736         ObjectLock olock(downtimes);
737
738         String id;
739         Dictionary::Ptr downtime;
740         BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
741
742                 if (!downtime)
743                         continue;
744
745                 if (Service::IsDowntimeExpired(downtime))
746                         continue;
747
748                 ids->Add(downtime->Get("legacy_id"));
749         }
750
751         return ids;
752 }
753
754 Value ServicesTable::DowntimesWithInfoAccessor(const Value& row)
755 {
756         Dictionary::Ptr downtimes = static_cast<Service::Ptr>(row)->GetDowntimes();
757
758         Array::Ptr ids = boost::make_shared<Array>();
759
760         ObjectLock olock(downtimes);
761
762         String id;
763         Dictionary::Ptr downtime;
764         BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
765
766                 if (!downtime)
767                         continue;
768
769                 if (Service::IsDowntimeExpired(downtime))
770                         continue;
771
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);
777         }
778
779         return ids;
780 }
781
782 Value ServicesTable::CommentsAccessor(const Value& row)
783 {
784         Dictionary::Ptr comments = static_cast<Service::Ptr>(row)->GetComments();
785
786         Array::Ptr ids = boost::make_shared<Array>();
787
788         ObjectLock olock(comments);
789
790         String id;
791         Dictionary::Ptr comment;
792         BOOST_FOREACH(boost::tie(id, comment), comments) {
793
794                 if (!comment)
795                         continue;
796
797                 if (Service::IsCommentExpired(comment))
798                         continue;
799
800                 ids->Add(comment->Get("legacy_id"));
801         }
802
803         return ids;
804 }
805
806 Value ServicesTable::CommentsWithInfoAccessor(const Value& row)
807 {
808         Dictionary::Ptr comments = static_cast<Service::Ptr>(row)->GetComments();
809
810         Array::Ptr ids = boost::make_shared<Array>();
811
812         ObjectLock olock(comments);
813
814         String id;
815         Dictionary::Ptr comment;
816         BOOST_FOREACH(boost::tie(id, comment), comments) {
817
818                 if (!comment)
819                         continue;
820
821                 if (Service::IsCommentExpired(comment))
822                         continue;
823
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);
829         }
830
831         return ids;
832 }
833
834 Value ServicesTable::CommentsWithExtraInfoAccessor(const Value& row)
835 {
836         Dictionary::Ptr comments = static_cast<Service::Ptr>(row)->GetComments();
837
838         Array::Ptr ids = boost::make_shared<Array>();
839
840         ObjectLock olock(comments);
841
842         String id;
843         Dictionary::Ptr comment;
844         BOOST_FOREACH(boost::tie(id, comment), comments) {
845
846                 if (!comment)
847                         continue;
848
849                 if (Service::IsCommentExpired(comment))
850                         continue;
851
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);
859         }
860
861         return ids;
862 }
863
864 Value ServicesTable::CustomVariableNamesAccessor(const Value& row)
865 {
866         Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
867
868         if (!custom)
869                 return Empty;
870
871         Array::Ptr cv = boost::make_shared<Array>();
872
873         ObjectLock olock(custom);
874         String key;
875         Value value;
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" ||
883                     key == "2d_coords")
884                         continue;
885
886                 cv->Add(key);
887         }
888
889         return cv;
890 }
891
892 Value ServicesTable::CustomVariableValuesAccessor(const Value& row)
893 {
894         Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
895
896         if (!custom)
897                 return Empty;
898
899         Array::Ptr cv = boost::make_shared<Array>();
900
901         ObjectLock olock(custom);
902         String key;
903         Value value;
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" ||
911                     key == "2d_coords")
912                         continue;
913
914                 cv->Add(value);
915         }
916
917         return cv;
918 }
919
920 Value ServicesTable::CustomVariablesAccessor(const Value& row)
921 {
922         Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
923
924         if (!custom)
925                 return Empty;
926
927         Array::Ptr cv = boost::make_shared<Array>();
928
929         ObjectLock olock(custom);
930         String key;
931         Value value;
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" ||
939                     key == "2d_coords")
940                         continue;
941
942                 Array::Ptr key_val = boost::make_shared<Array>();
943                 key_val->Add(key);
944                 key_val->Add(value);
945                 cv->Add(key_val);
946         }
947
948         return cv;
949 }
950
951 Value ServicesTable::GroupsAccessor(const Value& row)
952 {
953         Array::Ptr groups = static_cast<Service::Ptr>(row)->GetGroups();
954
955         if (!groups)
956                 return Empty;
957
958         return groups;
959 }
960
961 Value ServicesTable::ContactGroupsAccessor(const Value& row)
962 {
963         Array::Ptr contactgroup_names = boost::make_shared<Array>();
964
965         BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetServiceNotificationUserGroups(static_cast<Service::Ptr>(row))) {
966                 contactgroup_names->Add(usergroup->GetName());
967         }
968
969         return contactgroup_names;
970 }
971
972