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