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