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