]> granicus.if.org Git - icinga2/blob - components/compat/statusdatawriter.cpp
Avoid unnecessary copies when iterating over dictionaries.
[icinga2] / components / compat / statusdatawriter.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 "compat/statusdatawriter.h"
21 #include "icinga/icingaapplication.h"
22 #include "icinga/cib.h"
23 #include "icinga/hostgroup.h"
24 #include "icinga/servicegroup.h"
25 #include "icinga/checkcommand.h"
26 #include "icinga/eventcommand.h"
27 #include "icinga/timeperiod.h"
28 #include "icinga/notificationcommand.h"
29 #include "icinga/compatutility.h"
30 #include "base/dynamictype.h"
31 #include "base/objectlock.h"
32 #include "base/convert.h"
33 #include "base/logger_fwd.h"
34 #include "base/exception.h"
35 #include "base/application.h"
36 #include <boost/foreach.hpp>
37 #include <boost/tuple/tuple.hpp>
38 #include <boost/algorithm/string/replace.hpp>
39 #include <fstream>
40
41 using namespace icinga;
42
43 REGISTER_TYPE(StatusDataWriter);
44
45 /**
46  * Hint: The reason why we're using "\n" rather than std::endl is because
47  * std::endl also _flushes_ the output stream which severely degrades
48  * performance (see http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html).
49  */
50
51 /**
52  * Starts the component.
53  */
54 void StatusDataWriter::Start(void)
55 {
56         DynamicObject::Start();
57
58         m_StatusTimer = make_shared<Timer>();
59         m_StatusTimer->SetInterval(15);
60         m_StatusTimer->OnTimerExpired.connect(boost::bind(&StatusDataWriter::StatusTimerHandler, this));
61         m_StatusTimer->Start();
62         m_StatusTimer->Reschedule(0);
63 }
64
65 void StatusDataWriter::DumpComments(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
66 {
67         Service::Ptr service;
68         Dictionary::Ptr comments = owner->GetComments();
69
70         Host::Ptr host = owner->GetHost();
71
72         ObjectLock olock(comments);
73
74         BOOST_FOREACH(const Dictionary::Pair& kv, comments) {
75                 Comment::Ptr comment = kv.second;
76
77                 if (comment->IsExpired())
78                         continue;
79
80                 if (type == CompatTypeHost)
81                         fp << "hostcomment {" << "\n";
82                 else
83                         fp << "servicecomment {" << "\n"
84                            << "\t" << "service_description=" << owner->GetShortName() << "\n";
85
86                 fp << "\t" << "host_name=" << host->GetName() << "\n"
87                    << "\t" << "comment_id=" << comment->GetLegacyId() << "\n"
88                    << "\t" << "entry_time=" << comment->GetEntryTime() << "\n"
89                    << "\t" << "entry_type=" << comment->GetEntryType() << "\n"
90                    << "\t" << "persistent=" << 1 << "\n"
91                    << "\t" << "author=" << comment->GetAuthor() << "\n"
92                    << "\t" << "comment_data=" << comment->GetText() << "\n"
93                    << "\t" << "expires=" << (comment->GetExpireTime() != 0 ? 1 : 0) << "\n"
94                    << "\t" << "expire_time=" << comment->GetExpireTime() << "\n"
95                    << "\t" << "}" << "\n"
96                    << "\n";
97         }
98 }
99
100 void StatusDataWriter::DumpTimePeriod(std::ostream& fp, const TimePeriod::Ptr& tp)
101 {
102         fp << "define timeperiod {" << "\n"
103            << "\t" << "timeperiod_name" << "\t" << tp->GetName() << "\n"
104            << "\t" << "alias" << "\t" << tp->GetName() << "\n";
105
106         Dictionary::Ptr ranges = tp->GetRanges();
107
108         if (ranges) {
109                 ObjectLock olock(ranges);
110                 BOOST_FOREACH(const Dictionary::Pair& kv, ranges) {
111                         fp << "\t" << kv.first << "\t" << kv.second << "\n";
112                 }
113         }
114
115         fp << "\t" << "}" << "\n"
116            << "\n";
117 }
118
119 void StatusDataWriter::DumpCommand(std::ostream& fp, const Command::Ptr& command)
120 {
121         fp << "define command {" << "\n"
122            << "\t" << "command_name\t";
123
124
125         if (command->GetType() == DynamicType::GetByName("CheckCommand"))
126                 fp << "check_";
127         else if (command->GetType() == DynamicType::GetByName("NotificationCommand"))
128                 fp << "notification_";
129         else if (command->GetType() == DynamicType::GetByName("EventCommand"))
130                 fp << "event_";
131
132         fp << command->GetName() << "\n";
133
134         fp << "\t" << "command_line\t";
135
136         Value commandLine = command->GetCommandLine();
137
138         if (commandLine.IsObjectType<Array>()) {
139                 Array::Ptr args = commandLine;
140
141                 ObjectLock olock(args);
142                 String arg;
143                 BOOST_FOREACH(arg, args) {
144                         // This is obviously incorrect for non-trivial cases.
145                         fp << " \"" << CompatUtility::EscapeString(arg) << "\"";
146                 }
147         } else if (!commandLine.IsEmpty()) {
148                 fp << CompatUtility::EscapeString(commandLine);
149         } else {
150                 fp << "<internal>";
151         }
152
153         fp << "\n";
154
155         fp << "\t" << "}" << "\n"
156            << "\n";
157
158 }
159
160 void StatusDataWriter::DumpDowntimes(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
161 {
162         Host::Ptr host = owner->GetHost();
163
164         Dictionary::Ptr downtimes = owner->GetDowntimes();
165
166         ObjectLock olock(downtimes);
167
168         BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
169                 Downtime::Ptr downtime = kv.second;
170
171                 if (downtime->IsExpired())
172                         continue;
173
174                 if (type == CompatTypeHost)
175                         fp << "hostdowntime {" << "\n";
176                 else
177                         fp << "servicedowntime {" << "\n"
178                            << "\t" << "service_description=" << owner->GetShortName() << "\n";
179
180                 Downtime::Ptr triggeredByObj = Service::GetDowntimeByID(downtime->GetTriggeredBy());
181                 int triggeredByLegacy = 0;
182                 if (triggeredByObj)
183                         triggeredByLegacy = triggeredByObj->GetLegacyId();
184
185                 fp << "\t" << "host_name=" << host->GetName() << "\n"
186                    << "\t" << "downtime_id=" << downtime->GetLegacyId() << "\n"
187                    << "\t" << "entry_time=" << downtime->GetEntryTime() << "\n"
188                    << "\t" << "start_time=" << downtime->GetStartTime() << "\n"
189                    << "\t" << "end_time=" << downtime->GetEndTime() << "\n"
190                    << "\t" << "triggered_by=" << triggeredByLegacy << "\n"
191                    << "\t" << "fixed=" << static_cast<long>(downtime->GetFixed()) << "\n"
192                    << "\t" << "duration=" << static_cast<long>(downtime->GetDuration()) << "\n"
193                    << "\t" << "is_in_effect=" << (downtime->IsActive() ? 1 : 0) << "\n"
194                    << "\t" << "author=" << downtime->GetAuthor() << "\n"
195                    << "\t" << "comment=" << downtime->GetComment() << "\n"
196                    << "\t" << "trigger_time=" << downtime->GetTriggerTime() << "\n"
197                    << "\t" << "}" << "\n"
198                    << "\n";
199         }
200 }
201
202 void StatusDataWriter::DumpHostStatus(std::ostream& fp, const Host::Ptr& host)
203 {
204         fp << "hoststatus {" << "\n"
205            << "\t" << "host_name=" << host->GetName() << "\n";
206
207         Service::Ptr hc = host->GetCheckService();
208         ObjectLock olock(hc);
209
210         if (hc)
211                 DumpServiceStatusAttrs(fp, hc, CompatTypeHost);
212
213         /* ugly but cgis parse only that */
214         fp << "\t" << "last_time_up=" << host->GetLastStateUp() << "\n"
215            << "\t" << "last_time_down=" << host->GetLastStateDown() << "\n"
216            << "\t" << "last_time_unreachable=" << host->GetLastStateUnreachable() << "\n";
217
218         fp << "\t" << "}" << "\n"
219            << "\n";
220
221         if (hc) {
222                 DumpDowntimes(fp, hc, CompatTypeHost);
223                 DumpComments(fp, hc, CompatTypeHost);
224         }
225 }
226
227 void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host)
228 {
229         fp << "define host {" << "\n"
230            << "\t" << "host_name" << "\t" << host->GetName() << "\n"
231            << "\t" << "display_name" << "\t" << host->GetDisplayName() << "\n"
232            << "\t" << "alias" << "\t" << host->GetDisplayName() << "\n";
233
234         Dictionary::Ptr macros = host->GetMacros();
235
236         if (macros) {
237                 fp << "\t" << "address" << "\t" << macros->Get("address") << "\n"
238                    << "\t" << "address6" << "\t" << macros->Get("address6") << "\n";
239         }
240
241         std::set<Host::Ptr> parents = host->GetParentHosts();
242
243         if (!parents.empty()) {
244                 fp << "\t" << "parents" << "\t";
245                 DumpNameList(fp, parents);
246                 fp << "\n";
247         }
248
249         Service::Ptr hc = host->GetCheckService();
250         if (hc) {
251                 ObjectLock olock(hc);
252
253                 fp << "\t" << "check_interval" << "\t" << hc->GetCheckInterval() / 60.0 << "\n"
254                    << "\t" << "retry_interval" << "\t" << hc->GetRetryInterval() / 60.0 << "\n"
255                    << "\t" << "max_check_attempts" << "\t" << hc->GetMaxCheckAttempts() << "\n"
256                    << "\t" << "active_checks_enabled" << "\t" << (hc->GetEnableActiveChecks() ? 1 : 0) << "\n"
257                    << "\t" << "passive_checks_enabled" << "\t" << (hc->GetEnablePassiveChecks() ? 1 : 0) << "\n"
258                    << "\t" << "notifications_enabled" << "\t" << (hc->GetEnableNotifications() ? 1 : 0) << "\n"
259                    << "\t" << "notification_options" << "\t" << "d,u,r" << "\n"
260                    << "\t" << "notification_interval" << "\t" << 1 << "\n"
261                    << "\t" << "event_handler_enabled" << "\t" << (hc->GetEnableEventHandler() ? 1 : 0) << "\n";
262
263                 CheckCommand::Ptr checkcommand = hc->GetCheckCommand();
264                 if (checkcommand)
265                         fp << "\t" << "check_command" << "\t" << "check_" << checkcommand->GetName() << "\n";
266
267                 EventCommand::Ptr eventcommand = hc->GetEventCommand();
268                 if (eventcommand)
269                         fp << "\t" << "event_handler" << "\t" << "event_" << eventcommand->GetName() << "\n";
270
271                 TimePeriod::Ptr check_period = hc->GetCheckPeriod();
272                 if (check_period)
273                         fp << "\t" << "check_period" << "\t" << check_period->GetName() << "\n";
274
275                 fp << "\t" << "contacts" << "\t";
276                 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(hc));
277                 fp << "\n";
278
279                 fp << "\t" << "contact_groups" << "\t";
280                 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(hc));
281                 fp << "\n";
282
283                 fp << "\t" << "initial_state" << "\t" << "o" << "\n"
284                    << "\t" << "low_flap_threshold" << "\t" << hc->GetFlappingThreshold() << "\n"
285                    << "\t" << "high_flap_threshold" << "\t" << hc->GetFlappingThreshold() << "\n"
286                    << "\t" << "process_perf_data" << "\t" << 1 << "\n"
287                    << "\t" << "check_freshness" << "\t" << 1 << "\n";
288
289         } else {
290                 fp << "\t" << "check_interval" << "\t" << 60 << "\n"
291                    << "\t" << "retry_interval" << "\t" << 60 << "\n"
292                    << "\t" << "max_check_attempts" << "\t" << 1 << "\n"
293                    << "\t" << "active_checks_enabled" << "\t" << 0 << "\n"
294                    << "\t" << "passive_checks_enabled" << "\t" << 0 << "\n"
295                    << "\t" << "notifications_enabled" << "\t" << 0 << "\n";
296
297         }
298
299         fp << "\t" << "host_groups" << "\t";
300         bool first = true;
301
302         Array::Ptr groups = host->GetGroups();
303
304         if (groups) {
305                 ObjectLock olock(groups);
306
307                 BOOST_FOREACH(const String& name, groups) {
308                         HostGroup::Ptr hg = HostGroup::GetByName(name);
309
310                         if (hg) {
311                                 if (!first)
312                                         fp << ",";
313                                 else
314                                         first = false;
315
316                                 fp << hg->GetName();
317                         }
318                 }
319         }
320
321         fp << "\n";
322
323         DumpCustomAttributes(fp, host);
324
325         fp << "\t" << "}" << "\n"
326            << "\n";
327 }
328
329 void StatusDataWriter::DumpServiceStatusAttrs(std::ostream& fp, const Service::Ptr& service, CompatObjectType type)
330 {
331         Dictionary::Ptr attrs = CompatUtility::GetServiceStatusAttributes(service, type);
332
333         fp << "\t" << "check_command=" << attrs->Get("check_command") << "\n"
334            << "\t" << "event_handler=" << attrs->Get("event_handler") << "\n"
335            << "\t" << "check_period=" << attrs->Get("check_period") << "\n"
336            << "\t" << "check_interval=" << static_cast<double>(attrs->Get("check_interval")) << "\n"
337            << "\t" << "retry_interval=" << static_cast<double>(attrs->Get("retry_interval")) << "\n"
338            << "\t" << "has_been_checked=" << attrs->Get("has_been_checked") << "\n"
339            << "\t" << "should_be_scheduled=" << attrs->Get("should_be_scheduled") << "\n"
340            << "\t" << "check_execution_time=" << static_cast<double>(attrs->Get("check_execution_time")) << "\n"
341            << "\t" << "check_latency=" << static_cast<double>(attrs->Get("check_latency")) << "\n"
342            << "\t" << "current_state=" << attrs->Get("current_state") << "\n"
343            << "\t" << "state_type=" << attrs->Get("state_type") << "\n"
344            << "\t" << "plugin_output=" << attrs->Get("plugin_output") << "\n"
345            << "\t" << "long_plugin_output=" << attrs->Get("long_plugin_output") << "\n"
346            << "\t" << "performance_data=" << attrs->Get("performance_data") << "\n"
347            << "\t" << "check_source=" << attrs->Get("check_source") << "\n"
348            << "\t" << "last_check=" << static_cast<long>(attrs->Get("last_check")) << "\n"
349            << "\t" << "next_check=" << static_cast<long>(attrs->Get("next_check")) << "\n"
350            << "\t" << "current_attempt=" << attrs->Get("current_attempt") << "\n"
351            << "\t" << "max_attempts=" << attrs->Get("max_attempts") << "\n"
352            << "\t" << "last_state_change=" << static_cast<long>(attrs->Get("last_state_change")) << "\n"
353            << "\t" << "last_hard_state_change=" << static_cast<long>(attrs->Get("last_hard_state_change")) << "\n"
354            << "\t" << "last_time_ok=" << static_cast<long>(attrs->Get("last_time_ok")) << "\n"
355            << "\t" << "last_time_warn=" << static_cast<long>(attrs->Get("last_time_warn")) << "\n"
356            << "\t" << "last_time_critical=" << static_cast<long>(attrs->Get("last_time_critical")) << "\n"
357            << "\t" << "last_time_unknown=" << static_cast<long>(attrs->Get("last_time_unknown")) << "\n"
358            << "\t" << "last_update=" << static_cast<long>(attrs->Get("last_update")) << "\n"
359            << "\t" << "notifications_enabled=" << attrs->Get("notifications_enabled") << "\n"
360            << "\t" << "active_checks_enabled=" << attrs->Get("active_checks_enabled") << "\n"
361            << "\t" << "passive_checks_enabled=" << attrs->Get("passive_checks_enabled") << "\n"
362            << "\t" << "flap_detection_enabled=" << attrs->Get("flap_detection_enabled") << "\n"
363            << "\t" << "is_flapping=" << attrs->Get("is_flapping") << "\n"
364            << "\t" << "percent_state_change=" << attrs->Get("percent_state_change") << "\n"
365            << "\t" << "problem_has_been_acknowledged=" << attrs->Get("problem_has_been_acknowledged") << "\n"
366            << "\t" << "acknowledgement_type=" << attrs->Get("acknowledgement_type") << "\n"
367            << "\t" << "acknowledgement_end_time=" << attrs->Get("acknowledgement_end_time") << "\n"
368            << "\t" << "scheduled_downtime_depth=" << attrs->Get("scheduled_downtime_depth") << "\n"
369            << "\t" << "last_notification=" << static_cast<long>(attrs->Get("last_notification")) << "\n"
370            << "\t" << "next_notification=" << static_cast<long>(attrs->Get("next_notification")) << "\n"
371            << "\t" << "current_notification_number=" << attrs->Get("current_notification_number") << "\n"
372            << "\t" << "modified_attributes=" << attrs->Get("modified_attributes") << "\n";
373 }
374
375 void StatusDataWriter::DumpServiceStatus(std::ostream& fp, const Service::Ptr& service)
376 {
377         Host::Ptr host = service->GetHost();
378
379         fp << "servicestatus {" << "\n"
380            << "\t" << "host_name=" << host->GetName() << "\n"
381            << "\t" << "service_description=" << service->GetShortName() << "\n";
382
383         {
384                 ObjectLock olock(service);
385                 DumpServiceStatusAttrs(fp, service, CompatTypeService);
386         }
387
388         fp << "\t" << "}" << "\n"
389            << "\n";
390
391         DumpDowntimes(fp, service, CompatTypeService);
392         DumpComments(fp, service, CompatTypeService);
393 }
394
395 void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& service)
396 {
397         Host::Ptr host = service->GetHost();
398
399         String check_period_str;
400         TimePeriod::Ptr check_period = service->GetCheckPeriod();
401         if (check_period)
402                 check_period_str = check_period->GetName();
403         else
404                 check_period_str = "24x7";
405
406         double notification_interval = -1;
407         BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
408                 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
409                         notification_interval = notification->GetNotificationInterval();
410         }
411
412         if (notification_interval == -1)
413                 notification_interval = 60;
414
415         {
416                 ObjectLock olock(service);
417
418                 fp << "define service {" << "\n"
419                    << "\t" << "host_name" << "\t" << host->GetName() << "\n"
420                    << "\t" << "service_description" << "\t" << service->GetShortName() << "\n"
421                    << "\t" << "display_name" << "\t" << service->GetDisplayName() << "\n"
422                    << "\t" << "check_period" << "\t" << check_period_str << "\n"
423                    << "\t" << "check_interval" << "\t" << service->GetCheckInterval() / 60.0 << "\n"
424                    << "\t" << "retry_interval" << "\t" << service->GetRetryInterval() / 60.0 << "\n"
425                    << "\t" << "max_check_attempts" << "\t" << service->GetMaxCheckAttempts() << "\n"
426                    << "\t" << "active_checks_enabled" << "\t" << (service->GetEnableActiveChecks() ? 1 : 0) << "\n"
427                    << "\t" << "passive_checks_enabled" << "\t" << (service->GetEnablePassiveChecks() ? 1 : 0) << "\n"
428                    << "\t" << "flap_detection_enabled" << "\t" << (service->GetEnableFlapping() ? 1 : 0) << "\n"
429                    << "\t" << "is_volatile" << "\t" << (service->GetVolatile() ? 1 : 0) << "\n"
430                    << "\t" << "notifications_enabled" << "\t" << (service->GetEnableNotifications() ? 1 : 0) << "\n"
431                    << "\t" << "notification_options" << "\t" << "u,w,c,r" << "\n"
432                    << "\t" << "notification_interval" << "\t" << notification_interval / 60.0 << "\n"
433                    << "\t" << "event_handler_enabled" << "\t" << (service->GetEnableEventHandler() ? 1 : 0) << "\n";
434
435                 CheckCommand::Ptr checkcommand = service->GetCheckCommand();
436                 if (checkcommand)
437                         fp << "\t" << "check_command" << "\t" << "check_" << checkcommand->GetName() << "\n";
438
439                 EventCommand::Ptr eventcommand = service->GetEventCommand();
440                 if (eventcommand)
441                         fp << "\t" << "event_handler" << "\t" << "event_" << eventcommand->GetName() << "\n";
442
443                 TimePeriod::Ptr check_period = service->GetCheckPeriod();
444                 if (check_period)
445                         fp << "\t" << "check_period" << "\t" << check_period->GetName() << "\n";
446
447                 fp << "\t" << "contacts" << "\t";
448                 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(service));
449                 fp << "\n";
450
451                 fp << "\t" << "contact_groups" << "\t";
452                 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(service));
453                 fp << "\n";
454
455                 fp << "\t" << "initial_state" << "\t" << "o" << "\n"
456                    << "\t" << "low_flap_threshold" << "\t" << service->GetFlappingThreshold() << "\n"
457                    << "\t" << "high_flap_threshold" << "\t" << service->GetFlappingThreshold() << "\n"
458                    << "\t" << "process_perf_data" << "\t" << 1 << "\n"
459                    << "\t" << "check_freshness" << "\t" << 1 << "\n";
460         }
461
462         fp << "\t" << "service_groups" << "\t";
463         bool first = true;
464
465         Array::Ptr groups = service->GetGroups();
466
467         if (groups) {
468                 ObjectLock olock(groups);
469
470                 BOOST_FOREACH(const String& name, groups) {
471                         ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);
472
473                         if (sg) {
474                                 if (!first)
475                                         fp << ",";
476                                 else
477                                         first = false;
478
479                                 fp << sg->GetName();
480                         }
481                 }
482         }
483
484         fp << "\n";
485
486         DumpCustomAttributes(fp, service);
487
488         fp << "\t" << "}" << "\n"
489            << "\n";
490
491         BOOST_FOREACH(const Service::Ptr& parent, service->GetParentServices()) {
492                 Host::Ptr host = service->GetHost();
493
494                 Host::Ptr parent_host = parent->GetHost();
495
496                 if (!parent_host)
497                         continue;
498
499                 fp << "define servicedependency {" << "\n"
500                    << "\t" << "dependent_host_name" << "\t" << host->GetName() << "\n"
501                    << "\t" << "dependent_service_description" << "\t" << service->GetShortName() << "\n"
502                    << "\t" << "host_name" << "\t" << parent_host->GetName() << "\n"
503                    << "\t" << "service_description" << "\t" << parent->GetShortName() << "\n"
504                    << "\t" << "execution_failure_criteria" << "\t" << "n" << "\n"
505                    << "\t" << "notification_failure_criteria" << "\t" << "w,u,c" << "\n"
506                    << "\t" << "}" << "\n"
507                    << "\n";
508         }
509 }
510
511 void StatusDataWriter::DumpCustomAttributes(std::ostream& fp, const DynamicObject::Ptr& object)
512 {
513         Dictionary::Ptr custom = object->GetCustom();
514
515         if (!custom)
516                 return;
517
518         ObjectLock olock(custom);
519         BOOST_FOREACH(const Dictionary::Pair& kv, custom) {
520                 fp << "\t";
521
522                 if (kv.first != "notes" && kv.first != "action_url" && kv.first != "notes_url" &&
523                     kv.first != "icon_image" && kv.first != "icon_image_alt" && kv.first != "statusmap_image" && kv.first != "2d_coords")
524                         fp << "_";
525
526                 fp << kv.first << "\t" << kv.second << "\n";
527         }
528 }
529
530 /**
531  * Periodically writes the status.dat and objects.cache files.
532  */
533 void StatusDataWriter::StatusTimerHandler(void)
534 {
535         Log(LogInformation, "compat", "Writing compat status information");
536
537         String statuspath = GetStatusPath();
538         String objectspath = GetObjectsPath();
539         String statuspathtmp = statuspath + ".tmp"; /* XXX make this a global definition */
540         String objectspathtmp = objectspath + ".tmp";
541
542         std::ofstream statusfp;
543         statusfp.open(statuspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
544
545         statusfp << std::fixed;
546
547         statusfp << "# Icinga status file" << "\n"
548                  << "# This file is auto-generated. Do not modify this file." << "\n"
549                  << "\n";
550
551         statusfp << "info {" << "\n"
552                  << "\t" << "created=" << Utility::GetTime() << "\n"
553                  << "\t" << "version=" << Application::GetVersion() << "\n"
554                  << "\t" << "}" << "\n"
555                  << "\n";
556
557         statusfp << "programstatus {" << "\n"
558                  << "\t" << "icinga_pid=" << Utility::GetPid() << "\n"
559                  << "\t" << "daemon_mode=1" << "\n"
560                  << "\t" << "program_start=" << static_cast<long>(Application::GetStartTime()) << "\n"
561                  << "\t" << "active_service_checks_enabled=" << (IcingaApplication::GetInstance()->GetEnableChecks() ? 1 : 0) << "\n"
562                  << "\t" << "passive_service_checks_enabled=1" << "\n"
563                  << "\t" << "active_host_checks_enabled=1" << "\n"
564                  << "\t" << "passive_host_checks_enabled=1" << "\n"
565                  << "\t" << "check_service_freshness=1" << "\n"
566                  << "\t" << "check_host_freshness=1" << "\n"
567                  << "\t" << "enable_notifications=" << (IcingaApplication::GetInstance()->GetEnableNotifications() ? 1 : 0) << "\n"
568                  << "\t" << "enable_flap_detection=" << (IcingaApplication::GetInstance()->GetEnableFlapping() ? 1 : 0) << "\n"
569                  << "\t" << "enable_failure_prediction=0" << "\n"
570                  << "\t" << "process_performance_data=" << (IcingaApplication::GetInstance()->GetEnablePerfdata() ? 1 : 0) << "\n"
571                  << "\t" << "active_scheduled_service_check_stats=" << CIB::GetActiveChecksStatistics(60) << "," << CIB::GetActiveChecksStatistics(5 * 60) << "," << CIB::GetActiveChecksStatistics(15 * 60) << "\n"
572                  << "\t" << "passive_service_check_stats=" << CIB::GetPassiveChecksStatistics(60) << "," << CIB::GetPassiveChecksStatistics(5 * 60) << "," << CIB::GetPassiveChecksStatistics(15 * 60) << "\n"
573                  << "\t" << "next_downtime_id=" << Service::GetNextDowntimeID() << "\n"
574                  << "\t" << "next_comment_id=" << Service::GetNextCommentID() << "\n"
575                  << "\t" << "}" << "\n"
576                  << "\n";
577
578         std::ofstream objectfp;
579         objectfp.open(objectspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
580
581         objectfp << std::fixed;
582
583         objectfp << "# Icinga objects cache file" << "\n"
584                  << "# This file is auto-generated. Do not modify this file." << "\n"
585                  << "\n";
586
587         BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
588                 std::ostringstream tempstatusfp;
589                 tempstatusfp << std::fixed;
590                 DumpHostStatus(tempstatusfp, host);
591                 statusfp << tempstatusfp.str();
592
593                 std::ostringstream tempobjectfp;
594                 tempobjectfp << std::fixed;
595                 DumpHostObject(tempobjectfp, host);
596                 objectfp << tempobjectfp.str();
597         }
598
599         BOOST_FOREACH(const HostGroup::Ptr& hg, DynamicType::GetObjects<HostGroup>()) {
600                 std::ostringstream tempobjectfp;
601                 tempobjectfp << std::fixed;
602
603                 tempobjectfp << "define hostgroup {" << "\n"
604                          << "\t" << "hostgroup_name" << "\t" << hg->GetName() << "\n";
605
606                 DumpCustomAttributes(tempobjectfp, hg);
607
608                 tempobjectfp << "\t" << "members" << "\t";
609                 DumpNameList(tempobjectfp, hg->GetMembers());
610                 tempobjectfp << "\n"
611                              << "\t" << "}" << "\n";
612
613                 objectfp << tempobjectfp.str();
614         }
615
616         BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
617                 std::ostringstream tempstatusfp;
618                 tempstatusfp << std::fixed;
619                 DumpServiceStatus(tempstatusfp, service);
620                 statusfp << tempstatusfp.str();
621
622                 std::ostringstream tempobjectfp;
623                 tempobjectfp << std::fixed;
624                 DumpServiceObject(tempobjectfp, service);
625                 objectfp << tempobjectfp.str();
626         }
627
628         BOOST_FOREACH(const ServiceGroup::Ptr& sg, DynamicType::GetObjects<ServiceGroup>()) {
629                 std::ostringstream tempobjectfp;
630                 tempobjectfp << std::fixed;
631
632                 tempobjectfp << "define servicegroup {" << "\n"
633                          << "\t" << "servicegroup_name" << "\t" << sg->GetName() << "\n";
634
635                 DumpCustomAttributes(tempobjectfp, sg);
636
637                 tempobjectfp << "\t" << "members" << "\t";
638
639                 std::vector<String> sglist;
640                 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
641                         Host::Ptr host = service->GetHost();
642
643                         sglist.push_back(host->GetName());
644                         sglist.push_back(service->GetShortName());
645                 }
646
647                 DumpStringList(tempobjectfp, sglist);
648
649                 tempobjectfp << "\n"
650                          << "}" << "\n";
651
652                 objectfp << tempobjectfp.str();
653         }
654
655         BOOST_FOREACH(const User::Ptr& user, DynamicType::GetObjects<User>()) {
656                 std::ostringstream tempobjectfp;
657                 tempobjectfp << std::fixed;
658
659                 tempobjectfp << "define contact {" << "\n"
660                          << "\t" << "contact_name" << "\t" << user->GetName() << "\n"
661                          << "\t" << "alias" << "\t" << user->GetDisplayName() << "\n"
662                          << "\t" << "service_notification_options" << "\t" << "w,u,c,r,f,s" << "\n"
663                          << "\t" << "host_notification_options" << "\t" << "d,u,r,f,s" << "\n"
664                          << "\t" << "host_notifications_enabled" << "\t" << 1 << "\n"
665                          << "\t" << "service_notifications_enabled" << "\t" << 1 << "\n"
666                          << "\t" << "}" << "\n"
667                          << "\n";
668
669                 objectfp << tempobjectfp.str();
670         }
671
672         BOOST_FOREACH(const UserGroup::Ptr& ug, DynamicType::GetObjects<UserGroup>()) {
673                 std::ostringstream tempobjectfp;
674                 tempobjectfp << std::fixed;
675
676                 tempobjectfp << "define contactgroup {" << "\n"
677                          << "\t" << "contactgroup_name" << "\t" << ug->GetName() << "\n"
678                          << "\t" << "alias" << "\t" << ug->GetDisplayName() << "\n";
679
680                 tempobjectfp << "\t" << "members" << "\t";
681                 DumpNameList(tempobjectfp, ug->GetMembers());
682                 tempobjectfp << "\n"
683                              << "\t" << "}" << "\n";
684
685                 objectfp << tempobjectfp.str();
686         }
687
688         BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<CheckCommand>()) {
689                 DumpCommand(objectfp, command);
690         }
691
692         BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<NotificationCommand>()) {
693                 DumpCommand(objectfp, command);
694         }
695
696         BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<EventCommand>()) {
697                 DumpCommand(objectfp, command);
698         }
699
700         BOOST_FOREACH(const TimePeriod::Ptr& tp, DynamicType::GetObjects<TimePeriod>()) {
701                 DumpTimePeriod(objectfp, tp);
702         }
703
704         statusfp.close();
705         objectfp.close();
706
707 #ifdef _WIN32
708         _unlink(statuspath.CStr());
709         _unlink(objectspath.CStr());
710 #endif /* _WIN32 */
711
712         statusfp.close();
713         if (rename(statuspathtmp.CStr(), statuspath.CStr()) < 0) {
714                 BOOST_THROW_EXCEPTION(posix_error()
715                     << boost::errinfo_api_function("rename")
716                     << boost::errinfo_errno(errno)
717                     << boost::errinfo_file_name(statuspathtmp));
718         }
719
720         objectfp.close();
721         if (rename(objectspathtmp.CStr(), objectspath.CStr()) < 0) {
722                 BOOST_THROW_EXCEPTION(posix_error()
723                     << boost::errinfo_api_function("rename")
724                     << boost::errinfo_errno(errno)
725                     << boost::errinfo_file_name(objectspathtmp));
726         }
727 }
728