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