]> granicus.if.org Git - icinga2/blob - components/livestatus/servicestable.cpp
compatutility: Use User(Group) Ptr, not name.
[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(&ServicesTable::InitialStateAccessor, 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(&ServicesTable::ProcessPerformanceDataAccessor, objectAccessor));
97         table->AddColumn(prefix + "is_executing", Column(&ServicesTable::IsExecutingAccessor, 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(&ServicesTable::ObsessOverServiceAccessor, 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(&ServicesTable::PnpgraphPresentAccessor, 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(&ServicesTable::FirstNotificationDelayAccessor, 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         return static_cast<Service::Ptr>(service)->GetHost();
155 }
156
157 Value ServicesTable::ShortNameAccessor(const Value& row)
158 {
159         return static_cast<Service::Ptr>(row)->GetShortName();
160 }
161
162 Value ServicesTable::DisplayNameAccessor(const Value& row)
163 {
164         return static_cast<Service::Ptr>(row)->GetDisplayName();
165 }
166
167 Value ServicesTable::CheckCommandAccessor(const Value& row)
168 {
169         CheckCommand::Ptr checkcommand = static_cast<Service::Ptr>(row)->GetCheckCommand();
170
171         if (checkcommand)
172                 return checkcommand->GetName(); /* this is the name without '!' args */
173
174         return Empty;
175 }
176
177 Value ServicesTable::CheckCommandExpandedAccessor(const Value& row)
178 {
179         Service::Ptr service = static_cast<Service::Ptr>(row);
180
181         CheckCommand::Ptr commandObj = service->GetCheckCommand();
182
183         if (!commandObj)
184                 return Empty;
185
186         Value raw_command = commandObj->GetCommandLine();
187
188         std::vector<MacroResolver::Ptr> resolvers;
189         resolvers.push_back(service);
190         resolvers.push_back(service->GetHost());
191         resolvers.push_back(commandObj);
192         resolvers.push_back(IcingaApplication::GetInstance());
193
194         Value commandLine = MacroProcessor::ResolveMacros(raw_command, resolvers, Dictionary::Ptr(), Utility::EscapeShellCmd);
195
196         String buf;
197         if (commandLine.IsObjectType<Array>()) {
198                 Array::Ptr args = commandLine;
199
200                 ObjectLock olock(args);
201                 String arg;
202                 BOOST_FOREACH(arg, args) {
203                         // This is obviously incorrect for non-trivial cases.
204                         String argitem = " \"" + arg + "\"";
205                         boost::algorithm::replace_all(argitem, "\n", "\\n");
206                         buf += argitem;
207                 }
208         } else if (!commandLine.IsEmpty()) {
209                 String args = Convert::ToString(commandLine);
210                 boost::algorithm::replace_all(args, "\n", "\\n");
211                 buf += args;
212         } else {
213                 buf += "<internal>";
214         }
215
216         return buf;
217 }
218
219 Value ServicesTable::EventHandlerAccessor(const Value& row)
220 {
221         EventCommand::Ptr eventcommand = static_cast<Service::Ptr>(row)->GetEventCommand();
222
223         if (eventcommand)
224                 return eventcommand->GetName();
225
226         return Empty;
227 }
228
229 Value ServicesTable::PluginOutputAccessor(const Value& row)
230 {
231         return static_cast<Service::Ptr>(row)->GetLastCheckOutput();
232 }
233
234 Value ServicesTable::LongPluginOutputAccessor(const Value& row)
235 {
236         return static_cast<Service::Ptr>(row)->GetLastCheckLongOutput();
237 }
238
239 Value ServicesTable::PerfDataAccessor(const Value& row)
240 {
241         return static_cast<Service::Ptr>(row)->GetLastCheckPerfData();
242 }
243
244 Value ServicesTable::NotificationPeriodAccessor(const Value& row)
245 {
246         BOOST_FOREACH(const Notification::Ptr& notification, static_cast<Service::Ptr>(row)->GetNotifications()) {
247                 ObjectLock olock(notification);
248
249                 TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod();
250
251                 /* XXX first notification wins */
252                 if (timeperiod)
253                         return timeperiod->GetName();
254         }
255
256         return Empty;
257 }
258
259 Value ServicesTable::CheckPeriodAccessor(const Value& row)
260 {
261         TimePeriod::Ptr timeperiod = static_cast<Service::Ptr>(row)->GetCheckPeriod();
262
263         if (!timeperiod)
264                 return Empty;
265
266         return timeperiod->GetName();
267 }
268
269 Value ServicesTable::NotesAccessor(const Value& row)
270 {
271         Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
272
273         if (!custom)
274                 return Empty;
275
276         return custom->Get("notes");
277 }
278
279 Value ServicesTable::NotesExpandedAccessor(const Value& row)
280 {
281         Service::Ptr service = static_cast<Service::Ptr>(row);
282         Dictionary::Ptr custom = service->GetCustom();
283
284         if (!custom)
285                 return Empty;
286
287         std::vector<MacroResolver::Ptr> resolvers;
288         resolvers.push_back(service);
289         resolvers.push_back(service->GetHost());
290         resolvers.push_back(IcingaApplication::GetInstance());
291
292         Value value = custom->Get("notes");
293
294         Dictionary::Ptr cr;
295         Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
296
297         return value_expanded;
298 }
299
300 Value ServicesTable::NotesUrlAccessor(const Value& row)
301 {
302         Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
303
304         if (!custom)
305                 return Empty;
306
307         return custom->Get("notes_url");
308 }
309
310 Value ServicesTable::NotesUrlExpandedAccessor(const Value& row)
311 {
312         Service::Ptr service = static_cast<Service::Ptr>(row);
313         Dictionary::Ptr custom = service->GetCustom();
314
315         if (!custom)
316                 return Empty;
317
318         std::vector<MacroResolver::Ptr> resolvers;
319         resolvers.push_back(service);
320         resolvers.push_back(service->GetHost());
321         resolvers.push_back(IcingaApplication::GetInstance());
322
323         Value value = custom->Get("notes_url");
324
325         Dictionary::Ptr cr;
326         Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
327
328         return value_expanded;
329 }
330
331 Value ServicesTable::ActionUrlAccessor(const Value& row)
332 {
333         Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
334
335         if (!custom)
336                 return Empty;
337
338         return custom->Get("action_url");
339 }
340
341 Value ServicesTable::ActionUrlExpandedAccessor(const Value& row)
342 {
343         Service::Ptr service = static_cast<Service::Ptr>(row);
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("action_url");
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::IconImageAccessor(const Value& row)
363 {
364         Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
365
366         if (!custom)
367                 return Empty;
368
369         return custom->Get("icon_image");
370 }
371
372 Value ServicesTable::IconImageExpandedAccessor(const Value& row)
373 {
374         Service::Ptr service = static_cast<Service::Ptr>(row);
375         Dictionary::Ptr custom = service->GetCustom();
376
377         if (!custom)
378                 return Empty;
379
380         std::vector<MacroResolver::Ptr> resolvers;
381         resolvers.push_back(service);
382         resolvers.push_back(service->GetHost());
383         resolvers.push_back(IcingaApplication::GetInstance());
384
385         Value value = custom->Get("icon_image");
386
387         Dictionary::Ptr cr;
388         Value value_expanded = MacroProcessor::ResolveMacros(value, resolvers, cr, Utility::EscapeShellCmd);
389
390         return value_expanded;
391 }
392
393 Value ServicesTable::IconImageAltAccessor(const Value& row)
394 {
395         Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
396
397         if (!custom)
398                 return Empty;
399
400         return custom->Get("icon_image_alt");
401 }
402
403 Value ServicesTable::InitialStateAccessor(const Value& row)
404 {
405         /* not supported */
406         return Empty;
407 }
408
409 Value ServicesTable::MaxCheckAttemptsAccessor(const Value& row)
410 {
411         return static_cast<Service::Ptr>(row)->GetMaxCheckAttempts();
412 }
413
414 Value ServicesTable::CurrentAttemptAccessor(const Value& row)
415 {
416         return static_cast<Service::Ptr>(row)->GetCurrentCheckAttempt();
417 }
418
419 Value ServicesTable::StateAccessor(const Value& row)
420 {
421         return static_cast<Service::Ptr>(row)->GetState();
422 }
423
424 Value ServicesTable::HasBeenCheckedAccessor(const Value& row)
425 {
426         return (static_cast<Service::Ptr>(row)->HasBeenChecked() ? 1 : 0);
427 }
428
429 Value ServicesTable::LastStateAccessor(const Value& row)
430 {
431         return static_cast<Service::Ptr>(row)->GetLastState();
432 }
433
434 Value ServicesTable::LastHardStateAccessor(const Value& row)
435 {
436         return static_cast<Service::Ptr>(row)->GetLastHardState();
437 }
438
439 Value ServicesTable::StateTypeAccessor(const Value& row)
440 {
441         return static_cast<Service::Ptr>(row)->GetStateType();
442 }
443
444 Value ServicesTable::CheckTypeAccessor(const Value& row)
445 {
446         return (static_cast<Service::Ptr>(row)->GetEnableActiveChecks() ? 0 : 1);
447 }
448
449 Value ServicesTable::AcknowledgedAccessor(const Value& row)
450 {
451         Service::Ptr service = static_cast<Service::Ptr>(row);
452
453         /* important: lock acknowledgements */
454         ObjectLock olock(service);
455
456         return (service->IsAcknowledged() ? 1 : 0);
457 }
458
459 Value ServicesTable::AcknowledgementTypeAccessor(const Value& row)
460 {
461         Service::Ptr service = static_cast<Service::Ptr>(row);
462
463         /* important: lock acknowledgements */
464         ObjectLock olock(service);
465
466         return static_cast<int>(service->GetAcknowledgement());
467 }
468
469 Value ServicesTable::NoMoreNotificationsAccessor(const Value& row)
470 {
471         Service::Ptr service = static_cast<Service::Ptr>(row);
472
473         /* XXX take the smallest notification_interval */
474         double notification_interval = -1;
475         BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
476                 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
477                         notification_interval = notification->GetNotificationInterval();
478         }
479
480         if (notification_interval == 0 && !service->IsVolatile())
481                 return 1;
482
483         return 0;
484 }
485
486 Value ServicesTable::LastTimeOkAccessor(const Value& row)
487 {
488         return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastStateOK());
489 }
490
491 Value ServicesTable::LastTimeWarningAccessor(const Value& row)
492 {
493         return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastStateWarning());
494 }
495
496 Value ServicesTable::LastTimeCriticalAccessor(const Value& row)
497 {
498         return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastStateCritical());
499 }
500
501 Value ServicesTable::LastTimeUnknownAccessor(const Value& row)
502 {
503         return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastStateUnknown());
504 }
505
506 Value ServicesTable::LastCheckAccessor(const Value& row)
507 {
508         return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastCheck());
509 }
510
511 Value ServicesTable::NextCheckAccessor(const Value& row)
512 {
513         return static_cast<int>(static_cast<Service::Ptr>(row)->GetNextCheck());
514 }
515
516 Value ServicesTable::LastNotificationAccessor(const Value& row)
517 {
518         Service::Ptr service = static_cast<Service::Ptr>(row);
519
520         /* XXX Service -> Notifications, latest wins */
521         double last_notification = 0;
522         BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
523                 if (notification->GetLastNotification() > last_notification)
524                         last_notification = notification->GetLastNotification();
525         }
526
527         return static_cast<int>(last_notification);
528 }
529
530 Value ServicesTable::NextNotificationAccessor(const Value& row)
531 {
532         Service::Ptr service = static_cast<Service::Ptr>(row);
533
534         /* XXX Service -> Notifications, latest wins */
535         double next_notification = 0;
536         BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
537                 if (notification->GetNextNotification() < next_notification)
538                         next_notification = notification->GetNextNotification();
539         }
540
541         return static_cast<int>(next_notification);
542 }
543
544 Value ServicesTable::CurrentNotificationNumberAccessor(const Value& row)
545 {
546         Service::Ptr service = static_cast<Service::Ptr>(row);
547
548         /* XXX Service -> Notifications, biggest wins */
549         int notification_number = 0;
550         BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
551                 if (notification->GetNotificationNumber() > notification_number)
552                         notification_number = notification->GetNotificationNumber();
553         }
554
555         return notification_number;
556 }
557
558 Value ServicesTable::LastStateChangeAccessor(const Value& row)
559 {
560         return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastStateChange());
561 }
562
563 Value ServicesTable::LastHardStateChangeAccessor(const Value& row)
564 {
565         return static_cast<int>(static_cast<Service::Ptr>(row)->GetLastHardStateChange());
566 }
567
568 Value ServicesTable::ScheduledDowntimeDepthAccessor(const Value& row)
569 {
570         return static_cast<Service::Ptr>(row)->GetDowntimeDepth();
571 }
572
573 Value ServicesTable::IsFlappingAccessor(const Value& row)
574 {
575         return static_cast<Service::Ptr>(row)->IsFlapping();
576 }
577
578 Value ServicesTable::ChecksEnabledAccessor(const Value& row)
579 {
580         return (static_cast<Service::Ptr>(row)->GetEnableActiveChecks() ? 1 : 0);
581 }
582
583 Value ServicesTable::AcceptPassiveChecksAccessor(const Value& row)
584 {
585         return (static_cast<Service::Ptr>(row)->GetEnablePassiveChecks() ? 1 : 0);
586 }
587
588 Value ServicesTable::EventHandlerEnabledAccessor(const Value& row)
589 {
590         /* always enabled */
591         return 1;
592 }
593
594 Value ServicesTable::NotificationsEnabledAccessor(const Value& row)
595 {
596         return (static_cast<Service::Ptr>(row)->GetEnableNotifications() ? 1 : 0);
597 }
598
599 Value ServicesTable::ProcessPerformanceDataAccessor(const Value& row)
600 {
601         /* always enabled */
602         return 1;
603 }
604
605 Value ServicesTable::IsExecutingAccessor(const Value& row)
606 {
607         /* not supported */
608         return Empty;
609 }
610
611 Value ServicesTable::ActiveChecksEnabledAccessor(const Value& row)
612 {
613         return (static_cast<Service::Ptr>(row)->GetEnableActiveChecks() ? 1 : 0);
614 }
615
616 Value ServicesTable::CheckOptionsAccessor(const Value& row)
617 {
618         /* TODO - forcexec, freshness, orphan, none */
619         return Empty;
620 }
621
622 Value ServicesTable::FlapDetectionEnabledAccessor(const Value& row)
623 {
624         return (static_cast<Service::Ptr>(row)->GetEnableFlapping() ? 1 : 0);
625 }
626
627 Value ServicesTable::CheckFreshnessAccessor(const Value& row)
628 {
629         /* always enabled */
630         return 1;
631 }
632
633 Value ServicesTable::ObsessOverServiceAccessor(const Value& row)
634 {
635         /* not supported */
636         return Empty;
637 }
638
639 Value ServicesTable::ModifiedAttributesAccessor(const Value& row)
640 {
641         /* not supported */
642         return Empty;
643 }
644
645 Value ServicesTable::ModifiedAttributesListAccessor(const Value& row)
646 {
647         /* not supported */
648         return Empty;
649 }
650
651 Value ServicesTable::PnpgraphPresentAccessor(const Value& row)
652 {
653         /* not supported */
654         return Empty;
655 }
656
657 Value ServicesTable::StalenessAccessor(const Value& row)
658 {
659         Service::Ptr service = static_cast<Service::Ptr>(row);
660
661         if (service->HasBeenChecked() && service->GetLastCheck() > 0)
662                 return (Utility::GetTime() - service->GetLastCheck()) / (service->GetCheckInterval() * 3600);
663
664         return Empty;
665 }
666
667 Value ServicesTable::CheckIntervalAccessor(const Value& row)
668 {
669         return (static_cast<Service::Ptr>(row)->GetCheckInterval() / 60.0);
670 }
671
672 Value ServicesTable::RetryIntervalAccessor(const Value& row)
673 {
674         return (static_cast<Service::Ptr>(row)->GetRetryInterval() / 60.0);
675 }
676
677 Value ServicesTable::NotificationIntervalAccessor(const Value& row)
678 {
679         Service::Ptr service = static_cast<Service::Ptr>(row);
680
681         /* XXX take the smallest notification_interval */
682         double notification_interval = -1;
683         BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
684                 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
685                         notification_interval = notification->GetNotificationInterval();
686         }
687
688         if (notification_interval == -1)
689                 notification_interval = 60;
690
691         return (notification_interval / 60.0);
692 }
693
694 Value ServicesTable::FirstNotificationDelayAccessor(const Value& row)
695 {
696         /* not supported */
697         return Empty;
698 }
699
700 Value ServicesTable::LowFlapThresholdAccessor(const Value& row)
701 {
702         return static_cast<Service::Ptr>(row)->GetFlappingThreshold();
703 }
704
705 Value ServicesTable::HighFlapThresholdAccessor(const Value& row)
706 {
707         return static_cast<Service::Ptr>(row)->GetFlappingThreshold();
708 }
709
710 Value ServicesTable::LatencyAccessor(const Value& row)
711 {
712         return (Service::CalculateLatency(static_cast<Service::Ptr>(row)->GetLastCheckResult()));
713 }
714
715 Value ServicesTable::ExecutionTimeAccessor(const Value& row)
716 {
717         return (Service::CalculateExecutionTime(static_cast<Service::Ptr>(row)->GetLastCheckResult()));
718 }
719
720 Value ServicesTable::PercentStateChangeAccessor(const Value& row)
721 {
722         return static_cast<Service::Ptr>(row)->GetFlappingCurrent();
723 }
724
725 Value ServicesTable::InCheckPeriodAccessor(const Value& row)
726 {
727         TimePeriod::Ptr timeperiod = static_cast<Service::Ptr>(row)->GetCheckPeriod();
728
729         /* none set means always checked */
730         if (!timeperiod)
731                 return 1;
732
733         return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
734 }
735
736 Value ServicesTable::InNotificationPeriodAccessor(const Value& row)
737 {
738         BOOST_FOREACH(const Notification::Ptr& notification, static_cast<Service::Ptr>(row)->GetNotifications()) {
739                 ObjectLock olock(notification);
740
741                 TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod();
742
743                 /* XXX first notification wins */
744                 if (timeperiod)
745                         return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0);
746         }
747
748         /* none set means always notified */
749         return 1;
750 }
751
752 Value ServicesTable::ContactsAccessor(const Value& row)
753 {
754         Array::Ptr contacts = CompatUtility::GetServiceNotificationUsers(static_cast<Service::Ptr>(row));
755         Array::Ptr contact_names = boost::make_shared<Array>();
756
757         ObjectLock olock(contacts);
758         BOOST_FOREACH(const User::Ptr& user, contacts) {
759                 contact_names->Add(user->GetName());
760         }
761
762         return contact_names;
763 }
764
765 Value ServicesTable::DowntimesAccessor(const Value& row)
766 {
767         Dictionary::Ptr downtimes = static_cast<Service::Ptr>(row)->GetDowntimes();
768
769         if (!downtimes)
770                 return Empty;
771
772         Array::Ptr ids = boost::make_shared<Array>();
773
774         ObjectLock olock(downtimes);
775
776         String id;
777         Dictionary::Ptr downtime;
778         BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
779
780                 if (!downtime)
781                         continue;
782
783                 if (Service::IsDowntimeExpired(downtime))
784                         continue;
785
786                 ids->Add(downtime->Get("legacy_id"));
787         }
788
789         return ids;
790 }
791
792 Value ServicesTable::DowntimesWithInfoAccessor(const Value& row)
793 {
794         Dictionary::Ptr downtimes = static_cast<Service::Ptr>(row)->GetDowntimes();
795
796         if (!downtimes)
797                 return Empty;
798
799         Array::Ptr ids = boost::make_shared<Array>();
800
801         ObjectLock olock(downtimes);
802
803         String id;
804         Dictionary::Ptr downtime;
805         BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
806
807                 if (!downtime)
808                         continue;
809
810                 if (Service::IsDowntimeExpired(downtime))
811                         continue;
812
813                 Array::Ptr downtime_info = boost::make_shared<Array>();
814                 downtime_info->Add(downtime->Get("legacy_id"));
815                 downtime_info->Add(downtime->Get("author"));
816                 downtime_info->Add(downtime->Get("comment"));
817                 ids->Add(downtime_info);
818         }
819
820         return ids;
821 }
822
823 Value ServicesTable::CommentsAccessor(const Value& row)
824 {
825         Dictionary::Ptr comments = static_cast<Service::Ptr>(row)->GetComments();
826
827         if (!comments)
828                 return Empty;
829
830         Array::Ptr ids = boost::make_shared<Array>();
831
832         ObjectLock olock(comments);
833
834         String id;
835         Dictionary::Ptr comment;
836         BOOST_FOREACH(boost::tie(id, comment), comments) {
837
838                 if (!comment)
839                         continue;
840
841                 if (Service::IsCommentExpired(comment))
842                         continue;
843
844                 ids->Add(comment->Get("legacy_id"));
845         }
846
847         return ids;
848 }
849
850 Value ServicesTable::CommentsWithInfoAccessor(const Value& row)
851 {
852         Dictionary::Ptr comments = static_cast<Service::Ptr>(row)->GetComments();
853
854         if (!comments)
855                 return Empty;
856
857         Array::Ptr ids = boost::make_shared<Array>();
858
859         ObjectLock olock(comments);
860
861         String id;
862         Dictionary::Ptr comment;
863         BOOST_FOREACH(boost::tie(id, comment), comments) {
864
865                 if (!comment)
866                         continue;
867
868                 if (Service::IsCommentExpired(comment))
869                         continue;
870
871                 Array::Ptr comment_info = boost::make_shared<Array>();
872                 comment_info->Add(comment->Get("legacy_id"));
873                 comment_info->Add(comment->Get("author"));
874                 comment_info->Add(comment->Get("text"));
875                 ids->Add(comment_info);
876         }
877
878         return ids;
879 }
880
881 Value ServicesTable::CommentsWithExtraInfoAccessor(const Value& row)
882 {
883         Dictionary::Ptr comments = static_cast<Service::Ptr>(row)->GetComments();
884
885         if (!comments)
886                 return Empty;
887
888         Array::Ptr ids = boost::make_shared<Array>();
889
890         ObjectLock olock(comments);
891
892         String id;
893         Dictionary::Ptr comment;
894         BOOST_FOREACH(boost::tie(id, comment), comments) {
895
896                 if (!comment)
897                         continue;
898
899                 if (Service::IsCommentExpired(comment))
900                         continue;
901
902                 Array::Ptr comment_info = boost::make_shared<Array>();
903                 comment_info->Add(comment->Get("legacy_id"));
904                 comment_info->Add(comment->Get("author"));
905                 comment_info->Add(comment->Get("text"));
906                 comment_info->Add(comment->Get("entry_type"));
907                 comment_info->Add(static_cast<int>(comment->Get("entry_time")));
908                 ids->Add(comment_info);
909         }
910
911         return ids;
912 }
913
914 Value ServicesTable::CustomVariableNamesAccessor(const Value& row)
915 {
916         Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
917
918         if (!custom)
919                 return Empty;
920
921         Array::Ptr cv = boost::make_shared<Array>();
922
923         ObjectLock olock(custom);
924         String key;
925         Value value;
926         BOOST_FOREACH(boost::tie(key, value), custom) {
927                 if (key == "notes" ||
928                     key == "action_url" ||
929                     key == "notes_url" ||
930                     key == "icon_image" ||
931                     key == "icon_image_alt" ||
932                     key == "statusmap_image" ||
933                     key == "2d_coords")
934                         continue;
935
936                 cv->Add(key);
937         }
938
939         return cv;
940 }
941
942 Value ServicesTable::CustomVariableValuesAccessor(const Value& row)
943 {
944         Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
945
946         if (!custom)
947                 return Empty;
948
949         Array::Ptr cv = boost::make_shared<Array>();
950
951         ObjectLock olock(custom);
952         String key;
953         Value value;
954         BOOST_FOREACH(boost::tie(key, value), custom) {
955                 if (key == "notes" ||
956                     key == "action_url" ||
957                     key == "notes_url" ||
958                     key == "icon_image" ||
959                     key == "icon_image_alt" ||
960                     key == "statusmap_image" ||
961                     key == "2d_coords")
962                         continue;
963
964                 cv->Add(value);
965         }
966
967         return cv;
968 }
969
970 Value ServicesTable::CustomVariablesAccessor(const Value& row)
971 {
972         Dictionary::Ptr custom = static_cast<Service::Ptr>(row)->GetCustom();
973
974         if (!custom)
975                 return Empty;
976
977         Array::Ptr cv = boost::make_shared<Array>();
978
979         ObjectLock olock(custom);
980         String key;
981         Value value;
982         BOOST_FOREACH(boost::tie(key, value), custom) {
983                 if (key == "notes" ||
984                     key == "action_url" ||
985                     key == "notes_url" ||
986                     key == "icon_image" ||
987                     key == "icon_image_alt" ||
988                     key == "statusmap_image" ||
989                     key == "2d_coords")
990                         continue;
991
992                 Array::Ptr key_val = boost::make_shared<Array>();
993                 key_val->Add(key);
994                 key_val->Add(value);
995                 cv->Add(key_val);
996         }
997
998         return cv;
999 }
1000
1001 Value ServicesTable::GroupsAccessor(const Value& row)
1002 {
1003         Array::Ptr groups = static_cast<Service::Ptr>(row)->GetGroups();
1004
1005         if (!groups)
1006                 return Empty;
1007
1008         return groups;
1009 }
1010
1011 Value ServicesTable::ContactGroupsAccessor(const Value& row)
1012 {
1013         Array::Ptr contactgroups = CompatUtility::GetServiceNotificationUserGroups(static_cast<Service::Ptr>(row));
1014
1015         Array::Ptr contactgroup_names = boost::make_shared<Array>();
1016
1017         ObjectLock olock(contactgroups);
1018         BOOST_FOREACH(const UserGroup::Ptr& usergroup, contactgroups) {
1019                 contactgroup_names->Add(usergroup->GetName());
1020         }
1021
1022         return contactgroup_names;
1023 }
1024
1025