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