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