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