1 /******************************************************************************
3 * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
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. *
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. *
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 ******************************************************************************/
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>
42 using namespace icinga;
44 REGISTER_TYPE(StatusDataWriter);
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).
53 * Starts the component.
55 void StatusDataWriter::Start(void)
57 DynamicObject::Start();
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);
66 void StatusDataWriter::DumpComments(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
69 Dictionary::Ptr comments = owner->GetComments();
71 Host::Ptr host = owner->GetHost();
76 ObjectLock olock(comments);
80 BOOST_FOREACH(boost::tie(id, comment), comments) {
81 if (comment->IsExpired())
84 if (type == CompatTypeHost)
85 fp << "hostcomment {" << "\n";
87 fp << "servicecomment {" << "\n"
88 << "\t" << "service_description=" << owner->GetShortName() << "\n";
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"
104 void StatusDataWriter::DumpTimePeriod(std::ostream& fp, const TimePeriod::Ptr& tp)
106 fp << "define timeperiod {" << "\n"
107 << "\t" << "timeperiod_name" << "\t" << tp->GetName() << "\n"
108 << "\t" << "alias" << "\t" << tp->GetName() << "\n";
110 Dictionary::Ptr ranges = tp->GetRanges();
113 ObjectLock olock(ranges);
116 BOOST_FOREACH(boost::tie(key, value), ranges) {
117 fp << "\t" << key << "\t" << value << "\n";
121 fp << "\t" << "}" << "\n"
125 void StatusDataWriter::DumpCommand(std::ostream& fp, const Command::Ptr& command)
127 fp << "define command {" << "\n"
128 << "\t" << "command_name\t";
131 if (command->GetType() == DynamicType::GetByName("CheckCommand"))
133 else if (command->GetType() == DynamicType::GetByName("NotificationCommand"))
134 fp << "notification_";
135 else if (command->GetType() == DynamicType::GetByName("EventCommand"))
138 fp << command->GetName() << "\n";
140 fp << "\t" << "command_line\t";
142 Value commandLine = command->GetCommandLine();
144 if (commandLine.IsObjectType<Array>()) {
145 Array::Ptr args = commandLine;
147 ObjectLock olock(args);
149 BOOST_FOREACH(arg, args) {
150 // This is obviously incorrect for non-trivial cases.
151 fp << " \"" << CompatUtility::EscapeString(arg) << "\"";
153 } else if (!commandLine.IsEmpty()) {
154 fp << CompatUtility::EscapeString(commandLine);
161 fp << "\t" << "}" << "\n"
166 void StatusDataWriter::DumpDowntimes(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
168 Host::Ptr host = owner->GetHost();
173 Dictionary::Ptr downtimes = owner->GetDowntimes();
175 ObjectLock olock(downtimes);
178 Downtime::Ptr downtime;
179 BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
180 if (downtime->IsExpired())
183 if (type == CompatTypeHost)
184 fp << "hostdowntime {" << "\n";
186 fp << "servicedowntime {" << "\n"
187 << "\t" << "service_description=" << owner->GetShortName() << "\n";
189 Downtime::Ptr triggeredByObj = Service::GetDowntimeByID(downtime->GetTriggeredBy());
190 int triggeredByLegacy = 0;
192 triggeredByLegacy = triggeredByObj->GetLegacyId();
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"
211 void StatusDataWriter::DumpHostStatus(std::ostream& fp, const Host::Ptr& host)
213 fp << "hoststatus {" << "\n"
214 << "\t" << "host_name=" << host->GetName() << "\n";
216 Service::Ptr hc = host->GetCheckService();
217 ObjectLock olock(hc);
220 DumpServiceStatusAttrs(fp, hc, CompatTypeHost);
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";
227 fp << "\t" << "}" << "\n"
231 DumpDowntimes(fp, hc, CompatTypeHost);
232 DumpComments(fp, hc, CompatTypeHost);
236 void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host)
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";
243 Dictionary::Ptr macros = host->GetMacros();
246 fp << "\t" << "address" << "\t" << macros->Get("address") << "\n"
247 << "\t" << "address6" << "\t" << macros->Get("address6") << "\n";
250 std::set<Host::Ptr> parents = host->GetParentHosts();
252 if (!parents.empty()) {
253 fp << "\t" << "parents" << "\t";
254 DumpNameList(fp, parents);
258 Service::Ptr hc = host->GetCheckService();
260 ObjectLock olock(hc);
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";
272 CheckCommand::Ptr checkcommand = hc->GetCheckCommand();
274 fp << "\t" << "check_command" << "\t" << "check_" << checkcommand->GetName() << "\n";
276 EventCommand::Ptr eventcommand = hc->GetEventCommand();
278 fp << "\t" << "event_handler" << "\t" << "event_" << eventcommand->GetName() << "\n";
280 TimePeriod::Ptr check_period = hc->GetCheckPeriod();
282 fp << "\t" << "check_period" << "\t" << check_period->GetName() << "\n";
284 fp << "\t" << "contacts" << "\t";
285 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(hc));
288 fp << "\t" << "contact_groups" << "\t";
289 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(hc));
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";
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";
308 fp << "\t" << "host_groups" << "\t";
311 Array::Ptr groups = host->GetGroups();
314 ObjectLock olock(groups);
316 BOOST_FOREACH(const String& name, groups) {
317 HostGroup::Ptr hg = HostGroup::GetByName(name);
332 DumpCustomAttributes(fp, host);
334 fp << "\t" << "}" << "\n"
338 void StatusDataWriter::DumpServiceStatusAttrs(std::ostream& fp, const Service::Ptr& service, CompatObjectType type)
340 Dictionary::Ptr attrs = CompatUtility::GetServiceStatusAttributes(service, type);
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";
384 void StatusDataWriter::DumpServiceStatus(std::ostream& fp, const Service::Ptr& service)
386 Host::Ptr host = service->GetHost();
391 fp << "servicestatus {" << "\n"
392 << "\t" << "host_name=" << host->GetName() << "\n"
393 << "\t" << "service_description=" << service->GetShortName() << "\n";
396 ObjectLock olock(service);
397 DumpServiceStatusAttrs(fp, service, CompatTypeService);
400 fp << "\t" << "}" << "\n"
403 DumpDowntimes(fp, service, CompatTypeService);
404 DumpComments(fp, service, CompatTypeService);
407 void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& service)
409 Host::Ptr host = service->GetHost();
414 String check_period_str;
415 TimePeriod::Ptr check_period = service->GetCheckPeriod();
417 check_period_str = check_period->GetName();
419 check_period_str = "24x7";
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();
427 if (notification_interval == -1)
428 notification_interval = 60;
431 ObjectLock olock(service);
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";
450 CheckCommand::Ptr checkcommand = service->GetCheckCommand();
452 fp << "\t" << "check_command" << "\t" << "check_" << checkcommand->GetName() << "\n";
454 EventCommand::Ptr eventcommand = service->GetEventCommand();
456 fp << "\t" << "event_handler" << "\t" << "event_" << eventcommand->GetName() << "\n";
458 TimePeriod::Ptr check_period = service->GetCheckPeriod();
460 fp << "\t" << "check_period" << "\t" << check_period->GetName() << "\n";
462 fp << "\t" << "contacts" << "\t";
463 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(service));
466 fp << "\t" << "contact_groups" << "\t";
467 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(service));
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";
477 fp << "\t" << "service_groups" << "\t";
480 Array::Ptr groups = service->GetGroups();
483 ObjectLock olock(groups);
485 BOOST_FOREACH(const String& name, groups) {
486 ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);
501 DumpCustomAttributes(fp, service);
503 fp << "\t" << "}" << "\n"
506 BOOST_FOREACH(const Service::Ptr& parent, service->GetParentServices()) {
507 Host::Ptr host = service->GetHost();
512 Host::Ptr parent_host = parent->GetHost();
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"
529 void StatusDataWriter::DumpCustomAttributes(std::ostream& fp, const DynamicObject::Ptr& object)
531 Dictionary::Ptr custom = object->GetCustom();
536 ObjectLock olock(custom);
539 BOOST_FOREACH(boost::tie(key, value), custom) {
542 if (key != "notes" && key != "action_url" && key != "notes_url" &&
543 key != "icon_image" && key != "icon_image_alt" && key != "statusmap_image" && "2d_coords")
546 fp << key << "\t" << value << "\n";
551 * Periodically writes the status.dat and objects.cache files.
553 void StatusDataWriter::StatusTimerHandler(void)
555 Log(LogInformation, "compat", "Writing compat status information");
557 String statuspath = GetStatusPath();
558 String objectspath = GetObjectsPath();
559 String statuspathtmp = statuspath + ".tmp"; /* XXX make this a global definition */
560 String objectspathtmp = objectspath + ".tmp";
562 std::ofstream statusfp;
563 statusfp.open(statuspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
565 statusfp << std::fixed;
567 statusfp << "# Icinga status file" << "\n"
568 << "# This file is auto-generated. Do not modify this file." << "\n"
571 statusfp << "info {" << "\n"
572 << "\t" << "created=" << Utility::GetTime() << "\n"
573 << "\t" << "version=" << Application::GetVersion() << "\n"
574 << "\t" << "}" << "\n"
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"
598 std::ofstream objectfp;
599 objectfp.open(objectspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
601 objectfp << std::fixed;
603 objectfp << "# Icinga objects cache file" << "\n"
604 << "# This file is auto-generated. Do not modify this file." << "\n"
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();
613 std::ostringstream tempobjectfp;
614 tempobjectfp << std::fixed;
615 DumpHostObject(tempobjectfp, host);
616 objectfp << tempobjectfp.str();
619 BOOST_FOREACH(const HostGroup::Ptr& hg, DynamicType::GetObjects<HostGroup>()) {
620 std::ostringstream tempobjectfp;
621 tempobjectfp << std::fixed;
623 tempobjectfp << "define hostgroup {" << "\n"
624 << "\t" << "hostgroup_name" << "\t" << hg->GetName() << "\n";
626 DumpCustomAttributes(tempobjectfp, hg);
628 tempobjectfp << "\t" << "members" << "\t";
629 DumpNameList(tempobjectfp, hg->GetMembers());
631 << "\t" << "}" << "\n";
633 objectfp << tempobjectfp.str();
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();
642 std::ostringstream tempobjectfp;
643 tempobjectfp << std::fixed;
644 DumpServiceObject(tempobjectfp, service);
645 objectfp << tempobjectfp.str();
648 BOOST_FOREACH(const ServiceGroup::Ptr& sg, DynamicType::GetObjects<ServiceGroup>()) {
649 std::ostringstream tempobjectfp;
650 tempobjectfp << std::fixed;
652 tempobjectfp << "define servicegroup {" << "\n"
653 << "\t" << "servicegroup_name" << "\t" << sg->GetName() << "\n";
655 DumpCustomAttributes(tempobjectfp, sg);
657 tempobjectfp << "\t" << "members" << "\t";
659 std::vector<String> sglist;
660 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
661 Host::Ptr host = service->GetHost();
666 sglist.push_back(host->GetName());
667 sglist.push_back(service->GetShortName());
670 DumpStringList(tempobjectfp, sglist);
675 objectfp << tempobjectfp.str();
678 BOOST_FOREACH(const User::Ptr& user, DynamicType::GetObjects<User>()) {
679 std::ostringstream tempobjectfp;
680 tempobjectfp << std::fixed;
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"
692 objectfp << tempobjectfp.str();
695 BOOST_FOREACH(const UserGroup::Ptr& ug, DynamicType::GetObjects<UserGroup>()) {
696 std::ostringstream tempobjectfp;
697 tempobjectfp << std::fixed;
699 tempobjectfp << "define contactgroup {" << "\n"
700 << "\t" << "contactgroup_name" << "\t" << ug->GetName() << "\n"
701 << "\t" << "alias" << "\t" << ug->GetDisplayName() << "\n";
703 tempobjectfp << "\t" << "members" << "\t";
704 DumpNameList(tempobjectfp, ug->GetMembers());
706 << "\t" << "}" << "\n";
708 objectfp << tempobjectfp.str();
711 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<CheckCommand>()) {
712 DumpCommand(objectfp, command);
715 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<NotificationCommand>()) {
716 DumpCommand(objectfp, command);
719 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<EventCommand>()) {
720 DumpCommand(objectfp, command);
723 BOOST_FOREACH(const TimePeriod::Ptr& tp, DynamicType::GetObjects<TimePeriod>()) {
724 DumpTimePeriod(objectfp, tp);
731 _unlink(statuspath.CStr());
732 _unlink(objectspath.CStr());
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));
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));