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