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/smart_ptr/make_shared.hpp>
37 #include <boost/foreach.hpp>
38 #include <boost/tuple/tuple.hpp>
39 #include <boost/exception/diagnostic_information.hpp>
40 #include <boost/algorithm/string/replace.hpp>
43 using namespace icinga;
45 REGISTER_TYPE(StatusDataWriter);
48 * Hint: The reason why we're using "\n" rather than std::endl is because
49 * std::endl also _flushes_ the output stream which severely degrades
50 * performance (see http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html).
54 * Starts the component.
56 void StatusDataWriter::Start(void)
58 DynamicObject::Start();
60 m_StatusTimer = boost::make_shared<Timer>();
61 m_StatusTimer->SetInterval(15);
62 m_StatusTimer->OnTimerExpired.connect(boost::bind(&StatusDataWriter::StatusTimerHandler, this));
63 m_StatusTimer->Start();
64 m_StatusTimer->Reschedule(0);
67 void StatusDataWriter::DumpComments(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
70 Dictionary::Ptr comments = owner->GetComments();
72 Host::Ptr host = owner->GetHost();
77 ObjectLock olock(comments);
80 Dictionary::Ptr comment;
81 BOOST_FOREACH(boost::tie(id, comment), comments) {
82 if (Service::IsCommentExpired(comment))
85 if (type == CompatTypeHost)
86 fp << "hostcomment {" << "\n";
88 fp << "servicecomment {" << "\n"
89 << "\t" << "service_description=" << owner->GetShortName() << "\n";
91 fp << "\t" << "host_name=" << host->GetName() << "\n"
92 << "\t" << "comment_id=" << static_cast<String>(comment->Get("legacy_id")) << "\n"
93 << "\t" << "entry_time=" << static_cast<double>(comment->Get("entry_time")) << "\n"
94 << "\t" << "entry_type=" << static_cast<long>(comment->Get("entry_type")) << "\n"
95 << "\t" << "persistent=" << 1 << "\n"
96 << "\t" << "author=" << static_cast<String>(comment->Get("author")) << "\n"
97 << "\t" << "comment_data=" << static_cast<String>(comment->Get("text")) << "\n"
98 << "\t" << "expires=" << (static_cast<double>(comment->Get("expire_time")) != 0 ? 1 : 0) << "\n"
99 << "\t" << "expire_time=" << static_cast<double>(comment->Get("expire_time")) << "\n"
100 << "\t" << "}" << "\n"
105 void StatusDataWriter::DumpTimePeriod(std::ostream& fp, const TimePeriod::Ptr& tp)
107 fp << "define timeperiod {" << "\n"
108 << "\t" << "timeperiod_name" << "\t" << tp->GetName() << "\n"
109 << "\t" << "alias" << "\t" << tp->GetName() << "\n";
111 Dictionary::Ptr ranges = tp->GetRanges();
114 ObjectLock olock(ranges);
117 BOOST_FOREACH(boost::tie(key, value), ranges) {
118 fp << "\t" << key << "\t" << Convert::ToString(value) << "\n";
122 fp << "\t" << "}" << "\n"
126 void StatusDataWriter::DumpCommand(std::ostream& fp, const Command::Ptr& command)
128 fp << "define command {" << "\n"
129 << "\t" << "command_name\t";
132 if (command->GetType() == DynamicType::GetByName("CheckCommand"))
134 else if (command->GetType() == DynamicType::GetByName("NotificationCommand"))
135 fp << "notification_";
136 else if (command->GetType() == DynamicType::GetByName("EventCommand"))
139 fp << command->GetName() << "\n";
141 fp << "\t" << "command_line\t";
143 Value commandLine = command->GetCommandLine();
145 if (commandLine.IsObjectType<Array>()) {
146 Array::Ptr args = commandLine;
148 ObjectLock olock(args);
150 BOOST_FOREACH(arg, args) {
151 // This is obviously incorrect for non-trivial cases.
152 fp << " \"" << CompatUtility::EscapeString(arg) << "\"";
154 } else if (!commandLine.IsEmpty()) {
155 fp << CompatUtility::EscapeString(Convert::ToString(commandLine));
162 fp << "\t" << "}" << "\n"
167 void StatusDataWriter::DumpDowntimes(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
169 Host::Ptr host = owner->GetHost();
174 Dictionary::Ptr downtimes = owner->GetDowntimes();
176 ObjectLock olock(downtimes);
179 Dictionary::Ptr downtime;
180 BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
181 if (Service::IsDowntimeExpired(downtime))
184 if (type == CompatTypeHost)
185 fp << "hostdowntime {" << "\n";
187 fp << "servicedowntime {" << "\n"
188 << "\t" << "service_description=" << owner->GetShortName() << "\n";
190 Dictionary::Ptr triggeredByObj = Service::GetDowntimeByID(downtime->Get("triggered_by"));
191 int triggeredByLegacy = 0;
193 triggeredByLegacy = triggeredByObj->Get("legacy_id");
195 fp << "\t" << "host_name=" << host->GetName() << "\n"
196 << "\t" << "downtime_id=" << static_cast<String>(downtime->Get("legacy_id")) << "\n"
197 << "\t" << "entry_time=" << static_cast<double>(downtime->Get("entry_time")) << "\n"
198 << "\t" << "start_time=" << static_cast<double>(downtime->Get("start_time")) << "\n"
199 << "\t" << "end_time=" << static_cast<double>(downtime->Get("end_time")) << "\n"
200 << "\t" << "triggered_by=" << triggeredByLegacy << "\n"
201 << "\t" << "fixed=" << static_cast<long>(downtime->Get("fixed")) << "\n"
202 << "\t" << "duration=" << static_cast<long>(downtime->Get("duration")) << "\n"
203 << "\t" << "is_in_effect=" << (Service::IsDowntimeActive(downtime) ? 1 : 0) << "\n"
204 << "\t" << "author=" << static_cast<String>(downtime->Get("author")) << "\n"
205 << "\t" << "comment=" << static_cast<String>(downtime->Get("comment")) << "\n"
206 << "\t" << "trigger_time=" << static_cast<double>(downtime->Get("trigger_time")) << "\n"
207 << "\t" << "}" << "\n"
212 void StatusDataWriter::DumpHostStatus(std::ostream& fp, const Host::Ptr& host)
214 fp << "hoststatus {" << "\n"
215 << "\t" << "host_name=" << host->GetName() << "\n";
217 Service::Ptr hc = host->GetCheckService();
218 ObjectLock olock(hc);
221 DumpServiceStatusAttrs(fp, hc, CompatTypeHost);
223 /* ugly but cgis parse only that */
224 fp << "\t" << "last_time_up=" << host->GetLastStateUp() << "\n"
225 << "\t" << "last_time_down=" << host->GetLastStateDown() << "\n"
226 << "\t" << "last_time_unreachable=" << host->GetLastStateUnreachable() << "\n";
228 fp << "\t" << "}" << "\n"
232 DumpDowntimes(fp, hc, CompatTypeHost);
233 DumpComments(fp, hc, CompatTypeHost);
237 void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host)
239 fp << "define host {" << "\n"
240 << "\t" << "host_name" << "\t" << host->GetName() << "\n"
241 << "\t" << "display_name" << "\t" << host->GetDisplayName() << "\n"
242 << "\t" << "alias" << "\t" << host->GetDisplayName() << "\n";
244 Dictionary::Ptr macros = host->GetMacros();
247 fp << "\t" << "address" << "\t" << macros->Get("address") << "\n"
248 << "\t" << "address6" << "\t" << macros->Get("address6") << "\n";
251 std::set<Host::Ptr> parents = host->GetParentHosts();
253 if (!parents.empty()) {
254 fp << "\t" << "parents" << "\t";
255 DumpNameList(fp, parents);
259 Service::Ptr hc = host->GetCheckService();
261 ObjectLock olock(hc);
263 fp << "\t" << "check_interval" << "\t" << hc->GetCheckInterval() / 60.0 << "\n"
264 << "\t" << "retry_interval" << "\t" << hc->GetRetryInterval() / 60.0 << "\n"
265 << "\t" << "max_check_attempts" << "\t" << hc->GetMaxCheckAttempts() << "\n"
266 << "\t" << "active_checks_enabled" << "\t" << (hc->GetEnableActiveChecks() ? 1 : 0) << "\n"
267 << "\t" << "passive_checks_enabled" << "\t" << (hc->GetEnablePassiveChecks() ? 1 : 0) << "\n"
268 << "\t" << "notifications_enabled" << "\t" << (hc->GetEnableNotifications() ? 1 : 0) << "\n"
269 << "\t" << "notification_options" << "\t" << "d,u,r" << "\n"
270 << "\t" << "notification_interval" << "\t" << 1 << "\n"
271 << "\t" << "event_handler_enabled" << "\t" << (hc->GetEnableEventHandler() ? 1 : 0) << "\n";
273 CheckCommand::Ptr checkcommand = hc->GetCheckCommand();
275 fp << "\t" << "check_command" << "\t" << "check_" << checkcommand->GetName() << "\n";
277 EventCommand::Ptr eventcommand = hc->GetEventCommand();
279 fp << "\t" << "event_handler" << "\t" << "event_" << eventcommand->GetName() << "\n";
281 TimePeriod::Ptr check_period = hc->GetCheckPeriod();
283 fp << "\t" << "check_period" << "\t" << check_period->GetName() << "\n";
285 fp << "\t" << "contacts" << "\t";
286 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(hc));
289 fp << "\t" << "contact_groups" << "\t";
290 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(hc));
293 fp << "\t" << "initial_state" << "\t" << "o" << "\n"
294 << "\t" << "low_flap_threshold" << "\t" << hc->GetFlappingThreshold() << "\n"
295 << "\t" << "high_flap_threshold" << "\t" << hc->GetFlappingThreshold() << "\n"
296 << "\t" << "process_perf_data" << "\t" << 1 << "\n"
297 << "\t" << "check_freshness" << "\t" << 1 << "\n";
300 fp << "\t" << "check_interval" << "\t" << 60 << "\n"
301 << "\t" << "retry_interval" << "\t" << 60 << "\n"
302 << "\t" << "max_check_attempts" << "\t" << 1 << "\n"
303 << "\t" << "active_checks_enabled" << "\t" << 0 << "\n"
304 << "\t" << "passive_checks_enabled" << "\t" << 0 << "\n"
305 << "\t" << "notifications_enabled" << "\t" << 0 << "\n";
309 fp << "\t" << "host_groups" << "\t";
312 Array::Ptr groups = host->GetGroups();
315 ObjectLock olock(groups);
317 BOOST_FOREACH(const String& name, groups) {
318 HostGroup::Ptr hg = HostGroup::GetByName(name);
333 DumpCustomAttributes(fp, host);
335 fp << "\t" << "}" << "\n"
339 void StatusDataWriter::DumpServiceStatusAttrs(std::ostream& fp, const Service::Ptr& service, CompatObjectType type)
341 Dictionary::Ptr attrs = CompatUtility::GetServiceStatusAttributes(service, type);
343 fp << "\t" << "check_command=" << attrs->Get("check_command") << "\n"
344 << "\t" << "event_handler=" << attrs->Get("event_handler") << "\n"
345 << "\t" << "check_period=" << attrs->Get("check_period") << "\n"
346 << "\t" << "check_interval=" << static_cast<double>(attrs->Get("check_interval")) << "\n"
347 << "\t" << "retry_interval=" << static_cast<double>(attrs->Get("retry_interval")) << "\n"
348 << "\t" << "has_been_checked=" << attrs->Get("has_been_checked") << "\n"
349 << "\t" << "should_be_scheduled=" << attrs->Get("should_be_scheduled") << "\n"
350 << "\t" << "check_execution_time=" << static_cast<double>(attrs->Get("check_execution_time")) << "\n"
351 << "\t" << "check_latency=" << static_cast<double>(attrs->Get("check_latency")) << "\n"
352 << "\t" << "current_state=" << attrs->Get("current_state") << "\n"
353 << "\t" << "state_type=" << attrs->Get("state_type") << "\n"
354 << "\t" << "plugin_output=" << attrs->Get("plugin_output") << "\n"
355 << "\t" << "long_plugin_output=" << attrs->Get("long_plugin_output") << "\n"
356 << "\t" << "performance_data=" << attrs->Get("performance_data") << "\n"
357 << "\t" << "check_source=" << attrs->Get("check_source") << "\n"
358 << "\t" << "last_check=" << static_cast<long>(attrs->Get("last_check")) << "\n"
359 << "\t" << "next_check=" << static_cast<long>(attrs->Get("next_check")) << "\n"
360 << "\t" << "current_attempt=" << attrs->Get("current_attempt") << "\n"
361 << "\t" << "max_attempts=" << attrs->Get("max_attempts") << "\n"
362 << "\t" << "last_state_change=" << static_cast<long>(attrs->Get("last_state_change")) << "\n"
363 << "\t" << "last_hard_state_change=" << static_cast<long>(attrs->Get("last_hard_state_change")) << "\n"
364 << "\t" << "last_time_ok=" << static_cast<long>(attrs->Get("last_time_ok")) << "\n"
365 << "\t" << "last_time_warn=" << static_cast<long>(attrs->Get("last_time_warn")) << "\n"
366 << "\t" << "last_time_critical=" << static_cast<long>(attrs->Get("last_time_critical")) << "\n"
367 << "\t" << "last_time_unknown=" << static_cast<long>(attrs->Get("last_time_unknown")) << "\n"
368 << "\t" << "last_update=" << static_cast<long>(attrs->Get("last_update")) << "\n"
369 << "\t" << "notifications_enabled=" << attrs->Get("notifications_enabled") << "\n"
370 << "\t" << "active_checks_enabled=" << attrs->Get("active_checks_enabled") << "\n"
371 << "\t" << "passive_checks_enabled=" << attrs->Get("passive_checks_enabled") << "\n"
372 << "\t" << "flap_detection_enabled=" << attrs->Get("flap_detection_enabled") << "\n"
373 << "\t" << "is_flapping=" << attrs->Get("is_flapping") << "\n"
374 << "\t" << "percent_state_change=" << attrs->Get("percent_state_change") << "\n"
375 << "\t" << "problem_has_been_acknowledged=" << attrs->Get("problem_has_been_acknowledged") << "\n"
376 << "\t" << "acknowledgement_type=" << attrs->Get("acknowledgement_type") << "\n"
377 << "\t" << "acknowledgement_end_time=" << attrs->Get("acknowledgement_end_time") << "\n"
378 << "\t" << "scheduled_downtime_depth=" << attrs->Get("scheduled_downtime_depth") << "\n"
379 << "\t" << "last_notification=" << static_cast<long>(attrs->Get("last_notification")) << "\n"
380 << "\t" << "next_notification=" << static_cast<long>(attrs->Get("next_notification")) << "\n"
381 << "\t" << "current_notification_number=" << attrs->Get("current_notification_number") << "\n"
382 << "\t" << "modified_attributes=" << attrs->Get("modified_attributes") << "\n";
385 void StatusDataWriter::DumpServiceStatus(std::ostream& fp, const Service::Ptr& service)
387 Host::Ptr host = service->GetHost();
392 fp << "servicestatus {" << "\n"
393 << "\t" << "host_name=" << host->GetName() << "\n"
394 << "\t" << "service_description=" << service->GetShortName() << "\n";
397 ObjectLock olock(service);
398 DumpServiceStatusAttrs(fp, service, CompatTypeService);
401 fp << "\t" << "}" << "\n"
404 DumpDowntimes(fp, service, CompatTypeService);
405 DumpComments(fp, service, CompatTypeService);
408 void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& service)
410 Host::Ptr host = service->GetHost();
415 String check_period_str;
416 TimePeriod::Ptr check_period = service->GetCheckPeriod();
418 check_period_str = check_period->GetName();
420 check_period_str = "24x7";
422 double notification_interval = -1;
423 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
424 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
425 notification_interval = notification->GetNotificationInterval();
428 if (notification_interval == -1)
429 notification_interval = 60;
432 ObjectLock olock(service);
434 fp << "define service {" << "\n"
435 << "\t" << "host_name" << "\t" << host->GetName() << "\n"
436 << "\t" << "service_description" << "\t" << service->GetShortName() << "\n"
437 << "\t" << "display_name" << "\t" << service->GetDisplayName() << "\n"
438 << "\t" << "check_period" << "\t" << check_period_str << "\n"
439 << "\t" << "check_interval" << "\t" << service->GetCheckInterval() / 60.0 << "\n"
440 << "\t" << "retry_interval" << "\t" << service->GetRetryInterval() / 60.0 << "\n"
441 << "\t" << "max_check_attempts" << "\t" << service->GetMaxCheckAttempts() << "\n"
442 << "\t" << "active_checks_enabled" << "\t" << (service->GetEnableActiveChecks() ? 1 : 0) << "\n"
443 << "\t" << "passive_checks_enabled" << "\t" << (service->GetEnablePassiveChecks() ? 1 : 0) << "\n"
444 << "\t" << "flap_detection_enabled" << "\t" << (service->GetEnableFlapping() ? 1 : 0) << "\n"
445 << "\t" << "is_volatile" << "\t" << (service->GetVolatile() ? 1 : 0) << "\n"
446 << "\t" << "notifications_enabled" << "\t" << (service->GetEnableNotifications() ? 1 : 0) << "\n"
447 << "\t" << "notification_options" << "\t" << "u,w,c,r" << "\n"
448 << "\t" << "notification_interval" << "\t" << notification_interval / 60.0 << "\n"
449 << "\t" << "event_handler_enabled" << "\t" << (service->GetEnableEventHandler() ? 1 : 0) << "\n";
451 CheckCommand::Ptr checkcommand = service->GetCheckCommand();
453 fp << "\t" << "check_command" << "\t" << "check_" << checkcommand->GetName() << "\n";
455 EventCommand::Ptr eventcommand = service->GetEventCommand();
457 fp << "\t" << "event_handler" << "\t" << "event_" << eventcommand->GetName() << "\n";
459 TimePeriod::Ptr check_period = service->GetCheckPeriod();
461 fp << "\t" << "check_period" << "\t" << check_period->GetName() << "\n";
463 fp << "\t" << "contacts" << "\t";
464 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(service));
467 fp << "\t" << "contact_groups" << "\t";
468 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(service));
471 fp << "\t" << "initial_state" << "\t" << "o" << "\n"
472 << "\t" << "low_flap_threshold" << "\t" << service->GetFlappingThreshold() << "\n"
473 << "\t" << "high_flap_threshold" << "\t" << service->GetFlappingThreshold() << "\n"
474 << "\t" << "process_perf_data" << "\t" << 1 << "\n"
475 << "\t" << "check_freshness" << "\t" << 1 << "\n";
478 fp << "\t" << "service_groups" << "\t";
481 Array::Ptr groups = service->GetGroups();
484 ObjectLock olock(groups);
486 BOOST_FOREACH(const String& name, groups) {
487 ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);
502 DumpCustomAttributes(fp, service);
504 fp << "\t" << "}" << "\n"
507 BOOST_FOREACH(const Service::Ptr& parent, service->GetParentServices()) {
508 Host::Ptr host = service->GetHost();
513 Host::Ptr parent_host = parent->GetHost();
518 fp << "define servicedependency {" << "\n"
519 << "\t" << "dependent_host_name" << "\t" << host->GetName() << "\n"
520 << "\t" << "dependent_service_description" << "\t" << service->GetShortName() << "\n"
521 << "\t" << "host_name" << "\t" << parent_host->GetName() << "\n"
522 << "\t" << "service_description" << "\t" << parent->GetShortName() << "\n"
523 << "\t" << "execution_failure_criteria" << "\t" << "n" << "\n"
524 << "\t" << "notification_failure_criteria" << "\t" << "w,u,c" << "\n"
525 << "\t" << "}" << "\n"
530 void StatusDataWriter::DumpCustomAttributes(std::ostream& fp, const DynamicObject::Ptr& object)
532 Dictionary::Ptr custom = object->GetCustom();
537 ObjectLock olock(custom);
540 BOOST_FOREACH(boost::tie(key, value), custom) {
543 if (key != "notes" && key != "action_url" && key != "notes_url" &&
544 key != "icon_image" && key != "icon_image_alt" && key != "statusmap_image" && "2d_coords")
547 fp << key << "\t" << Convert::ToString(value) << "\n";
552 * Periodically writes the status.dat and objects.cache files.
554 void StatusDataWriter::StatusTimerHandler(void)
556 Log(LogInformation, "compat", "Writing compat status information");
558 String statuspath = GetStatusPath();
559 String objectspath = GetObjectsPath();
560 String statuspathtmp = statuspath + ".tmp"; /* XXX make this a global definition */
561 String objectspathtmp = objectspath + ".tmp";
563 std::ofstream statusfp;
564 statusfp.open(statuspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
566 statusfp << std::fixed;
568 statusfp << "# Icinga status file" << "\n"
569 << "# This file is auto-generated. Do not modify this file." << "\n"
572 statusfp << "info {" << "\n"
573 << "\t" << "created=" << Utility::GetTime() << "\n"
574 << "\t" << "version=" << Application::GetVersion() << "\n"
575 << "\t" << "}" << "\n"
578 statusfp << "programstatus {" << "\n"
579 << "\t" << "icinga_pid=" << Utility::GetPid() << "\n"
580 << "\t" << "daemon_mode=1" << "\n"
581 << "\t" << "program_start=" << static_cast<long>(Application::GetStartTime()) << "\n"
582 << "\t" << "active_service_checks_enabled=" << (IcingaApplication::GetInstance()->GetEnableChecks() ? 1 : 0) << "\n"
583 << "\t" << "passive_service_checks_enabled=1" << "\n"
584 << "\t" << "active_host_checks_enabled=1" << "\n"
585 << "\t" << "passive_host_checks_enabled=1" << "\n"
586 << "\t" << "check_service_freshness=1" << "\n"
587 << "\t" << "check_host_freshness=1" << "\n"
588 << "\t" << "enable_notifications=" << (IcingaApplication::GetInstance()->GetEnableNotifications() ? 1 : 0) << "\n"
589 << "\t" << "enable_flap_detection=" << (IcingaApplication::GetInstance()->GetEnableFlapping() ? 1 : 0) << "\n"
590 << "\t" << "enable_failure_prediction=0" << "\n"
591 << "\t" << "process_performance_data=" << (IcingaApplication::GetInstance()->GetEnablePerfdata() ? 1 : 0) << "\n"
592 << "\t" << "active_scheduled_service_check_stats=" << CIB::GetActiveChecksStatistics(60) << "," << CIB::GetActiveChecksStatistics(5 * 60) << "," << CIB::GetActiveChecksStatistics(15 * 60) << "\n"
593 << "\t" << "passive_service_check_stats=" << CIB::GetPassiveChecksStatistics(60) << "," << CIB::GetPassiveChecksStatistics(5 * 60) << "," << CIB::GetPassiveChecksStatistics(15 * 60) << "\n"
594 << "\t" << "next_downtime_id=" << Service::GetNextDowntimeID() << "\n"
595 << "\t" << "next_comment_id=" << Service::GetNextCommentID() << "\n"
596 << "\t" << "}" << "\n"
599 std::ofstream objectfp;
600 objectfp.open(objectspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
602 objectfp << std::fixed;
604 objectfp << "# Icinga objects cache file" << "\n"
605 << "# This file is auto-generated. Do not modify this file." << "\n"
608 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
609 std::ostringstream tempstatusfp;
610 tempstatusfp << std::fixed;
611 DumpHostStatus(tempstatusfp, host);
612 statusfp << tempstatusfp.str();
614 std::ostringstream tempobjectfp;
615 tempobjectfp << std::fixed;
616 DumpHostObject(tempobjectfp, host);
617 objectfp << tempobjectfp.str();
620 BOOST_FOREACH(const HostGroup::Ptr& hg, DynamicType::GetObjects<HostGroup>()) {
621 std::ostringstream tempobjectfp;
622 tempobjectfp << std::fixed;
624 tempobjectfp << "define hostgroup {" << "\n"
625 << "\t" << "hostgroup_name" << "\t" << hg->GetName() << "\n";
627 DumpCustomAttributes(tempobjectfp, hg);
629 tempobjectfp << "\t" << "members" << "\t";
630 DumpNameList(tempobjectfp, hg->GetMembers());
632 << "\t" << "}" << "\n";
634 objectfp << tempobjectfp.str();
637 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
638 std::ostringstream tempstatusfp;
639 tempstatusfp << std::fixed;
640 DumpServiceStatus(tempstatusfp, service);
641 statusfp << tempstatusfp.str();
643 std::ostringstream tempobjectfp;
644 tempobjectfp << std::fixed;
645 DumpServiceObject(tempobjectfp, service);
646 objectfp << tempobjectfp.str();
649 BOOST_FOREACH(const ServiceGroup::Ptr& sg, DynamicType::GetObjects<ServiceGroup>()) {
650 std::ostringstream tempobjectfp;
651 tempobjectfp << std::fixed;
653 tempobjectfp << "define servicegroup {" << "\n"
654 << "\t" << "servicegroup_name" << "\t" << sg->GetName() << "\n";
656 DumpCustomAttributes(tempobjectfp, sg);
658 tempobjectfp << "\t" << "members" << "\t";
660 std::vector<String> sglist;
661 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
662 Host::Ptr host = service->GetHost();
667 sglist.push_back(host->GetName());
668 sglist.push_back(service->GetShortName());
671 DumpStringList(tempobjectfp, sglist);
676 objectfp << tempobjectfp.str();
679 BOOST_FOREACH(const User::Ptr& user, DynamicType::GetObjects<User>()) {
680 std::ostringstream tempobjectfp;
681 tempobjectfp << std::fixed;
683 tempobjectfp << "define contact {" << "\n"
684 << "\t" << "contact_name" << "\t" << user->GetName() << "\n"
685 << "\t" << "alias" << "\t" << user->GetDisplayName() << "\n"
686 << "\t" << "service_notification_options" << "\t" << "w,u,c,r,f,s" << "\n"
687 << "\t" << "host_notification_options" << "\t" << "d,u,r,f,s" << "\n"
688 << "\t" << "host_notifications_enabled" << "\t" << 1 << "\n"
689 << "\t" << "service_notifications_enabled" << "\t" << 1 << "\n"
690 << "\t" << "}" << "\n"
693 objectfp << tempobjectfp.str();
696 BOOST_FOREACH(const UserGroup::Ptr& ug, DynamicType::GetObjects<UserGroup>()) {
697 std::ostringstream tempobjectfp;
698 tempobjectfp << std::fixed;
700 tempobjectfp << "define contactgroup {" << "\n"
701 << "\t" << "contactgroup_name" << "\t" << ug->GetName() << "\n"
702 << "\t" << "alias" << "\t" << ug->GetDisplayName() << "\n";
704 tempobjectfp << "\t" << "members" << "\t";
705 DumpNameList(tempobjectfp, ug->GetMembers());
707 << "\t" << "}" << "\n";
709 objectfp << tempobjectfp.str();
712 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<CheckCommand>()) {
713 DumpCommand(objectfp, command);
716 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<NotificationCommand>()) {
717 DumpCommand(objectfp, command);
720 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<EventCommand>()) {
721 DumpCommand(objectfp, command);
724 BOOST_FOREACH(const TimePeriod::Ptr& tp, DynamicType::GetObjects<TimePeriod>()) {
725 DumpTimePeriod(objectfp, tp);
732 _unlink(statuspath.CStr());
733 _unlink(objectspath.CStr());
737 if (rename(statuspathtmp.CStr(), statuspath.CStr()) < 0) {
738 BOOST_THROW_EXCEPTION(posix_error()
739 << boost::errinfo_api_function("rename")
740 << boost::errinfo_errno(errno)
741 << boost::errinfo_file_name(statuspathtmp));
745 if (rename(objectspathtmp.CStr(), objectspath.CStr()) < 0) {
746 BOOST_THROW_EXCEPTION(posix_error()
747 << boost::errinfo_api_function("rename")
748 << boost::errinfo_errno(errno)
749 << boost::errinfo_file_name(objectspathtmp));