1 /******************************************************************************
3 * Copyright (C) 2012-present 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 "base/context.h"
37 #include "base/statsfunction.h"
38 #include <boost/foreach.hpp>
39 #include <boost/tuple/tuple.hpp>
40 #include <boost/algorithm/string/replace.hpp>
43 using namespace icinga;
45 REGISTER_TYPE(StatusDataWriter);
47 REGISTER_STATSFUNCTION(StatusDataWriterStats, &StatusDataWriter::StatsFunc);
49 Value StatusDataWriter::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata)
51 Dictionary::Ptr nodes = make_shared<Dictionary>();
53 BOOST_FOREACH(const StatusDataWriter::Ptr& statusdatawriter, DynamicType::GetObjects<StatusDataWriter>()) {
54 nodes->Set(statusdatawriter->GetName(), 1); //add more stats
57 status->Set("statusdatawriter", nodes);
63 * Hint: The reason why we're using "\n" rather than std::endl is because
64 * std::endl also _flushes_ the output stream which severely degrades
65 * performance (see http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html).
69 * Starts the component.
71 void StatusDataWriter::Start(void)
73 DynamicObject::Start();
75 m_StatusTimer = make_shared<Timer>();
76 m_StatusTimer->SetInterval(GetUpdateInterval());
77 m_StatusTimer->OnTimerExpired.connect(boost::bind(&StatusDataWriter::StatusTimerHandler, this));
78 m_StatusTimer->Start();
79 m_StatusTimer->Reschedule(0);
81 Utility::QueueAsyncCallback(boost::bind(&StatusDataWriter::UpdateObjectsCache, this));
84 void StatusDataWriter::DumpComments(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
87 Dictionary::Ptr comments = owner->GetComments();
89 Host::Ptr host = owner->GetHost();
91 ObjectLock olock(comments);
93 BOOST_FOREACH(const Dictionary::Pair& kv, comments) {
94 Comment::Ptr comment = kv.second;
96 if (comment->IsExpired())
99 if (type == CompatTypeHost)
100 fp << "hostcomment {" << "\n";
102 fp << "servicecomment {" << "\n"
103 << "\t" << "service_description=" << owner->GetShortName() << "\n";
105 fp << "\t" "host_name=" << host->GetName() << "\n"
106 "\t" "comment_id=" << comment->GetLegacyId() << "\n"
107 "\t" "entry_time=" << comment->GetEntryTime() << "\n"
108 "\t" "entry_type=" << comment->GetEntryType() << "\n"
109 "\t" "persistent=" "1" "\n"
110 "\t" "author=" << comment->GetAuthor() << "\n"
111 "\t" "comment_data=" << comment->GetText() << "\n"
112 "\t" "expires=" << (comment->GetExpireTime() != 0 ? 1 : 0) << "\n"
113 "\t" "expire_time=" << comment->GetExpireTime() << "\n"
119 void StatusDataWriter::DumpTimePeriod(std::ostream& fp, const TimePeriod::Ptr& tp)
121 fp << "define timeperiod {" "\n"
122 "\t" "timeperiod_name" "\t" << tp->GetName() << "\n"
123 "\t" "alias" "\t" << tp->GetName() << "\n";
125 Dictionary::Ptr ranges = tp->GetRanges();
128 ObjectLock olock(ranges);
129 BOOST_FOREACH(const Dictionary::Pair& kv, ranges) {
130 fp << "\t" << kv.first << "\t" << kv.second << "\n";
138 void StatusDataWriter::DumpCommand(std::ostream& fp, const Command::Ptr& command)
140 fp << "define command {" "\n"
141 "\t" "command_name\t";
144 if (command->GetType() == DynamicType::GetByName("CheckCommand"))
146 else if (command->GetType() == DynamicType::GetByName("NotificationCommand"))
147 fp << "notification_";
148 else if (command->GetType() == DynamicType::GetByName("EventCommand"))
151 fp << command->GetName() << "\n";
153 fp << "\t" "command_line" "\t" << CompatUtility::GetCommandLine(command);
155 fp << "\n" "\t" "}" "\n"
159 void StatusDataWriter::DumpDowntimes(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
161 Host::Ptr host = owner->GetHost();
163 Dictionary::Ptr downtimes = owner->GetDowntimes();
165 ObjectLock olock(downtimes);
167 BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
168 Downtime::Ptr downtime = kv.second;
170 if (downtime->IsExpired())
173 if (type == CompatTypeHost)
174 fp << "hostdowntime {" "\n";
176 fp << "servicedowntime {" << "\n"
177 "\t" "service_description=" << owner->GetShortName() << "\n";
179 Downtime::Ptr triggeredByObj = Service::GetDowntimeByID(downtime->GetTriggeredBy());
180 int triggeredByLegacy = 0;
182 triggeredByLegacy = triggeredByObj->GetLegacyId();
184 fp << "\t" << "host_name=" << host->GetName() << "\n"
185 "\t" "downtime_id=" << downtime->GetLegacyId() << "\n"
186 "\t" "entry_time=" << downtime->GetEntryTime() << "\n"
187 "\t" "start_time=" << downtime->GetStartTime() << "\n"
188 "\t" "end_time=" << downtime->GetEndTime() << "\n"
189 "\t" "triggered_by=" << triggeredByLegacy << "\n"
190 "\t" "fixed=" << static_cast<long>(downtime->GetFixed()) << "\n"
191 "\t" "duration=" << static_cast<long>(downtime->GetDuration()) << "\n"
192 "\t" "is_in_effect=" << (downtime->IsActive() ? 1 : 0) << "\n"
193 "\t" "author=" << downtime->GetAuthor() << "\n"
194 "\t" "comment=" << downtime->GetComment() << "\n"
195 "\t" "trigger_time=" << downtime->GetTriggerTime() << "\n"
201 void StatusDataWriter::DumpHostStatus(std::ostream& fp, const Host::Ptr& host)
203 fp << "hoststatus {" << "\n"
204 << "\t" << "host_name=" << host->GetName() << "\n";
206 Service::Ptr hc = host->GetCheckService();
207 ObjectLock olock(hc);
210 DumpServiceStatusAttrs(fp, hc, CompatTypeHost);
212 /* ugly but cgis parse only that */
213 fp << "\t" "last_time_up=" << host->GetLastStateUp() << "\n"
214 "\t" "last_time_down=" << host->GetLastStateDown() << "\n"
215 "\t" "last_time_unreachable=" << host->GetLastStateUnreachable() << "\n";
221 DumpDowntimes(fp, hc, CompatTypeHost);
222 DumpComments(fp, hc, CompatTypeHost);
226 void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host)
228 String notes = CompatUtility::GetCustomAttributeConfig(host, "notes");
229 String notes_url = CompatUtility::GetCustomAttributeConfig(host, "notes_url");
230 String action_url = CompatUtility::GetCustomAttributeConfig(host, "action_url");
231 String icon_image = CompatUtility::GetCustomAttributeConfig(host, "icon_image");
232 String icon_image_alt = CompatUtility::GetCustomAttributeConfig(host, "icon_image_alt");
233 String statusmap_image = CompatUtility::GetCustomAttributeConfig(host, "statusmap_image");
235 fp << "define host {" "\n"
236 "\t" "host_name" "\t" << host->GetName() << "\n"
237 "\t" "display_name" "\t" << host->GetDisplayName() << "\n"
238 "\t" "alias" "\t" << host->GetDisplayName() << "\n"
239 "\t" "address" "\t" << CompatUtility::GetHostAddress(host) << "\n"
240 "\t" "address6" "\t" << CompatUtility::GetHostAddress6(host) << "\n";
241 if (!notes.IsEmpty())
242 fp << "\t" "notes" "\t" << notes << "\n";
243 if (!notes_url.IsEmpty())
244 fp << "\t" "notes_url" "\t" << notes_url << "\n";
245 if (!action_url.IsEmpty())
246 fp << "\t" "action_url" "\t" << action_url << "\n";
247 if (!icon_image.IsEmpty())
248 fp << "\t" "icon_image" "\t" << icon_image << "\n";
249 if (!icon_image_alt.IsEmpty())
250 fp << "\t" "icon_image_alt" "\t" << icon_image_alt << "\n";
251 if (!statusmap_image.IsEmpty())
252 fp << "\t" "statusmap_image" "\t" << statusmap_image << "\n";
254 std::set<Host::Ptr> parents = host->GetParentHosts();
256 if (!parents.empty()) {
257 fp << "\t" "parents" "\t";
258 DumpNameList(fp, parents);
262 Service::Ptr hc = host->GetCheckService();
264 ObjectLock olock(hc);
266 fp << "\t" "check_interval" "\t" << CompatUtility::GetServiceCheckInterval(hc) << "\n"
267 "\t" "retry_interval" "\t" << CompatUtility::GetServiceRetryInterval(hc) << "\n"
268 "\t" "max_check_attempts" "\t" << hc->GetMaxCheckAttempts() << "\n"
269 "\t" "active_checks_enabled" "\t" << CompatUtility::GetServiceActiveChecksEnabled(hc) << "\n"
270 "\t" "passive_checks_enabled" "\t" << CompatUtility::GetServicePassiveChecksEnabled(hc) << "\n"
271 "\t" "notifications_enabled" "\t" << CompatUtility::GetServiceNotificationsEnabled(hc) << "\n"
272 "\t" "notification_options" "\t" << "d,u,r" << "\n"
273 "\t" "notification_interval" "\t" << CompatUtility::GetServiceNotificationNotificationInterval(hc) << "\n"
274 "\t" "event_handler_enabled" "\t" << CompatUtility::GetServiceEventHandlerEnabled(hc) << "\n";
276 CheckCommand::Ptr checkcommand = hc->GetCheckCommand();
278 fp << "\t" "check_command" "\t" "check_" << checkcommand->GetName() << "\n";
280 EventCommand::Ptr eventcommand = hc->GetEventCommand();
282 fp << "\t" "event_handler" "\t" "event_" << eventcommand->GetName() << "\n";
284 fp << "\t" "check_period" "\t" << CompatUtility::GetServiceCheckPeriod(hc) << "\n";
286 fp << "\t" "contacts" "\t";
287 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(hc));
290 fp << "\t" "contact_groups" "\t";
291 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(hc));
294 fp << "\t" << "initial_state" "\t" "o" "\n"
295 "\t" "low_flap_threshold" "\t" << hc->GetFlappingThreshold() << "\n"
296 "\t" "high_flap_threshold" "\t" << hc->GetFlappingThreshold() << "\n"
297 "\t" "process_perf_data" "\t" "1" "\n"
298 "\t" "check_freshness" "\t" "1" "\n";
301 fp << "\t" << "check_interval" "\t" "60" "\n"
302 "\t" "retry_interval" "\t" "60" "\n"
303 "\t" "max_check_attempts" "\t" "1" "\n"
304 "\t" "active_checks_enabled" "\t" "0" << "\n"
305 "\t" "passive_checks_enabled" "\t" "0" "\n"
306 "\t" "notifications_enabled" "\t" "0" "\n";
310 fp << "\t" "host_groups" "\t";
313 Array::Ptr groups = host->GetGroups();
316 ObjectLock olock(groups);
318 BOOST_FOREACH(const String& name, groups) {
319 HostGroup::Ptr hg = HostGroup::GetByName(name);
334 DumpCustomAttributes(fp, host);
340 void StatusDataWriter::DumpServiceStatusAttrs(std::ostream& fp, const Service::Ptr& service, CompatObjectType type)
342 CheckResult::Ptr cr = service->GetLastCheckResult();
344 fp << "\t" << "check_command=check_" << CompatUtility::GetServiceCheckCommand(service) << "\n"
345 "\t" "event_handler=event_" << CompatUtility::GetServiceEventHandler(service) << "\n"
346 "\t" "check_period=" << CompatUtility::GetServiceCheckPeriod(service) << "\n"
347 "\t" "check_interval=" << CompatUtility::GetServiceCheckInterval(service) << "\n"
348 "\t" "retry_interval=" << CompatUtility::GetServiceRetryInterval(service) << "\n"
349 "\t" "has_been_checked=" << CompatUtility::GetServiceHasBeenChecked(service) << "\n"
350 "\t" "should_be_scheduled=" << CompatUtility::GetServiceShouldBeScheduled(service) << "\n";
353 fp << "\t" << "check_execution_time=" << Convert::ToString(Service::CalculateExecutionTime(cr)) << "\n"
354 "\t" "check_latency=" << Convert::ToString(Service::CalculateLatency(cr)) << "\n";
357 if (type == CompatTypeHost && service->IsHostCheck()) {
358 fp << "\t" << "current_state=" << service->GetHost()->GetState() << "\n";
360 fp << "\t" << "current_state=" << CompatUtility::GetServiceCurrentState(service) << "\n";
363 fp << "\t" "state_type=" << service->GetStateType() << "\n"
364 "\t" "plugin_output=" << CompatUtility::GetCheckResultOutput(cr) << "\n"
365 "\t" "long_plugin_output=" << CompatUtility::GetCheckResultLongOutput(cr) << "\n"
366 "\t" "performance_data=" << CompatUtility::GetCheckResultPerfdata(cr) << "\n";
369 fp << "\t" << "check_source=" << cr->GetCheckSource() << "\n"
370 "\t" "last_check=" << static_cast<long>(cr->GetScheduleEnd()) << "\n";
373 fp << "\t" << "next_check=" << static_cast<long>(service->GetNextCheck()) << "\n"
374 "\t" "current_attempt=" << service->GetCheckAttempt() << "\n"
375 "\t" "max_attempts=" << service->GetMaxCheckAttempts() << "\n"
376 "\t" "last_state_change=" << static_cast<long>(service->GetLastStateChange()) << "\n"
377 "\t" "last_hard_state_change=" << static_cast<long>(service->GetLastHardStateChange()) << "\n"
378 "\t" "last_time_ok=" << static_cast<int>(service->GetLastStateOK()) << "\n"
379 "\t" "last_time_warn=" << static_cast<int>(service->GetLastStateWarning()) << "\n"
380 "\t" "last_time_critical=" << static_cast<int>(service->GetLastStateCritical()) << "\n"
381 "\t" "last_time_unknown=" << static_cast<int>(service->GetLastStateUnknown()) << "\n"
382 "\t" "last_update=" << static_cast<long>(time(NULL)) << "\n"
383 "\t" "notifications_enabled=" << CompatUtility::GetServiceNotificationsEnabled(service) << "\n"
384 "\t" "active_checks_enabled=" << CompatUtility::GetServiceActiveChecksEnabled(service) << "\n"
385 "\t" "passive_checks_enabled=" << CompatUtility::GetServicePassiveChecksEnabled(service) << "\n"
386 "\t" "flap_detection_enabled=" << CompatUtility::GetServiceFlapDetectionEnabled(service) << "\n"
387 "\t" "is_flapping=" << CompatUtility::GetServiceIsFlapping(service) << "\n"
388 "\t" "percent_state_change=" << CompatUtility::GetServicePercentStateChange(service) << "\n"
389 "\t" "problem_has_been_acknowledged=" << CompatUtility::GetServiceProblemHasBeenAcknowledged(service) << "\n"
390 "\t" "acknowledgement_type=" << CompatUtility::GetServiceAcknowledgementType(service) << "\n"
391 "\t" "acknowledgement_end_time=" << service->GetAcknowledgementExpiry() << "\n"
392 "\t" "scheduled_downtime_depth=" << service->GetDowntimeDepth() << "\n"
393 "\t" "last_notification=" << CompatUtility::GetServiceNotificationLastNotification(service) << "\n"
394 "\t" "next_notification=" << CompatUtility::GetServiceNotificationNextNotification(service) << "\n"
395 "\t" "current_notification_number=" << CompatUtility::GetServiceNotificationNotificationNumber(service) << "\n"
396 "\t" "modified_attributes=" << service->GetModifiedAttributes() << "\n";
399 void StatusDataWriter::DumpServiceStatus(std::ostream& fp, const Service::Ptr& service)
401 Host::Ptr host = service->GetHost();
403 fp << "servicestatus {" "\n"
404 "\t" "host_name=" << host->GetName() << "\n"
405 "\t" "service_description=" << service->GetShortName() << "\n";
408 ObjectLock olock(service);
409 DumpServiceStatusAttrs(fp, service, CompatTypeService);
415 DumpDowntimes(fp, service, CompatTypeService);
416 DumpComments(fp, service, CompatTypeService);
419 void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& service)
421 Host::Ptr host = service->GetHost();
424 ObjectLock olock(service);
426 fp << "define service {" "\n"
427 "\t" "host_name" "\t" << host->GetName() << "\n"
428 "\t" "service_description" "\t" << service->GetShortName() << "\n"
429 "\t" "display_name" "\t" << service->GetDisplayName() << "\n"
430 "\t" "check_period" "\t" << CompatUtility::GetServiceCheckPeriod(service) << "\n"
431 "\t" "check_interval" "\t" << CompatUtility::GetServiceCheckInterval(service) << "\n"
432 "\t" "retry_interval" "\t" << CompatUtility::GetServiceRetryInterval(service) << "\n"
433 "\t" "max_check_attempts" "\t" << service->GetMaxCheckAttempts() << "\n"
434 "\t" "active_checks_enabled" "\t" << CompatUtility::GetServiceActiveChecksEnabled(service) << "\n"
435 "\t" "passive_checks_enabled" "\t" << CompatUtility::GetServicePassiveChecksEnabled(service) << "\n"
436 "\t" "flap_detection_enabled" "\t" << CompatUtility::GetServiceFlapDetectionEnabled(service) << "\n"
437 "\t" "is_volatile" "\t" << CompatUtility::GetServiceIsVolatile(service) << "\n"
438 "\t" "notifications_enabled" "\t" << CompatUtility::GetServiceNotificationsEnabled(service) << "\n"
439 "\t" "notification_options" "\t" << CompatUtility::GetServiceNotificationNotificationOptions(service) << "\n"
440 "\t" "notification_interval" "\t" << CompatUtility::GetServiceNotificationNotificationInterval(service) << "\n"
441 "\t" "notification_period" "\t" << CompatUtility::GetServiceNotificationNotificationPeriod(service) << "\n"
442 "\t" "event_handler_enabled" "\t" << CompatUtility::GetServiceEventHandlerEnabled(service) << "\n";
444 CheckCommand::Ptr checkcommand = service->GetCheckCommand();
446 fp << "\t" "check_command" "\t" "check_" << checkcommand->GetName() << "\n";
448 EventCommand::Ptr eventcommand = service->GetEventCommand();
450 fp << "\t" "event_handler" "\t" "event_" << eventcommand->GetName() << "\n";
452 fp << "\t" "contacts" "\t";
453 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(service));
456 fp << "\t" "contact_groups" "\t";
457 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(service));
460 String notes = CompatUtility::GetCustomAttributeConfig(service, "notes");
461 String notes_url = CompatUtility::GetCustomAttributeConfig(service, "notes_url");
462 String action_url = CompatUtility::GetCustomAttributeConfig(service, "action_url");
463 String icon_image = CompatUtility::GetCustomAttributeConfig(service, "icon_image");
464 String icon_image_alt = CompatUtility::GetCustomAttributeConfig(service, "icon_image_alt");
466 fp << "\t" "initial_state" "\t" "o" "\n"
467 "\t" "low_flap_threshold" "\t" << service->GetFlappingThreshold() << "\n"
468 "\t" "high_flap_threshold" "\t" << service->GetFlappingThreshold() << "\n"
469 "\t" "process_perf_data" "\t" "1" "\n"
470 "\t" "check_freshness" << "\t" "1" "\n";
471 if (!notes.IsEmpty())
472 fp << "\t" "notes" "\t" << notes << "\n";
473 if (!notes_url.IsEmpty())
474 fp << "\t" "notes_url" "\t" << notes_url << "\n";
475 if (!action_url.IsEmpty())
476 fp << "\t" "action_url" "\t" << action_url << "\n";
477 if (!icon_image.IsEmpty())
478 fp << "\t" "icon_image" "\t" << icon_image << "\n";
479 if (!icon_image_alt.IsEmpty())
480 fp << "\t" "icon_image_alt" "\t" << icon_image_alt << "\n";
483 fp << "\t" "service_groups" "\t";
486 Array::Ptr groups = service->GetGroups();
489 ObjectLock olock(groups);
491 BOOST_FOREACH(const String& name, groups) {
492 ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);
507 DumpCustomAttributes(fp, service);
512 BOOST_FOREACH(const Service::Ptr& parent, service->GetParentServices()) {
513 Host::Ptr host = service->GetHost();
515 Host::Ptr parent_host = parent->GetHost();
520 fp << "define servicedependency {" "\n"
521 "\t" "dependent_host_name" "\t" << host->GetName() << "\n"
522 "\t" "dependent_service_description" "\t" << service->GetShortName() << "\n"
523 "\t" "host_name" "\t" << parent_host->GetName() << "\n"
524 "\t" "service_description" "\t" << parent->GetShortName() << "\n"
525 "\t" "execution_failure_criteria" "\t" "n" "\n"
526 "\t" "notification_failure_criteria" "\t" "w,u,c" "\n"
532 void StatusDataWriter::DumpCustomAttributes(std::ostream& fp, const DynamicObject::Ptr& object)
534 Dictionary::Ptr custom = object->GetCustom();
539 ObjectLock olock(custom);
540 BOOST_FOREACH(const Dictionary::Pair& kv, custom) {
541 if (!kv.first.IsEmpty()) {
544 if (kv.first != "notes" && kv.first != "action_url" && kv.first != "notes_url" &&
545 kv.first != "icon_image" && kv.first != "icon_image_alt" && kv.first != "statusmap_image" && kv.first != "2d_coords")
548 fp << kv.first << "\t" << kv.second << "\n";
553 void StatusDataWriter::UpdateObjectsCache(void)
555 CONTEXT("Writing objects.cache file");
557 String objectspath = GetObjectsPath();
558 String objectspathtmp = objectspath + ".tmp";
560 std::ofstream objectfp;
561 objectfp.open(objectspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
563 objectfp << std::fixed;
565 objectfp << "# Icinga objects cache file" "\n"
566 "# This file is auto-generated. Do not modify this file." "\n"
569 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
570 std::ostringstream tempobjectfp;
571 tempobjectfp << std::fixed;
572 DumpHostObject(tempobjectfp, host);
573 objectfp << tempobjectfp.str();
576 BOOST_FOREACH(const HostGroup::Ptr& hg, DynamicType::GetObjects<HostGroup>()) {
577 std::ostringstream tempobjectfp;
578 tempobjectfp << std::fixed;
580 tempobjectfp << "define hostgroup {" "\n"
581 "\t" "hostgroup_name" "\t" << hg->GetName() << "\n";
583 DumpCustomAttributes(tempobjectfp, hg);
585 tempobjectfp << "\t" "members" "\t";
586 DumpNameList(tempobjectfp, hg->GetMembers());
590 objectfp << tempobjectfp.str();
593 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
594 std::ostringstream tempobjectfp;
595 tempobjectfp << std::fixed;
596 DumpServiceObject(tempobjectfp, service);
597 objectfp << tempobjectfp.str();
600 BOOST_FOREACH(const ServiceGroup::Ptr& sg, DynamicType::GetObjects<ServiceGroup>()) {
601 std::ostringstream tempobjectfp;
602 tempobjectfp << std::fixed;
604 tempobjectfp << "define servicegroup {" "\n"
605 "\t" "servicegroup_name" "\t" << sg->GetName() << "\n";
607 DumpCustomAttributes(tempobjectfp, sg);
609 tempobjectfp << "\t" "members" "\t";
611 std::vector<String> sglist;
612 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
613 Host::Ptr host = service->GetHost();
615 sglist.push_back(host->GetName());
616 sglist.push_back(service->GetShortName());
619 DumpStringList(tempobjectfp, sglist);
624 objectfp << tempobjectfp.str();
627 BOOST_FOREACH(const User::Ptr& user, DynamicType::GetObjects<User>()) {
628 std::ostringstream tempobjectfp;
629 tempobjectfp << std::fixed;
631 tempobjectfp << "define contact {" "\n"
632 "\t" "contact_name" "\t" << user->GetName() << "\n"
633 "\t" "alias" "\t" << user->GetDisplayName() << "\n"
634 "\t" "service_notification_options" "\t" "w,u,c,r,f,s" "\n"
635 "\t" "host_notification_options""\t" "d,u,r,f,s" "\n"
636 "\t" "host_notifications_enabled" "\t" "1" "\n"
637 "\t" "service_notifications_enabled" "\t" "1" "\n"
641 objectfp << tempobjectfp.str();
644 BOOST_FOREACH(const UserGroup::Ptr& ug, DynamicType::GetObjects<UserGroup>()) {
645 std::ostringstream tempobjectfp;
646 tempobjectfp << std::fixed;
648 tempobjectfp << "define contactgroup {" "\n"
649 "\t" "contactgroup_name" "\t" << ug->GetName() << "\n"
650 "\t" "alias" "\t" << ug->GetDisplayName() << "\n";
652 tempobjectfp << "\t" "members" "\t";
653 DumpNameList(tempobjectfp, ug->GetMembers());
657 objectfp << tempobjectfp.str();
660 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<CheckCommand>()) {
661 DumpCommand(objectfp, command);
664 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<NotificationCommand>()) {
665 DumpCommand(objectfp, command);
668 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<EventCommand>()) {
669 DumpCommand(objectfp, command);
672 BOOST_FOREACH(const TimePeriod::Ptr& tp, DynamicType::GetObjects<TimePeriod>()) {
673 DumpTimePeriod(objectfp, tp);
679 _unlink(objectspath.CStr());
682 if (rename(objectspathtmp.CStr(), objectspath.CStr()) < 0) {
683 BOOST_THROW_EXCEPTION(posix_error()
684 << boost::errinfo_api_function("rename")
685 << boost::errinfo_errno(errno)
686 << boost::errinfo_file_name(objectspathtmp));
691 * Periodically writes the status.dat and objects.cache files.
693 void StatusDataWriter::StatusTimerHandler(void)
695 Log(LogInformation, "compat", "Writing status.dat file");
697 String statuspath = GetStatusPath();
698 String statuspathtmp = statuspath + ".tmp"; /* XXX make this a global definition */
700 std::ofstream statusfp;
701 statusfp.open(statuspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
703 statusfp << std::fixed;
705 statusfp << "# Icinga status file" "\n"
706 "# This file is auto-generated. Do not modify this file." "\n"
709 statusfp << "info {" "\n"
710 "\t" "created=" << Utility::GetTime() << "\n"
711 "\t" "version=" << Application::GetVersion() << "\n"
715 statusfp << "programstatus {" "\n"
716 "\t" "icinga_pid=" << Utility::GetPid() << "\n"
717 "\t" "daemon_mode=1" "\n"
718 "\t" "program_start=" << static_cast<long>(Application::GetStartTime()) << "\n"
719 "\t" "active_service_checks_enabled=" << (IcingaApplication::GetInstance()->GetEnableChecks() ? 1 : 0) << "\n"
720 "\t" "passive_service_checks_enabled=1" "\n"
721 "\t" "active_host_checks_enabled=1" "\n"
722 "\t" "passive_host_checks_enabled=1" "\n"
723 "\t" "check_service_freshness=1" "\n"
724 "\t" "check_host_freshness=1" "\n"
725 "\t" "enable_notifications=" << (IcingaApplication::GetInstance()->GetEnableNotifications() ? 1 : 0) << "\n"
726 "\t" "enable_flap_detection=" << (IcingaApplication::GetInstance()->GetEnableFlapping() ? 1 : 0) << "\n"
727 "\t" "enable_failure_prediction=0" "\n"
728 "\t" "process_performance_data=" << (IcingaApplication::GetInstance()->GetEnablePerfdata() ? 1 : 0) << "\n"
729 "\t" "active_scheduled_service_check_stats=" << CIB::GetActiveChecksStatistics(60) << "," << CIB::GetActiveChecksStatistics(5 * 60) << "," << CIB::GetActiveChecksStatistics(15 * 60) << "\n"
730 "\t" "passive_service_check_stats=" << CIB::GetPassiveChecksStatistics(60) << "," << CIB::GetPassiveChecksStatistics(5 * 60) << "," << CIB::GetPassiveChecksStatistics(15 * 60) << "\n"
731 "\t" "next_downtime_id=" << Service::GetNextDowntimeID() << "\n"
732 "\t" "next_comment_id=" << Service::GetNextCommentID() << "\n"
736 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
737 std::ostringstream tempstatusfp;
738 tempstatusfp << std::fixed;
739 DumpHostStatus(tempstatusfp, host);
740 statusfp << tempstatusfp.str();
743 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
744 std::ostringstream tempstatusfp;
745 tempstatusfp << std::fixed;
746 DumpServiceStatus(tempstatusfp, service);
747 statusfp << tempstatusfp.str();
753 _unlink(statuspath.CStr());
756 if (rename(statuspathtmp.CStr(), statuspath.CStr()) < 0) {
757 BOOST_THROW_EXCEPTION(posix_error()
758 << boost::errinfo_api_function("rename")
759 << boost::errinfo_errno(errno)
760 << boost::errinfo_file_name(statuspathtmp));
763 Log(LogInformation, "compat", "Finished writing status.dat file");