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);
68 * Retrieves the status.dat path.
70 * @returns statuspath from config, or static default
72 String StatusDataWriter::GetStatusPath(void) const
74 if (m_StatusPath.IsEmpty())
75 return Application::GetLocalStateDir() + "/cache/icinga2/status.dat";
81 * Retrieves the objects.cache path.
83 * @returns objectspath from config, or static default
85 String StatusDataWriter::GetObjectsPath(void) const
87 if (m_ObjectsPath.IsEmpty())
88 return Application::GetLocalStateDir() + "/cache/icinga2/objects.cache";
93 void StatusDataWriter::DumpComments(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
96 Dictionary::Ptr comments = owner->GetComments();
101 Host::Ptr host = owner->GetHost();
106 ObjectLock olock(comments);
109 Dictionary::Ptr comment;
110 BOOST_FOREACH(boost::tie(id, comment), comments) {
111 if (Service::IsCommentExpired(comment))
114 if (type == CompatTypeHost)
115 fp << "hostcomment {" << "\n";
117 fp << "servicecomment {" << "\n"
118 << "\t" << "service_description=" << owner->GetShortName() << "\n";
120 fp << "\t" << "host_name=" << host->GetName() << "\n"
121 << "\t" << "comment_id=" << static_cast<String>(comment->Get("legacy_id")) << "\n"
122 << "\t" << "entry_time=" << static_cast<double>(comment->Get("entry_time")) << "\n"
123 << "\t" << "entry_type=" << static_cast<long>(comment->Get("entry_type")) << "\n"
124 << "\t" << "persistent=" << 1 << "\n"
125 << "\t" << "author=" << static_cast<String>(comment->Get("author")) << "\n"
126 << "\t" << "comment_data=" << static_cast<String>(comment->Get("text")) << "\n"
127 << "\t" << "expires=" << (static_cast<double>(comment->Get("expire_time")) != 0 ? 1 : 0) << "\n"
128 << "\t" << "expire_time=" << static_cast<double>(comment->Get("expire_time")) << "\n"
129 << "\t" << "}" << "\n"
134 void StatusDataWriter::DumpTimePeriod(std::ostream& fp, const TimePeriod::Ptr& tp)
136 fp << "define timeperiod {" << "\n"
137 << "\t" << "timeperiod_name" << "\t" << tp->GetName() << "\n"
138 << "\t" << "alias" << "\t" << tp->GetName() << "\n";
140 Dictionary::Ptr ranges = tp->GetRanges();
143 ObjectLock olock(ranges);
146 BOOST_FOREACH(boost::tie(key, value), ranges) {
147 fp << "\t" << key << "\t" << Convert::ToString(value) << "\n";
151 fp << "\t" << "}" << "\n"
155 void StatusDataWriter::DumpCommand(std::ostream& fp, const Command::Ptr& command)
157 fp << "define command {" << "\n"
158 << "\t" << "command_name\t";
161 if (command->GetType() == DynamicType::GetByName("CheckCommand"))
163 else if (command->GetType() == DynamicType::GetByName("NotificationCommand"))
164 fp << "notification_";
165 else if (command->GetType() == DynamicType::GetByName("EventCommand"))
168 fp << command->GetName() << "\n";
170 fp << "\t" << "command_line\t";
172 Value commandLine = command->GetCommandLine();
174 if (commandLine.IsObjectType<Array>()) {
175 Array::Ptr args = commandLine;
177 ObjectLock olock(args);
179 BOOST_FOREACH(arg, args) {
180 // This is obviously incorrect for non-trivial cases.
181 fp << " \"" << CompatUtility::EscapeString(arg) << "\"";
183 } else if (!commandLine.IsEmpty()) {
184 fp << CompatUtility::EscapeString(Convert::ToString(commandLine));
191 fp << "\t" << "}" << "\n"
196 void StatusDataWriter::DumpDowntimes(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
198 Host::Ptr host = owner->GetHost();
203 Dictionary::Ptr downtimes = owner->GetDowntimes();
208 ObjectLock olock(downtimes);
211 Dictionary::Ptr downtime;
212 BOOST_FOREACH(boost::tie(id, downtime), downtimes) {
213 if (Service::IsDowntimeExpired(downtime))
216 if (type == CompatTypeHost)
217 fp << "hostdowntime {" << "\n";
219 fp << "servicedowntime {" << "\n"
220 << "\t" << "service_description=" << owner->GetShortName() << "\n";
222 Dictionary::Ptr triggeredByObj = Service::GetDowntimeByID(downtime->Get("triggered_by"));
223 int triggeredByLegacy = 0;
225 triggeredByLegacy = triggeredByObj->Get("legacy_id");
227 fp << "\t" << "host_name=" << host->GetName() << "\n"
228 << "\t" << "downtime_id=" << static_cast<String>(downtime->Get("legacy_id")) << "\n"
229 << "\t" << "entry_time=" << static_cast<double>(downtime->Get("entry_time")) << "\n"
230 << "\t" << "start_time=" << static_cast<double>(downtime->Get("start_time")) << "\n"
231 << "\t" << "end_time=" << static_cast<double>(downtime->Get("end_time")) << "\n"
232 << "\t" << "triggered_by=" << triggeredByLegacy << "\n"
233 << "\t" << "fixed=" << static_cast<long>(downtime->Get("fixed")) << "\n"
234 << "\t" << "duration=" << static_cast<long>(downtime->Get("duration")) << "\n"
235 << "\t" << "is_in_effect=" << (Service::IsDowntimeActive(downtime) ? 1 : 0) << "\n"
236 << "\t" << "author=" << static_cast<String>(downtime->Get("author")) << "\n"
237 << "\t" << "comment=" << static_cast<String>(downtime->Get("comment")) << "\n"
238 << "\t" << "trigger_time=" << static_cast<double>(downtime->Get("trigger_time")) << "\n"
239 << "\t" << "}" << "\n"
244 void StatusDataWriter::DumpHostStatus(std::ostream& fp, const Host::Ptr& host)
246 fp << "hoststatus {" << "\n"
247 << "\t" << "host_name=" << host->GetName() << "\n";
249 Service::Ptr hc = host->GetCheckService();
250 ObjectLock olock(hc);
253 DumpServiceStatusAttrs(fp, hc, CompatTypeHost);
255 /* ugly but cgis parse only that */
256 fp << "\t" << "last_time_up=" << host->GetLastStateUp() << "\n"
257 << "\t" << "last_time_down=" << host->GetLastStateDown() << "\n"
258 << "\t" << "last_time_unreachable=" << host->GetLastStateUnreachable() << "\n";
260 fp << "\t" << "}" << "\n"
264 DumpDowntimes(fp, hc, CompatTypeHost);
265 DumpComments(fp, hc, CompatTypeHost);
269 void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host)
271 fp << "define host {" << "\n"
272 << "\t" << "host_name" << "\t" << host->GetName() << "\n"
273 << "\t" << "display_name" << "\t" << host->GetDisplayName() << "\n"
274 << "\t" << "alias" << "\t" << host->GetDisplayName() << "\n";
276 Dictionary::Ptr macros = host->GetMacros();
279 fp << "\t" << "address" << "\t" << macros->Get("address") << "\n"
280 << "\t" << "address6" << "\t" << macros->Get("address6") << "\n";
283 std::set<Host::Ptr> parents = host->GetParentHosts();
285 if (!parents.empty()) {
286 fp << "\t" << "parents" << "\t";
287 DumpNameList(fp, parents);
291 Service::Ptr hc = host->GetCheckService();
293 ObjectLock olock(hc);
295 fp << "\t" << "check_interval" << "\t" << hc->GetCheckInterval() / 60.0 << "\n"
296 << "\t" << "retry_interval" << "\t" << hc->GetRetryInterval() / 60.0 << "\n"
297 << "\t" << "max_check_attempts" << "\t" << hc->GetMaxCheckAttempts() << "\n"
298 << "\t" << "active_checks_enabled" << "\t" << (hc->GetEnableActiveChecks() ? 1 : 0) << "\n"
299 << "\t" << "passive_checks_enabled" << "\t" << (hc->GetEnablePassiveChecks() ? 1 : 0) << "\n"
300 << "\t" << "notifications_enabled" << "\t" << (hc->GetEnableNotifications() ? 1 : 0) << "\n"
301 << "\t" << "notification_options" << "\t" << "d,u,r" << "\n"
302 << "\t" << "notification_interval" << "\t" << 1 << "\n"
303 << "\t" << "event_handler_enabled" << "\t" << (hc->GetEnableEventHandler() ? 1 : 0) << "\n";
305 CheckCommand::Ptr checkcommand = hc->GetCheckCommand();
307 fp << "\t" << "check_command" << "\t" << "check_" << checkcommand->GetName() << "\n";
309 EventCommand::Ptr eventcommand = hc->GetEventCommand();
311 fp << "\t" << "event_handler" << "\t" << "event_" << eventcommand->GetName() << "\n";
313 TimePeriod::Ptr check_period = hc->GetCheckPeriod();
315 fp << "\t" << "check_period" << "\t" << check_period->GetName() << "\n";
317 fp << "\t" << "contacts" << "\t";
318 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(hc));
321 fp << "\t" << "contact_groups" << "\t";
322 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(hc));
325 fp << "\t" << "initial_state" << "\t" << "o" << "\n"
326 << "\t" << "low_flap_threshold" << "\t" << hc->GetFlappingThreshold() << "\n"
327 << "\t" << "high_flap_threshold" << "\t" << hc->GetFlappingThreshold() << "\n"
328 << "\t" << "process_perf_data" << "\t" << 1 << "\n"
329 << "\t" << "check_freshness" << "\t" << 1 << "\n";
332 fp << "\t" << "check_interval" << "\t" << 60 << "\n"
333 << "\t" << "retry_interval" << "\t" << 60 << "\n"
334 << "\t" << "max_check_attempts" << "\t" << 1 << "\n"
335 << "\t" << "active_checks_enabled" << "\t" << 0 << "\n"
336 << "\t" << "passive_checks_enabled" << "\t" << 0 << "\n"
337 << "\t" << "notifications_enabled" << "\t" << 0 << "\n";
341 fp << "\t" << "host_groups" << "\t";
344 Array::Ptr groups = host->GetGroups();
347 ObjectLock olock(groups);
349 BOOST_FOREACH(const String& name, groups) {
350 HostGroup::Ptr hg = HostGroup::GetByName(name);
365 DumpCustomAttributes(fp, host);
367 fp << "\t" << "}" << "\n"
371 void StatusDataWriter::DumpServiceStatusAttrs(std::ostream& fp, const Service::Ptr& service, CompatObjectType type)
373 Dictionary::Ptr attrs = CompatUtility::GetServiceStatusAttributes(service, type);
375 fp << "\t" << "check_command=" << attrs->Get("check_command") << "\n"
376 << "\t" << "event_handler=" << attrs->Get("event_handler") << "\n"
377 << "\t" << "check_period=" << attrs->Get("check_period") << "\n"
378 << "\t" << "check_interval=" << static_cast<double>(attrs->Get("check_interval")) << "\n"
379 << "\t" << "retry_interval=" << static_cast<double>(attrs->Get("retry_interval")) << "\n"
380 << "\t" << "has_been_checked=" << attrs->Get("has_been_checked") << "\n"
381 << "\t" << "should_be_scheduled=" << attrs->Get("should_be_scheduled") << "\n"
382 << "\t" << "check_execution_time=" << static_cast<double>(attrs->Get("check_execution_time")) << "\n"
383 << "\t" << "check_latency=" << static_cast<double>(attrs->Get("check_latency")) << "\n"
384 << "\t" << "current_state=" << attrs->Get("current_state") << "\n"
385 << "\t" << "state_type=" << attrs->Get("state_type") << "\n"
386 << "\t" << "plugin_output=" << attrs->Get("plugin_output") << "\n"
387 << "\t" << "long_plugin_output=" << attrs->Get("long_plugin_output") << "\n"
388 << "\t" << "performance_data=" << attrs->Get("performance_data") << "\n"
389 << "\t" << "check_source=" << attrs->Get("check_source") << "\n"
390 << "\t" << "last_check=" << static_cast<long>(attrs->Get("last_check")) << "\n"
391 << "\t" << "next_check=" << static_cast<long>(attrs->Get("next_check")) << "\n"
392 << "\t" << "current_attempt=" << attrs->Get("current_attempt") << "\n"
393 << "\t" << "max_attempts=" << attrs->Get("max_attempts") << "\n"
394 << "\t" << "last_state_change=" << static_cast<long>(attrs->Get("last_state_change")) << "\n"
395 << "\t" << "last_hard_state_change=" << static_cast<long>(attrs->Get("last_hard_state_change")) << "\n"
396 << "\t" << "last_time_ok=" << static_cast<long>(attrs->Get("last_time_ok")) << "\n"
397 << "\t" << "last_time_warn=" << static_cast<long>(attrs->Get("last_time_warn")) << "\n"
398 << "\t" << "last_time_critical=" << static_cast<long>(attrs->Get("last_time_critical")) << "\n"
399 << "\t" << "last_time_unknown=" << static_cast<long>(attrs->Get("last_time_unknown")) << "\n"
400 << "\t" << "last_update=" << static_cast<long>(attrs->Get("last_update")) << "\n"
401 << "\t" << "notifications_enabled=" << attrs->Get("notifications_enabled") << "\n"
402 << "\t" << "active_checks_enabled=" << attrs->Get("active_checks_enabled") << "\n"
403 << "\t" << "passive_checks_enabled=" << attrs->Get("passive_checks_enabled") << "\n"
404 << "\t" << "flap_detection_enabled=" << attrs->Get("flap_detection_enabled") << "\n"
405 << "\t" << "is_flapping=" << attrs->Get("is_flapping") << "\n"
406 << "\t" << "percent_state_change=" << attrs->Get("percent_state_change") << "\n"
407 << "\t" << "problem_has_been_acknowledged=" << attrs->Get("problem_has_been_acknowledged") << "\n"
408 << "\t" << "acknowledgement_type=" << attrs->Get("acknowledgement_type") << "\n"
409 << "\t" << "acknowledgement_end_time=" << attrs->Get("acknowledgement_end_time") << "\n"
410 << "\t" << "scheduled_downtime_depth=" << attrs->Get("scheduled_downtime_depth") << "\n"
411 << "\t" << "last_notification=" << static_cast<long>(attrs->Get("last_notification")) << "\n"
412 << "\t" << "next_notification=" << static_cast<long>(attrs->Get("next_notification")) << "\n"
413 << "\t" << "current_notification_number=" << attrs->Get("current_notification_number") << "\n"
414 << "\t" << "modified_attributes=" << attrs->Get("modified_attributes") << "\n";
417 void StatusDataWriter::DumpServiceStatus(std::ostream& fp, const Service::Ptr& service)
419 Host::Ptr host = service->GetHost();
424 fp << "servicestatus {" << "\n"
425 << "\t" << "host_name=" << host->GetName() << "\n"
426 << "\t" << "service_description=" << service->GetShortName() << "\n";
429 ObjectLock olock(service);
430 DumpServiceStatusAttrs(fp, service, CompatTypeService);
433 fp << "\t" << "}" << "\n"
436 DumpDowntimes(fp, service, CompatTypeService);
437 DumpComments(fp, service, CompatTypeService);
440 void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& service)
442 Host::Ptr host = service->GetHost();
447 String check_period_str;
448 TimePeriod::Ptr check_period = service->GetCheckPeriod();
450 check_period_str = check_period->GetName();
452 check_period_str = "24x7";
454 double notification_interval = -1;
455 BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
456 if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval)
457 notification_interval = notification->GetNotificationInterval();
460 if (notification_interval == -1)
461 notification_interval = 60;
464 ObjectLock olock(service);
466 fp << "define service {" << "\n"
467 << "\t" << "host_name" << "\t" << host->GetName() << "\n"
468 << "\t" << "service_description" << "\t" << service->GetShortName() << "\n"
469 << "\t" << "display_name" << "\t" << service->GetDisplayName() << "\n"
470 << "\t" << "check_period" << "\t" << check_period_str << "\n"
471 << "\t" << "check_interval" << "\t" << service->GetCheckInterval() / 60.0 << "\n"
472 << "\t" << "retry_interval" << "\t" << service->GetRetryInterval() / 60.0 << "\n"
473 << "\t" << "max_check_attempts" << "\t" << service->GetMaxCheckAttempts() << "\n"
474 << "\t" << "active_checks_enabled" << "\t" << (service->GetEnableActiveChecks() ? 1 : 0) << "\n"
475 << "\t" << "passive_checks_enabled" << "\t" << (service->GetEnablePassiveChecks() ? 1 : 0) << "\n"
476 << "\t" << "flap_detection_enabled" << "\t" << (service->GetEnableFlapping() ? 1 : 0) << "\n"
477 << "\t" << "is_volatile" << "\t" << (service->IsVolatile() ? 1 : 0) << "\n"
478 << "\t" << "notifications_enabled" << "\t" << (service->GetEnableNotifications() ? 1 : 0) << "\n"
479 << "\t" << "notification_options" << "\t" << "u,w,c,r" << "\n"
480 << "\t" << "notification_interval" << "\t" << notification_interval / 60.0 << "\n"
481 << "\t" << "event_handler_enabled" << "\t" << (service->GetEnableEventHandler() ? 1 : 0) << "\n";
483 CheckCommand::Ptr checkcommand = service->GetCheckCommand();
485 fp << "\t" << "check_command" << "\t" << "check_" << checkcommand->GetName() << "\n";
487 EventCommand::Ptr eventcommand = service->GetEventCommand();
489 fp << "\t" << "event_handler" << "\t" << "event_" << eventcommand->GetName() << "\n";
491 TimePeriod::Ptr check_period = service->GetCheckPeriod();
493 fp << "\t" << "check_period" << "\t" << check_period->GetName() << "\n";
495 fp << "\t" << "contacts" << "\t";
496 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(service));
499 fp << "\t" << "contact_groups" << "\t";
500 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(service));
503 fp << "\t" << "initial_state" << "\t" << "o" << "\n"
504 << "\t" << "low_flap_threshold" << "\t" << service->GetFlappingThreshold() << "\n"
505 << "\t" << "high_flap_threshold" << "\t" << service->GetFlappingThreshold() << "\n"
506 << "\t" << "process_perf_data" << "\t" << 1 << "\n"
507 << "\t" << "check_freshness" << "\t" << 1 << "\n";
510 fp << "\t" << "service_groups" << "\t";
513 Array::Ptr groups = service->GetGroups();
516 ObjectLock olock(groups);
518 BOOST_FOREACH(const String& name, groups) {
519 ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);
534 DumpCustomAttributes(fp, service);
536 fp << "\t" << "}" << "\n"
539 BOOST_FOREACH(const Service::Ptr& parent, service->GetParentServices()) {
540 Host::Ptr host = service->GetHost();
545 Host::Ptr parent_host = parent->GetHost();
550 fp << "define servicedependency {" << "\n"
551 << "\t" << "dependent_host_name" << "\t" << host->GetName() << "\n"
552 << "\t" << "dependent_service_description" << "\t" << service->GetShortName() << "\n"
553 << "\t" << "host_name" << "\t" << parent_host->GetName() << "\n"
554 << "\t" << "service_description" << "\t" << parent->GetShortName() << "\n"
555 << "\t" << "execution_failure_criteria" << "\t" << "n" << "\n"
556 << "\t" << "notification_failure_criteria" << "\t" << "w,u,c" << "\n"
557 << "\t" << "}" << "\n"
562 void StatusDataWriter::DumpCustomAttributes(std::ostream& fp, const DynamicObject::Ptr& object)
564 Dictionary::Ptr custom = object->GetCustom();
569 ObjectLock olock(custom);
572 BOOST_FOREACH(boost::tie(key, value), custom) {
575 if (key != "notes" && key != "action_url" && key != "notes_url" &&
576 key != "icon_image" && key != "icon_image_alt" && key != "statusmap_image" && "2d_coords")
579 fp << key << "\t" << Convert::ToString(value) << "\n";
584 * Periodically writes the status.dat and objects.cache files.
586 void StatusDataWriter::StatusTimerHandler(void)
588 Log(LogInformation, "compat", "Writing compat status information");
590 String statuspath = GetStatusPath();
591 String objectspath = GetObjectsPath();
592 String statuspathtmp = statuspath + ".tmp"; /* XXX make this a global definition */
593 String objectspathtmp = objectspath + ".tmp";
595 std::ofstream statusfp;
596 statusfp.open(statuspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
598 statusfp << std::fixed;
600 statusfp << "# Icinga status file" << "\n"
601 << "# This file is auto-generated. Do not modify this file." << "\n"
604 statusfp << "info {" << "\n"
605 << "\t" << "created=" << Utility::GetTime() << "\n"
606 << "\t" << "version=" << Application::GetVersion() << "\n"
607 << "\t" << "}" << "\n"
610 statusfp << "programstatus {" << "\n"
611 << "\t" << "icinga_pid=" << Utility::GetPid() << "\n"
612 << "\t" << "daemon_mode=1" << "\n"
613 << "\t" << "program_start=" << static_cast<long>(IcingaApplication::GetInstance()->GetStartTime()) << "\n"
614 << "\t" << "active_service_checks_enabled=" << (IcingaApplication::GetInstance()->GetEnableChecks() ? 1 : 0) << "\n"
615 << "\t" << "passive_service_checks_enabled=1" << "\n"
616 << "\t" << "active_host_checks_enabled=1" << "\n"
617 << "\t" << "passive_host_checks_enabled=1" << "\n"
618 << "\t" << "check_service_freshness=1" << "\n"
619 << "\t" << "check_host_freshness=1" << "\n"
620 << "\t" << "enable_notifications=" << (IcingaApplication::GetInstance()->GetEnableNotifications() ? 1 : 0) << "\n"
621 << "\t" << "enable_flap_detection=" << (IcingaApplication::GetInstance()->GetEnableFlapping() ? 1 : 0) << "\n"
622 << "\t" << "enable_failure_prediction=0" << "\n"
623 << "\t" << "process_performance_data=" << (IcingaApplication::GetInstance()->GetEnablePerfdata() ? 1 : 0) << "\n"
624 << "\t" << "active_scheduled_service_check_stats=" << CIB::GetActiveChecksStatistics(60) << "," << CIB::GetActiveChecksStatistics(5 * 60) << "," << CIB::GetActiveChecksStatistics(15 * 60) << "\n"
625 << "\t" << "passive_service_check_stats=" << CIB::GetPassiveChecksStatistics(60) << "," << CIB::GetPassiveChecksStatistics(5 * 60) << "," << CIB::GetPassiveChecksStatistics(15 * 60) << "\n"
626 << "\t" << "next_downtime_id=" << Service::GetNextDowntimeID() << "\n"
627 << "\t" << "next_comment_id=" << Service::GetNextCommentID() << "\n"
628 << "\t" << "}" << "\n"
631 std::ofstream objectfp;
632 objectfp.open(objectspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
634 objectfp << std::fixed;
636 objectfp << "# Icinga objects cache file" << "\n"
637 << "# This file is auto-generated. Do not modify this file." << "\n"
640 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
641 std::ostringstream tempstatusfp;
642 tempstatusfp << std::fixed;
643 DumpHostStatus(tempstatusfp, host);
644 statusfp << tempstatusfp.str();
646 std::ostringstream tempobjectfp;
647 tempobjectfp << std::fixed;
648 DumpHostObject(tempobjectfp, host);
649 objectfp << tempobjectfp.str();
652 BOOST_FOREACH(const HostGroup::Ptr& hg, DynamicType::GetObjects<HostGroup>()) {
653 std::ostringstream tempobjectfp;
654 tempobjectfp << std::fixed;
656 tempobjectfp << "define hostgroup {" << "\n"
657 << "\t" << "hostgroup_name" << "\t" << hg->GetName() << "\n";
659 DumpCustomAttributes(tempobjectfp, hg);
661 tempobjectfp << "\t" << "members" << "\t";
662 DumpNameList(tempobjectfp, hg->GetMembers());
664 << "\t" << "}" << "\n";
666 objectfp << tempobjectfp.str();
669 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
670 std::ostringstream tempstatusfp;
671 tempstatusfp << std::fixed;
672 DumpServiceStatus(tempstatusfp, service);
673 statusfp << tempstatusfp.str();
675 std::ostringstream tempobjectfp;
676 tempobjectfp << std::fixed;
677 DumpServiceObject(tempobjectfp, service);
678 objectfp << tempobjectfp.str();
681 BOOST_FOREACH(const ServiceGroup::Ptr& sg, DynamicType::GetObjects<ServiceGroup>()) {
682 std::ostringstream tempobjectfp;
683 tempobjectfp << std::fixed;
685 tempobjectfp << "define servicegroup {" << "\n"
686 << "\t" << "servicegroup_name" << "\t" << sg->GetName() << "\n";
688 DumpCustomAttributes(tempobjectfp, sg);
690 tempobjectfp << "\t" << "members" << "\t";
692 std::vector<String> sglist;
693 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
694 Host::Ptr host = service->GetHost();
699 sglist.push_back(host->GetName());
700 sglist.push_back(service->GetShortName());
703 DumpStringList(tempobjectfp, sglist);
708 objectfp << tempobjectfp.str();
711 BOOST_FOREACH(const User::Ptr& user, DynamicType::GetObjects<User>()) {
712 std::ostringstream tempobjectfp;
713 tempobjectfp << std::fixed;
715 tempobjectfp << "define contact {" << "\n"
716 << "\t" << "contact_name" << "\t" << user->GetName() << "\n"
717 << "\t" << "alias" << "\t" << user->GetDisplayName() << "\n"
718 << "\t" << "service_notification_options" << "\t" << "w,u,c,r,f,s" << "\n"
719 << "\t" << "host_notification_options" << "\t" << "d,u,r,f,s" << "\n"
720 << "\t" << "host_notifications_enabled" << "\t" << 1 << "\n"
721 << "\t" << "service_notifications_enabled" << "\t" << 1 << "\n"
722 << "\t" << "}" << "\n"
725 objectfp << tempobjectfp.str();
728 BOOST_FOREACH(const UserGroup::Ptr& ug, DynamicType::GetObjects<UserGroup>()) {
729 std::ostringstream tempobjectfp;
730 tempobjectfp << std::fixed;
732 tempobjectfp << "define contactgroup {" << "\n"
733 << "\t" << "contactgroup_name" << "\t" << ug->GetName() << "\n"
734 << "\t" << "alias" << "\t" << ug->GetDisplayName() << "\n";
736 tempobjectfp << "\t" << "members" << "\t";
737 DumpNameList(tempobjectfp, ug->GetMembers());
739 << "\t" << "}" << "\n";
741 objectfp << tempobjectfp.str();
744 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<CheckCommand>()) {
745 DumpCommand(objectfp, command);
748 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<NotificationCommand>()) {
749 DumpCommand(objectfp, command);
752 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<EventCommand>()) {
753 DumpCommand(objectfp, command);
756 BOOST_FOREACH(const TimePeriod::Ptr& tp, DynamicType::GetObjects<TimePeriod>()) {
757 DumpTimePeriod(objectfp, tp);
764 _unlink(statuspath.CStr());
765 _unlink(objectspath.CStr());
769 if (rename(statuspathtmp.CStr(), statuspath.CStr()) < 0) {
770 BOOST_THROW_EXCEPTION(posix_error()
771 << boost::errinfo_api_function("rename")
772 << boost::errinfo_errno(errno)
773 << boost::errinfo_file_name(statuspathtmp));
777 if (rename(objectspathtmp.CStr(), objectspath.CStr()) < 0) {
778 BOOST_THROW_EXCEPTION(posix_error()
779 << boost::errinfo_api_function("rename")
780 << boost::errinfo_errno(errno)
781 << boost::errinfo_file_name(objectspathtmp));
785 void StatusDataWriter::InternalSerialize(const Dictionary::Ptr& bag, int attributeTypes) const
787 DynamicObject::InternalSerialize(bag, attributeTypes);
789 if (attributeTypes & Attribute_Config) {
790 bag->Set("status_path", m_StatusPath);
791 bag->Set("objects_path", m_ObjectsPath);
795 void StatusDataWriter::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes)
797 DynamicObject::InternalDeserialize(bag, attributeTypes);
799 if (attributeTypes & Attribute_Config) {
800 m_StatusPath = bag->Get("status_path");
801 m_ObjectsPath = bag->Get("objects_path");