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