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