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