]> granicus.if.org Git - icinga2/blob - components/compat/statusdatawriter.cpp
Change copyright header (again).
[icinga2] / components / compat / statusdatawriter.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 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 "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>
42 #include <fstream>
43
44 using namespace icinga;
45
46 REGISTER_TYPE(StatusDataWriter);
47
48 REGISTER_STATSFUNCTION(StatusDataWriterStats, &StatusDataWriter::StatsFunc);
49
50 Value StatusDataWriter::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata)
51 {
52         Dictionary::Ptr nodes = make_shared<Dictionary>();
53
54         BOOST_FOREACH(const StatusDataWriter::Ptr& statusdatawriter, DynamicType::GetObjects<StatusDataWriter>()) {
55                 nodes->Set(statusdatawriter->GetName(), 1); //add more stats
56         }
57
58         status->Set("statusdatawriter", nodes);
59
60         return 0;
61 }
62
63 /**
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).
67  */
68
69 /**
70  * Starts the component.
71  */
72 void StatusDataWriter::Start(void)
73 {
74         DynamicObject::Start();
75
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);
81
82         Utility::QueueAsyncCallback(boost::bind(&StatusDataWriter::UpdateObjectsCache, this));
83 }
84
85 void StatusDataWriter::DumpComments(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
86 {
87         Service::Ptr service;
88         Dictionary::Ptr comments = owner->GetComments();
89
90         Host::Ptr host = owner->GetHost();
91
92         ObjectLock olock(comments);
93
94         BOOST_FOREACH(const Dictionary::Pair& kv, comments) {
95                 Comment::Ptr comment = kv.second;
96
97                 if (comment->IsExpired())
98                         continue;
99
100                 if (type == CompatTypeHost)
101                         fp << "hostcomment {" << "\n";
102                 else
103                         fp << "servicecomment {" << "\n"
104                            << "\t" << "service_description=" << owner->GetShortName() << "\n";
105
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"
115                       "\t" "}" "\n"
116                       "\n";
117         }
118 }
119
120 void StatusDataWriter::DumpTimePeriod(std::ostream& fp, const TimePeriod::Ptr& tp)
121 {
122         fp << "define timeperiod {" "\n"
123               "\t" "timeperiod_name" "\t" << tp->GetName() << "\n"
124               "\t" "alias" "\t" << tp->GetName() << "\n";
125
126         Dictionary::Ptr ranges = tp->GetRanges();
127
128         if (ranges) {
129                 ObjectLock olock(ranges);
130                 BOOST_FOREACH(const Dictionary::Pair& kv, ranges) {
131                         fp << "\t" << kv.first << "\t" << kv.second << "\n";
132                 }
133         }
134
135         fp << "\t" "}" "\n"
136               "\n";
137 }
138
139 void StatusDataWriter::DumpCommand(std::ostream& fp, const Command::Ptr& command)
140 {
141         fp << "define command {" "\n"
142               "\t" "command_name\t";
143
144
145         if (command->GetType() == DynamicType::GetByName("CheckCommand"))
146                 fp << "check_";
147         else if (command->GetType() == DynamicType::GetByName("NotificationCommand"))
148                 fp << "notification_";
149         else if (command->GetType() == DynamicType::GetByName("EventCommand"))
150                 fp << "event_";
151
152         fp << command->GetName() << "\n";
153
154         fp << "\t" "command_line" "\t" << CompatUtility::GetCommandLine(command);
155
156         fp << "\n" "\t" "}" "\n"
157               "\n";
158 }
159
160 void StatusDataWriter::DumpDowntimes(std::ostream& fp, const Service::Ptr& owner, CompatObjectType type)
161 {
162         Host::Ptr host = owner->GetHost();
163
164         Dictionary::Ptr downtimes = owner->GetDowntimes();
165
166         ObjectLock olock(downtimes);
167
168         BOOST_FOREACH(const Dictionary::Pair& kv, downtimes) {
169                 Downtime::Ptr downtime = kv.second;
170
171                 if (downtime->IsExpired())
172                         continue;
173
174                 if (type == CompatTypeHost)
175                         fp << "hostdowntime {" "\n";
176                 else
177                         fp << "servicedowntime {" << "\n"
178                               "\t" "service_description=" << owner->GetShortName() << "\n";
179
180                 Downtime::Ptr triggeredByObj = Service::GetDowntimeByID(downtime->GetTriggeredBy());
181                 int triggeredByLegacy = 0;
182                 if (triggeredByObj)
183                         triggeredByLegacy = triggeredByObj->GetLegacyId();
184
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"
197                       "\t" "}" "\n"
198                       "\n";
199         }
200 }
201
202 void StatusDataWriter::DumpHostStatus(std::ostream& fp, const Host::Ptr& host)
203 {
204         fp << "hoststatus {" << "\n"
205            << "\t" << "host_name=" << host->GetName() << "\n";
206
207         Service::Ptr hc = host->GetCheckService();
208         ObjectLock olock(hc);
209
210         if (hc)
211                 DumpServiceStatusAttrs(fp, hc, CompatTypeHost);
212
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";
217
218         fp << "\t" "}" "\n"
219               "\n";
220
221         if (hc) {
222                 DumpDowntimes(fp, hc, CompatTypeHost);
223                 DumpComments(fp, hc, CompatTypeHost);
224         }
225 }
226
227 void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host)
228 {
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");
235
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";
254
255         std::set<Host::Ptr> parents = host->GetParentHosts();
256
257         if (!parents.empty()) {
258                 fp << "\t" "parents" "\t";
259                 DumpNameList(fp, parents);
260                 fp << "\n";
261         }
262
263         Service::Ptr hc = host->GetCheckService();
264         if (hc) {
265                 ObjectLock olock(hc);
266
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";
276
277                 CheckCommand::Ptr checkcommand = hc->GetCheckCommand();
278                 if (checkcommand)
279                         fp << "\t" "check_command" "\t" "check_" << checkcommand->GetName() << "\n";
280
281                 EventCommand::Ptr eventcommand = hc->GetEventCommand();
282                 if (eventcommand)
283                         fp << "\t" "event_handler" "\t" "event_" << eventcommand->GetName() << "\n";
284
285                 fp << "\t" "check_period" "\t" << CompatUtility::GetServiceCheckPeriod(hc) << "\n";
286
287                 fp << "\t" "contacts" "\t";
288                 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(hc));
289                 fp << "\n";
290
291                 fp << "\t" "contact_groups" "\t";
292                 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(hc));
293                 fp << "\n";
294
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";
300
301         } else {
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";
308
309         }
310
311         fp << "\t" "host_groups" "\t";
312         bool first = true;
313
314         Array::Ptr groups = host->GetGroups();
315
316         if (groups) {
317                 ObjectLock olock(groups);
318
319                 BOOST_FOREACH(const String& name, groups) {
320                         HostGroup::Ptr hg = HostGroup::GetByName(name);
321
322                         if (hg) {
323                                 if (!first)
324                                         fp << ",";
325                                 else
326                                         first = false;
327
328                                 fp << hg->GetName();
329                         }
330                 }
331         }
332
333         fp << "\n";
334
335         DumpCustomAttributes(fp, host);
336
337         fp << "\t" "}" "\n"
338               "\n";
339 }
340
341 void StatusDataWriter::DumpServiceStatusAttrs(std::ostream& fp, const Service::Ptr& service, CompatObjectType type)
342 {
343         CheckResult::Ptr cr = service->GetLastCheckResult();
344
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";
352
353         if (cr) {
354            fp << "\t" << "check_execution_time=" << Convert::ToString(Service::CalculateExecutionTime(cr)) << "\n"
355                  "\t" "check_latency=" << Convert::ToString(Service::CalculateLatency(cr)) << "\n";
356         }
357
358         if (type == CompatTypeHost && service->IsHostCheck()) {
359                 fp << "\t" << "current_state=" << service->GetHost()->GetState() << "\n";
360         } else {
361                 fp << "\t" << "current_state=" << CompatUtility::GetServiceCurrentState(service) << "\n";
362         }
363
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";
368
369         if (cr) {
370            fp << "\t" << "check_source=" << cr->GetCheckSource() << "\n"
371                  "\t" "last_check=" << static_cast<long>(cr->GetScheduleEnd()) << "\n";
372         }
373
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";
398 }
399
400 void StatusDataWriter::DumpServiceStatus(std::ostream& fp, const Service::Ptr& service)
401 {
402         Host::Ptr host = service->GetHost();
403
404         fp << "servicestatus {" "\n"
405               "\t" "host_name=" << host->GetName() << "\n"
406               "\t" "service_description=" << service->GetShortName() << "\n";
407
408         {
409                 ObjectLock olock(service);
410                 DumpServiceStatusAttrs(fp, service, CompatTypeService);
411         }
412
413         fp << "\t" "}" "\n"
414               "\n";
415
416         DumpDowntimes(fp, service, CompatTypeService);
417         DumpComments(fp, service, CompatTypeService);
418 }
419
420 void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& service)
421 {
422         Host::Ptr host = service->GetHost();
423
424         {
425                 ObjectLock olock(service);
426
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";
444
445                 CheckCommand::Ptr checkcommand = service->GetCheckCommand();
446                 if (checkcommand)
447                         fp << "\t" "check_command" "\t" "check_" << checkcommand->GetName() << "\n";
448
449                 EventCommand::Ptr eventcommand = service->GetEventCommand();
450                 if (eventcommand)
451                         fp << "\t" "event_handler" "\t" "event_" << eventcommand->GetName() << "\n";
452
453                 fp << "\t" "contacts" "\t";
454                 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(service));
455                 fp << "\n";
456
457                 fp << "\t" "contact_groups" "\t";
458                 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(service));
459                 fp << "\n";
460
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");
466
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";
482         }
483
484         fp << "\t" "service_groups" "\t";
485         bool first = true;
486
487         Array::Ptr groups = service->GetGroups();
488
489         if (groups) {
490                 ObjectLock olock(groups);
491
492                 BOOST_FOREACH(const String& name, groups) {
493                         ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);
494
495                         if (sg) {
496                                 if (!first)
497                                         fp << ",";
498                                 else
499                                         first = false;
500
501                                 fp << sg->GetName();
502                         }
503                 }
504         }
505
506         fp << "\n";
507
508         DumpCustomAttributes(fp, service);
509
510         fp << "\t" "}" "\n"
511               "\n";
512 }
513
514 void StatusDataWriter::DumpCustomAttributes(std::ostream& fp, const DynamicObject::Ptr& object)
515 {
516         Dictionary::Ptr custom = object->GetCustom();
517
518         if (!custom)
519                 return;
520
521         ObjectLock olock(custom);
522         BOOST_FOREACH(const Dictionary::Pair& kv, custom) {
523                 if (!kv.first.IsEmpty()) {
524                         fp << "\t";
525
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")
528                                 fp << "_";
529
530                         fp << kv.first << "\t" << kv.second << "\n";
531                 }
532         }
533 }
534
535 void StatusDataWriter::UpdateObjectsCache(void)
536 {
537         CONTEXT("Writing objects.cache file");
538
539         String objectspath = GetObjectsPath();
540         String objectspathtmp = objectspath + ".tmp";
541
542         std::ofstream objectfp;
543         objectfp.open(objectspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
544
545         objectfp << std::fixed;
546
547         objectfp << "# Icinga objects cache file" "\n"
548                     "# This file is auto-generated. Do not modify this file." "\n"
549                     "\n";
550
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();
556
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();
562                 }
563         }
564
565         BOOST_FOREACH(const HostGroup::Ptr& hg, DynamicType::GetObjects<HostGroup>()) {
566                 std::ostringstream tempobjectfp;
567                 tempobjectfp << std::fixed;
568
569                 tempobjectfp << "define hostgroup {" "\n"
570                                 "\t" "hostgroup_name" "\t" << hg->GetName() << "\n";
571
572                 DumpCustomAttributes(tempobjectfp, hg);
573
574                 tempobjectfp << "\t" "members" "\t";
575                 DumpNameList(tempobjectfp, hg->GetMembers());
576                 tempobjectfp << "\n"
577                                 "\t" "}" "\n";
578
579                 objectfp << tempobjectfp.str();
580         }
581
582         BOOST_FOREACH(const ServiceGroup::Ptr& sg, DynamicType::GetObjects<ServiceGroup>()) {
583                 std::ostringstream tempobjectfp;
584                 tempobjectfp << std::fixed;
585
586                 tempobjectfp << "define servicegroup {" "\n"
587                                 "\t" "servicegroup_name" "\t" << sg->GetName() << "\n";
588
589                 DumpCustomAttributes(tempobjectfp, sg);
590
591                 tempobjectfp << "\t" "members" "\t";
592
593                 std::vector<String> sglist;
594                 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
595                         Host::Ptr host = service->GetHost();
596
597                         sglist.push_back(host->GetName());
598                         sglist.push_back(service->GetShortName());
599                 }
600
601                 DumpStringList(tempobjectfp, sglist);
602
603                 tempobjectfp << "\n"
604                                 "}" "\n";
605
606                 objectfp << tempobjectfp.str();
607         }
608
609         BOOST_FOREACH(const User::Ptr& user, DynamicType::GetObjects<User>()) {
610                 std::ostringstream tempobjectfp;
611                 tempobjectfp << std::fixed;
612
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"
620                                 "\t" "}" "\n"
621                                 "\n";
622
623                 objectfp << tempobjectfp.str();
624         }
625
626         BOOST_FOREACH(const UserGroup::Ptr& ug, DynamicType::GetObjects<UserGroup>()) {
627                 std::ostringstream tempobjectfp;
628                 tempobjectfp << std::fixed;
629
630                 tempobjectfp << "define contactgroup {" "\n"
631                                 "\t" "contactgroup_name" "\t" << ug->GetName() << "\n"
632                                 "\t" "alias" "\t" << ug->GetDisplayName() << "\n";
633
634                 tempobjectfp << "\t" "members" "\t";
635                 DumpNameList(tempobjectfp, ug->GetMembers());
636                 tempobjectfp << "\n"
637                                 "\t" "}" "\n";
638
639                 objectfp << tempobjectfp.str();
640         }
641
642         BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<CheckCommand>()) {
643                 DumpCommand(objectfp, command);
644         }
645
646         BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<NotificationCommand>()) {
647                 DumpCommand(objectfp, command);
648         }
649
650         BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<EventCommand>()) {
651                 DumpCommand(objectfp, command);
652         }
653
654         BOOST_FOREACH(const TimePeriod::Ptr& tp, DynamicType::GetObjects<TimePeriod>()) {
655                 DumpTimePeriod(objectfp, tp);
656         }
657
658         BOOST_FOREACH(const Dependency::Ptr& dep, DynamicType::GetObjects<Dependency>()) {
659                 Service::Ptr parent_service = dep->GetParentService();
660
661                 if (!parent_service)
662                         continue;
663
664                 Service::Ptr child_service = dep->GetChildService();
665
666                 if (!child_service)
667                         continue;
668
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"
676                             "\t" "}" "\n"
677                             "\n";
678         }
679
680         objectfp.close();
681
682 #ifdef _WIN32
683         _unlink(objectspath.CStr());
684 #endif /* _WIN32 */
685
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));
691         }
692 }
693
694 /**
695  * Periodically writes the status.dat and objects.cache files.
696  */
697 void StatusDataWriter::StatusTimerHandler(void)
698 {
699         Log(LogInformation, "compat", "Writing status.dat file");
700
701         String statuspath = GetStatusPath();
702         String statuspathtmp = statuspath + ".tmp"; /* XXX make this a global definition */
703
704         std::ofstream statusfp;
705         statusfp.open(statuspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
706
707         statusfp << std::fixed;
708
709         statusfp << "# Icinga status file" "\n"
710                     "# This file is auto-generated. Do not modify this file." "\n"
711                     "\n";
712
713         statusfp << "info {" "\n"
714                     "\t" "created=" << Utility::GetTime() << "\n"
715                     "\t" "version=" << Application::GetVersion() << "\n"
716                     "\t" "}" "\n"
717                     "\n";
718
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"
737                     "\t" "}" "\n"
738                     "\n";
739
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();
745
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();
751                 }
752         }
753
754         statusfp.close();
755
756 #ifdef _WIN32
757         _unlink(statuspath.CStr());
758 #endif /* _WIN32 */
759
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));
765         }
766
767         Log(LogInformation, "compat", "Finished writing status.dat file");
768 }
769