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 DumpServiceStatusAttrs(fp, hc, CompatTypeHost);
213 /* ugly but cgis parse only that */
214 fp << "\t" "last_time_up=" << host->GetLastStateUp() << "\n"
215 "\t" "last_time_down=" << host->GetLastStateDown() << "\n"
216 "\t" "last_time_unreachable=" << host->GetLastStateUnreachable() << "\n";
222 DumpDowntimes(fp, hc, CompatTypeHost);
223 DumpComments(fp, hc, CompatTypeHost);
227 void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host)
229 String notes = CompatUtility::GetCustomAttributeConfig(host, "notes");
230 String notes_url = CompatUtility::GetCustomAttributeConfig(host, "notes_url");
231 String action_url = CompatUtility::GetCustomAttributeConfig(host, "action_url");
232 String icon_image = CompatUtility::GetCustomAttributeConfig(host, "icon_image");
233 String icon_image_alt = CompatUtility::GetCustomAttributeConfig(host, "icon_image_alt");
234 String statusmap_image = CompatUtility::GetCustomAttributeConfig(host, "statusmap_image");
236 fp << "define host {" "\n"
237 "\t" "host_name" "\t" << host->GetName() << "\n"
238 "\t" "display_name" "\t" << host->GetDisplayName() << "\n"
239 "\t" "alias" "\t" << host->GetDisplayName() << "\n"
240 "\t" "address" "\t" << CompatUtility::GetHostAddress(host) << "\n"
241 "\t" "address6" "\t" << CompatUtility::GetHostAddress6(host) << "\n";
242 if (!notes.IsEmpty())
243 fp << "\t" "notes" "\t" << notes << "\n";
244 if (!notes_url.IsEmpty())
245 fp << "\t" "notes_url" "\t" << notes_url << "\n";
246 if (!action_url.IsEmpty())
247 fp << "\t" "action_url" "\t" << action_url << "\n";
248 if (!icon_image.IsEmpty())
249 fp << "\t" "icon_image" "\t" << icon_image << "\n";
250 if (!icon_image_alt.IsEmpty())
251 fp << "\t" "icon_image_alt" "\t" << icon_image_alt << "\n";
252 if (!statusmap_image.IsEmpty())
253 fp << "\t" "statusmap_image" "\t" << statusmap_image << "\n";
255 std::set<Host::Ptr> parents = host->GetParentHosts();
257 if (!parents.empty()) {
258 fp << "\t" "parents" "\t";
259 DumpNameList(fp, parents);
263 Service::Ptr hc = host->GetCheckService();
265 ObjectLock olock(hc);
267 fp << "\t" "check_interval" "\t" << CompatUtility::GetServiceCheckInterval(hc) << "\n"
268 "\t" "retry_interval" "\t" << CompatUtility::GetServiceRetryInterval(hc) << "\n"
269 "\t" "max_check_attempts" "\t" << hc->GetMaxCheckAttempts() << "\n"
270 "\t" "active_checks_enabled" "\t" << CompatUtility::GetServiceActiveChecksEnabled(hc) << "\n"
271 "\t" "passive_checks_enabled" "\t" << CompatUtility::GetServicePassiveChecksEnabled(hc) << "\n"
272 "\t" "notifications_enabled" "\t" << CompatUtility::GetServiceNotificationsEnabled(hc) << "\n"
273 "\t" "notification_options" "\t" << "d,u,r" << "\n"
274 "\t" "notification_interval" "\t" << CompatUtility::GetServiceNotificationNotificationInterval(hc) << "\n"
275 "\t" "event_handler_enabled" "\t" << CompatUtility::GetServiceEventHandlerEnabled(hc) << "\n";
277 CheckCommand::Ptr checkcommand = hc->GetCheckCommand();
279 fp << "\t" "check_command" "\t" "check_" << checkcommand->GetName() << "\n";
281 EventCommand::Ptr eventcommand = hc->GetEventCommand();
283 fp << "\t" "event_handler" "\t" "event_" << eventcommand->GetName() << "\n";
285 fp << "\t" "check_period" "\t" << CompatUtility::GetServiceCheckPeriod(hc) << "\n";
287 fp << "\t" "contacts" "\t";
288 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(hc));
291 fp << "\t" "contact_groups" "\t";
292 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(hc));
295 fp << "\t" << "initial_state" "\t" "o" "\n"
296 "\t" "low_flap_threshold" "\t" << hc->GetFlappingThreshold() << "\n"
297 "\t" "high_flap_threshold" "\t" << hc->GetFlappingThreshold() << "\n"
298 "\t" "process_perf_data" "\t" << CompatUtility::GetServiceProcessPerformanceData(hc) << "\n"
299 "\t" "check_freshness" "\t" "1" "\n";
302 fp << "\t" << "check_interval" "\t" "60" "\n"
303 "\t" "retry_interval" "\t" "60" "\n"
304 "\t" "max_check_attempts" "\t" "1" "\n"
305 "\t" "active_checks_enabled" "\t" "0" << "\n"
306 "\t" "passive_checks_enabled" "\t" "0" "\n"
307 "\t" "notifications_enabled" "\t" "0" "\n";
311 fp << "\t" "host_groups" "\t";
314 Array::Ptr groups = host->GetGroups();
317 ObjectLock olock(groups);
319 BOOST_FOREACH(const String& name, groups) {
320 HostGroup::Ptr hg = HostGroup::GetByName(name);
335 DumpCustomAttributes(fp, host);
341 void StatusDataWriter::DumpServiceStatusAttrs(std::ostream& fp, const Service::Ptr& service, CompatObjectType type)
343 CheckResult::Ptr cr = service->GetLastCheckResult();
345 fp << "\t" << "check_command=check_" << CompatUtility::GetServiceCheckCommand(service) << "\n"
346 "\t" "event_handler=event_" << CompatUtility::GetServiceEventHandler(service) << "\n"
347 "\t" "check_period=" << CompatUtility::GetServiceCheckPeriod(service) << "\n"
348 "\t" "check_interval=" << CompatUtility::GetServiceCheckInterval(service) << "\n"
349 "\t" "retry_interval=" << CompatUtility::GetServiceRetryInterval(service) << "\n"
350 "\t" "has_been_checked=" << CompatUtility::GetServiceHasBeenChecked(service) << "\n"
351 "\t" "should_be_scheduled=" << CompatUtility::GetServiceShouldBeScheduled(service) << "\n";
354 fp << "\t" << "check_execution_time=" << Convert::ToString(Service::CalculateExecutionTime(cr)) << "\n"
355 "\t" "check_latency=" << Convert::ToString(Service::CalculateLatency(cr)) << "\n";
358 if (type == CompatTypeHost && service->IsHostCheck()) {
359 fp << "\t" << "current_state=" << service->GetHost()->GetState() << "\n";
361 fp << "\t" << "current_state=" << CompatUtility::GetServiceCurrentState(service) << "\n";
364 fp << "\t" "state_type=" << service->GetStateType() << "\n"
365 "\t" "plugin_output=" << CompatUtility::GetCheckResultOutput(cr) << "\n"
366 "\t" "long_plugin_output=" << CompatUtility::GetCheckResultLongOutput(cr) << "\n"
367 "\t" "performance_data=" << CompatUtility::GetCheckResultPerfdata(cr) << "\n";
370 fp << "\t" << "check_source=" << cr->GetCheckSource() << "\n"
371 "\t" "last_check=" << static_cast<long>(cr->GetScheduleEnd()) << "\n";
374 fp << "\t" << "next_check=" << static_cast<long>(service->GetNextCheck()) << "\n"
375 "\t" "current_attempt=" << service->GetCheckAttempt() << "\n"
376 "\t" "max_attempts=" << service->GetMaxCheckAttempts() << "\n"
377 "\t" "last_state_change=" << static_cast<long>(service->GetLastStateChange()) << "\n"
378 "\t" "last_hard_state_change=" << static_cast<long>(service->GetLastHardStateChange()) << "\n"
379 "\t" "last_time_ok=" << static_cast<int>(service->GetLastStateOK()) << "\n"
380 "\t" "last_time_warn=" << static_cast<int>(service->GetLastStateWarning()) << "\n"
381 "\t" "last_time_critical=" << static_cast<int>(service->GetLastStateCritical()) << "\n"
382 "\t" "last_time_unknown=" << static_cast<int>(service->GetLastStateUnknown()) << "\n"
383 "\t" "last_update=" << static_cast<long>(time(NULL)) << "\n"
384 "\t" "notifications_enabled=" << CompatUtility::GetServiceNotificationsEnabled(service) << "\n"
385 "\t" "active_checks_enabled=" << CompatUtility::GetServiceActiveChecksEnabled(service) << "\n"
386 "\t" "passive_checks_enabled=" << CompatUtility::GetServicePassiveChecksEnabled(service) << "\n"
387 "\t" "flap_detection_enabled=" << CompatUtility::GetServiceFlapDetectionEnabled(service) << "\n"
388 "\t" "is_flapping=" << CompatUtility::GetServiceIsFlapping(service) << "\n"
389 "\t" "percent_state_change=" << CompatUtility::GetServicePercentStateChange(service) << "\n"
390 "\t" "problem_has_been_acknowledged=" << CompatUtility::GetServiceProblemHasBeenAcknowledged(service) << "\n"
391 "\t" "acknowledgement_type=" << CompatUtility::GetServiceAcknowledgementType(service) << "\n"
392 "\t" "acknowledgement_end_time=" << service->GetAcknowledgementExpiry() << "\n"
393 "\t" "scheduled_downtime_depth=" << service->GetDowntimeDepth() << "\n"
394 "\t" "last_notification=" << CompatUtility::GetServiceNotificationLastNotification(service) << "\n"
395 "\t" "next_notification=" << CompatUtility::GetServiceNotificationNextNotification(service) << "\n"
396 "\t" "current_notification_number=" << CompatUtility::GetServiceNotificationNotificationNumber(service) << "\n"
397 "\t" "modified_attributes=" << service->GetModifiedAttributes() << "\n";
400 void StatusDataWriter::DumpServiceStatus(std::ostream& fp, const Service::Ptr& service)
402 Host::Ptr host = service->GetHost();
404 fp << "servicestatus {" "\n"
405 "\t" "host_name=" << host->GetName() << "\n"
406 "\t" "service_description=" << service->GetShortName() << "\n";
409 ObjectLock olock(service);
410 DumpServiceStatusAttrs(fp, service, CompatTypeService);
416 DumpDowntimes(fp, service, CompatTypeService);
417 DumpComments(fp, service, CompatTypeService);
420 void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& service)
422 Host::Ptr host = service->GetHost();
425 ObjectLock olock(service);
427 fp << "define service {" "\n"
428 "\t" "host_name" "\t" << host->GetName() << "\n"
429 "\t" "service_description" "\t" << service->GetShortName() << "\n"
430 "\t" "display_name" "\t" << service->GetDisplayName() << "\n"
431 "\t" "check_period" "\t" << CompatUtility::GetServiceCheckPeriod(service) << "\n"
432 "\t" "check_interval" "\t" << CompatUtility::GetServiceCheckInterval(service) << "\n"
433 "\t" "retry_interval" "\t" << CompatUtility::GetServiceRetryInterval(service) << "\n"
434 "\t" "max_check_attempts" "\t" << service->GetMaxCheckAttempts() << "\n"
435 "\t" "active_checks_enabled" "\t" << CompatUtility::GetServiceActiveChecksEnabled(service) << "\n"
436 "\t" "passive_checks_enabled" "\t" << CompatUtility::GetServicePassiveChecksEnabled(service) << "\n"
437 "\t" "flap_detection_enabled" "\t" << CompatUtility::GetServiceFlapDetectionEnabled(service) << "\n"
438 "\t" "is_volatile" "\t" << CompatUtility::GetServiceIsVolatile(service) << "\n"
439 "\t" "notifications_enabled" "\t" << CompatUtility::GetServiceNotificationsEnabled(service) << "\n"
440 "\t" "notification_options" "\t" << CompatUtility::GetServiceNotificationNotificationOptions(service) << "\n"
441 "\t" "notification_interval" "\t" << CompatUtility::GetServiceNotificationNotificationInterval(service) << "\n"
442 "\t" "notification_period" "\t" << CompatUtility::GetServiceNotificationNotificationPeriod(service) << "\n"
443 "\t" "event_handler_enabled" "\t" << CompatUtility::GetServiceEventHandlerEnabled(service) << "\n";
445 CheckCommand::Ptr checkcommand = service->GetCheckCommand();
447 fp << "\t" "check_command" "\t" "check_" << checkcommand->GetName() << "\n";
449 EventCommand::Ptr eventcommand = service->GetEventCommand();
451 fp << "\t" "event_handler" "\t" "event_" << eventcommand->GetName() << "\n";
453 fp << "\t" "contacts" "\t";
454 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(service));
457 fp << "\t" "contact_groups" "\t";
458 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(service));
461 String notes = CompatUtility::GetCustomAttributeConfig(service, "notes");
462 String notes_url = CompatUtility::GetCustomAttributeConfig(service, "notes_url");
463 String action_url = CompatUtility::GetCustomAttributeConfig(service, "action_url");
464 String icon_image = CompatUtility::GetCustomAttributeConfig(service, "icon_image");
465 String icon_image_alt = CompatUtility::GetCustomAttributeConfig(service, "icon_image_alt");
467 fp << "\t" "initial_state" "\t" "o" "\n"
468 "\t" "low_flap_threshold" "\t" << service->GetFlappingThreshold() << "\n"
469 "\t" "high_flap_threshold" "\t" << service->GetFlappingThreshold() << "\n"
470 "\t" "process_perf_data" "\t" << CompatUtility::GetServiceProcessPerformanceData(service) << "\n"
471 "\t" "check_freshness" << "\t" "1" "\n";
472 if (!notes.IsEmpty())
473 fp << "\t" "notes" "\t" << notes << "\n";
474 if (!notes_url.IsEmpty())
475 fp << "\t" "notes_url" "\t" << notes_url << "\n";
476 if (!action_url.IsEmpty())
477 fp << "\t" "action_url" "\t" << action_url << "\n";
478 if (!icon_image.IsEmpty())
479 fp << "\t" "icon_image" "\t" << icon_image << "\n";
480 if (!icon_image_alt.IsEmpty())
481 fp << "\t" "icon_image_alt" "\t" << icon_image_alt << "\n";
484 fp << "\t" "service_groups" "\t";
487 Array::Ptr groups = service->GetGroups();
490 ObjectLock olock(groups);
492 BOOST_FOREACH(const String& name, groups) {
493 ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);
508 DumpCustomAttributes(fp, service);
514 void StatusDataWriter::DumpCustomAttributes(std::ostream& fp, const DynamicObject::Ptr& object)
516 Dictionary::Ptr custom = object->GetCustom();
521 ObjectLock olock(custom);
522 BOOST_FOREACH(const Dictionary::Pair& kv, custom) {
523 if (!kv.first.IsEmpty()) {
526 if (kv.first != "notes" && kv.first != "action_url" && kv.first != "notes_url" &&
527 kv.first != "icon_image" && kv.first != "icon_image_alt" && kv.first != "statusmap_image" && kv.first != "2d_coords")
530 fp << kv.first << "\t" << kv.second << "\n";
535 void StatusDataWriter::UpdateObjectsCache(void)
537 CONTEXT("Writing objects.cache file");
539 String objectspath = GetObjectsPath();
540 String objectspathtmp = objectspath + ".tmp";
542 std::ofstream objectfp;
543 objectfp.open(objectspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
545 objectfp << std::fixed;
547 objectfp << "# Icinga objects cache file" "\n"
548 "# This file is auto-generated. Do not modify this file." "\n"
551 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
552 std::ostringstream tempobjectfp;
553 tempobjectfp << std::fixed;
554 DumpHostObject(tempobjectfp, host);
555 objectfp << tempobjectfp.str();
557 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
558 std::ostringstream tempobjectfp;
559 tempobjectfp << std::fixed;
560 DumpServiceObject(tempobjectfp, service);
561 objectfp << tempobjectfp.str();
565 BOOST_FOREACH(const HostGroup::Ptr& hg, DynamicType::GetObjects<HostGroup>()) {
566 std::ostringstream tempobjectfp;
567 tempobjectfp << std::fixed;
569 tempobjectfp << "define hostgroup {" "\n"
570 "\t" "hostgroup_name" "\t" << hg->GetName() << "\n";
572 DumpCustomAttributes(tempobjectfp, hg);
574 tempobjectfp << "\t" "members" "\t";
575 DumpNameList(tempobjectfp, hg->GetMembers());
579 objectfp << tempobjectfp.str();
582 BOOST_FOREACH(const ServiceGroup::Ptr& sg, DynamicType::GetObjects<ServiceGroup>()) {
583 std::ostringstream tempobjectfp;
584 tempobjectfp << std::fixed;
586 tempobjectfp << "define servicegroup {" "\n"
587 "\t" "servicegroup_name" "\t" << sg->GetName() << "\n";
589 DumpCustomAttributes(tempobjectfp, sg);
591 tempobjectfp << "\t" "members" "\t";
593 std::vector<String> sglist;
594 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
595 Host::Ptr host = service->GetHost();
597 sglist.push_back(host->GetName());
598 sglist.push_back(service->GetShortName());
601 DumpStringList(tempobjectfp, sglist);
606 objectfp << tempobjectfp.str();
609 BOOST_FOREACH(const User::Ptr& user, DynamicType::GetObjects<User>()) {
610 std::ostringstream tempobjectfp;
611 tempobjectfp << std::fixed;
613 tempobjectfp << "define contact {" "\n"
614 "\t" "contact_name" "\t" << user->GetName() << "\n"
615 "\t" "alias" "\t" << user->GetDisplayName() << "\n"
616 "\t" "service_notification_options" "\t" "w,u,c,r,f,s" "\n"
617 "\t" "host_notification_options""\t" "d,u,r,f,s" "\n"
618 "\t" "host_notifications_enabled" "\t" "1" "\n"
619 "\t" "service_notifications_enabled" "\t" "1" "\n"
623 objectfp << tempobjectfp.str();
626 BOOST_FOREACH(const UserGroup::Ptr& ug, DynamicType::GetObjects<UserGroup>()) {
627 std::ostringstream tempobjectfp;
628 tempobjectfp << std::fixed;
630 tempobjectfp << "define contactgroup {" "\n"
631 "\t" "contactgroup_name" "\t" << ug->GetName() << "\n"
632 "\t" "alias" "\t" << ug->GetDisplayName() << "\n";
634 tempobjectfp << "\t" "members" "\t";
635 DumpNameList(tempobjectfp, ug->GetMembers());
639 objectfp << tempobjectfp.str();
642 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<CheckCommand>()) {
643 DumpCommand(objectfp, command);
646 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<NotificationCommand>()) {
647 DumpCommand(objectfp, command);
650 BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<EventCommand>()) {
651 DumpCommand(objectfp, command);
654 BOOST_FOREACH(const TimePeriod::Ptr& tp, DynamicType::GetObjects<TimePeriod>()) {
655 DumpTimePeriod(objectfp, tp);
658 BOOST_FOREACH(const Dependency::Ptr& dep, DynamicType::GetObjects<Dependency>()) {
659 Service::Ptr parent_service = dep->GetParentService();
664 Service::Ptr child_service = dep->GetChildService();
669 objectfp << "define servicedependency {" "\n"
670 "\t" "dependent_host_name" "\t" << child_service->GetHost()->GetName() << "\n"
671 "\t" "dependent_service_description" "\t" << child_service->GetShortName() << "\n"
672 "\t" "host_name" "\t" << parent_service->GetHost()->GetName() << "\n"
673 "\t" "service_description" "\t" << parent_service->GetShortName() << "\n"
674 "\t" "execution_failure_criteria" "\t" "n" "\n"
675 "\t" "notification_failure_criteria" "\t" "w,u,c" "\n"
683 _unlink(objectspath.CStr());
686 if (rename(objectspathtmp.CStr(), objectspath.CStr()) < 0) {
687 BOOST_THROW_EXCEPTION(posix_error()
688 << boost::errinfo_api_function("rename")
689 << boost::errinfo_errno(errno)
690 << boost::errinfo_file_name(objectspathtmp));
695 * Periodically writes the status.dat and objects.cache files.
697 void StatusDataWriter::StatusTimerHandler(void)
699 Log(LogInformation, "compat", "Writing status.dat file");
701 String statuspath = GetStatusPath();
702 String statuspathtmp = statuspath + ".tmp"; /* XXX make this a global definition */
704 std::ofstream statusfp;
705 statusfp.open(statuspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
707 statusfp << std::fixed;
709 statusfp << "# Icinga status file" "\n"
710 "# This file is auto-generated. Do not modify this file." "\n"
713 statusfp << "info {" "\n"
714 "\t" "created=" << Utility::GetTime() << "\n"
715 "\t" "version=" << Application::GetVersion() << "\n"
719 statusfp << "programstatus {" "\n"
720 "\t" "icinga_pid=" << Utility::GetPid() << "\n"
721 "\t" "daemon_mode=1" "\n"
722 "\t" "program_start=" << static_cast<long>(Application::GetStartTime()) << "\n"
723 "\t" "active_service_checks_enabled=" << (IcingaApplication::GetInstance()->GetEnableChecks() ? 1 : 0) << "\n"
724 "\t" "passive_service_checks_enabled=1" "\n"
725 "\t" "active_host_checks_enabled=1" "\n"
726 "\t" "passive_host_checks_enabled=1" "\n"
727 "\t" "check_service_freshness=1" "\n"
728 "\t" "check_host_freshness=1" "\n"
729 "\t" "enable_notifications=" << (IcingaApplication::GetInstance()->GetEnableNotifications() ? 1 : 0) << "\n"
730 "\t" "enable_flap_detection=" << (IcingaApplication::GetInstance()->GetEnableFlapping() ? 1 : 0) << "\n"
731 "\t" "enable_failure_prediction=0" "\n"
732 "\t" "process_performance_data=" << (IcingaApplication::GetInstance()->GetEnablePerfdata() ? 1 : 0) << "\n"
733 "\t" "active_scheduled_service_check_stats=" << CIB::GetActiveChecksStatistics(60) << "," << CIB::GetActiveChecksStatistics(5 * 60) << "," << CIB::GetActiveChecksStatistics(15 * 60) << "\n"
734 "\t" "passive_service_check_stats=" << CIB::GetPassiveChecksStatistics(60) << "," << CIB::GetPassiveChecksStatistics(5 * 60) << "," << CIB::GetPassiveChecksStatistics(15 * 60) << "\n"
735 "\t" "next_downtime_id=" << Service::GetNextDowntimeID() << "\n"
736 "\t" "next_comment_id=" << Service::GetNextCommentID() << "\n"
740 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
741 std::ostringstream tempstatusfp;
742 tempstatusfp << std::fixed;
743 DumpHostStatus(tempstatusfp, host);
744 statusfp << tempstatusfp.str();
746 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
747 std::ostringstream tempstatusfp;
748 tempstatusfp << std::fixed;
749 DumpServiceStatus(tempstatusfp, service);
750 statusfp << tempstatusfp.str();
757 _unlink(statuspath.CStr());
760 if (rename(statuspathtmp.CStr(), statuspath.CStr()) < 0) {
761 BOOST_THROW_EXCEPTION(posix_error()
762 << boost::errinfo_api_function("rename")
763 << boost::errinfo_errno(errno)
764 << boost::errinfo_file_name(statuspathtmp));
767 Log(LogInformation, "compat", "Finished writing status.dat file");