]> granicus.if.org Git - icinga2/blob - components/compat/statusdatawriter.cpp
Compat: Do not dump empty strings into objects.cache.
[icinga2] / components / compat / statusdatawriter.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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 <boost/foreach.hpp>
38 #include <boost/tuple/tuple.hpp>
39 #include <boost/algorithm/string/replace.hpp>
40 #include <fstream>
41
42 using namespace icinga;
43
44 REGISTER_TYPE(StatusDataWriter);
45
46 /**
47  * Hint: The reason why we're using "\n" rather than std::endl is because
48  * std::endl also _flushes_ the output stream which severely degrades
49  * performance (see http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html).
50  */
51
52 /**
53  * Starts the component.
54  */
55 void StatusDataWriter::Start(void)
56 {
57         DynamicObject::Start();
58
59         m_StatusTimer = make_shared<Timer>();
60         m_StatusTimer->SetInterval(GetUpdateInterval());
61         m_StatusTimer->OnTimerExpired.connect(boost::bind(&StatusDataWriter::StatusTimerHandler, this));
62         m_StatusTimer->Start();
63         m_StatusTimer->Reschedule(0);
64
65         Utility::QueueAsyncCallback(boost::bind(&StatusDataWriter::UpdateObjectsCache, this));
66 }
67
68 void StatusDataWriter::DumpComments(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
69 {
70         Service::Ptr service;
71         Dictionary::Ptr comments = owner->GetComments();
72
73         Host::Ptr host = owner->GetHost();
74
75         ObjectLock olock(comments);
76
77         BOOST_FOREACH(const Dictionary::Pair& kv, comments) {
78                 Comment::Ptr comment = kv.second;
79
80                 if (comment->IsExpired())
81                         continue;
82
83                 if (type == CompatTypeHost)
84                         fp << "hostcomment {" << "\n";
85                 else
86                         fp << "servicecomment {" << "\n"
87                            << "\t" << "service_description=" << owner->GetShortName() << "\n";
88
89                 fp << "\t" "host_name=" << host->GetName() << "\n"
90                       "\t" "comment_id=" << comment->GetLegacyId() << "\n"
91                       "\t" "entry_time=" << comment->GetEntryTime() << "\n"
92                       "\t" "entry_type=" << comment->GetEntryType() << "\n"
93                       "\t" "persistent=" "1" "\n"
94                       "\t" "author=" << comment->GetAuthor() << "\n"
95                       "\t" "comment_data=" << comment->GetText() << "\n"
96                       "\t" "expires=" << (comment->GetExpireTime() != 0 ? 1 : 0) << "\n"
97                       "\t" "expire_time=" << comment->GetExpireTime() << "\n"
98                       "\t" "}" "\n"
99                       "\n";
100         }
101 }
102
103 void StatusDataWriter::DumpTimePeriod(std::ostream& fp, const TimePeriod::Ptr& tp)
104 {
105         fp << "define timeperiod {" "\n"
106               "\t" "timeperiod_name" "\t" << tp->GetName() << "\n"
107               "\t" "alias" "\t" << tp->GetName() << "\n";
108
109         Dictionary::Ptr ranges = tp->GetRanges();
110
111         if (ranges) {
112                 ObjectLock olock(ranges);
113                 BOOST_FOREACH(const Dictionary::Pair& kv, ranges) {
114                         fp << "\t" << kv.first << "\t" << kv.second << "\n";
115                 }
116         }
117
118         fp << "\t" "}" "\n"
119               "\n";
120 }
121
122 void StatusDataWriter::DumpCommand(std::ostream& fp, const Command::Ptr& command)
123 {
124         fp << "define command {" "\n"
125               "\t" "command_name\t";
126
127
128         if (command->GetType() == DynamicType::GetByName("CheckCommand"))
129                 fp << "check_";
130         else if (command->GetType() == DynamicType::GetByName("NotificationCommand"))
131                 fp << "notification_";
132         else if (command->GetType() == DynamicType::GetByName("EventCommand"))
133                 fp << "event_";
134
135         fp << command->GetName() << "\n";
136
137         fp << "\t" "command_line" "\t" << CompatUtility::GetCommandLine(command);
138
139         fp << "\n" "\t" "}" "\n"
140               "\n";
141 }
142
143 void StatusDataWriter::DumpDowntimes(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
144 {
145         Host::Ptr host = owner->GetHost();
146
147         Dictionary::Ptr downtimes = owner->GetDowntimes();
148
149         ObjectLock olock(downtimes);
150
151         BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
152                 Downtime::Ptr downtime = kv.second;
153
154                 if (downtime->IsExpired())
155                         continue;
156
157                 if (type == CompatTypeHost)
158                         fp << "hostdowntime {" "\n";
159                 else
160                         fp << "servicedowntime {" << "\n"
161                               "\t" "service_description=" << owner->GetShortName() << "\n";
162
163                 Downtime::Ptr triggeredByObj = Service::GetDowntimeByID(downtime->GetTriggeredBy());
164                 int triggeredByLegacy = 0;
165                 if (triggeredByObj)
166                         triggeredByLegacy = triggeredByObj->GetLegacyId();
167
168                 fp << "\t" << "host_name=" << host->GetName() << "\n"
169                       "\t" "downtime_id=" << downtime->GetLegacyId() << "\n"
170                       "\t" "entry_time=" << downtime->GetEntryTime() << "\n"
171                       "\t" "start_time=" << downtime->GetStartTime() << "\n"
172                       "\t" "end_time=" << downtime->GetEndTime() << "\n"
173                       "\t" "triggered_by=" << triggeredByLegacy << "\n"
174                       "\t" "fixed=" << static_cast<long>(downtime->GetFixed()) << "\n"
175                       "\t" "duration=" << static_cast<long>(downtime->GetDuration()) << "\n"
176                       "\t" "is_in_effect=" << (downtime->IsActive() ? 1 : 0) << "\n"
177                       "\t" "author=" << downtime->GetAuthor() << "\n"
178                       "\t" "comment=" << downtime->GetComment() << "\n"
179                       "\t" "trigger_time=" << downtime->GetTriggerTime() << "\n"
180                       "\t" "}" "\n"
181                       "\n";
182         }
183 }
184
185 void StatusDataWriter::DumpHostStatus(std::ostream& fp, const Host::Ptr& host)
186 {
187         fp << "hoststatus {" << "\n"
188            << "\t" << "host_name=" << host->GetName() << "\n";
189
190         Service::Ptr hc = host->GetCheckService();
191         ObjectLock olock(hc);
192
193         if (hc)
194                 DumpServiceStatusAttrs(fp, hc, CompatTypeHost);
195
196         /* ugly but cgis parse only that */
197         fp << "\t" "last_time_up=" << host->GetLastStateUp() << "\n"
198               "\t" "last_time_down=" << host->GetLastStateDown() << "\n"
199               "\t" "last_time_unreachable=" << host->GetLastStateUnreachable() << "\n";
200
201         fp << "\t" "}" "\n"
202               "\n";
203
204         if (hc) {
205                 DumpDowntimes(fp, hc, CompatTypeHost);
206                 DumpComments(fp, hc, CompatTypeHost);
207         }
208 }
209
210 void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host)
211 {
212         String notes = CompatUtility::GetCustomAttributeConfig(host, "notes");
213         String notes_url = CompatUtility::GetCustomAttributeConfig(host, "notes_url");
214         String action_url = CompatUtility::GetCustomAttributeConfig(host, "action_url");
215         String icon_image = CompatUtility::GetCustomAttributeConfig(host, "icon_image");
216         String icon_image_alt = CompatUtility::GetCustomAttributeConfig(host, "icon_image_alt");
217         String statusmap_image = CompatUtility::GetCustomAttributeConfig(host, "statusmap_image");
218
219         fp << "define host {" "\n"
220               "\t" "host_name" "\t" << host->GetName() << "\n"
221               "\t" "display_name" "\t" << host->GetDisplayName() << "\n"
222               "\t" "alias" "\t" << host->GetDisplayName() << "\n"
223               "\t" "address" "\t" << CompatUtility::GetHostAddress(host) << "\n"
224               "\t" "address6" "\t" << CompatUtility::GetHostAddress6(host) << "\n";
225         if (!notes.IsEmpty())
226               fp << "\t" "notes" "\t" << notes << "\n";
227         if (!notes_url.IsEmpty())
228               fp << "\t" "notes_url" "\t" << notes_url << "\n";
229         if (!action_url.IsEmpty())
230               fp << "\t" "action_url" "\t" << action_url << "\n";
231         if (!icon_image.IsEmpty())
232               fp << "\t" "icon_image" "\t" << icon_image << "\n";
233         if (!icon_image_alt.IsEmpty())
234               fp << "\t" "icon_image_alt" "\t" << icon_image_alt << "\n";
235         if (!statusmap_image.IsEmpty())
236               fp << "\t" "statusmap_image" "\t" << statusmap_image << "\n";
237
238         std::set<Host::Ptr> parents = host->GetParentHosts();
239
240         if (!parents.empty()) {
241                 fp << "\t" "parents" "\t";
242                 DumpNameList(fp, parents);
243                 fp << "\n";
244         }
245
246         Service::Ptr hc = host->GetCheckService();
247         if (hc) {
248                 ObjectLock olock(hc);
249
250                 fp << "\t" "check_interval" "\t" << CompatUtility::GetServiceCheckInterval(hc) << "\n"
251                       "\t" "retry_interval" "\t" << CompatUtility::GetServiceRetryInterval(hc) << "\n"
252                       "\t" "max_check_attempts" "\t" << hc->GetMaxCheckAttempts() << "\n"
253                       "\t" "active_checks_enabled" "\t" << CompatUtility::GetServiceActiveChecksEnabled(hc) << "\n"
254                       "\t" "passive_checks_enabled" "\t" << CompatUtility::GetServicePassiveChecksEnabled(hc) << "\n"
255                       "\t" "notifications_enabled" "\t" << CompatUtility::GetServiceNotificationsEnabled(hc) << "\n"
256                       "\t" "notification_options" "\t" << "d,u,r" << "\n"
257                       "\t" "notification_interval" "\t" << CompatUtility::GetServiceNotificationNotificationInterval(hc) << "\n"
258                       "\t" "event_handler_enabled" "\t" << CompatUtility::GetServiceEventHandlerEnabled(hc) << "\n";
259
260                 CheckCommand::Ptr checkcommand = hc->GetCheckCommand();
261                 if (checkcommand)
262                         fp << "\t" "check_command" "\t" "check_" << checkcommand->GetName() << "\n";
263
264                 EventCommand::Ptr eventcommand = hc->GetEventCommand();
265                 if (eventcommand)
266                         fp << "\t" "event_handler" "\t" "event_" << eventcommand->GetName() << "\n";
267
268                 fp << "\t" "check_period" "\t" << CompatUtility::GetServiceCheckPeriod(hc) << "\n";
269
270                 fp << "\t" "contacts" "\t";
271                 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(hc));
272                 fp << "\n";
273
274                 fp << "\t" "contact_groups" "\t";
275                 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(hc));
276                 fp << "\n";
277
278                 fp << "\t" << "initial_state" "\t" "o" "\n"
279                       "\t" "low_flap_threshold" "\t" << hc->GetFlappingThreshold() << "\n"
280                       "\t" "high_flap_threshold" "\t" << hc->GetFlappingThreshold() << "\n"
281                       "\t" "process_perf_data" "\t" "1" "\n"
282                       "\t" "check_freshness" "\t" "1" "\n";
283
284         } else {
285                 fp << "\t" << "check_interval" "\t" "60" "\n"
286                       "\t" "retry_interval" "\t" "60" "\n"
287                       "\t" "max_check_attempts" "\t" "1" "\n"
288                       "\t" "active_checks_enabled" "\t" "0" << "\n"
289                       "\t" "passive_checks_enabled" "\t" "0" "\n"
290                       "\t" "notifications_enabled" "\t" "0" "\n";
291
292         }
293
294         fp << "\t" "host_groups" "\t";
295         bool first = true;
296
297         Array::Ptr groups = host->GetGroups();
298
299         if (groups) {
300                 ObjectLock olock(groups);
301
302                 BOOST_FOREACH(const String& name, groups) {
303                         HostGroup::Ptr hg = HostGroup::GetByName(name);
304
305                         if (hg) {
306                                 if (!first)
307                                         fp << ",";
308                                 else
309                                         first = false;
310
311                                 fp << hg->GetName();
312                         }
313                 }
314         }
315
316         fp << "\n";
317
318         DumpCustomAttributes(fp, host);
319
320         fp << "\t" "}" "\n"
321               "\n";
322 }
323
324 void StatusDataWriter::DumpServiceStatusAttrs(std::ostream& fp, const Service::Ptr& service, CompatObjectType type)
325 {
326         CheckResult::Ptr cr = service->GetLastCheckResult();
327
328         fp << "\t" << "check_command=check_" << CompatUtility::GetServiceCheckCommand(service) << "\n"
329               "\t" "event_handler=event_" << CompatUtility::GetServiceEventHandler(service) << "\n"
330               "\t" "check_period=" << CompatUtility::GetServiceCheckPeriod(service) << "\n"
331               "\t" "check_interval=" << CompatUtility::GetServiceCheckInterval(service) << "\n"
332               "\t" "retry_interval=" << CompatUtility::GetServiceRetryInterval(service) << "\n"
333               "\t" "has_been_checked=" << CompatUtility::GetServiceHasBeenChecked(service) << "\n"
334               "\t" "should_be_scheduled=" << CompatUtility::GetServiceShouldBeScheduled(service) << "\n";
335
336         if (cr) {
337            fp << "\t" << "check_execution_time=" << Convert::ToString(Service::CalculateExecutionTime(cr)) << "\n"
338                  "\t" "check_latency=" << Convert::ToString(Service::CalculateLatency(cr)) << "\n";
339         }
340
341         fp << "\t" << "current_state=" << CompatUtility::GetServiceCurrentState(service) << "\n"
342               "\t" "state_type=" << service->GetStateType() << "\n"
343               "\t" "plugin_output=" << CompatUtility::GetCheckResultOutput(cr) << "\n"
344               "\t" "long_plugin_output=" << CompatUtility::GetCheckResultLongOutput(cr) << "\n"
345               "\t" "performance_data=" << CompatUtility::GetCheckResultPerfdata(cr) << "\n";
346
347         if (cr) {
348            fp << "\t" << "check_source=" << cr->GetCheckSource() << "\n"
349                  "\t" "last_check=" << static_cast<long>(cr->GetScheduleEnd()) << "\n";
350         }
351
352         fp << "\t" << "next_check=" << static_cast<long>(service->GetNextCheck()) << "\n"
353               "\t" "current_attempt=" << service->GetCheckAttempt() << "\n"
354               "\t" "max_attempts=" << service->GetMaxCheckAttempts() << "\n"
355               "\t" "last_state_change=" << static_cast<long>(service->GetLastStateChange()) << "\n"
356               "\t" "last_hard_state_change=" << static_cast<long>(service->GetLastHardStateChange()) << "\n"
357               "\t" "last_time_ok=" << static_cast<int>(service->GetLastStateOK()) << "\n"
358               "\t" "last_time_warn=" << static_cast<int>(service->GetLastStateWarning()) << "\n"
359               "\t" "last_time_critical=" << static_cast<int>(service->GetLastStateCritical()) << "\n"
360               "\t" "last_time_unknown=" << static_cast<int>(service->GetLastStateUnknown()) << "\n"
361               "\t" "last_update=" << static_cast<long>(time(NULL)) << "\n"
362               "\t" "notifications_enabled=" << CompatUtility::GetServiceNotificationsEnabled(service) << "\n"
363               "\t" "active_checks_enabled=" << CompatUtility::GetServiceActiveChecksEnabled(service) << "\n"
364               "\t" "passive_checks_enabled=" << CompatUtility::GetServicePassiveChecksEnabled(service) << "\n"
365               "\t" "flap_detection_enabled=" << CompatUtility::GetServiceFlapDetectionEnabled(service) << "\n"
366               "\t" "is_flapping=" << CompatUtility::GetServiceIsFlapping(service) << "\n"
367               "\t" "percent_state_change=" << CompatUtility::GetServicePercentStateChange(service) << "\n"
368               "\t" "problem_has_been_acknowledged=" << CompatUtility::GetServiceProblemHasBeenAcknowledged(service) << "\n"
369               "\t" "acknowledgement_type=" << CompatUtility::GetServiceAcknowledgementType(service) << "\n"
370               "\t" "acknowledgement_end_time=" << service->GetAcknowledgementExpiry() << "\n"
371               "\t" "scheduled_downtime_depth=" << service->GetDowntimeDepth() << "\n"
372               "\t" "last_notification=" << CompatUtility::GetServiceNotificationLastNotification(service) << "\n"
373               "\t" "next_notification=" << CompatUtility::GetServiceNotificationNextNotification(service) << "\n"
374               "\t" "current_notification_number=" << CompatUtility::GetServiceNotificationNotificationNumber(service) << "\n"
375               "\t" "modified_attributes=" << service->GetModifiedAttributes() << "\n";
376 }
377
378 void StatusDataWriter::DumpServiceStatus(std::ostream& fp, const Service::Ptr& service)
379 {
380         Host::Ptr host = service->GetHost();
381
382         fp << "servicestatus {" "\n"
383               "\t" "host_name=" << host->GetName() << "\n"
384               "\t" "service_description=" << service->GetShortName() << "\n";
385
386         {
387                 ObjectLock olock(service);
388                 DumpServiceStatusAttrs(fp, service, CompatTypeService);
389         }
390
391         fp << "\t" "}" "\n"
392               "\n";
393
394         DumpDowntimes(fp, service, CompatTypeService);
395         DumpComments(fp, service, CompatTypeService);
396 }
397
398 void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& service)
399 {
400         Host::Ptr host = service->GetHost();
401
402         {
403                 ObjectLock olock(service);
404
405                 fp << "define service {" "\n"
406                       "\t" "host_name" "\t" << host->GetName() << "\n"
407                       "\t" "service_description" "\t" << service->GetShortName() << "\n"
408                       "\t" "display_name" "\t" << service->GetDisplayName() << "\n"
409                       "\t" "check_period" "\t" << CompatUtility::GetServiceCheckPeriod(service) << "\n"
410                       "\t" "check_interval" "\t" << CompatUtility::GetServiceCheckInterval(service) << "\n"
411                       "\t" "retry_interval" "\t" << CompatUtility::GetServiceRetryInterval(service) << "\n"
412                       "\t" "max_check_attempts" "\t" << service->GetMaxCheckAttempts() << "\n"
413                       "\t" "active_checks_enabled" "\t" << CompatUtility::GetServiceActiveChecksEnabled(service) << "\n"
414                       "\t" "passive_checks_enabled" "\t" << CompatUtility::GetServicePassiveChecksEnabled(service) << "\n"
415                       "\t" "flap_detection_enabled" "\t" << CompatUtility::GetServiceFlapDetectionEnabled(service) << "\n"
416                       "\t" "is_volatile" "\t" << CompatUtility::GetServiceIsVolatile(service) << "\n"
417                       "\t" "notifications_enabled" "\t" << CompatUtility::GetServiceNotificationsEnabled(service) << "\n"
418                       "\t" "notification_options" "\t" << CompatUtility::GetServiceNotificationNotificationOptions(service) << "\n"
419                       "\t" "notification_interval" "\t" << CompatUtility::GetServiceNotificationNotificationInterval(service) << "\n"
420                       "\t" "notification_period" "\t" << CompatUtility::GetServiceNotificationNotificationPeriod(service) << "\n"
421                       "\t" "event_handler_enabled" "\t" << CompatUtility::GetServiceEventHandlerEnabled(service) << "\n";
422
423                 CheckCommand::Ptr checkcommand = service->GetCheckCommand();
424                 if (checkcommand)
425                         fp << "\t" "check_command" "\t" "check_" << checkcommand->GetName() << "\n";
426
427                 EventCommand::Ptr eventcommand = service->GetEventCommand();
428                 if (eventcommand)
429                         fp << "\t" "event_handler" "\t" "event_" << eventcommand->GetName() << "\n";
430
431                 fp << "\t" "contacts" "\t";
432                 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(service));
433                 fp << "\n";
434
435                 fp << "\t" "contact_groups" "\t";
436                 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(service));
437                 fp << "\n";
438
439                 String notes = CompatUtility::GetCustomAttributeConfig(service, "notes");
440                 String notes_url = CompatUtility::GetCustomAttributeConfig(service, "notes_url");
441                 String action_url = CompatUtility::GetCustomAttributeConfig(service, "action_url");
442                 String icon_image = CompatUtility::GetCustomAttributeConfig(service, "icon_image");
443                 String icon_image_alt = CompatUtility::GetCustomAttributeConfig(service, "icon_image_alt");
444
445                 fp << "\t" "initial_state" "\t" "o" "\n"
446                       "\t" "low_flap_threshold" "\t" << service->GetFlappingThreshold() << "\n"
447                       "\t" "high_flap_threshold" "\t" << service->GetFlappingThreshold() << "\n"
448                       "\t" "process_perf_data" "\t" "1" "\n"
449                       "\t" "check_freshness" << "\t" "1" "\n";
450                 if (!notes.IsEmpty())
451                       fp << "\t" "notes" "\t" << notes << "\n";
452                 if (!notes_url.IsEmpty())
453                       fp << "\t" "notes_url" "\t" << notes_url << "\n";
454                 if (!action_url.IsEmpty())
455                       fp << "\t" "action_url" "\t" << action_url << "\n";
456                 if (!icon_image.IsEmpty())
457                       fp << "\t" "icon_image" "\t" << icon_image << "\n";
458                 if (!icon_image_alt.IsEmpty())
459                       fp << "\t" "icon_image_alt" "\t" << icon_image_alt << "\n";
460         }
461
462         fp << "\t" "service_groups" "\t";
463         bool first = true;
464
465         Array::Ptr groups = service->GetGroups();
466
467         if (groups) {
468                 ObjectLock olock(groups);
469
470                 BOOST_FOREACH(const String& name, groups) {
471                         ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);
472
473                         if (sg) {
474                                 if (!first)
475                                         fp << ",";
476                                 else
477                                         first = false;
478
479                                 fp << sg->GetName();
480                         }
481                 }
482         }
483
484         fp << "\n";
485
486         DumpCustomAttributes(fp, service);
487
488         fp << "\t" "}" "\n"
489               "\n";
490
491         BOOST_FOREACH(const Service::Ptr& parent, service->GetParentServices()) {
492                 Host::Ptr host = service->GetHost();
493
494                 Host::Ptr parent_host = parent->GetHost();
495
496                 if (!parent_host)
497                         continue;
498
499                 fp << "define servicedependency {" "\n"
500                       "\t" "dependent_host_name" "\t" << host->GetName() << "\n"
501                       "\t" "dependent_service_description" "\t" << service->GetShortName() << "\n"
502                       "\t" "host_name" "\t" << parent_host->GetName() << "\n"
503                       "\t" "service_description" "\t" << parent->GetShortName() << "\n"
504                       "\t" "execution_failure_criteria" "\t" "n" "\n"
505                       "\t" "notification_failure_criteria" "\t" "w,u,c" "\n"
506                       "\t" "}" "\n"
507                       "\n";
508         }
509 }
510
511 void StatusDataWriter::DumpCustomAttributes(std::ostream& fp, const DynamicObject::Ptr& object)
512 {
513         Dictionary::Ptr custom = object->GetCustom();
514
515         if (!custom)
516                 return;
517
518         ObjectLock olock(custom);
519         BOOST_FOREACH(const Dictionary::Pair& kv, custom) {
520                 if (!kv.first.IsEmpty()) {
521                         fp << "\t";
522
523                         if (kv.first != "notes" && kv.first != "action_url" && kv.first != "notes_url" &&
524                             kv.first != "icon_image" && kv.first != "icon_image_alt" && kv.first != "statusmap_image" && kv.first != "2d_coords")
525                                 fp << "_";
526
527                         fp << kv.first << "\t" << kv.second << "\n";
528                 }
529         }
530 }
531
532 void StatusDataWriter::UpdateObjectsCache(void)
533 {
534         CONTEXT("Writing objects.cache file");
535
536         String objectspath = GetObjectsPath();
537         String objectspathtmp = objectspath + ".tmp";
538
539         std::ofstream objectfp;
540         objectfp.open(objectspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
541
542         objectfp << std::fixed;
543
544         objectfp << "# Icinga objects cache file" "\n"
545                     "# This file is auto-generated. Do not modify this file." "\n"
546                     "\n";
547
548         BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
549                 std::ostringstream tempobjectfp;
550                 tempobjectfp << std::fixed;
551                 DumpHostObject(tempobjectfp, host);
552                 objectfp << tempobjectfp.str();
553         }
554
555         BOOST_FOREACH(const HostGroup::Ptr& hg, DynamicType::GetObjects<HostGroup>()) {
556                 std::ostringstream tempobjectfp;
557                 tempobjectfp << std::fixed;
558
559                 tempobjectfp << "define hostgroup {" "\n"
560                                 "\t" "hostgroup_name" "\t" << hg->GetName() << "\n";
561
562                 DumpCustomAttributes(tempobjectfp, hg);
563
564                 tempobjectfp << "\t" "members" "\t";
565                 DumpNameList(tempobjectfp, hg->GetMembers());
566                 tempobjectfp << "\n"
567                                 "\t" "}" "\n";
568
569                 objectfp << tempobjectfp.str();
570         }
571
572         BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
573                 std::ostringstream tempobjectfp;
574                 tempobjectfp << std::fixed;
575                 DumpServiceObject(tempobjectfp, service);
576                 objectfp << tempobjectfp.str();
577         }
578
579         BOOST_FOREACH(const ServiceGroup::Ptr& sg, DynamicType::GetObjects<ServiceGroup>()) {
580                 std::ostringstream tempobjectfp;
581                 tempobjectfp << std::fixed;
582
583                 tempobjectfp << "define servicegroup {" "\n"
584                                 "\t" "servicegroup_name" "\t" << sg->GetName() << "\n";
585
586                 DumpCustomAttributes(tempobjectfp, sg);
587
588                 tempobjectfp << "\t" "members" "\t";
589
590                 std::vector<String> sglist;
591                 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
592                         Host::Ptr host = service->GetHost();
593
594                         sglist.push_back(host->GetName());
595                         sglist.push_back(service->GetShortName());
596                 }
597
598                 DumpStringList(tempobjectfp, sglist);
599
600                 tempobjectfp << "\n"
601                                 "}" "\n";
602
603                 objectfp << tempobjectfp.str();
604         }
605
606         BOOST_FOREACH(const User::Ptr& user, DynamicType::GetObjects<User>()) {
607                 std::ostringstream tempobjectfp;
608                 tempobjectfp << std::fixed;
609
610                 tempobjectfp << "define contact {" "\n"
611                                 "\t" "contact_name" "\t" << user->GetName() << "\n"
612                                 "\t" "alias" "\t" << user->GetDisplayName() << "\n"
613                                 "\t" "service_notification_options" "\t" "w,u,c,r,f,s" "\n"
614                                 "\t" "host_notification_options""\t" "d,u,r,f,s" "\n"
615                                 "\t" "host_notifications_enabled" "\t" "1" "\n"
616                                 "\t" "service_notifications_enabled" "\t" "1" "\n"
617                                 "\t" "}" "\n"
618                                 "\n";
619
620                 objectfp << tempobjectfp.str();
621         }
622
623         BOOST_FOREACH(const UserGroup::Ptr& ug, DynamicType::GetObjects<UserGroup>()) {
624                 std::ostringstream tempobjectfp;
625                 tempobjectfp << std::fixed;
626
627                 tempobjectfp << "define contactgroup {" "\n"
628                                 "\t" "contactgroup_name" "\t" << ug->GetName() << "\n"
629                                 "\t" "alias" "\t" << ug->GetDisplayName() << "\n";
630
631                 tempobjectfp << "\t" "members" "\t";
632                 DumpNameList(tempobjectfp, ug->GetMembers());
633                 tempobjectfp << "\n"
634                                 "\t" "}" "\n";
635
636                 objectfp << tempobjectfp.str();
637         }
638
639         BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<CheckCommand>()) {
640                 DumpCommand(objectfp, command);
641         }
642
643         BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<NotificationCommand>()) {
644                 DumpCommand(objectfp, command);
645         }
646
647         BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<EventCommand>()) {
648                 DumpCommand(objectfp, command);
649         }
650
651         BOOST_FOREACH(const TimePeriod::Ptr& tp, DynamicType::GetObjects<TimePeriod>()) {
652                 DumpTimePeriod(objectfp, tp);
653         }
654
655         objectfp.close();
656
657 #ifdef _WIN32
658         _unlink(objectspath.CStr());
659 #endif /* _WIN32 */
660
661         if (rename(objectspathtmp.CStr(), objectspath.CStr()) < 0) {
662                 BOOST_THROW_EXCEPTION(posix_error()
663                     << boost::errinfo_api_function("rename")
664                     << boost::errinfo_errno(errno)
665                     << boost::errinfo_file_name(objectspathtmp));
666         }
667 }
668
669 /**
670  * Periodically writes the status.dat and objects.cache files.
671  */
672 void StatusDataWriter::StatusTimerHandler(void)
673 {
674         Log(LogInformation, "compat", "Writing status.dat file");
675
676         String statuspath = GetStatusPath();
677         String statuspathtmp = statuspath + ".tmp"; /* XXX make this a global definition */
678
679         std::ofstream statusfp;
680         statusfp.open(statuspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
681
682         statusfp << std::fixed;
683
684         statusfp << "# Icinga status file" "\n"
685                     "# This file is auto-generated. Do not modify this file." "\n"
686                     "\n";
687
688         statusfp << "info {" "\n"
689                     "\t" "created=" << Utility::GetTime() << "\n"
690                     "\t" "version=" << Application::GetVersion() << "\n"
691                     "\t" "}" "\n"
692                     "\n";
693
694         statusfp << "programstatus {" "\n"
695                     "\t" "icinga_pid=" << Utility::GetPid() << "\n"
696                     "\t" "daemon_mode=1" "\n"
697                     "\t" "program_start=" << static_cast<long>(Application::GetStartTime()) << "\n"
698                     "\t" "active_service_checks_enabled=" << (IcingaApplication::GetInstance()->GetEnableChecks() ? 1 : 0) << "\n"
699                     "\t" "passive_service_checks_enabled=1" "\n"
700                     "\t" "active_host_checks_enabled=1" "\n"
701                     "\t" "passive_host_checks_enabled=1" "\n"
702                     "\t" "check_service_freshness=1" "\n"
703                     "\t" "check_host_freshness=1" "\n"
704                     "\t" "enable_notifications=" << (IcingaApplication::GetInstance()->GetEnableNotifications() ? 1 : 0) << "\n"
705                     "\t" "enable_flap_detection=" << (IcingaApplication::GetInstance()->GetEnableFlapping() ? 1 : 0) << "\n"
706                     "\t" "enable_failure_prediction=0" "\n"
707                     "\t" "process_performance_data=" << (IcingaApplication::GetInstance()->GetEnablePerfdata() ? 1 : 0) << "\n"
708                     "\t" "active_scheduled_service_check_stats=" << CIB::GetActiveChecksStatistics(60) << "," << CIB::GetActiveChecksStatistics(5 * 60) << "," << CIB::GetActiveChecksStatistics(15 * 60) << "\n"
709                     "\t" "passive_service_check_stats=" << CIB::GetPassiveChecksStatistics(60) << "," << CIB::GetPassiveChecksStatistics(5 * 60) << "," << CIB::GetPassiveChecksStatistics(15 * 60) << "\n"
710                     "\t" "next_downtime_id=" << Service::GetNextDowntimeID() << "\n"
711                     "\t" "next_comment_id=" << Service::GetNextCommentID() << "\n"
712                     "\t" "}" "\n"
713                     "\n";
714
715         BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
716                 std::ostringstream tempstatusfp;
717                 tempstatusfp << std::fixed;
718                 DumpHostStatus(tempstatusfp, host);
719                 statusfp << tempstatusfp.str();
720         }
721
722         BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
723                 std::ostringstream tempstatusfp;
724                 tempstatusfp << std::fixed;
725                 DumpServiceStatus(tempstatusfp, service);
726                 statusfp << tempstatusfp.str();
727         }
728
729         statusfp.close();
730
731 #ifdef _WIN32
732         _unlink(statuspath.CStr());
733 #endif /* _WIN32 */
734
735         if (rename(statuspathtmp.CStr(), statuspath.CStr()) < 0) {
736                 BOOST_THROW_EXCEPTION(posix_error()
737                     << boost::errinfo_api_function("rename")
738                     << boost::errinfo_errno(errno)
739                     << boost::errinfo_file_name(statuspathtmp));
740         }
741
742         Log(LogInformation, "compat", "Finished writing status.dat file");
743 }
744