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