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 Service::Ptr& owner, CompatObjectType type)
88 Dictionary::Ptr comments = owner->GetComments();
90 Host::Ptr host = owner->GetHost();
92 ObjectLock olock(comments);
94 BOOST_FOREACH(const Dictionary::Pair& kv, comments) {
95 Comment::Ptr comment = kv.second;
97 if (comment->IsExpired())
100 if (type == CompatTypeHost)
101 fp << "hostcomment {" << "\n";
103 fp << "servicecomment {" << "\n"
104 << "\t" << "service_description=" << owner->GetShortName() << "\n";
106 fp << "\t" "host_name=" << host->GetName() << "\n"
107 "\t" "comment_id=" << comment->GetLegacyId() << "\n"
108 "\t" "entry_time=" << comment->GetEntryTime() << "\n"
109 "\t" "entry_type=" << comment->GetEntryType() << "\n"
110 "\t" "persistent=" "1" "\n"
111 "\t" "author=" << comment->GetAuthor() << "\n"
112 "\t" "comment_data=" << comment->GetText() << "\n"
113 "\t" "expires=" << (comment->GetExpireTime() != 0 ? 1 : 0) << "\n"
114 "\t" "expire_time=" << comment->GetExpireTime() << "\n"
120 void StatusDataWriter::DumpTimePeriod(std::ostream& fp, const TimePeriod::Ptr& tp)
122 fp << "define timeperiod {" "\n"
123 "\t" "timeperiod_name" "\t" << tp->GetName() << "\n"
124 "\t" "alias" "\t" << tp->GetName() << "\n";
126 Dictionary::Ptr ranges = tp->GetRanges();
129 ObjectLock olock(ranges);
130 BOOST_FOREACH(const Dictionary::Pair& kv, ranges) {
131 fp << "\t" << kv.first << "\t" << kv.second << "\n";
139 void StatusDataWriter::DumpCommand(std::ostream& fp, const Command::Ptr& command)
141 fp << "define command {" "\n"
142 "\t" "command_name\t";
145 if (command->GetType() == DynamicType::GetByName("CheckCommand"))
147 else if (command->GetType() == DynamicType::GetByName("NotificationCommand"))
148 fp << "notification_";
149 else if (command->GetType() == DynamicType::GetByName("EventCommand"))
152 fp << command->GetName() << "\n";
154 fp << "\t" "command_line" "\t" << CompatUtility::GetCommandLine(command);
156 fp << "\n" "\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"
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 /* only dump status data information for Classic UI */
212 fp << "\t" "check_service=" << hc->GetShortName() << "\n";
213 DumpServiceStatusAttrs(fp, hc, CompatTypeHost);
216 /* ugly but cgis parse only that */
217 fp << "\t" "last_time_up=" << host->GetLastStateUp() << "\n"
218 "\t" "last_time_down=" << host->GetLastStateDown() << "\n"
219 "\t" "last_time_unreachable=" << host->GetLastStateUnreachable() << "\n";
225 DumpDowntimes(fp, hc, CompatTypeHost);
226 DumpComments(fp, hc, CompatTypeHost);
230 void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host)
232 String notes = CompatUtility::GetCustomAttributeConfig(host, "notes");
233 String notes_url = CompatUtility::GetCustomAttributeConfig(host, "notes_url");
234 String action_url = CompatUtility::GetCustomAttributeConfig(host, "action_url");
235 String icon_image = CompatUtility::GetCustomAttributeConfig(host, "icon_image");
236 String icon_image_alt = CompatUtility::GetCustomAttributeConfig(host, "icon_image_alt");
237 String statusmap_image = CompatUtility::GetCustomAttributeConfig(host, "statusmap_image");
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"
243 "\t" "address" "\t" << CompatUtility::GetHostAddress(host) << "\n"
244 "\t" "address6" "\t" << CompatUtility::GetHostAddress6(host) << "\n";
245 if (!notes.IsEmpty())
246 fp << "\t" "notes" "\t" << notes << "\n";
247 if (!notes_url.IsEmpty())
248 fp << "\t" "notes_url" "\t" << notes_url << "\n";
249 if (!action_url.IsEmpty())
250 fp << "\t" "action_url" "\t" << action_url << "\n";
251 if (!icon_image.IsEmpty())
252 fp << "\t" "icon_image" "\t" << icon_image << "\n";
253 if (!icon_image_alt.IsEmpty())
254 fp << "\t" "icon_image_alt" "\t" << icon_image_alt << "\n";
255 if (!statusmap_image.IsEmpty())
256 fp << "\t" "statusmap_image" "\t" << statusmap_image << "\n";
258 std::set<Host::Ptr> parents = host->GetParentHosts();
260 if (!parents.empty()) {
261 fp << "\t" "parents" "\t";
262 DumpNameList(fp, parents);
266 Service::Ptr hc = host->GetCheckService();
268 ObjectLock olock(hc);
270 fp << "\t" "check_interval" "\t" << CompatUtility::GetServiceCheckInterval(hc) << "\n"
271 "\t" "retry_interval" "\t" << CompatUtility::GetServiceRetryInterval(hc) << "\n"
272 "\t" "max_check_attempts" "\t" << hc->GetMaxCheckAttempts() << "\n"
273 "\t" "active_checks_enabled" "\t" << CompatUtility::GetServiceActiveChecksEnabled(hc) << "\n"
274 "\t" "passive_checks_enabled" "\t" << CompatUtility::GetServicePassiveChecksEnabled(hc) << "\n"
275 "\t" "notifications_enabled" "\t" << CompatUtility::GetServiceNotificationsEnabled(hc) << "\n"
276 "\t" "notification_options" "\t" << "d,u,r" << "\n"
277 "\t" "notification_interval" "\t" << CompatUtility::GetServiceNotificationNotificationInterval(hc) << "\n"
278 "\t" "event_handler_enabled" "\t" << CompatUtility::GetServiceEventHandlerEnabled(hc) << "\n";
280 CheckCommand::Ptr checkcommand = hc->GetCheckCommand();
282 fp << "\t" "check_command" "\t" "check_" << checkcommand->GetName() << "\n";
284 EventCommand::Ptr eventcommand = hc->GetEventCommand();
286 fp << "\t" "event_handler" "\t" "event_" << eventcommand->GetName() << "\n";
288 fp << "\t" "check_period" "\t" << CompatUtility::GetServiceCheckPeriod(hc) << "\n";
290 fp << "\t" "contacts" "\t";
291 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(hc));
294 fp << "\t" "contact_groups" "\t";
295 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(hc));
298 fp << "\t" << "initial_state" "\t" "o" "\n"
299 "\t" "low_flap_threshold" "\t" << hc->GetFlappingThreshold() << "\n"
300 "\t" "high_flap_threshold" "\t" << hc->GetFlappingThreshold() << "\n"
301 "\t" "process_perf_data" "\t" << CompatUtility::GetServiceProcessPerformanceData(hc) << "\n"
302 "\t" "check_freshness" "\t" "1" "\n";
305 fp << "\t" << "check_interval" "\t" "60" "\n"
306 "\t" "retry_interval" "\t" "60" "\n"
307 "\t" "max_check_attempts" "\t" "1" "\n"
308 "\t" "active_checks_enabled" "\t" "0" << "\n"
309 "\t" "passive_checks_enabled" "\t" "0" "\n"
310 "\t" "notifications_enabled" "\t" "0" "\n";
314 fp << "\t" "host_groups" "\t";
317 Array::Ptr groups = host->GetGroups();
320 ObjectLock olock(groups);
322 BOOST_FOREACH(const String& name, groups) {
323 HostGroup::Ptr hg = HostGroup::GetByName(name);
338 DumpCustomAttributes(fp, host);
344 void StatusDataWriter::DumpServiceStatusAttrs(std::ostream& fp, const Service::Ptr& service, CompatObjectType type)
346 CheckResult::Ptr cr = service->GetLastCheckResult();
348 fp << "\t" << "check_command=check_" << CompatUtility::GetServiceCheckCommand(service) << "\n"
349 "\t" "event_handler=event_" << CompatUtility::GetServiceEventHandler(service) << "\n"
350 "\t" "check_period=" << CompatUtility::GetServiceCheckPeriod(service) << "\n"
351 "\t" "check_interval=" << CompatUtility::GetServiceCheckInterval(service) << "\n"
352 "\t" "retry_interval=" << CompatUtility::GetServiceRetryInterval(service) << "\n"
353 "\t" "has_been_checked=" << CompatUtility::GetServiceHasBeenChecked(service) << "\n"
354 "\t" "should_be_scheduled=" << CompatUtility::GetServiceShouldBeScheduled(service) << "\n";
357 fp << "\t" << "check_execution_time=" << Convert::ToString(Service::CalculateExecutionTime(cr)) << "\n"
358 "\t" "check_latency=" << Convert::ToString(Service::CalculateLatency(cr)) << "\n";
361 if (type == CompatTypeHost && service->IsHostCheck()) {
362 fp << "\t" << "current_state=" << service->GetHost()->GetState() << "\n";
364 fp << "\t" << "current_state=" << CompatUtility::GetServiceCurrentState(service) << "\n";
367 fp << "\t" "state_type=" << service->GetStateType() << "\n"
368 "\t" "plugin_output=" << CompatUtility::GetCheckResultOutput(cr) << "\n"
369 "\t" "long_plugin_output=" << CompatUtility::GetCheckResultLongOutput(cr) << "\n"
370 "\t" "performance_data=" << CompatUtility::GetCheckResultPerfdata(cr) << "\n";
373 fp << "\t" << "check_source=" << cr->GetCheckSource() << "\n"
374 "\t" "last_check=" << static_cast<long>(cr->GetScheduleEnd()) << "\n";
377 fp << "\t" << "next_check=" << static_cast<long>(service->GetNextCheck()) << "\n"
378 "\t" "current_attempt=" << service->GetCheckAttempt() << "\n"
379 "\t" "max_attempts=" << service->GetMaxCheckAttempts() << "\n"
380 "\t" "last_state_change=" << static_cast<long>(service->GetLastStateChange()) << "\n"
381 "\t" "last_hard_state_change=" << static_cast<long>(service->GetLastHardStateChange()) << "\n"
382 "\t" "last_time_ok=" << static_cast<int>(service->GetLastStateOK()) << "\n"
383 "\t" "last_time_warn=" << static_cast<int>(service->GetLastStateWarning()) << "\n"
384 "\t" "last_time_critical=" << static_cast<int>(service->GetLastStateCritical()) << "\n"
385 "\t" "last_time_unknown=" << static_cast<int>(service->GetLastStateUnknown()) << "\n"
386 "\t" "last_update=" << static_cast<long>(time(NULL)) << "\n"
387 "\t" "notifications_enabled=" << CompatUtility::GetServiceNotificationsEnabled(service) << "\n"
388 "\t" "active_checks_enabled=" << CompatUtility::GetServiceActiveChecksEnabled(service) << "\n"
389 "\t" "passive_checks_enabled=" << CompatUtility::GetServicePassiveChecksEnabled(service) << "\n"
390 "\t" "flap_detection_enabled=" << CompatUtility::GetServiceFlapDetectionEnabled(service) << "\n"
391 "\t" "is_flapping=" << CompatUtility::GetServiceIsFlapping(service) << "\n"
392 "\t" "percent_state_change=" << CompatUtility::GetServicePercentStateChange(service) << "\n"
393 "\t" "problem_has_been_acknowledged=" << CompatUtility::GetServiceProblemHasBeenAcknowledged(service) << "\n"
394 "\t" "acknowledgement_type=" << CompatUtility::GetServiceAcknowledgementType(service) << "\n"
395 "\t" "acknowledgement_end_time=" << service->GetAcknowledgementExpiry() << "\n"
396 "\t" "scheduled_downtime_depth=" << service->GetDowntimeDepth() << "\n"
397 "\t" "last_notification=" << CompatUtility::GetServiceNotificationLastNotification(service) << "\n"
398 "\t" "next_notification=" << CompatUtility::GetServiceNotificationNextNotification(service) << "\n"
399 "\t" "current_notification_number=" << CompatUtility::GetServiceNotificationNotificationNumber(service) << "\n"
400 "\t" "modified_attributes=" << service->GetModifiedAttributes() << "\n";
403 void StatusDataWriter::DumpServiceStatus(std::ostream& fp, const Service::Ptr& service)
405 Host::Ptr host = service->GetHost();
407 fp << "servicestatus {" "\n"
408 "\t" "host_name=" << host->GetName() << "\n"
409 "\t" "service_description=" << service->GetShortName() << "\n";
412 ObjectLock olock(service);
413 DumpServiceStatusAttrs(fp, service, CompatTypeService);
419 DumpDowntimes(fp, service, CompatTypeService);
420 DumpComments(fp, service, CompatTypeService);
423 void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& service)
425 Host::Ptr host = service->GetHost();
428 ObjectLock olock(service);
430 fp << "define service {" "\n"
431 "\t" "host_name" "\t" << host->GetName() << "\n"
432 "\t" "service_description" "\t" << service->GetShortName() << "\n"
433 "\t" "display_name" "\t" << service->GetDisplayName() << "\n"
434 "\t" "check_period" "\t" << CompatUtility::GetServiceCheckPeriod(service) << "\n"
435 "\t" "check_interval" "\t" << CompatUtility::GetServiceCheckInterval(service) << "\n"
436 "\t" "retry_interval" "\t" << CompatUtility::GetServiceRetryInterval(service) << "\n"
437 "\t" "max_check_attempts" "\t" << service->GetMaxCheckAttempts() << "\n"
438 "\t" "active_checks_enabled" "\t" << CompatUtility::GetServiceActiveChecksEnabled(service) << "\n"
439 "\t" "passive_checks_enabled" "\t" << CompatUtility::GetServicePassiveChecksEnabled(service) << "\n"
440 "\t" "flap_detection_enabled" "\t" << CompatUtility::GetServiceFlapDetectionEnabled(service) << "\n"
441 "\t" "is_volatile" "\t" << CompatUtility::GetServiceIsVolatile(service) << "\n"
442 "\t" "notifications_enabled" "\t" << CompatUtility::GetServiceNotificationsEnabled(service) << "\n"
443 "\t" "notification_options" "\t" << CompatUtility::GetServiceNotificationNotificationOptions(service) << "\n"
444 "\t" "notification_interval" "\t" << CompatUtility::GetServiceNotificationNotificationInterval(service) << "\n"
445 "\t" "notification_period" "\t" << CompatUtility::GetServiceNotificationNotificationPeriod(service) << "\n"
446 "\t" "event_handler_enabled" "\t" << CompatUtility::GetServiceEventHandlerEnabled(service) << "\n";
448 CheckCommand::Ptr checkcommand = service->GetCheckCommand();
450 fp << "\t" "check_command" "\t" "check_" << checkcommand->GetName() << "\n";
452 EventCommand::Ptr eventcommand = service->GetEventCommand();
454 fp << "\t" "event_handler" "\t" "event_" << eventcommand->GetName() << "\n";
456 fp << "\t" "contacts" "\t";
457 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(service));
460 fp << "\t" "contact_groups" "\t";
461 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(service));
464 String notes = CompatUtility::GetCustomAttributeConfig(service, "notes");
465 String notes_url = CompatUtility::GetCustomAttributeConfig(service, "notes_url");
466 String action_url = CompatUtility::GetCustomAttributeConfig(service, "action_url");
467 String icon_image = CompatUtility::GetCustomAttributeConfig(service, "icon_image");
468 String icon_image_alt = CompatUtility::GetCustomAttributeConfig(service, "icon_image_alt");
470 fp << "\t" "initial_state" "\t" "o" "\n"
471 "\t" "low_flap_threshold" "\t" << service->GetFlappingThreshold() << "\n"
472 "\t" "high_flap_threshold" "\t" << service->GetFlappingThreshold() << "\n"
473 "\t" "process_perf_data" "\t" << CompatUtility::GetServiceProcessPerformanceData(service) << "\n"
474 "\t" "check_freshness" << "\t" "1" "\n";
475 if (!notes.IsEmpty())
476 fp << "\t" "notes" "\t" << notes << "\n";
477 if (!notes_url.IsEmpty())
478 fp << "\t" "notes_url" "\t" << notes_url << "\n";
479 if (!action_url.IsEmpty())
480 fp << "\t" "action_url" "\t" << action_url << "\n";
481 if (!icon_image.IsEmpty())
482 fp << "\t" "icon_image" "\t" << icon_image << "\n";
483 if (!icon_image_alt.IsEmpty())
484 fp << "\t" "icon_image_alt" "\t" << icon_image_alt << "\n";
487 fp << "\t" "service_groups" "\t";
490 Array::Ptr groups = service->GetGroups();
493 ObjectLock olock(groups);
495 BOOST_FOREACH(const String& name, groups) {
496 ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);
511 DumpCustomAttributes(fp, service);
517 void StatusDataWriter::DumpCustomAttributes(std::ostream& fp, const DynamicObject::Ptr& object)
519 Dictionary::Ptr custom = object->GetCustom();
524 ObjectLock olock(custom);
525 BOOST_FOREACH(const Dictionary::Pair& kv, custom) {
526 if (!kv.first.IsEmpty()) {
529 if (kv.first != "notes" && kv.first != "action_url" && kv.first != "notes_url" &&
530 kv.first != "icon_image" && kv.first != "icon_image_alt" && kv.first != "statusmap_image" && kv.first != "2d_coords")
533 fp << kv.first << "\t" << kv.second << "\n";
538 void StatusDataWriter::UpdateObjectsCache(void)
540 CONTEXT("Writing objects.cache file");
542 String objectspath = GetObjectsPath();
543 String objectspathtmp = objectspath + ".tmp";
545 std::ofstream objectfp;
546 objectfp.open(objectspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
548 objectfp << std::fixed;
550 objectfp << "# Icinga objects cache file" "\n"
551 "# This file is auto-generated. Do not modify this file." "\n"
554 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
555 std::ostringstream tempobjectfp;
556 tempobjectfp << std::fixed;
557 DumpHostObject(tempobjectfp, host);
558 objectfp << tempobjectfp.str();
560 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
561 std::ostringstream tempobjectfp;
562 tempobjectfp << std::fixed;
563 DumpServiceObject(tempobjectfp, service);
564 objectfp << tempobjectfp.str();
568 BOOST_FOREACH(const HostGroup::Ptr& hg, DynamicType::GetObjects<HostGroup>()) {
569 std::ostringstream tempobjectfp;
570 tempobjectfp << std::fixed;
572 tempobjectfp << "define hostgroup {" "\n"
573 "\t" "hostgroup_name" "\t" << hg->GetName() << "\n";
575 DumpCustomAttributes(tempobjectfp, hg);
577 tempobjectfp << "\t" "members" "\t";
578 DumpNameList(tempobjectfp, hg->GetMembers());
582 objectfp << tempobjectfp.str();
585 BOOST_FOREACH(const ServiceGroup::Ptr& sg, DynamicType::GetObjects<ServiceGroup>()) {
586 std::ostringstream tempobjectfp;
587 tempobjectfp << std::fixed;
589 tempobjectfp << "define servicegroup {" "\n"
590 "\t" "servicegroup_name" "\t" << sg->GetName() << "\n";
592 DumpCustomAttributes(tempobjectfp, sg);
594 tempobjectfp << "\t" "members" "\t";
596 std::vector<String> sglist;
597 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
598 Host::Ptr host = service->GetHost();
600 sglist.push_back(host->GetName());
601 sglist.push_back(service->GetShortName());
604 DumpStringList(tempobjectfp, sglist);
609 objectfp << tempobjectfp.str();
612 BOOST_FOREACH(const User::Ptr& user, DynamicType::GetObjects<User>()) {
613 std::ostringstream tempobjectfp;
614 tempobjectfp << std::fixed;
616 tempobjectfp << "define contact {" "\n"
617 "\t" "contact_name" "\t" << user->GetName() << "\n"
618 "\t" "alias" "\t" << user->GetDisplayName() << "\n"
619 "\t" "service_notification_options" "\t" "w,u,c,r,f,s" "\n"
620 "\t" "host_notification_options""\t" "d,u,r,f,s" "\n"
621 "\t" "host_notifications_enabled" "\t" "1" "\n"
622 "\t" "service_notifications_enabled" "\t" "1" "\n"
626 objectfp << tempobjectfp.str();
629 BOOST_FOREACH(const UserGroup::Ptr& ug, DynamicType::GetObjects<UserGroup>()) {
630 std::ostringstream tempobjectfp;
631 tempobjectfp << std::fixed;
633 tempobjectfp << "define contactgroup {" "\n"
634 "\t" "contactgroup_name" "\t" << ug->GetName() << "\n"
635 "\t" "alias" "\t" << ug->GetDisplayName() << "\n";
637 tempobjectfp << "\t" "members" "\t";
638 DumpNameList(tempobjectfp, ug->GetMembers());
642 objectfp << tempobjectfp.str();
645 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<CheckCommand>()) {
646 DumpCommand(objectfp, command);
649 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<NotificationCommand>()) {
650 DumpCommand(objectfp, command);
653 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<EventCommand>()) {
654 DumpCommand(objectfp, command);
657 BOOST_FOREACH(const TimePeriod::Ptr& tp, DynamicType::GetObjects<TimePeriod>()) {
658 DumpTimePeriod(objectfp, tp);
661 BOOST_FOREACH(const Dependency::Ptr& dep, DynamicType::GetObjects<Dependency>()) {
662 Service::Ptr parent_service = dep->GetParentService();
667 Service::Ptr child_service = dep->GetChildService();
672 objectfp << "define servicedependency {" "\n"
673 "\t" "dependent_host_name" "\t" << child_service->GetHost()->GetName() << "\n"
674 "\t" "dependent_service_description" "\t" << child_service->GetShortName() << "\n"
675 "\t" "host_name" "\t" << parent_service->GetHost()->GetName() << "\n"
676 "\t" "service_description" "\t" << parent_service->GetShortName() << "\n"
677 "\t" "execution_failure_criteria" "\t" "n" "\n"
678 "\t" "notification_failure_criteria" "\t" "w,u,c" "\n"
686 _unlink(objectspath.CStr());
689 if (rename(objectspathtmp.CStr(), objectspath.CStr()) < 0) {
690 BOOST_THROW_EXCEPTION(posix_error()
691 << boost::errinfo_api_function("rename")
692 << boost::errinfo_errno(errno)
693 << boost::errinfo_file_name(objectspathtmp));
698 * Periodically writes the status.dat and objects.cache files.
700 void StatusDataWriter::StatusTimerHandler(void)
702 Log(LogInformation, "compat", "Writing status.dat file");
704 String statuspath = GetStatusPath();
705 String statuspathtmp = statuspath + ".tmp"; /* XXX make this a global definition */
707 std::ofstream statusfp;
708 statusfp.open(statuspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
710 statusfp << std::fixed;
712 statusfp << "# Icinga status file" "\n"
713 "# This file is auto-generated. Do not modify this file." "\n"
716 statusfp << "info {" "\n"
717 "\t" "created=" << Utility::GetTime() << "\n"
718 "\t" "version=" << Application::GetVersion() << "\n"
722 statusfp << "programstatus {" "\n"
723 "\t" "icinga_pid=" << Utility::GetPid() << "\n"
724 "\t" "daemon_mode=1" "\n"
725 "\t" "program_start=" << static_cast<long>(Application::GetStartTime()) << "\n"
726 "\t" "active_service_checks_enabled=" << (IcingaApplication::GetInstance()->GetEnableChecks() ? 1 : 0) << "\n"
727 "\t" "passive_service_checks_enabled=1" "\n"
728 "\t" "active_host_checks_enabled=1" "\n"
729 "\t" "passive_host_checks_enabled=1" "\n"
730 "\t" "check_service_freshness=1" "\n"
731 "\t" "check_host_freshness=1" "\n"
732 "\t" "enable_notifications=" << (IcingaApplication::GetInstance()->GetEnableNotifications() ? 1 : 0) << "\n"
733 "\t" "enable_flap_detection=" << (IcingaApplication::GetInstance()->GetEnableFlapping() ? 1 : 0) << "\n"
734 "\t" "enable_failure_prediction=0" "\n"
735 "\t" "process_performance_data=" << (IcingaApplication::GetInstance()->GetEnablePerfdata() ? 1 : 0) << "\n"
736 "\t" "active_scheduled_service_check_stats=" << CIB::GetActiveChecksStatistics(60) << "," << CIB::GetActiveChecksStatistics(5 * 60) << "," << CIB::GetActiveChecksStatistics(15 * 60) << "\n"
737 "\t" "passive_service_check_stats=" << CIB::GetPassiveChecksStatistics(60) << "," << CIB::GetPassiveChecksStatistics(5 * 60) << "," << CIB::GetPassiveChecksStatistics(15 * 60) << "\n"
738 "\t" "next_downtime_id=" << Service::GetNextDowntimeID() << "\n"
739 "\t" "next_comment_id=" << Service::GetNextCommentID() << "\n"
743 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
744 std::ostringstream tempstatusfp;
745 tempstatusfp << std::fixed;
746 DumpHostStatus(tempstatusfp, host);
747 statusfp << tempstatusfp.str();
749 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
750 std::ostringstream tempstatusfp;
751 tempstatusfp << std::fixed;
752 DumpServiceStatus(tempstatusfp, service);
753 statusfp << tempstatusfp.str();
760 _unlink(statuspath.CStr());
763 if (rename(statuspathtmp.CStr(), statuspath.CStr()) < 0) {
764 BOOST_THROW_EXCEPTION(posix_error()
765 << boost::errinfo_api_function("rename")
766 << boost::errinfo_errno(errno)
767 << boost::errinfo_file_name(statuspathtmp));
770 Log(LogInformation, "compat", "Finished writing status.dat file");