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/algorithm/string/replace.hpp>
41 using namespace icinga;
43 REGISTER_TYPE(StatusDataWriter);
46 * Hint: The reason why we're using "\n" rather than std::endl is because
47 * std::endl also _flushes_ the output stream which severely degrades
48 * performance (see http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html).
52 * Starts the component.
54 void StatusDataWriter::Start(void)
56 DynamicObject::Start();
58 m_StatusTimer = make_shared<Timer>();
59 m_StatusTimer->SetInterval(15);
60 m_StatusTimer->OnTimerExpired.connect(boost::bind(&StatusDataWriter::StatusTimerHandler, this));
61 m_StatusTimer->Start();
62 m_StatusTimer->Reschedule(0);
65 void StatusDataWriter::DumpComments(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
68 Dictionary::Ptr comments = owner->GetComments();
70 Host::Ptr host = owner->GetHost();
75 ObjectLock olock(comments);
79 BOOST_FOREACH(boost::tie(id, comment), comments) {
80 if (comment->IsExpired())
83 if (type == CompatTypeHost)
84 fp << "hostcomment {" << "\n";
86 fp << "servicecomment {" << "\n"
87 << "\t" << "service_description=" << owner->GetShortName() << "\n";
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"
103 void StatusDataWriter::DumpTimePeriod(std::ostream& fp, const TimePeriod::Ptr& tp)
105 fp << "define timeperiod {" << "\n"
106 << "\t" << "timeperiod_name" << "\t" << tp->GetName() << "\n"
107 << "\t" << "alias" << "\t" << tp->GetName() << "\n";
109 Dictionary::Ptr ranges = tp->GetRanges();
112 ObjectLock olock(ranges);
115 BOOST_FOREACH(boost::tie(key, value), ranges) {
116 fp << "\t" << key << "\t" << value << "\n";
120 fp << "\t" << "}" << "\n"
124 void StatusDataWriter::DumpCommand(std::ostream& fp, const Command::Ptr& command)
126 fp << "define command {" << "\n"
127 << "\t" << "command_name\t";
130 if (command->GetType() == DynamicType::GetByName("CheckCommand"))
132 else if (command->GetType() == DynamicType::GetByName("NotificationCommand"))
133 fp << "notification_";
134 else if (command->GetType() == DynamicType::GetByName("EventCommand"))
137 fp << command->GetName() << "\n";
139 fp << "\t" << "command_line\t";
141 Value commandLine = command->GetCommandLine();
143 if (commandLine.IsObjectType<Array>()) {
144 Array::Ptr args = commandLine;
146 ObjectLock olock(args);
148 BOOST_FOREACH(arg, args) {
149 // This is obviously incorrect for non-trivial cases.
150 fp << " \"" << CompatUtility::EscapeString(arg) << "\"";
152 } else if (!commandLine.IsEmpty()) {
153 fp << CompatUtility::EscapeString(commandLine);
160 fp << "\t" << "}" << "\n"
165 void StatusDataWriter::DumpDowntimes(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
167 Host::Ptr host = owner->GetHost();
172 Dictionary::Ptr downtimes = owner->GetDowntimes();
174 ObjectLock olock(downtimes);
177 Downtime::Ptr downtime;
178 BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
179 if (downtime->IsExpired())
182 if (type == CompatTypeHost)
183 fp << "hostdowntime {" << "\n";
185 fp << "servicedowntime {" << "\n"
186 << "\t" << "service_description=" << owner->GetShortName() << "\n";
188 Downtime::Ptr triggeredByObj = Service::GetDowntimeByID(downtime->GetTriggeredBy());
189 int triggeredByLegacy = 0;
191 triggeredByLegacy = triggeredByObj->GetLegacyId();
193 fp << "\t" << "host_name=" << host->GetName() << "\n"
194 << "\t" << "downtime_id=" << downtime->GetLegacyId() << "\n"
195 << "\t" << "entry_time=" << downtime->GetEntryTime() << "\n"
196 << "\t" << "start_time=" << downtime->GetStartTime() << "\n"
197 << "\t" << "end_time=" << downtime->GetEndTime() << "\n"
198 << "\t" << "triggered_by=" << triggeredByLegacy << "\n"
199 << "\t" << "fixed=" << static_cast<long>(downtime->GetFixed()) << "\n"
200 << "\t" << "duration=" << static_cast<long>(downtime->GetDuration()) << "\n"
201 << "\t" << "is_in_effect=" << (downtime->IsActive() ? 1 : 0) << "\n"
202 << "\t" << "author=" << downtime->GetAuthor() << "\n"
203 << "\t" << "comment=" << downtime->GetComment() << "\n"
204 << "\t" << "trigger_time=" << downtime->GetTriggerTime() << "\n"
205 << "\t" << "}" << "\n"
210 void StatusDataWriter::DumpHostStatus(std::ostream& fp, const Host::Ptr& host)
212 fp << "hoststatus {" << "\n"
213 << "\t" << "host_name=" << host->GetName() << "\n";
215 Service::Ptr hc = host->GetCheckService();
216 ObjectLock olock(hc);
219 DumpServiceStatusAttrs(fp, hc, CompatTypeHost);
221 /* ugly but cgis parse only that */
222 fp << "\t" << "last_time_up=" << host->GetLastStateUp() << "\n"
223 << "\t" << "last_time_down=" << host->GetLastStateDown() << "\n"
224 << "\t" << "last_time_unreachable=" << host->GetLastStateUnreachable() << "\n";
226 fp << "\t" << "}" << "\n"
230 DumpDowntimes(fp, hc, CompatTypeHost);
231 DumpComments(fp, hc, CompatTypeHost);
235 void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host)
237 fp << "define host {" << "\n"
238 << "\t" << "host_name" << "\t" << host->GetName() << "\n"
239 << "\t" << "display_name" << "\t" << host->GetDisplayName() << "\n"
240 << "\t" << "alias" << "\t" << host->GetDisplayName() << "\n";
242 Dictionary::Ptr macros = host->GetMacros();
245 fp << "\t" << "address" << "\t" << macros->Get("address") << "\n"
246 << "\t" << "address6" << "\t" << macros->Get("address6") << "\n";
249 std::set<Host::Ptr> parents = host->GetParentHosts();
251 if (!parents.empty()) {
252 fp << "\t" << "parents" << "\t";
253 DumpNameList(fp, parents);
257 Service::Ptr hc = host->GetCheckService();
259 ObjectLock olock(hc);
261 fp << "\t" << "check_interval" << "\t" << hc->GetCheckInterval() / 60.0 << "\n"
262 << "\t" << "retry_interval" << "\t" << hc->GetRetryInterval() / 60.0 << "\n"
263 << "\t" << "max_check_attempts" << "\t" << hc->GetMaxCheckAttempts() << "\n"
264 << "\t" << "active_checks_enabled" << "\t" << (hc->GetEnableActiveChecks() ? 1 : 0) << "\n"
265 << "\t" << "passive_checks_enabled" << "\t" << (hc->GetEnablePassiveChecks() ? 1 : 0) << "\n"
266 << "\t" << "notifications_enabled" << "\t" << (hc->GetEnableNotifications() ? 1 : 0) << "\n"
267 << "\t" << "notification_options" << "\t" << "d,u,r" << "\n"
268 << "\t" << "notification_interval" << "\t" << 1 << "\n"
269 << "\t" << "event_handler_enabled" << "\t" << (hc->GetEnableEventHandler() ? 1 : 0) << "\n";
271 CheckCommand::Ptr checkcommand = hc->GetCheckCommand();
273 fp << "\t" << "check_command" << "\t" << "check_" << checkcommand->GetName() << "\n";
275 EventCommand::Ptr eventcommand = hc->GetEventCommand();
277 fp << "\t" << "event_handler" << "\t" << "event_" << eventcommand->GetName() << "\n";
279 TimePeriod::Ptr check_period = hc->GetCheckPeriod();
281 fp << "\t" << "check_period" << "\t" << check_period->GetName() << "\n";
283 fp << "\t" << "contacts" << "\t";
284 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(hc));
287 fp << "\t" << "contact_groups" << "\t";
288 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(hc));
291 fp << "\t" << "initial_state" << "\t" << "o" << "\n"
292 << "\t" << "low_flap_threshold" << "\t" << hc->GetFlappingThreshold() << "\n"
293 << "\t" << "high_flap_threshold" << "\t" << hc->GetFlappingThreshold() << "\n"
294 << "\t" << "process_perf_data" << "\t" << 1 << "\n"
295 << "\t" << "check_freshness" << "\t" << 1 << "\n";
298 fp << "\t" << "check_interval" << "\t" << 60 << "\n"
299 << "\t" << "retry_interval" << "\t" << 60 << "\n"
300 << "\t" << "max_check_attempts" << "\t" << 1 << "\n"
301 << "\t" << "active_checks_enabled" << "\t" << 0 << "\n"
302 << "\t" << "passive_checks_enabled" << "\t" << 0 << "\n"
303 << "\t" << "notifications_enabled" << "\t" << 0 << "\n";
307 fp << "\t" << "host_groups" << "\t";
310 Array::Ptr groups = host->GetGroups();
313 ObjectLock olock(groups);
315 BOOST_FOREACH(const String& name, groups) {
316 HostGroup::Ptr hg = HostGroup::GetByName(name);
331 DumpCustomAttributes(fp, host);
333 fp << "\t" << "}" << "\n"
337 void StatusDataWriter::DumpServiceStatusAttrs(std::ostream& fp, const Service::Ptr& service, CompatObjectType type)
339 Dictionary::Ptr attrs = CompatUtility::GetServiceStatusAttributes(service, type);
341 fp << "\t" << "check_command=" << attrs->Get("check_command") << "\n"
342 << "\t" << "event_handler=" << attrs->Get("event_handler") << "\n"
343 << "\t" << "check_period=" << attrs->Get("check_period") << "\n"
344 << "\t" << "check_interval=" << static_cast<double>(attrs->Get("check_interval")) << "\n"
345 << "\t" << "retry_interval=" << static_cast<double>(attrs->Get("retry_interval")) << "\n"
346 << "\t" << "has_been_checked=" << attrs->Get("has_been_checked") << "\n"
347 << "\t" << "should_be_scheduled=" << attrs->Get("should_be_scheduled") << "\n"
348 << "\t" << "check_execution_time=" << static_cast<double>(attrs->Get("check_execution_time")) << "\n"
349 << "\t" << "check_latency=" << static_cast<double>(attrs->Get("check_latency")) << "\n"
350 << "\t" << "current_state=" << attrs->Get("current_state") << "\n"
351 << "\t" << "state_type=" << attrs->Get("state_type") << "\n"
352 << "\t" << "plugin_output=" << attrs->Get("plugin_output") << "\n"
353 << "\t" << "long_plugin_output=" << attrs->Get("long_plugin_output") << "\n"
354 << "\t" << "performance_data=" << attrs->Get("performance_data") << "\n"
355 << "\t" << "check_source=" << attrs->Get("check_source") << "\n"
356 << "\t" << "last_check=" << static_cast<long>(attrs->Get("last_check")) << "\n"
357 << "\t" << "next_check=" << static_cast<long>(attrs->Get("next_check")) << "\n"
358 << "\t" << "current_attempt=" << attrs->Get("current_attempt") << "\n"
359 << "\t" << "max_attempts=" << attrs->Get("max_attempts") << "\n"
360 << "\t" << "last_state_change=" << static_cast<long>(attrs->Get("last_state_change")) << "\n"
361 << "\t" << "last_hard_state_change=" << static_cast<long>(attrs->Get("last_hard_state_change")) << "\n"
362 << "\t" << "last_time_ok=" << static_cast<long>(attrs->Get("last_time_ok")) << "\n"
363 << "\t" << "last_time_warn=" << static_cast<long>(attrs->Get("last_time_warn")) << "\n"
364 << "\t" << "last_time_critical=" << static_cast<long>(attrs->Get("last_time_critical")) << "\n"
365 << "\t" << "last_time_unknown=" << static_cast<long>(attrs->Get("last_time_unknown")) << "\n"
366 << "\t" << "last_update=" << static_cast<long>(attrs->Get("last_update")) << "\n"
367 << "\t" << "notifications_enabled=" << attrs->Get("notifications_enabled") << "\n"
368 << "\t" << "active_checks_enabled=" << attrs->Get("active_checks_enabled") << "\n"
369 << "\t" << "passive_checks_enabled=" << attrs->Get("passive_checks_enabled") << "\n"
370 << "\t" << "flap_detection_enabled=" << attrs->Get("flap_detection_enabled") << "\n"
371 << "\t" << "is_flapping=" << attrs->Get("is_flapping") << "\n"
372 << "\t" << "percent_state_change=" << attrs->Get("percent_state_change") << "\n"
373 << "\t" << "problem_has_been_acknowledged=" << attrs->Get("problem_has_been_acknowledged") << "\n"
374 << "\t" << "acknowledgement_type=" << attrs->Get("acknowledgement_type") << "\n"
375 << "\t" << "acknowledgement_end_time=" << attrs->Get("acknowledgement_end_time") << "\n"
376 << "\t" << "scheduled_downtime_depth=" << attrs->Get("scheduled_downtime_depth") << "\n"
377 << "\t" << "last_notification=" << static_cast<long>(attrs->Get("last_notification")) << "\n"
378 << "\t" << "next_notification=" << static_cast<long>(attrs->Get("next_notification")) << "\n"
379 << "\t" << "current_notification_number=" << attrs->Get("current_notification_number") << "\n"
380 << "\t" << "modified_attributes=" << attrs->Get("modified_attributes") << "\n";
383 void StatusDataWriter::DumpServiceStatus(std::ostream& fp, const Service::Ptr& service)
385 Host::Ptr host = service->GetHost();
390 fp << "servicestatus {" << "\n"
391 << "\t" << "host_name=" << host->GetName() << "\n"
392 << "\t" << "service_description=" << service->GetShortName() << "\n";
395 ObjectLock olock(service);
396 DumpServiceStatusAttrs(fp, service, CompatTypeService);
399 fp << "\t" << "}" << "\n"
402 DumpDowntimes(fp, service, CompatTypeService);
403 DumpComments(fp, service, CompatTypeService);
406 void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& service)
408 Host::Ptr host = service->GetHost();
413 String check_period_str;
414 TimePeriod::Ptr check_period = service->GetCheckPeriod();
416 check_period_str = check_period->GetName();
418 check_period_str = "24x7";
420 double notification_interval = -1;
421 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
422 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
423 notification_interval = notification->GetNotificationInterval();
426 if (notification_interval == -1)
427 notification_interval = 60;
430 ObjectLock olock(service);
432 fp << "define service {" << "\n"
433 << "\t" << "host_name" << "\t" << host->GetName() << "\n"
434 << "\t" << "service_description" << "\t" << service->GetShortName() << "\n"
435 << "\t" << "display_name" << "\t" << service->GetDisplayName() << "\n"
436 << "\t" << "check_period" << "\t" << check_period_str << "\n"
437 << "\t" << "check_interval" << "\t" << service->GetCheckInterval() / 60.0 << "\n"
438 << "\t" << "retry_interval" << "\t" << service->GetRetryInterval() / 60.0 << "\n"
439 << "\t" << "max_check_attempts" << "\t" << service->GetMaxCheckAttempts() << "\n"
440 << "\t" << "active_checks_enabled" << "\t" << (service->GetEnableActiveChecks() ? 1 : 0) << "\n"
441 << "\t" << "passive_checks_enabled" << "\t" << (service->GetEnablePassiveChecks() ? 1 : 0) << "\n"
442 << "\t" << "flap_detection_enabled" << "\t" << (service->GetEnableFlapping() ? 1 : 0) << "\n"
443 << "\t" << "is_volatile" << "\t" << (service->GetVolatile() ? 1 : 0) << "\n"
444 << "\t" << "notifications_enabled" << "\t" << (service->GetEnableNotifications() ? 1 : 0) << "\n"
445 << "\t" << "notification_options" << "\t" << "u,w,c,r" << "\n"
446 << "\t" << "notification_interval" << "\t" << notification_interval / 60.0 << "\n"
447 << "\t" << "event_handler_enabled" << "\t" << (service->GetEnableEventHandler() ? 1 : 0) << "\n";
449 CheckCommand::Ptr checkcommand = service->GetCheckCommand();
451 fp << "\t" << "check_command" << "\t" << "check_" << checkcommand->GetName() << "\n";
453 EventCommand::Ptr eventcommand = service->GetEventCommand();
455 fp << "\t" << "event_handler" << "\t" << "event_" << eventcommand->GetName() << "\n";
457 TimePeriod::Ptr check_period = service->GetCheckPeriod();
459 fp << "\t" << "check_period" << "\t" << check_period->GetName() << "\n";
461 fp << "\t" << "contacts" << "\t";
462 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(service));
465 fp << "\t" << "contact_groups" << "\t";
466 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(service));
469 fp << "\t" << "initial_state" << "\t" << "o" << "\n"
470 << "\t" << "low_flap_threshold" << "\t" << service->GetFlappingThreshold() << "\n"
471 << "\t" << "high_flap_threshold" << "\t" << service->GetFlappingThreshold() << "\n"
472 << "\t" << "process_perf_data" << "\t" << 1 << "\n"
473 << "\t" << "check_freshness" << "\t" << 1 << "\n";
476 fp << "\t" << "service_groups" << "\t";
479 Array::Ptr groups = service->GetGroups();
482 ObjectLock olock(groups);
484 BOOST_FOREACH(const String& name, groups) {
485 ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);
500 DumpCustomAttributes(fp, service);
502 fp << "\t" << "}" << "\n"
505 BOOST_FOREACH(const Service::Ptr& parent, service->GetParentServices()) {
506 Host::Ptr host = service->GetHost();
511 Host::Ptr parent_host = parent->GetHost();
516 fp << "define servicedependency {" << "\n"
517 << "\t" << "dependent_host_name" << "\t" << host->GetName() << "\n"
518 << "\t" << "dependent_service_description" << "\t" << service->GetShortName() << "\n"
519 << "\t" << "host_name" << "\t" << parent_host->GetName() << "\n"
520 << "\t" << "service_description" << "\t" << parent->GetShortName() << "\n"
521 << "\t" << "execution_failure_criteria" << "\t" << "n" << "\n"
522 << "\t" << "notification_failure_criteria" << "\t" << "w,u,c" << "\n"
523 << "\t" << "}" << "\n"
528 void StatusDataWriter::DumpCustomAttributes(std::ostream& fp, const DynamicObject::Ptr& object)
530 Dictionary::Ptr custom = object->GetCustom();
535 ObjectLock olock(custom);
538 BOOST_FOREACH(boost::tie(key, value), custom) {
541 if (key != "notes" && key != "action_url" && key != "notes_url" &&
542 key != "icon_image" && key != "icon_image_alt" && key != "statusmap_image" && "2d_coords")
545 fp << key << "\t" << value << "\n";
550 * Periodically writes the status.dat and objects.cache files.
552 void StatusDataWriter::StatusTimerHandler(void)
554 Log(LogInformation, "compat", "Writing compat status information");
556 String statuspath = GetStatusPath();
557 String objectspath = GetObjectsPath();
558 String statuspathtmp = statuspath + ".tmp"; /* XXX make this a global definition */
559 String objectspathtmp = objectspath + ".tmp";
561 std::ofstream statusfp;
562 statusfp.open(statuspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
564 statusfp << std::fixed;
566 statusfp << "# Icinga status file" << "\n"
567 << "# This file is auto-generated. Do not modify this file." << "\n"
570 statusfp << "info {" << "\n"
571 << "\t" << "created=" << Utility::GetTime() << "\n"
572 << "\t" << "version=" << Application::GetVersion() << "\n"
573 << "\t" << "}" << "\n"
576 statusfp << "programstatus {" << "\n"
577 << "\t" << "icinga_pid=" << Utility::GetPid() << "\n"
578 << "\t" << "daemon_mode=1" << "\n"
579 << "\t" << "program_start=" << static_cast<long>(Application::GetStartTime()) << "\n"
580 << "\t" << "active_service_checks_enabled=" << (IcingaApplication::GetInstance()->GetEnableChecks() ? 1 : 0) << "\n"
581 << "\t" << "passive_service_checks_enabled=1" << "\n"
582 << "\t" << "active_host_checks_enabled=1" << "\n"
583 << "\t" << "passive_host_checks_enabled=1" << "\n"
584 << "\t" << "check_service_freshness=1" << "\n"
585 << "\t" << "check_host_freshness=1" << "\n"
586 << "\t" << "enable_notifications=" << (IcingaApplication::GetInstance()->GetEnableNotifications() ? 1 : 0) << "\n"
587 << "\t" << "enable_flap_detection=" << (IcingaApplication::GetInstance()->GetEnableFlapping() ? 1 : 0) << "\n"
588 << "\t" << "enable_failure_prediction=0" << "\n"
589 << "\t" << "process_performance_data=" << (IcingaApplication::GetInstance()->GetEnablePerfdata() ? 1 : 0) << "\n"
590 << "\t" << "active_scheduled_service_check_stats=" << CIB::GetActiveChecksStatistics(60) << "," << CIB::GetActiveChecksStatistics(5 * 60) << "," << CIB::GetActiveChecksStatistics(15 * 60) << "\n"
591 << "\t" << "passive_service_check_stats=" << CIB::GetPassiveChecksStatistics(60) << "," << CIB::GetPassiveChecksStatistics(5 * 60) << "," << CIB::GetPassiveChecksStatistics(15 * 60) << "\n"
592 << "\t" << "next_downtime_id=" << Service::GetNextDowntimeID() << "\n"
593 << "\t" << "next_comment_id=" << Service::GetNextCommentID() << "\n"
594 << "\t" << "}" << "\n"
597 std::ofstream objectfp;
598 objectfp.open(objectspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
600 objectfp << std::fixed;
602 objectfp << "# Icinga objects cache file" << "\n"
603 << "# This file is auto-generated. Do not modify this file." << "\n"
606 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
607 std::ostringstream tempstatusfp;
608 tempstatusfp << std::fixed;
609 DumpHostStatus(tempstatusfp, host);
610 statusfp << tempstatusfp.str();
612 std::ostringstream tempobjectfp;
613 tempobjectfp << std::fixed;
614 DumpHostObject(tempobjectfp, host);
615 objectfp << tempobjectfp.str();
618 BOOST_FOREACH(const HostGroup::Ptr& hg, DynamicType::GetObjects<HostGroup>()) {
619 std::ostringstream tempobjectfp;
620 tempobjectfp << std::fixed;
622 tempobjectfp << "define hostgroup {" << "\n"
623 << "\t" << "hostgroup_name" << "\t" << hg->GetName() << "\n";
625 DumpCustomAttributes(tempobjectfp, hg);
627 tempobjectfp << "\t" << "members" << "\t";
628 DumpNameList(tempobjectfp, hg->GetMembers());
630 << "\t" << "}" << "\n";
632 objectfp << tempobjectfp.str();
635 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
636 std::ostringstream tempstatusfp;
637 tempstatusfp << std::fixed;
638 DumpServiceStatus(tempstatusfp, service);
639 statusfp << tempstatusfp.str();
641 std::ostringstream tempobjectfp;
642 tempobjectfp << std::fixed;
643 DumpServiceObject(tempobjectfp, service);
644 objectfp << tempobjectfp.str();
647 BOOST_FOREACH(const ServiceGroup::Ptr& sg, DynamicType::GetObjects<ServiceGroup>()) {
648 std::ostringstream tempobjectfp;
649 tempobjectfp << std::fixed;
651 tempobjectfp << "define servicegroup {" << "\n"
652 << "\t" << "servicegroup_name" << "\t" << sg->GetName() << "\n";
654 DumpCustomAttributes(tempobjectfp, sg);
656 tempobjectfp << "\t" << "members" << "\t";
658 std::vector<String> sglist;
659 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
660 Host::Ptr host = service->GetHost();
665 sglist.push_back(host->GetName());
666 sglist.push_back(service->GetShortName());
669 DumpStringList(tempobjectfp, sglist);
674 objectfp << tempobjectfp.str();
677 BOOST_FOREACH(const User::Ptr& user, DynamicType::GetObjects<User>()) {
678 std::ostringstream tempobjectfp;
679 tempobjectfp << std::fixed;
681 tempobjectfp << "define contact {" << "\n"
682 << "\t" << "contact_name" << "\t" << user->GetName() << "\n"
683 << "\t" << "alias" << "\t" << user->GetDisplayName() << "\n"
684 << "\t" << "service_notification_options" << "\t" << "w,u,c,r,f,s" << "\n"
685 << "\t" << "host_notification_options" << "\t" << "d,u,r,f,s" << "\n"
686 << "\t" << "host_notifications_enabled" << "\t" << 1 << "\n"
687 << "\t" << "service_notifications_enabled" << "\t" << 1 << "\n"
688 << "\t" << "}" << "\n"
691 objectfp << tempobjectfp.str();
694 BOOST_FOREACH(const UserGroup::Ptr& ug, DynamicType::GetObjects<UserGroup>()) {
695 std::ostringstream tempobjectfp;
696 tempobjectfp << std::fixed;
698 tempobjectfp << "define contactgroup {" << "\n"
699 << "\t" << "contactgroup_name" << "\t" << ug->GetName() << "\n"
700 << "\t" << "alias" << "\t" << ug->GetDisplayName() << "\n";
702 tempobjectfp << "\t" << "members" << "\t";
703 DumpNameList(tempobjectfp, ug->GetMembers());
705 << "\t" << "}" << "\n";
707 objectfp << tempobjectfp.str();
710 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<CheckCommand>()) {
711 DumpCommand(objectfp, command);
714 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<NotificationCommand>()) {
715 DumpCommand(objectfp, command);
718 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<EventCommand>()) {
719 DumpCommand(objectfp, command);
722 BOOST_FOREACH(const TimePeriod::Ptr& tp, DynamicType::GetObjects<TimePeriod>()) {
723 DumpTimePeriod(objectfp, tp);
730 _unlink(statuspath.CStr());
731 _unlink(objectspath.CStr());
735 if (rename(statuspathtmp.CStr(), statuspath.CStr()) < 0) {
736 BOOST_THROW_EXCEPTION(posix_error()
737 << boost::errinfo_api_function("rename")
738 << boost::errinfo_errno(errno)
739 << boost::errinfo_file_name(statuspathtmp));
743 if (rename(objectspathtmp.CStr(), objectspath.CStr()) < 0) {
744 BOOST_THROW_EXCEPTION(posix_error()
745 << boost::errinfo_api_function("rename")
746 << boost::errinfo_errno(errno)
747 << boost::errinfo_file_name(objectspathtmp));