1 /******************************************************************************
3 * Copyright (C) 2012-2014 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 "icinga/dependency.h"
31 #include "base/dynamictype.h"
32 #include "base/objectlock.h"
33 #include "base/convert.h"
34 #include "base/logger_fwd.h"
35 #include "base/exception.h"
36 #include "base/application.h"
37 #include "base/context.h"
38 #include "base/statsfunction.h"
39 #include <boost/foreach.hpp>
40 #include <boost/tuple/tuple.hpp>
41 #include <boost/algorithm/string/replace.hpp>
44 using namespace icinga;
46 REGISTER_TYPE(StatusDataWriter);
48 REGISTER_STATSFUNCTION(StatusDataWriterStats, &StatusDataWriter::StatsFunc);
50 Value StatusDataWriter::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata)
52 Dictionary::Ptr nodes = make_shared<Dictionary>();
54 BOOST_FOREACH(const StatusDataWriter::Ptr& statusdatawriter, DynamicType::GetObjects<StatusDataWriter>()) {
55 nodes->Set(statusdatawriter->GetName(), 1); //add more stats
58 status->Set("statusdatawriter", nodes);
64 * Hint: The reason why we're using "\n" rather than std::endl is because
65 * std::endl also _flushes_ the output stream which severely degrades
66 * performance (see http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html).
70 * Starts the component.
72 void StatusDataWriter::Start(void)
74 DynamicObject::Start();
76 m_StatusTimer = make_shared<Timer>();
77 m_StatusTimer->SetInterval(GetUpdateInterval());
78 m_StatusTimer->OnTimerExpired.connect(boost::bind(&StatusDataWriter::StatusTimerHandler, this));
79 m_StatusTimer->Start();
80 m_StatusTimer->Reschedule(0);
82 Utility::QueueAsyncCallback(boost::bind(&StatusDataWriter::UpdateObjectsCache, this));
85 void StatusDataWriter::DumpComments(std::ostream& fp, const Checkable::Ptr& checkable)
87 Dictionary::Ptr comments = checkable->GetComments();
91 tie(host, service) = GetHostService(checkable);
93 ObjectLock olock(comments);
95 BOOST_FOREACH(const Dictionary::Pair& kv, comments) {
96 Comment::Ptr comment = kv.second;
98 if (comment->IsExpired())
102 fp << "servicecomment {" << "\n"
103 << "\t" << "service_description=" << service->GetShortName() << "\n";
105 fp << "hostcomment {" << "\n";
107 fp << "\t" "host_name=" << host->GetName() << "\n"
108 "\t" "comment_id=" << comment->GetLegacyId() << "\n"
109 "\t" "entry_time=" << comment->GetEntryTime() << "\n"
110 "\t" "entry_type=" << comment->GetEntryType() << "\n"
111 "\t" "persistent=" "1" "\n"
112 "\t" "author=" << comment->GetAuthor() << "\n"
113 "\t" "comment_data=" << comment->GetText() << "\n"
114 "\t" "expires=" << (comment->GetExpireTime() != 0 ? 1 : 0) << "\n"
115 "\t" "expire_time=" << comment->GetExpireTime() << "\n"
121 void StatusDataWriter::DumpTimePeriod(std::ostream& fp, const TimePeriod::Ptr& tp)
123 fp << "define timeperiod {" "\n"
124 "\t" "timeperiod_name" "\t" << tp->GetName() << "\n"
125 "\t" "alias" "\t" << tp->GetName() << "\n";
127 Dictionary::Ptr ranges = tp->GetRanges();
130 ObjectLock olock(ranges);
131 BOOST_FOREACH(const Dictionary::Pair& kv, ranges) {
132 fp << "\t" << kv.first << "\t" << kv.second << "\n";
140 void StatusDataWriter::DumpCommand(std::ostream& fp, const Command::Ptr& command)
142 fp << "define command {" "\n"
143 "\t" "command_name\t";
146 if (command->GetType() == DynamicType::GetByName("CheckCommand"))
148 else if (command->GetType() == DynamicType::GetByName("NotificationCommand"))
149 fp << "notification_";
150 else if (command->GetType() == DynamicType::GetByName("EventCommand"))
153 fp << command->GetName() << "\n";
155 fp << "\t" "command_line" "\t" << CompatUtility::GetCommandLine(command);
159 DumpCustomAttributes(fp, command);
161 fp << "\n" "\t" "}" "\n"
165 void StatusDataWriter::DumpDowntimes(std::ostream& fp, const Checkable::Ptr& checkable)
167 Dictionary::Ptr downtimes = checkable->GetDowntimes();
170 Service::Ptr service;
171 tie(host, service) = GetHostService(checkable);
173 ObjectLock olock(downtimes);
175 BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
176 Downtime::Ptr downtime = kv.second;
178 if (downtime->IsExpired())
182 fp << "servicedowntime {" << "\n"
183 "\t" "service_description=" << service->GetShortName() << "\n";
185 fp << "hostdowntime {" "\n";
187 Downtime::Ptr triggeredByObj = Service::GetDowntimeByID(downtime->GetTriggeredBy());
188 int triggeredByLegacy = 0;
190 triggeredByLegacy = triggeredByObj->GetLegacyId();
192 fp << "\t" << "host_name=" << host->GetName() << "\n"
193 "\t" "downtime_id=" << downtime->GetLegacyId() << "\n"
194 "\t" "entry_time=" << downtime->GetEntryTime() << "\n"
195 "\t" "start_time=" << downtime->GetStartTime() << "\n"
196 "\t" "end_time=" << downtime->GetEndTime() << "\n"
197 "\t" "triggered_by=" << triggeredByLegacy << "\n"
198 "\t" "fixed=" << static_cast<long>(downtime->GetFixed()) << "\n"
199 "\t" "duration=" << static_cast<long>(downtime->GetDuration()) << "\n"
200 "\t" "is_in_effect=" << (downtime->IsActive() ? 1 : 0) << "\n"
201 "\t" "author=" << downtime->GetAuthor() << "\n"
202 "\t" "comment=" << downtime->GetComment() << "\n"
203 "\t" "trigger_time=" << downtime->GetTriggerTime() << "\n"
209 void StatusDataWriter::DumpHostStatus(std::ostream& fp, const Host::Ptr& host)
211 fp << "hoststatus {" << "\n"
212 << "\t" << "host_name=" << host->GetName() << "\n";
215 ObjectLock olock(host);
216 DumpCheckableStatusAttrs(fp, host);
219 /* ugly but cgis parse only that */
220 fp << "\t" "last_time_up=" << host->GetLastStateUp() << "\n"
221 "\t" "last_time_down=" << host->GetLastStateDown() << "\n"
222 "\t" "last_time_unreachable=" << host->GetLastStateUnreachable() << "\n";
227 DumpDowntimes(fp, host);
228 DumpComments(fp, host);
231 void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host)
233 String notes = CompatUtility::GetCustomAttributeConfig(host, "notes");
234 String notes_url = CompatUtility::GetCustomAttributeConfig(host, "notes_url");
235 String action_url = CompatUtility::GetCustomAttributeConfig(host, "action_url");
236 String icon_image = CompatUtility::GetCustomAttributeConfig(host, "icon_image");
237 String icon_image_alt = CompatUtility::GetCustomAttributeConfig(host, "icon_image_alt");
238 String statusmap_image = CompatUtility::GetCustomAttributeConfig(host, "statusmap_image");
240 fp << "define host {" "\n"
241 "\t" "host_name" "\t" << host->GetName() << "\n"
242 "\t" "display_name" "\t" << host->GetDisplayName() << "\n"
243 "\t" "alias" "\t" << host->GetDisplayName() << "\n"
244 "\t" "address" "\t" << CompatUtility::GetHostAddress(host) << "\n"
245 "\t" "address6" "\t" << CompatUtility::GetHostAddress6(host) << "\n";
246 if (!notes.IsEmpty())
247 fp << "\t" "notes" "\t" << notes << "\n";
248 if (!notes_url.IsEmpty())
249 fp << "\t" "notes_url" "\t" << notes_url << "\n";
250 if (!action_url.IsEmpty())
251 fp << "\t" "action_url" "\t" << action_url << "\n";
252 if (!icon_image.IsEmpty())
253 fp << "\t" "icon_image" "\t" << icon_image << "\n";
254 if (!icon_image_alt.IsEmpty())
255 fp << "\t" "icon_image_alt" "\t" << icon_image_alt << "\n";
256 if (!statusmap_image.IsEmpty())
257 fp << "\t" "statusmap_image" "\t" << statusmap_image << "\n";
259 std::set<Checkable::Ptr> parents = host->GetParents();
261 if (!parents.empty()) {
262 fp << "\t" "parents" "\t";
263 DumpNameList(fp, parents);
267 ObjectLock olock(host);
269 fp << "\t" "check_interval" "\t" << CompatUtility::GetCheckableCheckInterval(host) << "\n"
270 "\t" "retry_interval" "\t" << CompatUtility::GetCheckableRetryInterval(host) << "\n"
271 "\t" "max_check_attempts" "\t" << host->GetMaxCheckAttempts() << "\n"
272 "\t" "active_checks_enabled" "\t" << CompatUtility::GetCheckableActiveChecksEnabled(host) << "\n"
273 "\t" "passive_checks_enabled" "\t" << CompatUtility::GetCheckablePassiveChecksEnabled(host) << "\n"
274 "\t" "notifications_enabled" "\t" << CompatUtility::GetCheckableNotificationsEnabled(host) << "\n"
275 "\t" "notification_options" "\t" << "d,u,r" << "\n"
276 "\t" "notification_interval" "\t" << CompatUtility::GetCheckableNotificationNotificationInterval(host) << "\n"
277 "\t" "event_handler_enabled" "\t" << CompatUtility::GetCheckableEventHandlerEnabled(host) << "\n";
279 CheckCommand::Ptr checkcommand = host->GetCheckCommand();
281 fp << "\t" "check_command" "\t" "check_" << checkcommand->GetName() << "\n";
283 EventCommand::Ptr eventcommand = host->GetEventCommand();
285 fp << "\t" "event_handler" "\t" "event_" << eventcommand->GetName() << "\n";
287 fp << "\t" "check_period" "\t" << CompatUtility::GetCheckableCheckPeriod(host) << "\n";
289 fp << "\t" "contacts" "\t";
290 DumpNameList(fp, CompatUtility::GetCheckableNotificationUsers(host));
293 fp << "\t" "contact_groups" "\t";
294 DumpNameList(fp, CompatUtility::GetCheckableNotificationUserGroups(host));
297 fp << "\t" << "initial_state" "\t" "o" "\n"
298 "\t" "low_flap_threshold" "\t" << host->GetFlappingThreshold() << "\n"
299 "\t" "high_flap_threshold" "\t" << host->GetFlappingThreshold() << "\n"
300 "\t" "process_perf_data" "\t" << CompatUtility::GetCheckableProcessPerformanceData(host) << "\n"
301 "\t" "check_freshness" "\t" "1" "\n";
303 fp << "\t" "host_groups" "\t";
306 Array::Ptr groups = host->GetGroups();
309 ObjectLock olock(groups);
311 BOOST_FOREACH(const String& name, groups) {
312 HostGroup::Ptr hg = HostGroup::GetByName(name);
327 DumpCustomAttributes(fp, host);
333 void StatusDataWriter::DumpCheckableStatusAttrs(std::ostream& fp, const Checkable::Ptr& checkable)
335 CheckResult::Ptr cr = checkable->GetLastCheckResult();
337 fp << "\t" << "check_command=check_" << CompatUtility::GetCheckableCheckCommand(checkable) << "\n"
338 "\t" "event_handler=event_" << CompatUtility::GetCheckableEventHandler(checkable) << "\n"
339 "\t" "check_period=" << CompatUtility::GetCheckableCheckPeriod(checkable) << "\n"
340 "\t" "check_interval=" << CompatUtility::GetCheckableCheckInterval(checkable) << "\n"
341 "\t" "retry_interval=" << CompatUtility::GetCheckableRetryInterval(checkable) << "\n"
342 "\t" "has_been_checked=" << CompatUtility::GetCheckableHasBeenChecked(checkable) << "\n"
343 "\t" "should_be_scheduled=" << CompatUtility::GetCheckableShouldBeScheduled(checkable) << "\n";
346 fp << "\t" << "check_execution_time=" << Convert::ToString(Service::CalculateExecutionTime(cr)) << "\n"
347 "\t" "check_latency=" << Convert::ToString(Service::CalculateLatency(cr)) << "\n";
351 Service::Ptr service;
352 tie(host, service) = GetHostService(checkable);
355 fp << "\t" << "current_state=" << service->GetState() << "\n";
357 fp << "\t" << "current_state=" << (host->IsReachable() ? host->GetState() : 2) << "\n";
359 fp << "\t" "state_type=" << checkable->GetStateType() << "\n"
360 "\t" "plugin_output=" << CompatUtility::GetCheckResultOutput(cr) << "\n"
361 "\t" "long_plugin_output=" << CompatUtility::GetCheckResultLongOutput(cr) << "\n"
362 "\t" "performance_data=" << CompatUtility::GetCheckResultPerfdata(cr) << "\n";
365 fp << "\t" << "check_source=" << cr->GetCheckSource() << "\n"
366 "\t" "last_check=" << static_cast<long>(cr->GetScheduleEnd()) << "\n";
369 fp << "\t" << "next_check=" << static_cast<long>(checkable->GetNextCheck()) << "\n"
370 "\t" "current_attempt=" << checkable->GetCheckAttempt() << "\n"
371 "\t" "max_attempts=" << checkable->GetMaxCheckAttempts() << "\n"
372 "\t" "last_state_change=" << static_cast<long>(checkable->GetLastStateChange()) << "\n"
373 "\t" "last_hard_state_change=" << static_cast<long>(checkable->GetLastHardStateChange()) << "\n"
374 "\t" "last_time_ok=" << static_cast<int>(checkable->GetLastStateOK()) << "\n"
375 "\t" "last_time_warn=" << static_cast<int>(checkable->GetLastStateWarning()) << "\n"
376 "\t" "last_time_critical=" << static_cast<int>(checkable->GetLastStateCritical()) << "\n"
377 "\t" "last_time_unknown=" << static_cast<int>(checkable->GetLastStateUnknown()) << "\n"
378 "\t" "last_update=" << static_cast<long>(time(NULL)) << "\n"
379 "\t" "notifications_enabled=" << CompatUtility::GetCheckableNotificationsEnabled(checkable) << "\n"
380 "\t" "active_checks_enabled=" << CompatUtility::GetCheckableActiveChecksEnabled(checkable) << "\n"
381 "\t" "passive_checks_enabled=" << CompatUtility::GetCheckablePassiveChecksEnabled(checkable) << "\n"
382 "\t" "flap_detection_enabled=" << CompatUtility::GetCheckableFlapDetectionEnabled(checkable) << "\n"
383 "\t" "is_flapping=" << CompatUtility::GetCheckableIsFlapping(checkable) << "\n"
384 "\t" "percent_state_change=" << CompatUtility::GetCheckablePercentStateChange(checkable) << "\n"
385 "\t" "problem_has_been_acknowledged=" << CompatUtility::GetCheckableProblemHasBeenAcknowledged(checkable) << "\n"
386 "\t" "acknowledgement_type=" << CompatUtility::GetCheckableAcknowledgementType(checkable) << "\n"
387 "\t" "acknowledgement_end_time=" << checkable->GetAcknowledgementExpiry() << "\n"
388 "\t" "scheduled_downtime_depth=" << checkable->GetDowntimeDepth() << "\n"
389 "\t" "last_notification=" << CompatUtility::GetCheckableNotificationLastNotification(checkable) << "\n"
390 "\t" "next_notification=" << CompatUtility::GetCheckableNotificationNextNotification(checkable) << "\n"
391 "\t" "current_notification_number=" << CompatUtility::GetCheckableNotificationNotificationNumber(checkable) << "\n"
392 "\t" "modified_attributes=" << checkable->GetModifiedAttributes() << "\n";
395 void StatusDataWriter::DumpServiceStatus(std::ostream& fp, const Service::Ptr& service)
397 Host::Ptr host = service->GetHost();
399 fp << "servicestatus {" "\n"
400 "\t" "host_name=" << host->GetName() << "\n"
401 "\t" "service_description=" << service->GetShortName() << "\n";
404 ObjectLock olock(service);
405 DumpCheckableStatusAttrs(fp, service);
411 DumpDowntimes(fp, service);
412 DumpComments(fp, service);
415 void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& service)
417 Host::Ptr host = service->GetHost();
420 ObjectLock olock(service);
422 fp << "define service {" "\n"
423 "\t" "host_name" "\t" << host->GetName() << "\n"
424 "\t" "service_description" "\t" << service->GetShortName() << "\n"
425 "\t" "display_name" "\t" << service->GetDisplayName() << "\n"
426 "\t" "check_period" "\t" << CompatUtility::GetCheckableCheckPeriod(service) << "\n"
427 "\t" "check_interval" "\t" << CompatUtility::GetCheckableCheckInterval(service) << "\n"
428 "\t" "retry_interval" "\t" << CompatUtility::GetCheckableRetryInterval(service) << "\n"
429 "\t" "max_check_attempts" "\t" << service->GetMaxCheckAttempts() << "\n"
430 "\t" "active_checks_enabled" "\t" << CompatUtility::GetCheckableActiveChecksEnabled(service) << "\n"
431 "\t" "passive_checks_enabled" "\t" << CompatUtility::GetCheckablePassiveChecksEnabled(service) << "\n"
432 "\t" "flap_detection_enabled" "\t" << CompatUtility::GetCheckableFlapDetectionEnabled(service) << "\n"
433 "\t" "is_volatile" "\t" << CompatUtility::GetCheckableIsVolatile(service) << "\n"
434 "\t" "notifications_enabled" "\t" << CompatUtility::GetCheckableNotificationsEnabled(service) << "\n"
435 "\t" "notification_options" "\t" << CompatUtility::GetCheckableNotificationNotificationOptions(service) << "\n"
436 "\t" "notification_interval" "\t" << CompatUtility::GetCheckableNotificationNotificationInterval(service) << "\n"
437 "\t" "notification_period" "\t" << CompatUtility::GetCheckableNotificationNotificationPeriod(service) << "\n"
438 "\t" "event_handler_enabled" "\t" << CompatUtility::GetCheckableEventHandlerEnabled(service) << "\n";
440 CheckCommand::Ptr checkcommand = service->GetCheckCommand();
442 fp << "\t" "check_command" "\t" "check_" << checkcommand->GetName() << "\n";
444 EventCommand::Ptr eventcommand = service->GetEventCommand();
446 fp << "\t" "event_handler" "\t" "event_" << eventcommand->GetName() << "\n";
448 fp << "\t" "contacts" "\t";
449 DumpNameList(fp, CompatUtility::GetCheckableNotificationUsers(service));
452 fp << "\t" "contact_groups" "\t";
453 DumpNameList(fp, CompatUtility::GetCheckableNotificationUserGroups(service));
456 String notes = CompatUtility::GetCustomAttributeConfig(service, "notes");
457 String notes_url = CompatUtility::GetCustomAttributeConfig(service, "notes_url");
458 String action_url = CompatUtility::GetCustomAttributeConfig(service, "action_url");
459 String icon_image = CompatUtility::GetCustomAttributeConfig(service, "icon_image");
460 String icon_image_alt = CompatUtility::GetCustomAttributeConfig(service, "icon_image_alt");
462 fp << "\t" "initial_state" "\t" "o" "\n"
463 "\t" "low_flap_threshold" "\t" << service->GetFlappingThreshold() << "\n"
464 "\t" "high_flap_threshold" "\t" << service->GetFlappingThreshold() << "\n"
465 "\t" "process_perf_data" "\t" << CompatUtility::GetCheckableProcessPerformanceData(service) << "\n"
466 "\t" "check_freshness" << "\t" "1" "\n";
467 if (!notes.IsEmpty())
468 fp << "\t" "notes" "\t" << notes << "\n";
469 if (!notes_url.IsEmpty())
470 fp << "\t" "notes_url" "\t" << notes_url << "\n";
471 if (!action_url.IsEmpty())
472 fp << "\t" "action_url" "\t" << action_url << "\n";
473 if (!icon_image.IsEmpty())
474 fp << "\t" "icon_image" "\t" << icon_image << "\n";
475 if (!icon_image_alt.IsEmpty())
476 fp << "\t" "icon_image_alt" "\t" << icon_image_alt << "\n";
479 fp << "\t" "service_groups" "\t";
482 Array::Ptr groups = service->GetGroups();
485 ObjectLock olock(groups);
487 BOOST_FOREACH(const String& name, groups) {
488 ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);
503 DumpCustomAttributes(fp, service);
509 void StatusDataWriter::DumpCustomAttributes(std::ostream& fp, const DynamicObject::Ptr& object)
511 Dictionary::Ptr vars = object->GetVars();
516 ObjectLock olock(vars);
517 BOOST_FOREACH(const Dictionary::Pair& kv, vars) {
518 if (!kv.first.IsEmpty()) {
521 if (!CompatUtility::IsLegacyAttribute(object, kv.first))
524 fp << kv.first << "\t" << kv.second << "\n";
529 void StatusDataWriter::UpdateObjectsCache(void)
531 CONTEXT("Writing objects.cache file");
533 String objectspath = GetObjectsPath();
534 String objectspathtmp = objectspath + ".tmp";
536 std::ofstream objectfp;
537 objectfp.open(objectspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
539 objectfp << std::fixed;
541 objectfp << "# Icinga objects cache file" "\n"
542 "# This file is auto-generated. Do not modify this file." "\n"
545 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
546 std::ostringstream tempobjectfp;
547 tempobjectfp << std::fixed;
548 DumpHostObject(tempobjectfp, host);
549 objectfp << tempobjectfp.str();
551 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
552 std::ostringstream tempobjectfp;
553 tempobjectfp << std::fixed;
554 DumpServiceObject(tempobjectfp, service);
555 objectfp << tempobjectfp.str();
559 BOOST_FOREACH(const HostGroup::Ptr& hg, DynamicType::GetObjects<HostGroup>()) {
560 std::ostringstream tempobjectfp;
561 tempobjectfp << std::fixed;
563 tempobjectfp << "define hostgroup {" "\n"
564 "\t" "hostgroup_name" "\t" << hg->GetName() << "\n";
566 DumpCustomAttributes(tempobjectfp, hg);
568 tempobjectfp << "\t" "members" "\t";
569 DumpNameList(tempobjectfp, hg->GetMembers());
573 objectfp << tempobjectfp.str();
576 BOOST_FOREACH(const ServiceGroup::Ptr& sg, DynamicType::GetObjects<ServiceGroup>()) {
577 std::ostringstream tempobjectfp;
578 tempobjectfp << std::fixed;
580 tempobjectfp << "define servicegroup {" "\n"
581 "\t" "servicegroup_name" "\t" << sg->GetName() << "\n";
583 DumpCustomAttributes(tempobjectfp, sg);
585 tempobjectfp << "\t" "members" "\t";
587 std::vector<String> sglist;
588 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
589 Host::Ptr host = service->GetHost();
591 sglist.push_back(host->GetName());
592 sglist.push_back(service->GetShortName());
595 DumpStringList(tempobjectfp, sglist);
600 objectfp << tempobjectfp.str();
603 BOOST_FOREACH(const User::Ptr& user, DynamicType::GetObjects<User>()) {
604 std::ostringstream tempobjectfp;
605 tempobjectfp << std::fixed;
607 tempobjectfp << "define contact {" "\n"
608 "\t" "contact_name" "\t" << user->GetName() << "\n"
609 "\t" "alias" "\t" << user->GetDisplayName() << "\n"
610 "\t" "service_notification_options" "\t" "w,u,c,r,f,s" "\n"
611 "\t" "host_notification_options""\t" "d,u,r,f,s" "\n"
612 "\t" "host_notifications_enabled" "\t" "1" "\n"
613 "\t" "service_notifications_enabled" "\t" "1" "\n"
617 objectfp << tempobjectfp.str();
620 BOOST_FOREACH(const UserGroup::Ptr& ug, DynamicType::GetObjects<UserGroup>()) {
621 std::ostringstream tempobjectfp;
622 tempobjectfp << std::fixed;
624 tempobjectfp << "define contactgroup {" "\n"
625 "\t" "contactgroup_name" "\t" << ug->GetName() << "\n"
626 "\t" "alias" "\t" << ug->GetDisplayName() << "\n";
628 tempobjectfp << "\t" "members" "\t";
629 DumpNameList(tempobjectfp, ug->GetMembers());
633 objectfp << tempobjectfp.str();
636 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<CheckCommand>()) {
637 DumpCommand(objectfp, command);
640 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<NotificationCommand>()) {
641 DumpCommand(objectfp, command);
644 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<EventCommand>()) {
645 DumpCommand(objectfp, command);
648 BOOST_FOREACH(const TimePeriod::Ptr& tp, DynamicType::GetObjects<TimePeriod>()) {
649 DumpTimePeriod(objectfp, tp);
652 BOOST_FOREACH(const Dependency::Ptr& dep, DynamicType::GetObjects<Dependency>()) {
653 Checkable::Ptr parent = dep->GetParent();
658 Host::Ptr parent_host;
659 Service::Ptr parent_service;
660 tie(parent_host, parent_service) = GetHostService(parent);
665 Checkable::Ptr child = dep->GetChild();
670 Host::Ptr child_host;
671 Service::Ptr child_service;
672 tie(child_host, child_service) = GetHostService(child);
677 objectfp << "define servicedependency {" "\n"
678 "\t" "dependent_host_name" "\t" << child_service->GetHost()->GetName() << "\n"
679 "\t" "dependent_service_description" "\t" << child_service->GetShortName() << "\n"
680 "\t" "host_name" "\t" << parent_service->GetHost()->GetName() << "\n"
681 "\t" "service_description" "\t" << parent_service->GetShortName() << "\n"
682 "\t" "execution_failure_criteria" "\t" "n" "\n"
683 "\t" "notification_failure_criteria" "\t" "w,u,c" "\n"
691 _unlink(objectspath.CStr());
694 if (rename(objectspathtmp.CStr(), objectspath.CStr()) < 0) {
695 BOOST_THROW_EXCEPTION(posix_error()
696 << boost::errinfo_api_function("rename")
697 << boost::errinfo_errno(errno)
698 << boost::errinfo_file_name(objectspathtmp));
703 * Periodically writes the status.dat and objects.cache files.
705 void StatusDataWriter::StatusTimerHandler(void)
707 Log(LogInformation, "compat", "Writing status.dat file");
709 String statuspath = GetStatusPath();
710 String statuspathtmp = statuspath + ".tmp"; /* XXX make this a global definition */
712 std::ofstream statusfp;
713 statusfp.open(statuspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
715 statusfp << std::fixed;
717 statusfp << "# Icinga status file" "\n"
718 "# This file is auto-generated. Do not modify this file." "\n"
721 statusfp << "info {" "\n"
722 "\t" "created=" << Utility::GetTime() << "\n"
723 "\t" "version=" << Application::GetVersion() << "\n"
727 statusfp << "programstatus {" "\n"
728 "\t" "icinga_pid=" << Utility::GetPid() << "\n"
729 "\t" "daemon_mode=1" "\n"
730 "\t" "program_start=" << static_cast<long>(Application::GetStartTime()) << "\n"
731 "\t" "active_service_checks_enabled=" << (IcingaApplication::GetInstance()->GetEnableChecks() ? 1 : 0) << "\n"
732 "\t" "passive_service_checks_enabled=1" "\n"
733 "\t" "active_host_checks_enabled=1" "\n"
734 "\t" "passive_host_checks_enabled=1" "\n"
735 "\t" "check_service_freshness=1" "\n"
736 "\t" "check_host_freshness=1" "\n"
737 "\t" "enable_notifications=" << (IcingaApplication::GetInstance()->GetEnableNotifications() ? 1 : 0) << "\n"
738 "\t" "enable_flap_detection=" << (IcingaApplication::GetInstance()->GetEnableFlapping() ? 1 : 0) << "\n"
739 "\t" "enable_failure_prediction=0" "\n"
740 "\t" "process_performance_data=" << (IcingaApplication::GetInstance()->GetEnablePerfdata() ? 1 : 0) << "\n"
741 "\t" "active_scheduled_service_check_stats=" << CIB::GetActiveChecksStatistics(60) << "," << CIB::GetActiveChecksStatistics(5 * 60) << "," << CIB::GetActiveChecksStatistics(15 * 60) << "\n"
742 "\t" "passive_service_check_stats=" << CIB::GetPassiveChecksStatistics(60) << "," << CIB::GetPassiveChecksStatistics(5 * 60) << "," << CIB::GetPassiveChecksStatistics(15 * 60) << "\n"
743 "\t" "next_downtime_id=" << Service::GetNextDowntimeID() << "\n"
744 "\t" "next_comment_id=" << Service::GetNextCommentID() << "\n";
746 DumpCustomAttributes(statusfp, IcingaApplication::GetInstance());
748 statusfp << "\t" "}" "\n"
751 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
752 std::ostringstream tempstatusfp;
753 tempstatusfp << std::fixed;
754 DumpHostStatus(tempstatusfp, host);
755 statusfp << tempstatusfp.str();
757 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
758 std::ostringstream tempstatusfp;
759 tempstatusfp << std::fixed;
760 DumpServiceStatus(tempstatusfp, service);
761 statusfp << tempstatusfp.str();
768 _unlink(statuspath.CStr());
771 if (rename(statuspathtmp.CStr(), statuspath.CStr()) < 0) {
772 BOOST_THROW_EXCEPTION(posix_error()
773 << boost::errinfo_api_function("rename")
774 << boost::errinfo_errno(errno)
775 << boost::errinfo_file_name(statuspathtmp));
778 Log(LogInformation, "compat", "Finished writing status.dat file");