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