]> granicus.if.org Git - icinga2/blob - components/compat/statusdatawriter.cpp
Merge 'macros' and 'custom' attributes into 'vars', part 1.
[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                 /* only dump status data information for Classic UI */
212                 fp << "\t" "check_service=" << hc->GetShortName() << "\n";
213                 DumpServiceStatusAttrs(fp, hc, CompatTypeHost);
214         }
215
216         /* ugly but cgis parse only that */
217         fp << "\t" "last_time_up=" << host->GetLastStateUp() << "\n"
218               "\t" "last_time_down=" << host->GetLastStateDown() << "\n"
219               "\t" "last_time_unreachable=" << host->GetLastStateUnreachable() << "\n";
220
221         fp << "\t" "}" "\n"
222               "\n";
223
224         if (hc) {
225                 DumpDowntimes(fp, hc, CompatTypeHost);
226                 DumpComments(fp, hc, CompatTypeHost);
227         }
228 }
229
230 void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host)
231 {
232         String notes = CompatUtility::GetCustomAttributeConfig(host, "notes");
233         String notes_url = CompatUtility::GetCustomAttributeConfig(host, "notes_url");
234         String action_url = CompatUtility::GetCustomAttributeConfig(host, "action_url");
235         String icon_image = CompatUtility::GetCustomAttributeConfig(host, "icon_image");
236         String icon_image_alt = CompatUtility::GetCustomAttributeConfig(host, "icon_image_alt");
237         String statusmap_image = CompatUtility::GetCustomAttributeConfig(host, "statusmap_image");
238
239         fp << "define host {" "\n"
240               "\t" "host_name" "\t" << host->GetName() << "\n"
241               "\t" "display_name" "\t" << host->GetDisplayName() << "\n"
242               "\t" "alias" "\t" << host->GetDisplayName() << "\n"
243               "\t" "address" "\t" << CompatUtility::GetHostAddress(host) << "\n"
244               "\t" "address6" "\t" << CompatUtility::GetHostAddress6(host) << "\n";
245         if (!notes.IsEmpty())
246               fp << "\t" "notes" "\t" << notes << "\n";
247         if (!notes_url.IsEmpty())
248               fp << "\t" "notes_url" "\t" << notes_url << "\n";
249         if (!action_url.IsEmpty())
250               fp << "\t" "action_url" "\t" << action_url << "\n";
251         if (!icon_image.IsEmpty())
252               fp << "\t" "icon_image" "\t" << icon_image << "\n";
253         if (!icon_image_alt.IsEmpty())
254               fp << "\t" "icon_image_alt" "\t" << icon_image_alt << "\n";
255         if (!statusmap_image.IsEmpty())
256               fp << "\t" "statusmap_image" "\t" << statusmap_image << "\n";
257
258         std::set<Host::Ptr> parents = host->GetParentHosts();
259
260         if (!parents.empty()) {
261                 fp << "\t" "parents" "\t";
262                 DumpNameList(fp, parents);
263                 fp << "\n";
264         }
265
266         Service::Ptr hc = host->GetCheckService();
267         if (hc) {
268                 ObjectLock olock(hc);
269
270                 fp << "\t" "check_interval" "\t" << CompatUtility::GetServiceCheckInterval(hc) << "\n"
271                       "\t" "retry_interval" "\t" << CompatUtility::GetServiceRetryInterval(hc) << "\n"
272                       "\t" "max_check_attempts" "\t" << hc->GetMaxCheckAttempts() << "\n"
273                       "\t" "active_checks_enabled" "\t" << CompatUtility::GetServiceActiveChecksEnabled(hc) << "\n"
274                       "\t" "passive_checks_enabled" "\t" << CompatUtility::GetServicePassiveChecksEnabled(hc) << "\n"
275                       "\t" "notifications_enabled" "\t" << CompatUtility::GetServiceNotificationsEnabled(hc) << "\n"
276                       "\t" "notification_options" "\t" << "d,u,r" << "\n"
277                       "\t" "notification_interval" "\t" << CompatUtility::GetServiceNotificationNotificationInterval(hc) << "\n"
278                       "\t" "event_handler_enabled" "\t" << CompatUtility::GetServiceEventHandlerEnabled(hc) << "\n";
279
280                 CheckCommand::Ptr checkcommand = hc->GetCheckCommand();
281                 if (checkcommand)
282                         fp << "\t" "check_command" "\t" "check_" << checkcommand->GetName() << "\n";
283
284                 EventCommand::Ptr eventcommand = hc->GetEventCommand();
285                 if (eventcommand)
286                         fp << "\t" "event_handler" "\t" "event_" << eventcommand->GetName() << "\n";
287
288                 fp << "\t" "check_period" "\t" << CompatUtility::GetServiceCheckPeriod(hc) << "\n";
289
290                 fp << "\t" "contacts" "\t";
291                 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(hc));
292                 fp << "\n";
293
294                 fp << "\t" "contact_groups" "\t";
295                 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(hc));
296                 fp << "\n";
297
298                 fp << "\t" << "initial_state" "\t" "o" "\n"
299                       "\t" "low_flap_threshold" "\t" << hc->GetFlappingThreshold() << "\n"
300                       "\t" "high_flap_threshold" "\t" << hc->GetFlappingThreshold() << "\n"
301                       "\t" "process_perf_data" "\t" << CompatUtility::GetServiceProcessPerformanceData(hc) << "\n"
302                       "\t" "check_freshness" "\t" "1" "\n";
303
304         } else {
305                 fp << "\t" << "check_interval" "\t" "60" "\n"
306                       "\t" "retry_interval" "\t" "60" "\n"
307                       "\t" "max_check_attempts" "\t" "1" "\n"
308                       "\t" "active_checks_enabled" "\t" "0" << "\n"
309                       "\t" "passive_checks_enabled" "\t" "0" "\n"
310                       "\t" "notifications_enabled" "\t" "0" "\n";
311
312         }
313
314         fp << "\t" "host_groups" "\t";
315         bool first = true;
316
317         Array::Ptr groups = host->GetGroups();
318
319         if (groups) {
320                 ObjectLock olock(groups);
321
322                 BOOST_FOREACH(const String& name, groups) {
323                         HostGroup::Ptr hg = HostGroup::GetByName(name);
324
325                         if (hg) {
326                                 if (!first)
327                                         fp << ",";
328                                 else
329                                         first = false;
330
331                                 fp << hg->GetName();
332                         }
333                 }
334         }
335
336         fp << "\n";
337
338         DumpCustomAttributes(fp, host);
339
340         fp << "\t" "}" "\n"
341               "\n";
342 }
343
344 void StatusDataWriter::DumpServiceStatusAttrs(std::ostream& fp, const Service::Ptr& service, CompatObjectType type)
345 {
346         CheckResult::Ptr cr = service->GetLastCheckResult();
347
348         fp << "\t" << "check_command=check_" << CompatUtility::GetServiceCheckCommand(service) << "\n"
349               "\t" "event_handler=event_" << CompatUtility::GetServiceEventHandler(service) << "\n"
350               "\t" "check_period=" << CompatUtility::GetServiceCheckPeriod(service) << "\n"
351               "\t" "check_interval=" << CompatUtility::GetServiceCheckInterval(service) << "\n"
352               "\t" "retry_interval=" << CompatUtility::GetServiceRetryInterval(service) << "\n"
353               "\t" "has_been_checked=" << CompatUtility::GetServiceHasBeenChecked(service) << "\n"
354               "\t" "should_be_scheduled=" << CompatUtility::GetServiceShouldBeScheduled(service) << "\n";
355
356         if (cr) {
357            fp << "\t" << "check_execution_time=" << Convert::ToString(Service::CalculateExecutionTime(cr)) << "\n"
358                  "\t" "check_latency=" << Convert::ToString(Service::CalculateLatency(cr)) << "\n";
359         }
360
361         if (type == CompatTypeHost && service->IsHostCheck()) {
362                 fp << "\t" << "current_state=" << service->GetHost()->GetState() << "\n";
363         } else {
364                 fp << "\t" << "current_state=" << CompatUtility::GetServiceCurrentState(service) << "\n";
365         }
366
367         fp << "\t" "state_type=" << service->GetStateType() << "\n"
368               "\t" "plugin_output=" << CompatUtility::GetCheckResultOutput(cr) << "\n"
369               "\t" "long_plugin_output=" << CompatUtility::GetCheckResultLongOutput(cr) << "\n"
370               "\t" "performance_data=" << CompatUtility::GetCheckResultPerfdata(cr) << "\n";
371
372         if (cr) {
373            fp << "\t" << "check_source=" << cr->GetCheckSource() << "\n"
374                  "\t" "last_check=" << static_cast<long>(cr->GetScheduleEnd()) << "\n";
375         }
376
377         fp << "\t" << "next_check=" << static_cast<long>(service->GetNextCheck()) << "\n"
378               "\t" "current_attempt=" << service->GetCheckAttempt() << "\n"
379               "\t" "max_attempts=" << service->GetMaxCheckAttempts() << "\n"
380               "\t" "last_state_change=" << static_cast<long>(service->GetLastStateChange()) << "\n"
381               "\t" "last_hard_state_change=" << static_cast<long>(service->GetLastHardStateChange()) << "\n"
382               "\t" "last_time_ok=" << static_cast<int>(service->GetLastStateOK()) << "\n"
383               "\t" "last_time_warn=" << static_cast<int>(service->GetLastStateWarning()) << "\n"
384               "\t" "last_time_critical=" << static_cast<int>(service->GetLastStateCritical()) << "\n"
385               "\t" "last_time_unknown=" << static_cast<int>(service->GetLastStateUnknown()) << "\n"
386               "\t" "last_update=" << static_cast<long>(time(NULL)) << "\n"
387               "\t" "notifications_enabled=" << CompatUtility::GetServiceNotificationsEnabled(service) << "\n"
388               "\t" "active_checks_enabled=" << CompatUtility::GetServiceActiveChecksEnabled(service) << "\n"
389               "\t" "passive_checks_enabled=" << CompatUtility::GetServicePassiveChecksEnabled(service) << "\n"
390               "\t" "flap_detection_enabled=" << CompatUtility::GetServiceFlapDetectionEnabled(service) << "\n"
391               "\t" "is_flapping=" << CompatUtility::GetServiceIsFlapping(service) << "\n"
392               "\t" "percent_state_change=" << CompatUtility::GetServicePercentStateChange(service) << "\n"
393               "\t" "problem_has_been_acknowledged=" << CompatUtility::GetServiceProblemHasBeenAcknowledged(service) << "\n"
394               "\t" "acknowledgement_type=" << CompatUtility::GetServiceAcknowledgementType(service) << "\n"
395               "\t" "acknowledgement_end_time=" << service->GetAcknowledgementExpiry() << "\n"
396               "\t" "scheduled_downtime_depth=" << service->GetDowntimeDepth() << "\n"
397               "\t" "last_notification=" << CompatUtility::GetServiceNotificationLastNotification(service) << "\n"
398               "\t" "next_notification=" << CompatUtility::GetServiceNotificationNextNotification(service) << "\n"
399               "\t" "current_notification_number=" << CompatUtility::GetServiceNotificationNotificationNumber(service) << "\n"
400               "\t" "modified_attributes=" << service->GetModifiedAttributes() << "\n";
401 }
402
403 void StatusDataWriter::DumpServiceStatus(std::ostream& fp, const Service::Ptr& service)
404 {
405         Host::Ptr host = service->GetHost();
406
407         fp << "servicestatus {" "\n"
408               "\t" "host_name=" << host->GetName() << "\n"
409               "\t" "service_description=" << service->GetShortName() << "\n";
410
411         {
412                 ObjectLock olock(service);
413                 DumpServiceStatusAttrs(fp, service, CompatTypeService);
414         }
415
416         fp << "\t" "}" "\n"
417               "\n";
418
419         DumpDowntimes(fp, service, CompatTypeService);
420         DumpComments(fp, service, CompatTypeService);
421 }
422
423 void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& service)
424 {
425         Host::Ptr host = service->GetHost();
426
427         {
428                 ObjectLock olock(service);
429
430                 fp << "define service {" "\n"
431                       "\t" "host_name" "\t" << host->GetName() << "\n"
432                       "\t" "service_description" "\t" << service->GetShortName() << "\n"
433                       "\t" "display_name" "\t" << service->GetDisplayName() << "\n"
434                       "\t" "check_period" "\t" << CompatUtility::GetServiceCheckPeriod(service) << "\n"
435                       "\t" "check_interval" "\t" << CompatUtility::GetServiceCheckInterval(service) << "\n"
436                       "\t" "retry_interval" "\t" << CompatUtility::GetServiceRetryInterval(service) << "\n"
437                       "\t" "max_check_attempts" "\t" << service->GetMaxCheckAttempts() << "\n"
438                       "\t" "active_checks_enabled" "\t" << CompatUtility::GetServiceActiveChecksEnabled(service) << "\n"
439                       "\t" "passive_checks_enabled" "\t" << CompatUtility::GetServicePassiveChecksEnabled(service) << "\n"
440                       "\t" "flap_detection_enabled" "\t" << CompatUtility::GetServiceFlapDetectionEnabled(service) << "\n"
441                       "\t" "is_volatile" "\t" << CompatUtility::GetServiceIsVolatile(service) << "\n"
442                       "\t" "notifications_enabled" "\t" << CompatUtility::GetServiceNotificationsEnabled(service) << "\n"
443                       "\t" "notification_options" "\t" << CompatUtility::GetServiceNotificationNotificationOptions(service) << "\n"
444                       "\t" "notification_interval" "\t" << CompatUtility::GetServiceNotificationNotificationInterval(service) << "\n"
445                       "\t" "notification_period" "\t" << CompatUtility::GetServiceNotificationNotificationPeriod(service) << "\n"
446                       "\t" "event_handler_enabled" "\t" << CompatUtility::GetServiceEventHandlerEnabled(service) << "\n";
447
448                 CheckCommand::Ptr checkcommand = service->GetCheckCommand();
449                 if (checkcommand)
450                         fp << "\t" "check_command" "\t" "check_" << checkcommand->GetName() << "\n";
451
452                 EventCommand::Ptr eventcommand = service->GetEventCommand();
453                 if (eventcommand)
454                         fp << "\t" "event_handler" "\t" "event_" << eventcommand->GetName() << "\n";
455
456                 fp << "\t" "contacts" "\t";
457                 DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(service));
458                 fp << "\n";
459
460                 fp << "\t" "contact_groups" "\t";
461                 DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(service));
462                 fp << "\n";
463
464                 String notes = CompatUtility::GetCustomAttributeConfig(service, "notes");
465                 String notes_url = CompatUtility::GetCustomAttributeConfig(service, "notes_url");
466                 String action_url = CompatUtility::GetCustomAttributeConfig(service, "action_url");
467                 String icon_image = CompatUtility::GetCustomAttributeConfig(service, "icon_image");
468                 String icon_image_alt = CompatUtility::GetCustomAttributeConfig(service, "icon_image_alt");
469
470                 fp << "\t" "initial_state" "\t" "o" "\n"
471                       "\t" "low_flap_threshold" "\t" << service->GetFlappingThreshold() << "\n"
472                       "\t" "high_flap_threshold" "\t" << service->GetFlappingThreshold() << "\n"
473                       "\t" "process_perf_data" "\t" << CompatUtility::GetServiceProcessPerformanceData(service) << "\n"
474                       "\t" "check_freshness" << "\t" "1" "\n";
475                 if (!notes.IsEmpty())
476                       fp << "\t" "notes" "\t" << notes << "\n";
477                 if (!notes_url.IsEmpty())
478                       fp << "\t" "notes_url" "\t" << notes_url << "\n";
479                 if (!action_url.IsEmpty())
480                       fp << "\t" "action_url" "\t" << action_url << "\n";
481                 if (!icon_image.IsEmpty())
482                       fp << "\t" "icon_image" "\t" << icon_image << "\n";
483                 if (!icon_image_alt.IsEmpty())
484                       fp << "\t" "icon_image_alt" "\t" << icon_image_alt << "\n";
485         }
486
487         fp << "\t" "service_groups" "\t";
488         bool first = true;
489
490         Array::Ptr groups = service->GetGroups();
491
492         if (groups) {
493                 ObjectLock olock(groups);
494
495                 BOOST_FOREACH(const String& name, groups) {
496                         ServiceGroup::Ptr sg = ServiceGroup::GetByName(name);
497
498                         if (sg) {
499                                 if (!first)
500                                         fp << ",";
501                                 else
502                                         first = false;
503
504                                 fp << sg->GetName();
505                         }
506                 }
507         }
508
509         fp << "\n";
510
511         DumpCustomAttributes(fp, service);
512
513         fp << "\t" "}" "\n"
514               "\n";
515 }
516
517 void StatusDataWriter::DumpCustomAttributes(std::ostream& fp, const DynamicObject::Ptr& object)
518 {
519         Dictionary::Ptr vars = object->GetVars();
520
521         if (!vars)
522                 return;
523
524         ObjectLock olock(vars);
525         BOOST_FOREACH(const Dictionary::Pair& kv, vars) {
526                 if (!kv.first.IsEmpty()) {
527                         fp << "\t";
528
529                         if (kv.first != "notes" && kv.first != "action_url" && kv.first != "notes_url" &&
530                             kv.first != "icon_image" && kv.first != "icon_image_alt" && kv.first != "statusmap_image" && kv.first != "2d_coords")
531                                 fp << "_";
532
533                         fp << kv.first << "\t" << kv.second << "\n";
534                 }
535         }
536 }
537
538 void StatusDataWriter::UpdateObjectsCache(void)
539 {
540         CONTEXT("Writing objects.cache file");
541
542         String objectspath = GetObjectsPath();
543         String objectspathtmp = objectspath + ".tmp";
544
545         std::ofstream objectfp;
546         objectfp.open(objectspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
547
548         objectfp << std::fixed;
549
550         objectfp << "# Icinga objects cache file" "\n"
551                     "# This file is auto-generated. Do not modify this file." "\n"
552                     "\n";
553
554         BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
555                 std::ostringstream tempobjectfp;
556                 tempobjectfp << std::fixed;
557                 DumpHostObject(tempobjectfp, host);
558                 objectfp << tempobjectfp.str();
559
560                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
561                         std::ostringstream tempobjectfp;
562                         tempobjectfp << std::fixed;
563                         DumpServiceObject(tempobjectfp, service);
564                         objectfp << tempobjectfp.str();
565                 }
566         }
567
568         BOOST_FOREACH(const HostGroup::Ptr& hg, DynamicType::GetObjects<HostGroup>()) {
569                 std::ostringstream tempobjectfp;
570                 tempobjectfp << std::fixed;
571
572                 tempobjectfp << "define hostgroup {" "\n"
573                                 "\t" "hostgroup_name" "\t" << hg->GetName() << "\n";
574
575                 DumpCustomAttributes(tempobjectfp, hg);
576
577                 tempobjectfp << "\t" "members" "\t";
578                 DumpNameList(tempobjectfp, hg->GetMembers());
579                 tempobjectfp << "\n"
580                                 "\t" "}" "\n";
581
582                 objectfp << tempobjectfp.str();
583         }
584
585         BOOST_FOREACH(const ServiceGroup::Ptr& sg, DynamicType::GetObjects<ServiceGroup>()) {
586                 std::ostringstream tempobjectfp;
587                 tempobjectfp << std::fixed;
588
589                 tempobjectfp << "define servicegroup {" "\n"
590                                 "\t" "servicegroup_name" "\t" << sg->GetName() << "\n";
591
592                 DumpCustomAttributes(tempobjectfp, sg);
593
594                 tempobjectfp << "\t" "members" "\t";
595
596                 std::vector<String> sglist;
597                 BOOST_FOREACH(const Service::Ptr& service, sg->GetMembers()) {
598                         Host::Ptr host = service->GetHost();
599
600                         sglist.push_back(host->GetName());
601                         sglist.push_back(service->GetShortName());
602                 }
603
604                 DumpStringList(tempobjectfp, sglist);
605
606                 tempobjectfp << "\n"
607                                 "}" "\n";
608
609                 objectfp << tempobjectfp.str();
610         }
611
612         BOOST_FOREACH(const User::Ptr& user, DynamicType::GetObjects<User>()) {
613                 std::ostringstream tempobjectfp;
614                 tempobjectfp << std::fixed;
615
616                 tempobjectfp << "define contact {" "\n"
617                                 "\t" "contact_name" "\t" << user->GetName() << "\n"
618                                 "\t" "alias" "\t" << user->GetDisplayName() << "\n"
619                                 "\t" "service_notification_options" "\t" "w,u,c,r,f,s" "\n"
620                                 "\t" "host_notification_options""\t" "d,u,r,f,s" "\n"
621                                 "\t" "host_notifications_enabled" "\t" "1" "\n"
622                                 "\t" "service_notifications_enabled" "\t" "1" "\n"
623                                 "\t" "}" "\n"
624                                 "\n";
625
626                 objectfp << tempobjectfp.str();
627         }
628
629         BOOST_FOREACH(const UserGroup::Ptr& ug, DynamicType::GetObjects<UserGroup>()) {
630                 std::ostringstream tempobjectfp;
631                 tempobjectfp << std::fixed;
632
633                 tempobjectfp << "define contactgroup {" "\n"
634                                 "\t" "contactgroup_name" "\t" << ug->GetName() << "\n"
635                                 "\t" "alias" "\t" << ug->GetDisplayName() << "\n";
636
637                 tempobjectfp << "\t" "members" "\t";
638                 DumpNameList(tempobjectfp, ug->GetMembers());
639                 tempobjectfp << "\n"
640                                 "\t" "}" "\n";
641
642                 objectfp << tempobjectfp.str();
643         }
644
645         BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<CheckCommand>()) {
646                 DumpCommand(objectfp, command);
647         }
648
649         BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<NotificationCommand>()) {
650                 DumpCommand(objectfp, command);
651         }
652
653         BOOST_FOREACH(const Command::Ptr& command, DynamicType::GetObjects<EventCommand>()) {
654                 DumpCommand(objectfp, command);
655         }
656
657         BOOST_FOREACH(const TimePeriod::Ptr& tp, DynamicType::GetObjects<TimePeriod>()) {
658                 DumpTimePeriod(objectfp, tp);
659         }
660
661         BOOST_FOREACH(const Dependency::Ptr& dep, DynamicType::GetObjects<Dependency>()) {
662                 Service::Ptr parent_service = dep->GetParentService();
663
664                 if (!parent_service)
665                         continue;
666
667                 Service::Ptr child_service = dep->GetChildService();
668
669                 if (!child_service)
670                         continue;
671
672                 objectfp << "define servicedependency {" "\n"
673                             "\t" "dependent_host_name" "\t" << child_service->GetHost()->GetName() << "\n"
674                             "\t" "dependent_service_description" "\t" << child_service->GetShortName() << "\n"
675                             "\t" "host_name" "\t" << parent_service->GetHost()->GetName() << "\n"
676                             "\t" "service_description" "\t" << parent_service->GetShortName() << "\n"
677                             "\t" "execution_failure_criteria" "\t" "n" "\n"
678                             "\t" "notification_failure_criteria" "\t" "w,u,c" "\n"
679                             "\t" "}" "\n"
680                             "\n";
681         }
682
683         objectfp.close();
684
685 #ifdef _WIN32
686         _unlink(objectspath.CStr());
687 #endif /* _WIN32 */
688
689         if (rename(objectspathtmp.CStr(), objectspath.CStr()) < 0) {
690                 BOOST_THROW_EXCEPTION(posix_error()
691                     << boost::errinfo_api_function("rename")
692                     << boost::errinfo_errno(errno)
693                     << boost::errinfo_file_name(objectspathtmp));
694         }
695 }
696
697 /**
698  * Periodically writes the status.dat and objects.cache files.
699  */
700 void StatusDataWriter::StatusTimerHandler(void)
701 {
702         Log(LogInformation, "compat", "Writing status.dat file");
703
704         String statuspath = GetStatusPath();
705         String statuspathtmp = statuspath + ".tmp"; /* XXX make this a global definition */
706
707         std::ofstream statusfp;
708         statusfp.open(statuspathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
709
710         statusfp << std::fixed;
711
712         statusfp << "# Icinga status file" "\n"
713                     "# This file is auto-generated. Do not modify this file." "\n"
714                     "\n";
715
716         statusfp << "info {" "\n"
717                     "\t" "created=" << Utility::GetTime() << "\n"
718                     "\t" "version=" << Application::GetVersion() << "\n"
719                     "\t" "}" "\n"
720                     "\n";
721
722         statusfp << "programstatus {" "\n"
723                     "\t" "icinga_pid=" << Utility::GetPid() << "\n"
724                     "\t" "daemon_mode=1" "\n"
725                     "\t" "program_start=" << static_cast<long>(Application::GetStartTime()) << "\n"
726                     "\t" "active_service_checks_enabled=" << (IcingaApplication::GetInstance()->GetEnableChecks() ? 1 : 0) << "\n"
727                     "\t" "passive_service_checks_enabled=1" "\n"
728                     "\t" "active_host_checks_enabled=1" "\n"
729                     "\t" "passive_host_checks_enabled=1" "\n"
730                     "\t" "check_service_freshness=1" "\n"
731                     "\t" "check_host_freshness=1" "\n"
732                     "\t" "enable_notifications=" << (IcingaApplication::GetInstance()->GetEnableNotifications() ? 1 : 0) << "\n"
733                     "\t" "enable_flap_detection=" << (IcingaApplication::GetInstance()->GetEnableFlapping() ? 1 : 0) << "\n"
734                     "\t" "enable_failure_prediction=0" "\n"
735                     "\t" "process_performance_data=" << (IcingaApplication::GetInstance()->GetEnablePerfdata() ? 1 : 0) << "\n"
736                     "\t" "active_scheduled_service_check_stats=" << CIB::GetActiveChecksStatistics(60) << "," << CIB::GetActiveChecksStatistics(5 * 60) << "," << CIB::GetActiveChecksStatistics(15 * 60) << "\n"
737                     "\t" "passive_service_check_stats=" << CIB::GetPassiveChecksStatistics(60) << "," << CIB::GetPassiveChecksStatistics(5 * 60) << "," << CIB::GetPassiveChecksStatistics(15 * 60) << "\n"
738                     "\t" "next_downtime_id=" << Service::GetNextDowntimeID() << "\n"
739                     "\t" "next_comment_id=" << Service::GetNextCommentID() << "\n"
740                     "\t" "}" "\n"
741                     "\n";
742
743         BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
744                 std::ostringstream tempstatusfp;
745                 tempstatusfp << std::fixed;
746                 DumpHostStatus(tempstatusfp, host);
747                 statusfp << tempstatusfp.str();
748
749                 BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
750                         std::ostringstream tempstatusfp;
751                         tempstatusfp << std::fixed;
752                         DumpServiceStatus(tempstatusfp, service);
753                         statusfp << tempstatusfp.str();
754                 }
755         }
756
757         statusfp.close();
758
759 #ifdef _WIN32
760         _unlink(statuspath.CStr());
761 #endif /* _WIN32 */
762
763         if (rename(statuspathtmp.CStr(), statuspath.CStr()) < 0) {
764                 BOOST_THROW_EXCEPTION(posix_error()
765                     << boost::errinfo_api_function("rename")
766                     << boost::errinfo_errno(errno)
767                     << boost::errinfo_file_name(statuspathtmp));
768         }
769
770         Log(LogInformation, "compat", "Finished writing status.dat file");
771 }
772