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(GetUpdateInterval());
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();
72 ObjectLock olock(comments);
74 BOOST_FOREACH(const Dictionary::Pair& kv, comments) {
75 Comment::Ptr comment = kv.second;
77 if (comment->IsExpired())
80 if (type == CompatTypeHost)
81 fp << "hostcomment {" << "\n";
83 fp << "servicecomment {" << "\n"
84 << "\t" << "service_description=" << owner->GetShortName() << "\n";
86 fp << "\t" << "host_name=" << host->GetName() << "\n"
87 << "\t" << "comment_id=" << comment->GetLegacyId() << "\n"
88 << "\t" << "entry_time=" << comment->GetEntryTime() << "\n"
89 << "\t" << "entry_type=" << comment->GetEntryType() << "\n"
90 << "\t" << "persistent=" << 1 << "\n"
91 << "\t" << "author=" << comment->GetAuthor() << "\n"
92 << "\t" << "comment_data=" << comment->GetText() << "\n"
93 << "\t" << "expires=" << (comment->GetExpireTime() != 0 ? 1 : 0) << "\n"
94 << "\t" << "expire_time=" << comment->GetExpireTime() << "\n"
95 << "\t" << "}" << "\n"
100 void StatusDataWriter::DumpTimePeriod(std::ostream& fp, const TimePeriod::Ptr& tp)
102 fp << "define timeperiod {" << "\n"
103 << "\t" << "timeperiod_name" << "\t" << tp->GetName() << "\n"
104 << "\t" << "alias" << "\t" << tp->GetName() << "\n";
106 Dictionary::Ptr ranges = tp->GetRanges();
109 ObjectLock olock(ranges);
110 BOOST_FOREACH(const Dictionary::Pair& kv, ranges) {
111 fp << "\t" << kv.first << "\t" << kv.second << "\n";
115 fp << "\t" << "}" << "\n"
119 void StatusDataWriter::DumpCommand(std::ostream& fp, const Command::Ptr& command)
121 fp << "define command {" << "\n"
122 << "\t" << "command_name\t";
125 if (command->GetType() == DynamicType::GetByName("CheckCommand"))
127 else if (command->GetType() == DynamicType::GetByName("NotificationCommand"))
128 fp << "notification_";
129 else if (command->GetType() == DynamicType::GetByName("EventCommand"))
132 fp << command->GetName() << "\n";
134 fp << "\t" << "command_line\t";
136 Value commandLine = command->GetCommandLine();
138 if (commandLine.IsObjectType<Array>()) {
139 Array::Ptr args = commandLine;
141 ObjectLock olock(args);
143 BOOST_FOREACH(arg, args) {
144 // This is obviously incorrect for non-trivial cases.
145 fp << " \"" << CompatUtility::EscapeString(arg) << "\"";
147 } else if (!commandLine.IsEmpty()) {
148 fp << CompatUtility::EscapeString(commandLine);
155 fp << "\t" << "}" << "\n"
160 void StatusDataWriter::DumpDowntimes(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
162 Host::Ptr host = owner->GetHost();
164 Dictionary::Ptr downtimes = owner->GetDowntimes();
166 ObjectLock olock(downtimes);
168 BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
169 Downtime::Ptr downtime = kv.second;
171 if (downtime->IsExpired())
174 if (type == CompatTypeHost)
175 fp << "hostdowntime {" << "\n";
177 fp << "servicedowntime {" << "\n"
178 << "\t" << "service_description=" << owner->GetShortName() << "\n";
180 Downtime::Ptr triggeredByObj = Service::GetDowntimeByID(downtime->GetTriggeredBy());
181 int triggeredByLegacy = 0;
183 triggeredByLegacy = triggeredByObj->GetLegacyId();
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"
202 void StatusDataWriter::DumpHostStatus(std::ostream& fp, const Host::Ptr& host)
204 fp << "hoststatus {" << "\n"
205 << "\t" << "host_name=" << host->GetName() << "\n";
207 Service::Ptr hc = host->GetCheckService();
208 ObjectLock olock(hc);
211 DumpServiceStatusAttrs(fp, hc, CompatTypeHost);
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";
218 fp << "\t" << "}" << "\n"
222 DumpDowntimes(fp, hc, CompatTypeHost);
223 DumpComments(fp, hc, CompatTypeHost);
227 void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host)
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";
234 Dictionary::Ptr macros = host->GetMacros();
237 fp << "\t" << "address" << "\t" << macros->Get("address") << "\n"
238 << "\t" << "address6" << "\t" << macros->Get("address6") << "\n";
241 std::set<Host::Ptr> parents = host->GetParentHosts();
243 if (!parents.empty()) {
244 fp << "\t" << "parents" << "\t";
245 DumpNameList(fp, parents);
249 Service::Ptr hc = host->GetCheckService();
251 ObjectLock olock(hc);
253 fp << "\t" << "check_interval" << "\t" << hc->GetCheckInterval() / 60.0 << "\n"
254 << "\t" << "retry_interval" << "\t" << hc->GetRetryInterval() / 60.0 << "\n"
255 << "\t" << "max_check_attempts" << "\t" << hc->GetMaxCheckAttempts() << "\n"
256 << "\t" << "active_checks_enabled" << "\t" << (hc->GetEnableActiveChecks() ? 1 : 0) << "\n"
257 << "\t" << "passive_checks_enabled" << "\t" << (hc->GetEnablePassiveChecks() ? 1 : 0) << "\n"
258 << "\t" << "notifications_enabled" << "\t" << (hc->GetEnableNotifications() ? 1 : 0) << "\n"
259 << "\t" << "notification_options" << "\t" << "d,u,r" << "\n"
260 << "\t" << "notification_interval" << "\t" << 1 << "\n"
261 << "\t" << "event_handler_enabled" << "\t" << (hc->GetEnableEventHandler() ? 1 : 0) << "\n";
263 CheckCommand::Ptr checkcommand = hc->GetCheckCommand();
265 fp << "\t" << "check_command" << "\t" << "check_" << checkcommand->GetName() << "\n";
267 EventCommand::Ptr eventcommand = hc->GetEventCommand();
269 fp << "\t" << "event_handler" << "\t" << "event_" << eventcommand->GetName() << "\n";
271 TimePeriod::Ptr check_period = hc->GetCheckPeriod();
273 fp << "\t" << "check_period" << "\t" << check_period->GetName() << "\n";
275 fp << "\t" << "contacts" << "\t";
276 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(hc));
279 fp << "\t" << "contact_groups" << "\t";
280 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(hc));
283 fp << "\t" << "initial_state" << "\t" << "o" << "\n"
284 << "\t" << "low_flap_threshold" << "\t" << hc->GetFlappingThreshold() << "\n"
285 << "\t" << "high_flap_threshold" << "\t" << hc->GetFlappingThreshold() << "\n"
286 << "\t" << "process_perf_data" << "\t" << 1 << "\n"
287 << "\t" << "check_freshness" << "\t" << 1 << "\n";
290 fp << "\t" << "check_interval" << "\t" << 60 << "\n"
291 << "\t" << "retry_interval" << "\t" << 60 << "\n"
292 << "\t" << "max_check_attempts" << "\t" << 1 << "\n"
293 << "\t" << "active_checks_enabled" << "\t" << 0 << "\n"
294 << "\t" << "passive_checks_enabled" << "\t" << 0 << "\n"
295 << "\t" << "notifications_enabled" << "\t" << 0 << "\n";
299 fp << "\t" << "host_groups" << "\t";
302 Array::Ptr groups = host->GetGroups();
305 ObjectLock olock(groups);
307 BOOST_FOREACH(const String& name, groups) {
308 HostGroup::Ptr hg = HostGroup::GetByName(name);
323 DumpCustomAttributes(fp, host);
325 fp << "\t" << "}" << "\n"
329 void StatusDataWriter::DumpServiceStatusAttrs(std::ostream& fp, const Service::Ptr& service, CompatObjectType type)
331 Dictionary::Ptr attrs = CompatUtility::GetServiceStatusAttributes(service, type);
333 fp << "\t" << "check_command=" << attrs->Get("check_command") << "\n"
334 << "\t" << "event_handler=" << attrs->Get("event_handler") << "\n"
335 << "\t" << "check_period=" << attrs->Get("check_period") << "\n"
336 << "\t" << "check_interval=" << static_cast<double>(attrs->Get("check_interval")) << "\n"
337 << "\t" << "retry_interval=" << static_cast<double>(attrs->Get("retry_interval")) << "\n"
338 << "\t" << "has_been_checked=" << attrs->Get("has_been_checked") << "\n"
339 << "\t" << "should_be_scheduled=" << attrs->Get("should_be_scheduled") << "\n"
340 << "\t" << "check_execution_time=" << static_cast<double>(attrs->Get("check_execution_time")) << "\n"
341 << "\t" << "check_latency=" << static_cast<double>(attrs->Get("check_latency")) << "\n"
342 << "\t" << "current_state=" << attrs->Get("current_state") << "\n"
343 << "\t" << "state_type=" << attrs->Get("state_type") << "\n"
344 << "\t" << "plugin_output=" << attrs->Get("plugin_output") << "\n"
345 << "\t" << "long_plugin_output=" << attrs->Get("long_plugin_output") << "\n"
346 << "\t" << "performance_data=" << attrs->Get("performance_data") << "\n"
347 << "\t" << "check_source=" << attrs->Get("check_source") << "\n"
348 << "\t" << "last_check=" << static_cast<long>(attrs->Get("last_check")) << "\n"
349 << "\t" << "next_check=" << static_cast<long>(attrs->Get("next_check")) << "\n"
350 << "\t" << "current_attempt=" << attrs->Get("current_attempt") << "\n"
351 << "\t" << "max_attempts=" << attrs->Get("max_attempts") << "\n"
352 << "\t" << "last_state_change=" << static_cast<long>(attrs->Get("last_state_change")) << "\n"
353 << "\t" << "last_hard_state_change=" << static_cast<long>(attrs->Get("last_hard_state_change")) << "\n"
354 << "\t" << "last_time_ok=" << static_cast<long>(attrs->Get("last_time_ok")) << "\n"
355 << "\t" << "last_time_warn=" << static_cast<long>(attrs->Get("last_time_warn")) << "\n"
356 << "\t" << "last_time_critical=" << static_cast<long>(attrs->Get("last_time_critical")) << "\n"
357 << "\t" << "last_time_unknown=" << static_cast<long>(attrs->Get("last_time_unknown")) << "\n"
358 << "\t" << "last_update=" << static_cast<long>(attrs->Get("last_update")) << "\n"
359 << "\t" << "notifications_enabled=" << attrs->Get("notifications_enabled") << "\n"
360 << "\t" << "active_checks_enabled=" << attrs->Get("active_checks_enabled") << "\n"
361 << "\t" << "passive_checks_enabled=" << attrs->Get("passive_checks_enabled") << "\n"
362 << "\t" << "flap_detection_enabled=" << attrs->Get("flap_detection_enabled") << "\n"
363 << "\t" << "is_flapping=" << attrs->Get("is_flapping") << "\n"
364 << "\t" << "percent_state_change=" << attrs->Get("percent_state_change") << "\n"
365 << "\t" << "problem_has_been_acknowledged=" << attrs->Get("problem_has_been_acknowledged") << "\n"
366 << "\t" << "acknowledgement_type=" << attrs->Get("acknowledgement_type") << "\n"
367 << "\t" << "acknowledgement_end_time=" << attrs->Get("acknowledgement_end_time") << "\n"
368 << "\t" << "scheduled_downtime_depth=" << attrs->Get("scheduled_downtime_depth") << "\n"
369 << "\t" << "last_notification=" << static_cast<long>(attrs->Get("last_notification")) << "\n"
370 << "\t" << "next_notification=" << static_cast<long>(attrs->Get("next_notification")) << "\n"
371 << "\t" << "current_notification_number=" << attrs->Get("current_notification_number") << "\n"
372 << "\t" << "modified_attributes=" << attrs->Get("modified_attributes") << "\n";
375 void StatusDataWriter::DumpServiceStatus(std::ostream& fp, const Service::Ptr& service)
377 Host::Ptr host = service->GetHost();
379 fp << "servicestatus {" << "\n"
380 << "\t" << "host_name=" << host->GetName() << "\n"
381 << "\t" << "service_description=" << service->GetShortName() << "\n";
384 ObjectLock olock(service);
385 DumpServiceStatusAttrs(fp, service, CompatTypeService);
388 fp << "\t" << "}" << "\n"
391 DumpDowntimes(fp, service, CompatTypeService);
392 DumpComments(fp, service, CompatTypeService);
395 void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& service)
397 Host::Ptr host = service->GetHost();
399 String check_period_str;
400 TimePeriod::Ptr check_period = service->GetCheckPeriod();
402 check_period_str = check_period->GetName();
404 check_period_str = "24x7";
406 double notification_interval = -1;
407 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
408 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
409 notification_interval = notification->GetNotificationInterval();
412 if (notification_interval == -1)
413 notification_interval = 60;
416 ObjectLock olock(service);
418 fp << "define service {" << "\n"
419 << "\t" << "host_name" << "\t" << host->GetName() << "\n"
420 << "\t" << "service_description" << "\t" << service->GetShortName() << "\n"
421 << "\t" << "display_name" << "\t" << service->GetDisplayName() << "\n"
422 << "\t" << "check_period" << "\t" << check_period_str << "\n"
423 << "\t" << "check_interval" << "\t" << service->GetCheckInterval() / 60.0 << "\n"
424 << "\t" << "retry_interval" << "\t" << service->GetRetryInterval() / 60.0 << "\n"
425 << "\t" << "max_check_attempts" << "\t" << service->GetMaxCheckAttempts() << "\n"
426 << "\t" << "active_checks_enabled" << "\t" << (service->GetEnableActiveChecks() ? 1 : 0) << "\n"
427 << "\t" << "passive_checks_enabled" << "\t" << (service->GetEnablePassiveChecks() ? 1 : 0) << "\n"
428 << "\t" << "flap_detection_enabled" << "\t" << (service->GetEnableFlapping() ? 1 : 0) << "\n"
429 << "\t" << "is_volatile" << "\t" << (service->GetVolatile() ? 1 : 0) << "\n"
430 << "\t" << "notifications_enabled" << "\t" << (service->GetEnableNotifications() ? 1 : 0) << "\n"
431 << "\t" << "notification_options" << "\t" << "u,w,c,r" << "\n"
432 << "\t" << "notification_interval" << "\t" << notification_interval / 60.0 << "\n"
433 << "\t" << "event_handler_enabled" << "\t" << (service->GetEnableEventHandler() ? 1 : 0) << "\n";
435 CheckCommand::Ptr checkcommand = service->GetCheckCommand();
437 fp << "\t" << "check_command" << "\t" << "check_" << checkcommand->GetName() << "\n";
439 EventCommand::Ptr eventcommand = service->GetEventCommand();
441 fp << "\t" << "event_handler" << "\t" << "event_" << eventcommand->GetName() << "\n";
443 TimePeriod::Ptr check_period = service->GetCheckPeriod();
445 fp << "\t" << "check_period" << "\t" << check_period->GetName() << "\n";
447 fp << "\t" << "contacts" << "\t";
448 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(service));
451 fp << "\t" << "contact_groups" << "\t";
452 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(service));
455 fp << "\t" << "initial_state" << "\t" << "o" << "\n"
456 << "\t" << "low_flap_threshold" << "\t" << service->GetFlappingThreshold() << "\n"
457 << "\t" << "high_flap_threshold" << "\t" << service->GetFlappingThreshold() << "\n"
458 << "\t" << "process_perf_data" << "\t" << 1 << "\n"
459 << "\t" << "check_freshness" << "\t" << 1 << "\n";
462 fp << "\t" << "service_groups" << "\t";
465 Array::Ptr groups = service->GetGroups();
468 ObjectLock olock(groups);
470 BOOST_FOREACH(const String& name, groups) {
471 ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);
486 DumpCustomAttributes(fp, service);
488 fp << "\t" << "}" << "\n"
491 BOOST_FOREACH(const Service::Ptr& parent, service->GetParentServices()) {
492 Host::Ptr host = service->GetHost();
494 Host::Ptr parent_host = parent->GetHost();
499 fp << "define servicedependency {" << "\n"
500 << "\t" << "dependent_host_name" << "\t" << host->GetName() << "\n"
501 << "\t" << "dependent_service_description" << "\t" << service->GetShortName() << "\n"
502 << "\t" << "host_name" << "\t" << parent_host->GetName() << "\n"
503 << "\t" << "service_description" << "\t" << parent->GetShortName() << "\n"
504 << "\t" << "execution_failure_criteria" << "\t" << "n" << "\n"
505 << "\t" << "notification_failure_criteria" << "\t" << "w,u,c" << "\n"
506 << "\t" << "}" << "\n"
511 void StatusDataWriter::DumpCustomAttributes(std::ostream& fp, const DynamicObject::Ptr& object)
513 Dictionary::Ptr custom = object->GetCustom();
518 ObjectLock olock(custom);
519 BOOST_FOREACH(const Dictionary::Pair& kv, custom) {
522 if (kv.first != "notes" && kv.first != "action_url" && kv.first != "notes_url" &&
523 kv.first != "icon_image" && kv.first != "icon_image_alt" && kv.first != "statusmap_image" && kv.first != "2d_coords")
526 fp << kv.first << "\t" << kv.second << "\n";
531 * Periodically writes the status.dat and objects.cache files.
533 void StatusDataWriter::StatusTimerHandler(void)
535 Log(LogInformation, "compat", "Writing compat status information");
537 String statuspath = GetStatusPath();
538 String objectspath = GetObjectsPath();
539 String statuspathtmp = statuspath + ".tmp"; /* XXX make this a global definition */
540 String objectspathtmp = objectspath + ".tmp";
542 std::ofstream statusfp;
543 statusfp.open(statuspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
545 statusfp << std::fixed;
547 statusfp << "# Icinga status file" << "\n"
548 << "# This file is auto-generated. Do not modify this file." << "\n"
551 statusfp << "info {" << "\n"
552 << "\t" << "created=" << Utility::GetTime() << "\n"
553 << "\t" << "version=" << Application::GetVersion() << "\n"
554 << "\t" << "}" << "\n"
557 statusfp << "programstatus {" << "\n"
558 << "\t" << "icinga_pid=" << Utility::GetPid() << "\n"
559 << "\t" << "daemon_mode=1" << "\n"
560 << "\t" << "program_start=" << static_cast<long>(Application::GetStartTime()) << "\n"
561 << "\t" << "active_service_checks_enabled=" << (IcingaApplication::GetInstance()->GetEnableChecks() ? 1 : 0) << "\n"
562 << "\t" << "passive_service_checks_enabled=1" << "\n"
563 << "\t" << "active_host_checks_enabled=1" << "\n"
564 << "\t" << "passive_host_checks_enabled=1" << "\n"
565 << "\t" << "check_service_freshness=1" << "\n"
566 << "\t" << "check_host_freshness=1" << "\n"
567 << "\t" << "enable_notifications=" << (IcingaApplication::GetInstance()->GetEnableNotifications() ? 1 : 0) << "\n"
568 << "\t" << "enable_flap_detection=" << (IcingaApplication::GetInstance()->GetEnableFlapping() ? 1 : 0) << "\n"
569 << "\t" << "enable_failure_prediction=0" << "\n"
570 << "\t" << "process_performance_data=" << (IcingaApplication::GetInstance()->GetEnablePerfdata() ? 1 : 0) << "\n"
571 << "\t" << "active_scheduled_service_check_stats=" << CIB::GetActiveChecksStatistics(60) << "," << CIB::GetActiveChecksStatistics(5 * 60) << "," << CIB::GetActiveChecksStatistics(15 * 60) << "\n"
572 << "\t" << "passive_service_check_stats=" << CIB::GetPassiveChecksStatistics(60) << "," << CIB::GetPassiveChecksStatistics(5 * 60) << "," << CIB::GetPassiveChecksStatistics(15 * 60) << "\n"
573 << "\t" << "next_downtime_id=" << Service::GetNextDowntimeID() << "\n"
574 << "\t" << "next_comment_id=" << Service::GetNextCommentID() << "\n"
575 << "\t" << "}" << "\n"
578 std::ofstream objectfp;
579 objectfp.open(objectspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
581 objectfp << std::fixed;
583 objectfp << "# Icinga objects cache file" << "\n"
584 << "# This file is auto-generated. Do not modify this file." << "\n"
587 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
588 std::ostringstream tempstatusfp;
589 tempstatusfp << std::fixed;
590 DumpHostStatus(tempstatusfp, host);
591 statusfp << tempstatusfp.str();
593 std::ostringstream tempobjectfp;
594 tempobjectfp << std::fixed;
595 DumpHostObject(tempobjectfp, host);
596 objectfp << tempobjectfp.str();
599 BOOST_FOREACH(const HostGroup::Ptr& hg, DynamicType::GetObjects<HostGroup>()) {
600 std::ostringstream tempobjectfp;
601 tempobjectfp << std::fixed;
603 tempobjectfp << "define hostgroup {" << "\n"
604 << "\t" << "hostgroup_name" << "\t" << hg->GetName() << "\n";
606 DumpCustomAttributes(tempobjectfp, hg);
608 tempobjectfp << "\t" << "members" << "\t";
609 DumpNameList(tempobjectfp, hg->GetMembers());
611 << "\t" << "}" << "\n";
613 objectfp << tempobjectfp.str();
616 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
617 std::ostringstream tempstatusfp;
618 tempstatusfp << std::fixed;
619 DumpServiceStatus(tempstatusfp, service);
620 statusfp << tempstatusfp.str();
622 std::ostringstream tempobjectfp;
623 tempobjectfp << std::fixed;
624 DumpServiceObject(tempobjectfp, service);
625 objectfp << tempobjectfp.str();
628 BOOST_FOREACH(const ServiceGroup::Ptr& sg, DynamicType::GetObjects<ServiceGroup>()) {
629 std::ostringstream tempobjectfp;
630 tempobjectfp << std::fixed;
632 tempobjectfp << "define servicegroup {" << "\n"
633 << "\t" << "servicegroup_name" << "\t" << sg->GetName() << "\n";
635 DumpCustomAttributes(tempobjectfp, sg);
637 tempobjectfp << "\t" << "members" << "\t";
639 std::vector<String> sglist;
640 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
641 Host::Ptr host = service->GetHost();
643 sglist.push_back(host->GetName());
644 sglist.push_back(service->GetShortName());
647 DumpStringList(tempobjectfp, sglist);
652 objectfp << tempobjectfp.str();
655 BOOST_FOREACH(const User::Ptr& user, DynamicType::GetObjects<User>()) {
656 std::ostringstream tempobjectfp;
657 tempobjectfp << std::fixed;
659 tempobjectfp << "define contact {" << "\n"
660 << "\t" << "contact_name" << "\t" << user->GetName() << "\n"
661 << "\t" << "alias" << "\t" << user->GetDisplayName() << "\n"
662 << "\t" << "service_notification_options" << "\t" << "w,u,c,r,f,s" << "\n"
663 << "\t" << "host_notification_options" << "\t" << "d,u,r,f,s" << "\n"
664 << "\t" << "host_notifications_enabled" << "\t" << 1 << "\n"
665 << "\t" << "service_notifications_enabled" << "\t" << 1 << "\n"
666 << "\t" << "}" << "\n"
669 objectfp << tempobjectfp.str();
672 BOOST_FOREACH(const UserGroup::Ptr& ug, DynamicType::GetObjects<UserGroup>()) {
673 std::ostringstream tempobjectfp;
674 tempobjectfp << std::fixed;
676 tempobjectfp << "define contactgroup {" << "\n"
677 << "\t" << "contactgroup_name" << "\t" << ug->GetName() << "\n"
678 << "\t" << "alias" << "\t" << ug->GetDisplayName() << "\n";
680 tempobjectfp << "\t" << "members" << "\t";
681 DumpNameList(tempobjectfp, ug->GetMembers());
683 << "\t" << "}" << "\n";
685 objectfp << tempobjectfp.str();
688 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<CheckCommand>()) {
689 DumpCommand(objectfp, command);
692 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<NotificationCommand>()) {
693 DumpCommand(objectfp, command);
696 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<EventCommand>()) {
697 DumpCommand(objectfp, command);
700 BOOST_FOREACH(const TimePeriod::Ptr& tp, DynamicType::GetObjects<TimePeriod>()) {
701 DumpTimePeriod(objectfp, tp);
708 _unlink(statuspath.CStr());
709 _unlink(objectspath.CStr());
713 if (rename(statuspathtmp.CStr(), statuspath.CStr()) < 0) {
714 BOOST_THROW_EXCEPTION(posix_error()
715 << boost::errinfo_api_function("rename")
716 << boost::errinfo_errno(errno)
717 << boost::errinfo_file_name(statuspathtmp));
721 if (rename(objectspathtmp.CStr(), objectspath.CStr()) < 0) {
722 BOOST_THROW_EXCEPTION(posix_error()
723 << boost::errinfo_api_function("rename")
724 << boost::errinfo_errno(errno)
725 << boost::errinfo_file_name(objectspathtmp));