]> granicus.if.org Git - icinga2/blob - components/livestatus/servicestable.cpp
Livestatus: Fix check_command_expanded column.
[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
39 ServicesTable::ServicesTable(void)
40 {
41         AddColumns(this);
42 }
43
44 void ServicesTable::AddColumns(Table *table, const String& prefix,
45     const Column::ObjectAccessor& objectAccessor)
46 {
47         table->AddColumn(prefix + "description", Column(&ServicesTable::ShortNameAccessor, objectAccessor));
48         table->AddColumn(prefix + "display_name", Column(&ServicesTable::DisplayNameAccessor, objectAccessor));
49         table->AddColumn(prefix + "check_command", Column(&ServicesTable::CheckCommandAccessor, objectAccessor));
50         table->AddColumn(prefix + "check_command_expanded", Column(&ServicesTable::CheckCommandExpandedAccessor, objectAccessor));
51         table->AddColumn(prefix + "event_handler", Column(&ServicesTable::EventHandlerAccessor, objectAccessor));
52         table->AddColumn(prefix + "plugin_output", Column(&ServicesTable::PluginOutputAccessor, objectAccessor));
53         table->AddColumn(prefix + "long_plugin_output", Column(&ServicesTable::LongPluginOutputAccessor, objectAccessor));
54         table->AddColumn(prefix + "perf_data", Column(&ServicesTable::PerfDataAccessor, objectAccessor));
55         table->AddColumn(prefix + "notification_period", Column(&ServicesTable::NotificationPeriodAccessor, objectAccessor));
56         table->AddColumn(prefix + "check_period", Column(&ServicesTable::CheckPeriodAccessor, objectAccessor));
57         table->AddColumn(prefix + "notes", Column(&ServicesTable::NotesAccessor, objectAccessor));
58         table->AddColumn(prefix + "notes_expanded", Column(&ServicesTable::NotesExpandedAccessor, objectAccessor));
59         table->AddColumn(prefix + "notes_url", Column(&ServicesTable::NotesUrlAccessor, objectAccessor));
60         table->AddColumn(prefix + "notes_url_expanded", Column(&ServicesTable::NotesUrlExpandedAccessor, objectAccessor));
61         table->AddColumn(prefix + "action_url", Column(&ServicesTable::ActionUrlAccessor, objectAccessor));
62         table->AddColumn(prefix + "action_url_expanded", Column(&ServicesTable::ActionUrlExpandedAccessor, objectAccessor));
63         table->AddColumn(prefix + "icon_image", Column(&ServicesTable::IconImageAccessor, objectAccessor));
64         table->AddColumn(prefix + "icon_image_expanded", Column(&ServicesTable::IconImageExpandedAccessor, objectAccessor));
65         table->AddColumn(prefix + "icon_image_alt", Column(&ServicesTable::IconImageAltAccessor, objectAccessor));
66         table->AddColumn(prefix + "initial_state", Column(&Table::EmptyStringAccessor, objectAccessor));
67         table->AddColumn(prefix + "max_check_attempts", Column(&ServicesTable::MaxCheckAttemptsAccessor, objectAccessor));
68         table->AddColumn(prefix + "current_attempt", Column(&ServicesTable::CurrentAttemptAccessor, objectAccessor));
69         table->AddColumn(prefix + "state", Column(&ServicesTable::StateAccessor, objectAccessor));
70         table->AddColumn(prefix + "has_been_checked", Column(&ServicesTable::HasBeenCheckedAccessor, objectAccessor));
71         table->AddColumn(prefix + "last_state", Column(&ServicesTable::LastStateAccessor, objectAccessor));
72         table->AddColumn(prefix + "last_hard_state", Column(&ServicesTable::LastHardStateAccessor, objectAccessor));
73         table->AddColumn(prefix + "state_type", Column(&ServicesTable::StateTypeAccessor, objectAccessor));
74         table->AddColumn(prefix + "check_type", Column(&ServicesTable::CheckTypeAccessor, objectAccessor));
75         table->AddColumn(prefix + "acknowledged", Column(&ServicesTable::AcknowledgedAccessor, objectAccessor));
76         table->AddColumn(prefix + "acknowledgement_type", Column(&ServicesTable::AcknowledgementTypeAccessor, objectAccessor));
77         table->AddColumn(prefix + "no_more_notifications", Column(&ServicesTable::NoMoreNotificationsAccessor, objectAccessor));
78         table->AddColumn(prefix + "last_time_ok", Column(&ServicesTable::LastTimeOkAccessor, objectAccessor));
79         table->AddColumn(prefix + "last_time_warning", Column(&ServicesTable::LastTimeWarningAccessor, objectAccessor));
80         table->AddColumn(prefix + "last_time_critical", Column(&ServicesTable::LastTimeCriticalAccessor, objectAccessor));
81         table->AddColumn(prefix + "last_time_unknown", Column(&ServicesTable::LastTimeUnknownAccessor, objectAccessor));
82         table->AddColumn(prefix + "last_check", Column(&ServicesTable::LastCheckAccessor, objectAccessor));
83         table->AddColumn(prefix + "next_check", Column(&ServicesTable::NextCheckAccessor, objectAccessor));
84         table->AddColumn(prefix + "last_notification", Column(&ServicesTable::LastNotificationAccessor, objectAccessor));
85         table->AddColumn(prefix + "next_notification", Column(&ServicesTable::NextNotificationAccessor, objectAccessor));
86         table->AddColumn(prefix + "current_notification_number", Column(&ServicesTable::CurrentNotificationNumberAccessor, objectAccessor));
87         table->AddColumn(prefix + "last_state_change", Column(&ServicesTable::LastStateChangeAccessor, objectAccessor));
88         table->AddColumn(prefix + "last_hard_state_change", Column(&ServicesTable::LastHardStateChangeAccessor, objectAccessor));
89         table->AddColumn(prefix + "scheduled_downtime_depth", Column(&ServicesTable::ScheduledDowntimeDepthAccessor, objectAccessor));
90         table->AddColumn(prefix + "is_flapping", Column(&ServicesTable::IsFlappingAccessor, objectAccessor));
91         table->AddColumn(prefix + "checks_enabled", Column(&ServicesTable::ChecksEnabledAccessor, objectAccessor));
92         table->AddColumn(prefix + "accept_passive_checks", Column(&ServicesTable::AcceptPassiveChecksAccessor, objectAccessor));
93         table->AddColumn(prefix + "event_handler_enabled", Column(&ServicesTable::EventHandlerEnabledAccessor, objectAccessor));
94         table->AddColumn(prefix + "notifications_enabled", Column(&ServicesTable::NotificationsEnabledAccessor, objectAccessor));
95         table->AddColumn(prefix + "process_performance_data", Column(&Table::OneAccessor, objectAccessor));
96         table->AddColumn(prefix + "is_executing", Column(&Table::ZeroAccessor, objectAccessor));
97         table->AddColumn(prefix + "active_checks_enabled", Column(&ServicesTable::ActiveChecksEnabledAccessor, objectAccessor));
98         table->AddColumn(prefix + "check_options", Column(&ServicesTable::CheckOptionsAccessor, objectAccessor));
99         table->AddColumn(prefix + "flap_detection_enabled", Column(&ServicesTable::FlapDetectionEnabledAccessor, objectAccessor));
100         table->AddColumn(prefix + "check_freshness", Column(&ServicesTable::CheckFreshnessAccessor, objectAccessor));
101         table->AddColumn(prefix + "obsess_over_service", Column(&Table::ZeroAccessor, objectAccessor));
102         table->AddColumn(prefix + "modified_attributes", Column(&ServicesTable::ModifiedAttributesAccessor, objectAccessor));
103         table->AddColumn(prefix + "modified_attributes_list", Column(&ServicesTable::ModifiedAttributesListAccessor, objectAccessor));
104         table->AddColumn(prefix + "pnpgraph_present", Column(&Table::ZeroAccessor, objectAccessor));
105         table->AddColumn(prefix + "staleness", Column(&ServicesTable::StalenessAccessor, objectAccessor));
106         table->AddColumn(prefix + "check_interval", Column(&ServicesTable::CheckIntervalAccessor, objectAccessor));
107         table->AddColumn(prefix + "retry_interval", Column(&ServicesTable::RetryIntervalAccessor, objectAccessor));
108         table->AddColumn(prefix + "notification_interval", Column(&ServicesTable::NotificationIntervalAccessor, objectAccessor));
109         table->AddColumn(prefix + "first_notification_delay", Column(&Table::EmptyStringAccessor, objectAccessor));
110         table->AddColumn(prefix + "low_flap_threshold", Column(&ServicesTable::LowFlapThresholdAccessor, objectAccessor));
111         table->AddColumn(prefix + "high_flap_threshold", Column(&ServicesTable::HighFlapThresholdAccessor, objectAccessor));
112         table->AddColumn(prefix + "latency", Column(&ServicesTable::LatencyAccessor, objectAccessor));
113         table->AddColumn(prefix + "execution_time", Column(&ServicesTable::ExecutionTimeAccessor, objectAccessor));
114         table->AddColumn(prefix + "percent_state_change", Column(&ServicesTable::PercentStateChangeAccessor, objectAccessor));
115         table->AddColumn(prefix + "in_check_period", Column(&ServicesTable::InCheckPeriodAccessor, objectAccessor));
116         table->AddColumn(prefix + "in_notification_period", Column(&ServicesTable::InNotificationPeriodAccessor, objectAccessor));
117         table->AddColumn(prefix + "contacts", Column(&ServicesTable::ContactsAccessor, objectAccessor));
118         table->AddColumn(prefix + "downtimes", Column(&ServicesTable::DowntimesAccessor, objectAccessor));
119         table->AddColumn(prefix + "downtimes_with_info", Column(&ServicesTable::DowntimesWithInfoAccessor, objectAccessor));
120         table->AddColumn(prefix + "comments", Column(&ServicesTable::CommentsAccessor, objectAccessor));
121         table->AddColumn(prefix + "comments_with_info", Column(&ServicesTable::CommentsWithInfoAccessor, objectAccessor));
122         table->AddColumn(prefix + "comments_with_extra_info", Column(&ServicesTable::CommentsWithExtraInfoAccessor, objectAccessor));
123         table->AddColumn(prefix + "custom_variable_names", Column(&ServicesTable::CustomVariableNamesAccessor, objectAccessor));
124         table->AddColumn(prefix + "custom_variable_values", Column(&ServicesTable::CustomVariableValuesAccessor, objectAccessor));
125         table->AddColumn(prefix + "custom_variables", Column(&ServicesTable::CustomVariablesAccessor, objectAccessor));
126         table->AddColumn(prefix + "groups", Column(&ServicesTable::GroupsAccessor, objectAccessor));
127         table->AddColumn(prefix + "contact_groups", Column(&ServicesTable::ContactGroupsAccessor, objectAccessor));
128
129         HostsTable::AddColumns(table, "host_", boost::bind(&ServicesTable::HostAccessor, _1, objectAccessor));
130 }
131
132 String ServicesTable::GetName(void) const
133 {
134         return "services";
135 }
136
137 void ServicesTable::FetchRows(const AddRowFunction& addRowFn)
138 {
139         BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
140                 addRowFn(service);
141         }
142 }
143
144 Object::Ptr ServicesTable::HostAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor)
145 {
146         Value service;
147
148         if (parentObjectAccessor)
149                 service = parentObjectAccessor(row);
150         else
151                 service = row;
152
153         Service::Ptr svc = static_cast<Service::Ptr>(service);
154
155         if (!svc)
156                 return Object::Ptr();
157
158         return svc->GetHost();
159 }
160
161 Value ServicesTable::ShortNameAccessor(const Value& row)
162 {
163         Service::Ptr service = static_cast<Service::Ptr>(row);
164
165         if (!service)
166                 return Empty;
167
168         return service->GetShortName();
169 }
170
171 Value ServicesTable::DisplayNameAccessor(const Value& row)
172 {
173         Service::Ptr service = static_cast<Service::Ptr>(row);
174
175         if (!service)
176                 return Empty;
177
178         return service->GetDisplayName();
179 }
180
181 Value ServicesTable::CheckCommandAccessor(const Value& row)
182 {
183         Service::Ptr service = static_cast<Service::Ptr>(row);
184
185         if (!service)
186                 return Empty;
187
188         CheckCommand::Ptr checkcommand = service->GetCheckCommand();
189
190         if (checkcommand)
191                 return checkcommand->GetName(); /* this is the name without '!' args */
192
193         return Empty;
194 }
195
196 Value ServicesTable::CheckCommandExpandedAccessor(const Value& row)
197 {
198         Service::Ptr service = static_cast<Service::Ptr>(row);
199
200         if (!service)
201                 return Empty;
202
203         CheckCommand::Ptr checkcommand = service->GetCheckCommand();
204
205         if (checkcommand)
206                 return checkcommand->GetName(); /* this is the name without '!' args */
207
208         return Empty;
209 }
210
211 Value ServicesTable::EventHandlerAccessor(const Value& row)
212 {
213         Service::Ptr service = static_cast<Service::Ptr>(row);
214
215         if (!service)
216                 return Empty;
217
218         EventCommand::Ptr eventcommand = service->GetEventCommand();
219
220         if (eventcommand)
221                 return eventcommand->GetName();
222
223         return Empty;
224 }
225
226 Value ServicesTable::PluginOutputAccessor(const Value& row)
227 {
228         Service::Ptr service = static_cast<Service::Ptr>(row);
229
230         if (!service)
231                 return Empty;
232
233         String output;
234         CheckResult::Ptr cr = service->GetLastCheckResult();
235
236         if (cr) {
237                 std::pair<String, String> output_bag = CompatUtility::GetCheckResultOutput(cr);
238                 output = output_bag.first;
239         }
240
241         return output;
242 }
243
244 Value ServicesTable::LongPluginOutputAccessor(const Value& row)
245 {
246         Service::Ptr service = static_cast<Service::Ptr>(row);
247
248         if (!service)
249                 return Empty;
250
251         String long_output;
252         CheckResult::Ptr cr = service->GetLastCheckResult();
253
254         if (cr) {
255                 std::pair<String, String> output_bag = CompatUtility::GetCheckResultOutput(cr);
256                 long_output = output_bag.second;
257         }
258
259         return long_output;
260 }
261
262 Value ServicesTable::PerfDataAccessor(const Value& row)
263 {
264         Service::Ptr service = static_cast<Service::Ptr>(row);
265
266         if (!service)
267                 return Empty;
268
269         String perfdata;
270         CheckResult::Ptr cr = service->GetLastCheckResult();
271
272         if (cr)
273                 perfdata = CompatUtility::GetCheckResultPerfdata(cr);
274
275         return perfdata;
276 }
277
278 Value ServicesTable::NotificationPeriodAccessor(const Value& row)
279 {
280         Service::Ptr service = static_cast<Service::Ptr>(row);
281
282         if (!service)
283                 return Empty;
284
285         BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
286                 ObjectLock olock(notification);
287
288                 TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod();
289
290                 /* XXX first notification wins */
291                 if (timeperiod)
292                         return timeperiod->GetName();
293         }
294
295         return Empty;
296 }
297
298 Value ServicesTable::CheckPeriodAccessor(const Value& row)
299 {
300         Service::Ptr service = static_cast<Service::Ptr>(row);
301
302         if (!service)
303                 return Empty;
304
305         TimePeriod::Ptr timeperiod = service->GetCheckPeriod();
306
307         if (!timeperiod)
308                 return Empty;
309
310         return timeperiod->GetName();
311 }
312
313 Value ServicesTable::NotesAccessor(const Value& row)
314 {
315         Service::Ptr service = static_cast<Service::Ptr>(row);
316
317         if (!service)
318                 return Empty;
319
320         Dictionary::Ptr custom = service->GetCustom();
321
322         if (!custom)
323                 return Empty;
324
325         return custom->Get("notes");
326 }
327
328 Value ServicesTable::NotesExpandedAccessor(const Value& row)
329 {
330         Service::Ptr service = static_cast<Service::Ptr>(row);
331
332         if (!service)
333                 return Empty;
334
335         Dictionary::Ptr custom = service->GetCustom();
336
337         if (!custom)
338                 return Empty;
339
340         std::vector<MacroResolver::Ptr> resolvers;
341         resolvers.push_back(service);
342         resolvers.push_back(service->GetHost());
343         resolvers.push_back(IcingaApplication::GetInstance());
344
345         Value value = custom->Get("notes");
346
347         return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd);
348 }
349
350 Value ServicesTable::NotesUrlAccessor(const Value& row)
351 {
352         Service::Ptr service = static_cast<Service::Ptr>(row);
353
354         if (!service)
355                 return Empty;
356
357         Dictionary::Ptr custom = service->GetCustom();
358
359         if (!custom)
360                 return Empty;
361
362         return custom->Get("notes_url");
363 }
364
365 Value ServicesTable::NotesUrlExpandedAccessor(const Value& row)
366 {
367         Service::Ptr service = static_cast<Service::Ptr>(row);
368
369         if (!service)
370                 return Empty;
371
372         Dictionary::Ptr custom = service->GetCustom();
373
374         if (!custom)
375                 return Empty;
376
377         std::vector<MacroResolver::Ptr> resolvers;
378         resolvers.push_back(service);
379         resolvers.push_back(service->GetHost());
380         resolvers.push_back(IcingaApplication::GetInstance());
381
382         Value value = custom->Get("notes_url");
383
384         return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd);
385 }
386
387 Value ServicesTable::ActionUrlAccessor(const Value& row)
388 {
389         Service::Ptr service = static_cast<Service::Ptr>(row);
390
391         if (!service)
392                 return Empty;
393
394         Dictionary::Ptr custom = service->GetCustom();
395
396         if (!custom)
397                 return Empty;
398
399         return custom->Get("action_url");
400 }
401
402 Value ServicesTable::ActionUrlExpandedAccessor(const Value& row)
403 {
404         Service::Ptr service = static_cast<Service::Ptr>(row);
405
406         if (!service)
407                 return Empty;
408
409         Dictionary::Ptr custom = service->GetCustom();
410
411         if (!custom)
412                 return Empty;
413
414         std::vector<MacroResolver::Ptr> resolvers;
415         resolvers.push_back(service);
416         resolvers.push_back(service->GetHost());
417         resolvers.push_back(IcingaApplication::GetInstance());
418
419         Value value = custom->Get("action_url");
420
421         return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd);
422 }
423
424 Value ServicesTable::IconImageAccessor(const Value& row)
425 {
426         Service::Ptr service = static_cast<Service::Ptr>(row);
427
428         if (!service)
429                 return Empty;
430
431         Dictionary::Ptr custom = service->GetCustom();
432
433         if (!custom)
434                 return Empty;
435
436         return custom->Get("icon_image");
437 }
438
439 Value ServicesTable::IconImageExpandedAccessor(const Value& row)
440 {
441         Service::Ptr service = static_cast<Service::Ptr>(row);
442
443         if (!service)
444                 return Empty;
445
446         Dictionary::Ptr custom = service->GetCustom();
447
448         if (!custom)
449                 return Empty;
450
451         std::vector<MacroResolver::Ptr> resolvers;
452         resolvers.push_back(service);
453         resolvers.push_back(service->GetHost());
454         resolvers.push_back(IcingaApplication::GetInstance());
455
456         Value value = custom->Get("icon_image");
457
458         return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd);
459 }
460
461 Value ServicesTable::IconImageAltAccessor(const Value& row)
462 {
463         Service::Ptr service = static_cast<Service::Ptr>(row);
464
465         if (!service)
466                 return Empty;
467
468         Dictionary::Ptr custom = service->GetCustom();
469
470         if (!custom)
471                 return Empty;
472
473         return custom->Get("icon_image_alt");
474 }
475
476 Value ServicesTable::MaxCheckAttemptsAccessor(const Value& row)
477 {
478         Service::Ptr service = static_cast<Service::Ptr>(row);
479
480         if (!service)
481                 return Empty;
482
483         return service->GetMaxCheckAttempts();
484 }
485
486 Value ServicesTable::CurrentAttemptAccessor(const Value& row)
487 {
488         Service::Ptr service = static_cast<Service::Ptr>(row);
489
490         if (!service)
491                 return Empty;
492
493         return service->GetCheckAttempt();
494 }
495
496 Value ServicesTable::StateAccessor(const Value& row)
497 {
498         Service::Ptr service = static_cast<Service::Ptr>(row);
499
500         if (!service)
501                 return Empty;
502
503         return service->GetState();
504 }
505
506 Value ServicesTable::HasBeenCheckedAccessor(const Value& row)
507 {
508         Service::Ptr service = static_cast<Service::Ptr>(row);
509
510         if (!service)
511                 return Empty;
512
513         return (service->HasBeenChecked() ? 1 : 0);
514 }
515
516 Value ServicesTable::LastStateAccessor(const Value& row)
517 {
518         Service::Ptr service = static_cast<Service::Ptr>(row);
519
520         if (!service)
521                 return Empty;
522
523         return service->GetLastState();
524 }
525
526 Value ServicesTable::LastHardStateAccessor(const Value& row)
527 {
528         Service::Ptr service = static_cast<Service::Ptr>(row);
529
530         if (!service)
531                 return Empty;
532
533         return service->GetLastHardState();
534 }
535
536 Value ServicesTable::StateTypeAccessor(const Value& row)
537 {
538         Service::Ptr service = static_cast<Service::Ptr>(row);
539
540         if (!service)
541                 return Empty;
542
543         return service->GetStateType();
544 }
545
546 Value ServicesTable::CheckTypeAccessor(const Value& row)
547 {
548         Service::Ptr service = static_cast<Service::Ptr>(row);
549
550         if (!service)
551                 return Empty;
552
553         return (service->GetEnableActiveChecks() ? 0 : 1);
554 }
555
556 Value ServicesTable::AcknowledgedAccessor(const Value& row)
557 {
558         Service::Ptr service = static_cast<Service::Ptr>(row);
559
560         if (!service)
561                 return Empty;
562
563         /* important: lock acknowledgements */
564         ObjectLock olock(service);
565
566         return (service->IsAcknowledged() ? 1 : 0);
567 }
568
569 Value ServicesTable::AcknowledgementTypeAccessor(const Value& row)
570 {
571         Service::Ptr service = static_cast<Service::Ptr>(row);
572
573         if (!service)
574                 return Empty;
575
576         /* important: lock acknowledgements */
577         ObjectLock olock(service);
578
579         return static_cast<int>(service->GetAcknowledgement());
580 }
581
582 Value ServicesTable::NoMoreNotificationsAccessor(const Value& row)
583 {
584         Service::Ptr service = static_cast<Service::Ptr>(row);
585
586         if (!service)
587                 return Empty;
588
589         /* XXX take the smallest notification_interval */
590         double notification_interval = -1;
591         BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
592                 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
593                         notification_interval = notification->GetNotificationInterval();
594         }
595
596         if (notification_interval == 0 && !service->GetVolatile())
597                 return 1;
598
599         return 0;
600 }
601
602 Value ServicesTable::LastTimeOkAccessor(const Value& row)
603 {
604         Service::Ptr service = static_cast<Service::Ptr>(row);
605
606         if (!service)
607                 return Empty;
608
609         return static_cast<int>(service->GetLastStateOK());
610 }
611
612 Value ServicesTable::LastTimeWarningAccessor(const Value& row)
613 {
614         Service::Ptr service = static_cast<Service::Ptr>(row);
615
616         if (!service)
617                 return Empty;
618
619         return static_cast<int>(service->GetLastStateWarning());
620 }
621
622 Value ServicesTable::LastTimeCriticalAccessor(const Value& row)
623 {
624         Service::Ptr service = static_cast<Service::Ptr>(row);
625
626         if (!service)
627                 return Empty;
628
629         return static_cast<int>(service->GetLastStateCritical());
630 }
631
632 Value ServicesTable::LastTimeUnknownAccessor(const Value& row)
633 {
634         Service::Ptr service = static_cast<Service::Ptr>(row);
635
636         if (!service)
637                 return Empty;
638
639         return static_cast<int>(service->GetLastStateUnknown());
640 }
641
642 Value ServicesTable::LastCheckAccessor(const Value& row)
643 {
644         Service::Ptr service = static_cast<Service::Ptr>(row);
645
646         if (!service)
647                 return Empty;
648
649         return static_cast<int>(service->GetLastCheck());
650 }
651
652 Value ServicesTable::NextCheckAccessor(const Value& row)
653 {
654         Service::Ptr service = static_cast<Service::Ptr>(row);
655
656         if (!service)
657                 return Empty;
658
659         return static_cast<int>(service->GetNextCheck());
660 }
661
662 Value ServicesTable::LastNotificationAccessor(const Value& row)
663 {
664         Service::Ptr service = static_cast<Service::Ptr>(row);
665
666         if (!service)
667                 return Empty;
668
669         /* XXX Service -> Notifications, latest wins */
670         double last_notification = 0;
671         BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
672                 if (notification->GetLastNotification() > last_notification)
673                         last_notification = notification->GetLastNotification();
674         }
675
676         return static_cast<int>(last_notification);
677 }
678
679 Value ServicesTable::NextNotificationAccessor(const Value& row)
680 {
681         Service::Ptr service = static_cast<Service::Ptr>(row);
682
683         if (!service)
684                 return Empty;
685
686         /* XXX Service -> Notifications, latest wins */
687         double next_notification = 0;
688         BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
689                 if (notification->GetNextNotification() < next_notification)
690                         next_notification = notification->GetNextNotification();
691         }
692
693         return static_cast<int>(next_notification);
694 }
695
696 Value ServicesTable::CurrentNotificationNumberAccessor(const Value& row)
697 {
698         Service::Ptr service = static_cast<Service::Ptr>(row);
699
700         if (!service)
701                 return Empty;
702
703         /* XXX Service -> Notifications, biggest wins */
704         int notification_number = 0;
705         BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
706                 if (notification->GetNotificationNumber() > notification_number)
707                         notification_number = notification->GetNotificationNumber();
708         }
709
710         return notification_number;
711 }
712
713 Value ServicesTable::LastStateChangeAccessor(const Value& row)
714 {
715         Service::Ptr service = static_cast<Service::Ptr>(row);
716
717         if (!service)
718                 return Empty;
719
720         return static_cast<int>(service->GetLastStateChange());
721 }
722
723 Value ServicesTable::LastHardStateChangeAccessor(const Value& row)
724 {
725         Service::Ptr service = static_cast<Service::Ptr>(row);
726
727         if (!service)
728                 return Empty;
729
730         return static_cast<int>(service->GetLastHardStateChange());
731 }
732
733 Value ServicesTable::ScheduledDowntimeDepthAccessor(const Value& row)
734 {
735         Service::Ptr service = static_cast<Service::Ptr>(row);
736
737         if (!service)
738                 return Empty;
739
740         return service->GetDowntimeDepth();
741 }
742
743 Value ServicesTable::IsFlappingAccessor(const Value& row)
744 {
745         Service::Ptr service = static_cast<Service::Ptr>(row);
746
747         if (!service)
748                 return Empty;
749
750         return service->IsFlapping();
751 }
752
753 Value ServicesTable::ChecksEnabledAccessor(const Value& row)
754 {
755         Service::Ptr service = static_cast<Service::Ptr>(row);
756
757         if (!service)
758                 return Empty;
759
760         return (service->GetEnableActiveChecks() ? 1 : 0);
761 }
762
763 Value ServicesTable::AcceptPassiveChecksAccessor(const Value& row)
764 {
765         Service::Ptr service = static_cast<Service::Ptr>(row);
766
767         if (!service)
768                 return Empty;
769
770         return (service->GetEnablePassiveChecks() ? 1 : 0);
771 }
772
773 Value ServicesTable::EventHandlerEnabledAccessor(const Value& row)
774 {
775         Service::Ptr service = static_cast<Service::Ptr>(row);
776
777         if (!service)
778                 return Empty;
779
780         EventCommand::Ptr eventcommand = service->GetEventCommand();
781
782         if (eventcommand)
783                 return 1;
784
785         return 0;
786 }
787
788 Value ServicesTable::NotificationsEnabledAccessor(const Value& row)
789 {
790         Service::Ptr service = static_cast<Service::Ptr>(row);
791
792         if (!service)
793                 return Empty;
794
795         return (service->GetEnableNotifications() ? 1 : 0);
796 }
797
798 Value ServicesTable::ActiveChecksEnabledAccessor(const Value& row)
799 {
800         Service::Ptr service = static_cast<Service::Ptr>(row);
801
802         if (!service)
803                 return Empty;
804
805         return (service->GetEnableActiveChecks() ? 1 : 0);
806 }
807
808 Value ServicesTable::CheckOptionsAccessor(const Value& row)
809 {
810         /* TODO - forcexec, freshness, orphan, none */
811         return Empty;
812 }
813
814 Value ServicesTable::FlapDetectionEnabledAccessor(const Value& row)
815 {
816         Service::Ptr service = static_cast<Service::Ptr>(row);
817
818         if (!service)
819                 return Empty;
820
821         return (service->GetEnableFlapping() ? 1 : 0);
822 }
823
824 Value ServicesTable::CheckFreshnessAccessor(const Value& row)
825 {
826         /* always enabled */
827         return 1;
828 }
829
830 Value ServicesTable::ModifiedAttributesAccessor(const Value& row)
831 {
832         Service::Ptr service = static_cast<Service::Ptr>(row);
833
834         if (!service)
835                 return Empty;
836
837         return service->GetModifiedAttributes();
838 }
839
840 Value ServicesTable::ModifiedAttributesListAccessor(const Value& row)
841 {
842         /* not supported */
843         return Empty;
844 }
845
846 Value ServicesTable::StalenessAccessor(const Value& row)
847 {
848         Service::Ptr service = static_cast<Service::Ptr>(row);
849
850         if (!service)
851                 return Empty;
852
853         if (service->HasBeenChecked() && service->GetLastCheck() > 0)
854                 return (Utility::GetTime() - service->GetLastCheck()) / (service->GetCheckInterval() * 3600);
855
856         return Empty;
857 }
858
859 Value ServicesTable::CheckIntervalAccessor(const Value& row)
860 {
861         Service::Ptr service = static_cast<Service::Ptr>(row);
862
863         if (!service)
864                 return Empty;
865
866         return (service->GetCheckInterval() / 60.0);
867 }
868
869 Value ServicesTable::RetryIntervalAccessor(const Value& row)
870 {
871         Service::Ptr service = static_cast<Service::Ptr>(row);
872
873         if (!service)
874                 return Empty;
875
876         return (service->GetRetryInterval() / 60.0);
877 }
878
879 Value ServicesTable::NotificationIntervalAccessor(const Value& row)
880 {
881         Service::Ptr service = static_cast<Service::Ptr>(row);
882
883         if (!service)
884                 return Empty;
885
886         /* XXX take the smallest notification_interval */
887         double notification_interval = -1;
888         BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
889                 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
890                         notification_interval = notification->GetNotificationInterval();
891         }
892
893         if (notification_interval == -1)
894                 notification_interval = 60;
895
896         return (notification_interval / 60.0);
897 }
898
899 Value ServicesTable::LowFlapThresholdAccessor(const Value& row)
900 {
901         Service::Ptr service = static_cast<Service::Ptr>(row);
902
903         if (!service)
904                 return Empty;
905
906         return service->GetFlappingThreshold();
907 }
908
909 Value ServicesTable::HighFlapThresholdAccessor(const Value& row)
910 {
911         Service::Ptr service = static_cast<Service::Ptr>(row);
912
913         if (!service)
914                 return Empty;
915
916         return service->GetFlappingThreshold();
917 }
918
919 Value ServicesTable::LatencyAccessor(const Value& row)
920 {
921         Service::Ptr service = static_cast<Service::Ptr>(row);
922
923         if (!service)
924                 return Empty;
925
926         return (Service::CalculateLatency(service->GetLastCheckResult()));
927 }
928
929 Value ServicesTable::ExecutionTimeAccessor(const Value& row)
930 {
931         Service::Ptr service = static_cast<Service::Ptr>(row);
932
933         if (!service)
934                 return Empty;
935
936         return (Service::CalculateExecutionTime(service->GetLastCheckResult()));
937 }
938
939 Value ServicesTable::PercentStateChangeAccessor(const Value& row)
940 {
941         Service::Ptr service = static_cast<Service::Ptr>(row);
942
943         if (!service)
944                 return Empty;
945
946         return service->GetFlappingCurrent();
947 }
948
949 Value ServicesTable::InCheckPeriodAccessor(const Value& row)
950 {
951         Service::Ptr service = static_cast<Service::Ptr>(row);
952
953         if (!service)
954                 return Empty;
955
956         TimePeriod::Ptr timeperiod = service->GetCheckPeriod();
957
958         /* none set means always checked */
959         if (!timeperiod)
960                 return 1;
961
962         return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
963 }
964
965 Value ServicesTable::InNotificationPeriodAccessor(const Value& row)
966 {
967         Service::Ptr service = static_cast<Service::Ptr>(row);
968
969         if (!service)
970                 return Empty;
971
972         BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
973                 ObjectLock olock(notification);
974
975                 TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod();
976
977                 /* XXX first notification wins */
978                 if (timeperiod)
979                         return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
980         }
981
982         /* none set means always notified */
983         return 1;
984 }
985
986 Value ServicesTable::ContactsAccessor(const Value& row)
987 {
988         Service::Ptr service = static_cast<Service::Ptr>(row);
989
990         if (!service)
991                 return Empty;
992
993         Array::Ptr contact_names = make_shared<Array>();
994
995         BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetServiceNotificationUsers(service)) {
996                 contact_names->Add(user->GetName());
997         }
998
999         return contact_names;
1000 }
1001
1002 Value ServicesTable::DowntimesAccessor(const Value& row)
1003 {
1004         Service::Ptr service = static_cast<Service::Ptr>(row);
1005
1006         if (!service)
1007                 return Empty;
1008
1009         Dictionary::Ptr downtimes = service->GetDowntimes();
1010
1011         Array::Ptr ids = make_shared<Array>();
1012
1013         ObjectLock olock(downtimes);
1014
1015         String id;
1016         Downtime::Ptr downtime;
1017         BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
1018
1019                 if (!downtime)
1020                         continue;
1021
1022                 if (downtime->IsExpired())
1023                         continue;
1024
1025                 ids->Add(downtime->GetLegacyId());
1026         }
1027
1028         return ids;
1029 }
1030
1031 Value ServicesTable::DowntimesWithInfoAccessor(const Value& row)
1032 {
1033         Service::Ptr service = static_cast<Service::Ptr>(row);
1034
1035         if (!service)
1036                 return Empty;
1037
1038         Dictionary::Ptr downtimes = service->GetDowntimes();
1039
1040         Array::Ptr ids = make_shared<Array>();
1041
1042         ObjectLock olock(downtimes);
1043
1044         String id;
1045         Downtime::Ptr downtime;
1046         BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
1047
1048                 if (!downtime)
1049                         continue;
1050
1051                 if (downtime->IsExpired())
1052                         continue;
1053
1054                 Array::Ptr downtime_info = make_shared<Array>();
1055                 downtime_info->Add(downtime->GetLegacyId());
1056                 downtime_info->Add(downtime->GetAuthor());
1057                 downtime_info->Add(downtime->GetComment());
1058                 ids->Add(downtime_info);
1059         }
1060
1061         return ids;
1062 }
1063
1064 Value ServicesTable::CommentsAccessor(const Value& row)
1065 {
1066         Service::Ptr service = static_cast<Service::Ptr>(row);
1067
1068         if (!service)
1069                 return Empty;
1070
1071         Dictionary::Ptr comments = service->GetComments();
1072
1073         Array::Ptr ids = make_shared<Array>();
1074
1075         ObjectLock olock(comments);
1076
1077         String id;
1078         Comment::Ptr comment;
1079         BOOST_FOREACH(boost::tie(id, comment), comments) {
1080
1081                 if (!comment)
1082                         continue;
1083
1084                 if (comment->IsExpired())
1085                         continue;
1086
1087                 ids->Add(comment->GetLegacyId());
1088         }
1089
1090         return ids;
1091 }
1092
1093 Value ServicesTable::CommentsWithInfoAccessor(const Value& row)
1094 {
1095         Service::Ptr service = static_cast<Service::Ptr>(row);
1096
1097         if (!service)
1098                 return Empty;
1099
1100         Dictionary::Ptr comments = service->GetComments();
1101
1102         Array::Ptr ids = make_shared<Array>();
1103
1104         ObjectLock olock(comments);
1105
1106         String id;
1107         Comment::Ptr comment;
1108         BOOST_FOREACH(boost::tie(id, comment), comments) {
1109
1110                 if (!comment)
1111                         continue;
1112
1113                 if (comment->IsExpired())
1114                         continue;
1115
1116                 Array::Ptr comment_info = make_shared<Array>();
1117                 comment_info->Add(comment->GetLegacyId());
1118                 comment_info->Add(comment->GetAuthor());
1119                 comment_info->Add(comment->GetText());
1120                 ids->Add(comment_info);
1121         }
1122
1123         return ids;
1124 }
1125
1126 Value ServicesTable::CommentsWithExtraInfoAccessor(const Value& row)
1127 {
1128         Service::Ptr service = static_cast<Service::Ptr>(row);
1129
1130         if (!service)
1131                 return Empty;
1132
1133         Dictionary::Ptr comments = service->GetComments();
1134
1135         Array::Ptr ids = make_shared<Array>();
1136
1137         ObjectLock olock(comments);
1138
1139         String id;
1140         Comment::Ptr comment;
1141         BOOST_FOREACH(boost::tie(id, comment), comments) {
1142
1143                 if (!comment)
1144                         continue;
1145
1146                 if (comment->IsExpired())
1147                         continue;
1148
1149                 Array::Ptr comment_info = make_shared<Array>();
1150                 comment_info->Add(comment->GetLegacyId());
1151                 comment_info->Add(comment->GetAuthor());
1152                 comment_info->Add(comment->GetText());
1153                 comment_info->Add(comment->GetEntryType());
1154                 comment_info->Add(static_cast<int>(comment->GetEntryTime()));
1155                 ids->Add(comment_info);
1156         }
1157
1158         return ids;
1159 }
1160
1161 Value ServicesTable::CustomVariableNamesAccessor(const Value& row)
1162 {
1163         Service::Ptr service = static_cast<Service::Ptr>(row);
1164
1165         if (!service)
1166                 return Empty;
1167
1168         Dictionary::Ptr customvars;
1169
1170         {
1171                 ObjectLock olock(service);
1172                 customvars = CompatUtility::GetCustomVariableConfig(service);
1173         }
1174
1175         if (!customvars)
1176                 return Empty;
1177
1178         Array::Ptr cv = make_shared<Array>();
1179
1180         String key;
1181         Value value;
1182         BOOST_FOREACH(boost::tie(key, value), customvars) {
1183                 cv->Add(key);
1184         }
1185
1186         return cv;
1187 }
1188
1189 Value ServicesTable::CustomVariableValuesAccessor(const Value& row)
1190 {
1191         Service::Ptr service = static_cast<Service::Ptr>(row);
1192
1193         if (!service)
1194                 return Empty;
1195
1196         Dictionary::Ptr customvars;
1197
1198         {
1199                 ObjectLock olock(service);
1200                 customvars = CompatUtility::GetCustomVariableConfig(service);
1201         }
1202
1203         if (!customvars)
1204                 return Empty;
1205
1206         Array::Ptr cv = make_shared<Array>();
1207
1208         String key;
1209         Value value;
1210         BOOST_FOREACH(boost::tie(key, value), customvars) {
1211                 cv->Add(value);
1212         }
1213
1214         return cv;
1215 }
1216
1217 Value ServicesTable::CustomVariablesAccessor(const Value& row)
1218 {
1219         Service::Ptr service = static_cast<Service::Ptr>(row);
1220
1221         if (!service)
1222                 return Empty;
1223
1224         Dictionary::Ptr customvars;
1225
1226         {
1227                 ObjectLock olock(service);
1228                 customvars = CompatUtility::GetCustomVariableConfig(service);
1229         }
1230
1231         if (!customvars)
1232                 return Empty;
1233
1234         Array::Ptr cv = make_shared<Array>();
1235
1236         String key;
1237         Value value;
1238         BOOST_FOREACH(boost::tie(key, value), customvars) {
1239                 Array::Ptr key_val = make_shared<Array>();
1240                 key_val->Add(key);
1241                 key_val->Add(value);
1242                 cv->Add(key_val);
1243         }
1244
1245         return cv;
1246 }
1247
1248 Value ServicesTable::GroupsAccessor(const Value& row)
1249 {
1250         Service::Ptr service = static_cast<Service::Ptr>(row);
1251
1252         if (!service)
1253                 return Empty;
1254
1255         Array::Ptr groups = service->GetGroups();
1256
1257         if (!groups)
1258                 return Empty;
1259
1260         return groups;
1261 }
1262
1263 Value ServicesTable::ContactGroupsAccessor(const Value& row)
1264 {
1265         Service::Ptr service = static_cast<Service::Ptr>(row);
1266
1267         if (!service)
1268                 return Empty;
1269
1270         Array::Ptr contactgroup_names = make_shared<Array>();
1271
1272         BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetServiceNotificationUserGroups(service)) {
1273                 contactgroup_names->Add(usergroup->GetName());
1274         }
1275
1276         return contactgroup_names;
1277 }
1278
1279