From 66765311c31be29883ec587a5af49ed669da4824 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Thu, 5 Dec 2013 14:54:16 +0100 Subject: [PATCH] Refactor CompatUtility::* for DB IDO, Livestatus, StatusData. Fixes #5241 --- components/compat/compatlogger.cpp | 24 +- components/compat/statusdatawriter.cpp | 157 ++--- components/livestatus/hoststable.cpp | 494 ++++++++----- components/livestatus/servicestable.cpp | 216 ++---- lib/db_ido/commanddbobject.cpp | 6 +- lib/db_ido/hostdbobject.cpp | 207 +++--- lib/db_ido/servicedbobject.cpp | 223 +++--- lib/icinga/compatutility.cpp | 888 +++++++++++++++--------- lib/icinga/compatutility.h | 81 ++- 9 files changed, 1291 insertions(+), 1005 deletions(-) diff --git a/components/compat/compatlogger.cpp b/components/compat/compatlogger.cpp index 75fd435ea..c064b49fa 100644 --- a/components/compat/compatlogger.cpp +++ b/components/compat/compatlogger.cpp @@ -98,10 +98,8 @@ void CompatLogger::CheckResultHandler(const Service::Ptr& service, const CheckRe } String output; - if (cr) { - std::pair output_bag = CompatUtility::GetCheckResultOutput(cr); - output = output_bag.first; - } + if (cr) + output = CompatUtility::GetCheckResultOutput(cr); std::ostringstream msgbuf; msgbuf << "SERVICE ALERT: " @@ -258,10 +256,8 @@ void CompatLogger::NotificationSentHandler(const Service::Ptr& service, const Us return; String output; - if (cr) { - std::pair output_bag = CompatUtility::GetCheckResultOutput(cr); - output = output_bag.first; - } + if (cr) + output = CompatUtility::GetCheckResultOutput(cr); std::ostringstream msgbuf; msgbuf << "SERVICE NOTIFICATION: " @@ -489,10 +485,8 @@ void CompatLogger::ReopenFile(bool rotate) String output; CheckResult::Ptr cr = hc->GetLastCheckResult(); - if (cr) { - std::pair output_bag = CompatUtility::GetCheckResultOutput(cr); - output = output_bag.first; - } + if (cr) + output = CompatUtility::GetCheckResultOutput(cr); std::ostringstream msgbuf; msgbuf << "CURRENT HOST STATE: " @@ -511,10 +505,8 @@ void CompatLogger::ReopenFile(bool rotate) String output; CheckResult::Ptr cr = service->GetLastCheckResult(); - if (cr) { - std::pair output_bag = CompatUtility::GetCheckResultOutput(cr); - output = output_bag.first; - } + if (cr) + output = CompatUtility::GetCheckResultOutput(cr); std::ostringstream msgbuf; msgbuf << "CURRENT SERVICE STATE: " diff --git a/components/compat/statusdatawriter.cpp b/components/compat/statusdatawriter.cpp index 9a79a2b3c..547243c46 100644 --- a/components/compat/statusdatawriter.cpp +++ b/components/compat/statusdatawriter.cpp @@ -232,14 +232,15 @@ void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host) fp << "define host {" << "\n" << "\t" << "host_name" << "\t" << host->GetName() << "\n" << "\t" << "display_name" << "\t" << host->GetDisplayName() << "\n" - << "\t" << "alias" << "\t" << host->GetDisplayName() << "\n"; - - Dictionary::Ptr macros = host->GetMacros(); - - if (macros) { - fp << "\t" << "address" << "\t" << macros->Get("address") << "\n" - << "\t" << "address6" << "\t" << macros->Get("address6") << "\n"; - } + << "\t" << "alias" << "\t" << host->GetDisplayName() << "\n" + << "\t" << "address" << "\t" << CompatUtility::GetHostAddress(host) << "\n" + << "\t" << "address6" << "\t" << CompatUtility::GetHostAddress6(host) << "\n" + << "\t" << "notes" << "\t" << CompatUtility::GetCustomAttributeConfig(host, "notes") << "\n" + << "\t" << "notes_url" << "\t" << CompatUtility::GetCustomAttributeConfig(host, "notes_url") << "\n" + << "\t" << "action_url" << "\t" << CompatUtility::GetCustomAttributeConfig(host, "action_url") << "\n" + << "\t" << "icon_image" << "\t" << CompatUtility::GetCustomAttributeConfig(host, "icon_image") << "\n" + << "\t" << "icon_image_alt" << "\t" << CompatUtility::GetCustomAttributeConfig(host, "icon_image_alt") << "\n" + << "\t" << "statusmap_image" << "\t" << CompatUtility::GetCustomAttributeConfig(host, "statusmap_image") << "\n"; std::set parents = host->GetParentHosts(); @@ -253,14 +254,14 @@ void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host) if (hc) { ObjectLock olock(hc); - fp << "\t" << "check_interval" << "\t" << hc->GetCheckInterval() / 60.0 << "\n" - << "\t" << "retry_interval" << "\t" << hc->GetRetryInterval() / 60.0 << "\n" + fp << "\t" << "check_interval" << "\t" << CompatUtility::GetServiceCheckInterval(hc) << "\n" + << "\t" << "retry_interval" << "\t" << CompatUtility::GetServiceRetryInterval(hc) << "\n" << "\t" << "max_check_attempts" << "\t" << hc->GetMaxCheckAttempts() << "\n" << "\t" << "active_checks_enabled" << "\t" << (hc->GetEnableActiveChecks() ? 1 : 0) << "\n" << "\t" << "passive_checks_enabled" << "\t" << (hc->GetEnablePassiveChecks() ? 1 : 0) << "\n" << "\t" << "notifications_enabled" << "\t" << (hc->GetEnableNotifications() ? 1 : 0) << "\n" << "\t" << "notification_options" << "\t" << "d,u,r" << "\n" - << "\t" << "notification_interval" << "\t" << 1 << "\n" + << "\t" << "notification_interval" << "\t" << CompatUtility::GetServiceNotificationNotificationInterval(hc) << "\n" << "\t" << "event_handler_enabled" << "\t" << (hc->GetEnableEventHandler() ? 1 : 0) << "\n"; CheckCommand::Ptr checkcommand = hc->GetCheckCommand(); @@ -271,9 +272,7 @@ void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host) if (eventcommand) fp << "\t" << "event_handler" << "\t" << "event_" << eventcommand->GetName() << "\n"; - TimePeriod::Ptr check_period = hc->GetCheckPeriod(); - if (check_period) - fp << "\t" << "check_period" << "\t" << check_period->GetName() << "\n"; + fp << "\t" << "check_period" << "\t" << CompatUtility::GetServiceCheckPeriod(hc) << "\n"; fp << "\t" << "contacts" << "\t"; DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(hc)); @@ -331,48 +330,56 @@ void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host) void StatusDataWriter::DumpServiceStatusAttrs(std::ostream& fp, const Service::Ptr& service, CompatObjectType type) { - Dictionary::Ptr attrs = CompatUtility::GetServiceStatusAttributes(service, type); - - fp << "\t" << "check_command=" << attrs->Get("check_command") << "\n" - << "\t" << "event_handler=" << attrs->Get("event_handler") << "\n" - << "\t" << "check_period=" << attrs->Get("check_period") << "\n" - << "\t" << "check_interval=" << static_cast(attrs->Get("check_interval")) << "\n" - << "\t" << "retry_interval=" << static_cast(attrs->Get("retry_interval")) << "\n" - << "\t" << "has_been_checked=" << attrs->Get("has_been_checked") << "\n" - << "\t" << "should_be_scheduled=" << attrs->Get("should_be_scheduled") << "\n" - << "\t" << "check_execution_time=" << static_cast(attrs->Get("check_execution_time")) << "\n" - << "\t" << "check_latency=" << static_cast(attrs->Get("check_latency")) << "\n" - << "\t" << "current_state=" << attrs->Get("current_state") << "\n" - << "\t" << "state_type=" << attrs->Get("state_type") << "\n" - << "\t" << "plugin_output=" << attrs->Get("plugin_output") << "\n" - << "\t" << "long_plugin_output=" << attrs->Get("long_plugin_output") << "\n" - << "\t" << "performance_data=" << attrs->Get("performance_data") << "\n" - << "\t" << "check_source=" << attrs->Get("check_source") << "\n" - << "\t" << "last_check=" << static_cast(attrs->Get("last_check")) << "\n" - << "\t" << "next_check=" << static_cast(attrs->Get("next_check")) << "\n" - << "\t" << "current_attempt=" << attrs->Get("current_attempt") << "\n" - << "\t" << "max_attempts=" << attrs->Get("max_attempts") << "\n" - << "\t" << "last_state_change=" << static_cast(attrs->Get("last_state_change")) << "\n" - << "\t" << "last_hard_state_change=" << static_cast(attrs->Get("last_hard_state_change")) << "\n" - << "\t" << "last_time_ok=" << static_cast(attrs->Get("last_time_ok")) << "\n" - << "\t" << "last_time_warn=" << static_cast(attrs->Get("last_time_warn")) << "\n" - << "\t" << "last_time_critical=" << static_cast(attrs->Get("last_time_critical")) << "\n" - << "\t" << "last_time_unknown=" << static_cast(attrs->Get("last_time_unknown")) << "\n" - << "\t" << "last_update=" << static_cast(attrs->Get("last_update")) << "\n" - << "\t" << "notifications_enabled=" << attrs->Get("notifications_enabled") << "\n" - << "\t" << "active_checks_enabled=" << attrs->Get("active_checks_enabled") << "\n" - << "\t" << "passive_checks_enabled=" << attrs->Get("passive_checks_enabled") << "\n" - << "\t" << "flap_detection_enabled=" << attrs->Get("flap_detection_enabled") << "\n" - << "\t" << "is_flapping=" << attrs->Get("is_flapping") << "\n" - << "\t" << "percent_state_change=" << attrs->Get("percent_state_change") << "\n" - << "\t" << "problem_has_been_acknowledged=" << attrs->Get("problem_has_been_acknowledged") << "\n" - << "\t" << "acknowledgement_type=" << attrs->Get("acknowledgement_type") << "\n" - << "\t" << "acknowledgement_end_time=" << attrs->Get("acknowledgement_end_time") << "\n" - << "\t" << "scheduled_downtime_depth=" << attrs->Get("scheduled_downtime_depth") << "\n" - << "\t" << "last_notification=" << static_cast(attrs->Get("last_notification")) << "\n" - << "\t" << "next_notification=" << static_cast(attrs->Get("next_notification")) << "\n" - << "\t" << "current_notification_number=" << attrs->Get("current_notification_number") << "\n" - << "\t" << "modified_attributes=" << attrs->Get("modified_attributes") << "\n"; + CheckResult::Ptr cr = service->GetLastCheckResult(); + + fp << "\t" << "check_command=check_" << CompatUtility::GetServiceCheckCommand(service) << "\n" + << "\t" << "event_handler=event_" << CompatUtility::GetServiceEventHandler(service) << "\n" + << "\t" << "check_period=" << CompatUtility::GetServiceCheckPeriod(service) << "\n" + << "\t" << "check_interval=" << CompatUtility::GetServiceCheckInterval(service) << "\n" + << "\t" << "retry_interval=" << CompatUtility::GetServiceRetryInterval(service) << "\n" + << "\t" << "has_been_checked=" << CompatUtility::GetServiceHasBeenChecked(service) << "\n" + << "\t" << "should_be_scheduled=" << CompatUtility::GetServiceShouldBeScheduled(service) << "\n"; + + if (cr) { + fp << "\t" << "check_execution_time=" << static_cast(Service::CalculateExecutionTime(cr)) << "\n" + << "\t" << "check_latency=" << static_cast(Service::CalculateLatency(cr)) << "\n"; + } + + fp << "\t" << "current_state=" << CompatUtility::GetServiceCurrentState(service) << "\n" + << "\t" << "state_type=" << service->GetStateType() << "\n" + << "\t" << "plugin_output=" << CompatUtility::GetCheckResultOutput(cr) << "\n" + << "\t" << "long_plugin_output=" << CompatUtility::GetCheckResultLongOutput(cr) << "\n" + << "\t" << "performance_data=" << CompatUtility::GetCheckResultPerfdata(cr) << "\n"; + + if (cr) { + fp << "\t" << "check_source=" << cr->GetCheckSource() << "\n" + << "\t" << "last_check=" << static_cast(cr->GetScheduleEnd()) << "\n"; + } + + fp << "\t" << "next_check=" << static_cast(service->GetNextCheck()) << "\n" + << "\t" << "current_attempt=" << service->GetCheckAttempt() << "\n" + << "\t" << "max_attempts=" << service->GetMaxCheckAttempts() << "\n" + << "\t" << "last_state_change=" << static_cast(service->GetLastStateChange()) << "\n" + << "\t" << "last_hard_state_change=" << static_cast(service->GetLastHardStateChange()) << "\n" + << "\t" << "last_time_ok=" << static_cast(service->GetLastStateOK()) << "\n" + << "\t" << "last_time_warn=" << static_cast(service->GetLastStateWarning()) << "\n" + << "\t" << "last_time_critical=" << static_cast(service->GetLastStateCritical()) << "\n" + << "\t" << "last_time_unknown=" << static_cast(service->GetLastStateUnknown()) << "\n" + << "\t" << "last_update=" << static_cast(time(NULL)) << "\n" + << "\t" << "notifications_enabled=" << CompatUtility::GetServiceNotificationsEnabled(service) << "\n" + << "\t" << "active_checks_enabled=" << CompatUtility::GetServiceActiveChecksEnabled(service) << "\n" + << "\t" << "passive_checks_enabled=" << CompatUtility::GetServicePassiveChecksEnabled(service) << "\n" + << "\t" << "flap_detection_enabled=" << CompatUtility::GetServiceFlapDetectionEnabled(service) << "\n" + << "\t" << "is_flapping=" << CompatUtility::GetServiceIsFlapping(service) << "\n" + << "\t" << "percent_state_change=" << CompatUtility::GetServicePercentStateChange(service) << "\n" + << "\t" << "problem_has_been_acknowledged=" << CompatUtility::GetServiceProblemHasBeenAcknowledged(service) << "\n" + << "\t" << "acknowledgement_type=" << CompatUtility::GetServiceAcknowledgementType(service) << "\n" + << "\t" << "acknowledgement_end_time=" << service->GetAcknowledgementExpiry() << "\n" + << "\t" << "scheduled_downtime_depth=" << service->GetDowntimeDepth() << "\n" + << "\t" << "last_notification=" << CompatUtility::GetServiceNotificationLastNotification(service) << "\n" + << "\t" << "next_notification=" << CompatUtility::GetServiceNotificationNextNotification(service) << "\n" + << "\t" << "current_notification_number=" << CompatUtility::GetServiceNotificationNotificationNumber(service) << "\n" + << "\t" << "modified_attributes=" << service->GetModifiedAttributes() << "\n"; } void StatusDataWriter::DumpServiceStatus(std::ostream& fp, const Service::Ptr& service) @@ -399,22 +406,6 @@ void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& s { Host::Ptr host = service->GetHost(); - String check_period_str; - TimePeriod::Ptr check_period = service->GetCheckPeriod(); - if (check_period) - check_period_str = check_period->GetName(); - else - check_period_str = "24x7"; - - double notification_interval = -1; - BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { - if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval) - notification_interval = notification->GetNotificationInterval(); - } - - if (notification_interval == -1) - notification_interval = 60; - { ObjectLock olock(service); @@ -422,17 +413,18 @@ void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& s << "\t" << "host_name" << "\t" << host->GetName() << "\n" << "\t" << "service_description" << "\t" << service->GetShortName() << "\n" << "\t" << "display_name" << "\t" << service->GetDisplayName() << "\n" - << "\t" << "check_period" << "\t" << check_period_str << "\n" - << "\t" << "check_interval" << "\t" << service->GetCheckInterval() / 60.0 << "\n" - << "\t" << "retry_interval" << "\t" << service->GetRetryInterval() / 60.0 << "\n" + << "\t" << "check_period" << "\t" << CompatUtility::GetServiceCheckPeriod(service) << "\n" + << "\t" << "check_interval" << "\t" << CompatUtility::GetServiceCheckInterval(service) << "\n" + << "\t" << "retry_interval" << "\t" << CompatUtility::GetServiceRetryInterval(service) << "\n" << "\t" << "max_check_attempts" << "\t" << service->GetMaxCheckAttempts() << "\n" << "\t" << "active_checks_enabled" << "\t" << (service->GetEnableActiveChecks() ? 1 : 0) << "\n" << "\t" << "passive_checks_enabled" << "\t" << (service->GetEnablePassiveChecks() ? 1 : 0) << "\n" << "\t" << "flap_detection_enabled" << "\t" << (service->GetEnableFlapping() ? 1 : 0) << "\n" << "\t" << "is_volatile" << "\t" << (service->GetVolatile() ? 1 : 0) << "\n" << "\t" << "notifications_enabled" << "\t" << (service->GetEnableNotifications() ? 1 : 0) << "\n" - << "\t" << "notification_options" << "\t" << "u,w,c,r" << "\n" - << "\t" << "notification_interval" << "\t" << notification_interval / 60.0 << "\n" + << "\t" << "notification_options" << "\t" << CompatUtility::GetServiceNotificationNotificationOptions(service) << "\n" + << "\t" << "notification_interval" << "\t" << CompatUtility::GetServiceNotificationNotificationInterval(service) << "\n" + << "\t" << "notification_period" << "\t" << CompatUtility::GetServiceNotificationNotificationPeriod(service) << "\n" << "\t" << "event_handler_enabled" << "\t" << (service->GetEnableEventHandler() ? 1 : 0) << "\n"; CheckCommand::Ptr checkcommand = service->GetCheckCommand(); @@ -443,10 +435,6 @@ void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& s if (eventcommand) fp << "\t" << "event_handler" << "\t" << "event_" << eventcommand->GetName() << "\n"; - TimePeriod::Ptr check_period = service->GetCheckPeriod(); - if (check_period) - fp << "\t" << "check_period" << "\t" << check_period->GetName() << "\n"; - fp << "\t" << "contacts" << "\t"; DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(service)); fp << "\n"; @@ -459,7 +447,12 @@ void StatusDataWriter::DumpServiceObject(std::ostream& fp, const Service::Ptr& s << "\t" << "low_flap_threshold" << "\t" << service->GetFlappingThreshold() << "\n" << "\t" << "high_flap_threshold" << "\t" << service->GetFlappingThreshold() << "\n" << "\t" << "process_perf_data" << "\t" << 1 << "\n" - << "\t" << "check_freshness" << "\t" << 1 << "\n"; + << "\t" << "check_freshness" << "\t" << 1 << "\n" + << "\t" << "notes" << "\t" << CompatUtility::GetCustomAttributeConfig(service, "notes") << "\n" + << "\t" << "notes_url" << "\t" << CompatUtility::GetCustomAttributeConfig(service, "notes_url") << "\n" + << "\t" << "action_url" << "\t" << CompatUtility::GetCustomAttributeConfig(service, "action_url") << "\n" + << "\t" << "icon_image" << "\t" << CompatUtility::GetCustomAttributeConfig(service, "icon_image") << "\n" + << "\t" << "icon_image_alt" << "\t" << CompatUtility::GetCustomAttributeConfig(service, "icon_image_alt") << "\n"; } fp << "\t" << "service_groups" << "\t"; diff --git a/components/livestatus/hoststable.cpp b/components/livestatus/hoststable.cpp index 5f9d4887e..486c439cb 100644 --- a/components/livestatus/hoststable.cpp +++ b/components/livestatus/hoststable.cpp @@ -175,6 +175,9 @@ Value HostsTable::NameAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + return host->GetName(); } @@ -182,6 +185,9 @@ Value HostsTable::DisplayNameAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + return host->GetDisplayName(); } @@ -189,30 +195,30 @@ Value HostsTable::AddressAccessor(const Value& row) { Host::Ptr host = static_cast(row); - Dictionary::Ptr macros = host->GetMacros(); - - if (!macros) + if (!host) return Empty; - return macros->Get("address"); + + return CompatUtility::GetHostAddress(host); } Value HostsTable::Address6Accessor(const Value& row) { Host::Ptr host = static_cast(row); - Dictionary::Ptr macros = host->GetMacros(); - - if (!macros) + if (!host) return Empty; - return macros->Get("address6"); + return CompatUtility::GetHostAddress6(host); } Value HostsTable::CheckCommandAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + /* use hostcheck service */ Service::Ptr hc = host->GetCheckService(); @@ -231,6 +237,9 @@ Value HostsTable::CheckCommandExpandedAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -248,6 +257,9 @@ Value HostsTable::EventHandlerAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -265,22 +277,15 @@ Value HostsTable::NotificationPeriodAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - BOOST_FOREACH(const Notification::Ptr& notification, hc->GetNotifications()) { - ObjectLock olock(notification); - - TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod(); - - /* XXX first notification wins */ - if (timeperiod) - return timeperiod->GetName(); - } - - return Empty; + return CompatUtility::GetServiceNotificationNotificationPeriod(hc); } Value HostsTable::CheckPeriodAccessor(const Value& row) @@ -288,43 +293,37 @@ Value HostsTable::CheckPeriodAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); - Service::Ptr hc = host->GetCheckService(); - - if (!hc) + if (!host) return Empty; - TimePeriod::Ptr timeperiod = hc->GetCheckPeriod(); + Service::Ptr hc = host->GetCheckService(); - if (!timeperiod) + if (!hc) return Empty; - return timeperiod->GetName(); + return CompatUtility::GetServiceCheckPeriod(hc); } Value HostsTable::NotesAccessor(const Value& row) { Host::Ptr host = static_cast(row); - Dictionary::Ptr custom = host->GetCustom(); - - if (!custom) + if (!host) return Empty; - return custom->Get("notes"); + return CompatUtility::GetCustomAttributeConfig(host, "notes"); } Value HostsTable::NotesExpandedAccessor(const Value& row) { Host::Ptr host = static_cast(row); - Service::Ptr service = host->GetCheckService(); - - if (!service) + if (!host) return Empty; - Dictionary::Ptr custom = host->GetCustom(); + Service::Ptr service = host->GetCheckService(); - if (!custom) + if (!service) return Empty; std::vector resolvers; @@ -335,7 +334,7 @@ Value HostsTable::NotesExpandedAccessor(const Value& row) resolvers.push_back(host); resolvers.push_back(IcingaApplication::GetInstance()); - Value value = custom->Get("notes"); + Value value = CompatUtility::GetCustomAttributeConfig(host, "notes"); return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd); } @@ -344,26 +343,27 @@ Value HostsTable::NotesUrlAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Dictionary::Ptr custom = host->GetCustom(); if (!custom) return Empty; - return custom->Get("notes_url"); + return CompatUtility::GetCustomAttributeConfig(host, "notes_url"); } Value HostsTable::NotesUrlExpandedAccessor(const Value& row) { Host::Ptr host = static_cast(row); - Service::Ptr service = host->GetCheckService(); - - if (!service) + if (!host) return Empty; - Dictionary::Ptr custom = host->GetCustom(); + Service::Ptr service = host->GetCheckService(); - if (!custom) + if (!service) return Empty; std::vector resolvers; @@ -374,7 +374,7 @@ Value HostsTable::NotesUrlExpandedAccessor(const Value& row) resolvers.push_back(host); resolvers.push_back(IcingaApplication::GetInstance()); - Value value = custom->Get("notes_url"); + Value value = CompatUtility::GetCustomAttributeConfig(host, "notes_url"); return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd); } @@ -383,26 +383,22 @@ Value HostsTable::ActionUrlAccessor(const Value& row) { Host::Ptr host = static_cast(row); - Dictionary::Ptr custom = host->GetCustom(); - - if (!custom) + if (!host) return Empty; - return custom->Get("action_url"); + return CompatUtility::GetCustomAttributeConfig(host, "action_url"); } Value HostsTable::ActionUrlExpandedAccessor(const Value& row) { Host::Ptr host = static_cast(row); - Service::Ptr service = host->GetCheckService(); - - if (!service) + if (!host) return Empty; - Dictionary::Ptr custom = host->GetCustom(); + Service::Ptr service = host->GetCheckService(); - if (!custom) + if (!service) return Empty; std::vector resolvers; @@ -413,7 +409,7 @@ Value HostsTable::ActionUrlExpandedAccessor(const Value& row) resolvers.push_back(host); resolvers.push_back(IcingaApplication::GetInstance()); - Value value = custom->Get("action_url"); + Value value = CompatUtility::GetCustomAttributeConfig(host, "action_url"); return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd); } @@ -423,17 +419,17 @@ Value HostsTable::PluginOutputAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); String output; if(hc) { - String output; CheckResult::Ptr cr = hc->GetLastCheckResult(); - if (cr) { - std::pair output_bag = CompatUtility::GetCheckResultOutput(cr); - output = output_bag.first; - } + if (cr) + output = CompatUtility::GetCheckResultOutput(cr); } return output; @@ -444,6 +440,9 @@ Value HostsTable::PerfDataAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); String perfdata; @@ -462,26 +461,22 @@ Value HostsTable::IconImageAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); - Dictionary::Ptr custom = host->GetCustom(); - - if (!custom) + if (!host) return Empty; - return custom->Get("icon_image"); + return CompatUtility::GetCustomAttributeConfig(host, "icon_image"); } Value HostsTable::IconImageExpandedAccessor(const Value& row) { Host::Ptr host = static_cast(row); - Service::Ptr service = host->GetCheckService(); - - if (!service) + if (!host) return Empty; - Dictionary::Ptr custom = host->GetCustom(); + Service::Ptr service = host->GetCheckService(); - if (!custom) + if (!service) return Empty; std::vector resolvers; @@ -492,7 +487,7 @@ Value HostsTable::IconImageExpandedAccessor(const Value& row) resolvers.push_back(host); resolvers.push_back(IcingaApplication::GetInstance()); - Value value = custom->Get("icon_image"); + Value value = CompatUtility::GetCustomAttributeConfig(host, "icon_image"); return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd); } @@ -501,24 +496,20 @@ Value HostsTable::IconImageAltAccessor(const Value& row) { Host::Ptr host = static_cast(row); - Dictionary::Ptr custom = host->GetCustom(); - - if (!custom) + if (!host) return Empty; - return custom->Get("icon_image_alt"); + return CompatUtility::GetCustomAttributeConfig(host, "icon_image_alt"); } Value HostsTable::StatusmapImageAccessor(const Value& row) { Host::Ptr host = static_cast(row); - Dictionary::Ptr custom = host->GetCustom(); - - if (!custom) + if (!host) return Empty; - return custom->Get("statusmap_image"); + return CompatUtility::GetCustomAttributeConfig(host, "statusmap_image"); } Value HostsTable::LongPluginOutputAccessor(const Value& row) @@ -526,17 +517,17 @@ Value HostsTable::LongPluginOutputAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); String long_output; if (hc) { - String long_output; CheckResult::Ptr cr = hc->GetLastCheckResult(); - if (cr) { - std::pair output_bag = CompatUtility::GetCheckResultOutput(cr); - long_output = output_bag.second; - } + if (cr) + long_output = CompatUtility::GetCheckResultLongOutput(cr); } return long_output; @@ -547,6 +538,9 @@ Value HostsTable::MaxCheckAttemptsAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -560,12 +554,15 @@ Value HostsTable::FlapDetectionEnabledAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - return (hc->GetEnableFlapping() ? 1 : 0); + return CompatUtility::GetServiceFlapDetectionEnabled(hc); } Value HostsTable::AcceptPassiveChecksAccessor(const Value& row) @@ -573,12 +570,15 @@ Value HostsTable::AcceptPassiveChecksAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - return (hc->GetEnablePassiveChecks() ? 1 : 0); + return CompatUtility::GetServicePassiveChecksEnabled(hc); } Value HostsTable::EventHandlerEnabledAccessor(const Value& row) @@ -586,16 +586,15 @@ Value HostsTable::EventHandlerEnabledAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - EventCommand::Ptr eventcommand = hc->GetEventCommand(); - if (eventcommand) - return 1; - - return 0; + return CompatUtility::GetServiceEventHandlerEnabled(hc); } Value HostsTable::AcknowledgementTypeAccessor(const Value& row) @@ -603,15 +602,15 @@ Value HostsTable::AcknowledgementTypeAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - /* important: lock acknowledgements */ - ObjectLock olock(hc); - - return static_cast(hc->GetAcknowledgement()); + return CompatUtility::GetServiceAcknowledgementType(hc); } Value HostsTable::CheckTypeAccessor(const Value& row) @@ -619,18 +618,24 @@ Value HostsTable::CheckTypeAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - return (hc->GetEnableActiveChecks() ? 0 : 1); + return CompatUtility::GetServiceCheckType(hc); } Value HostsTable::LastStateAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + return host->GetLastState(); } @@ -638,6 +643,9 @@ Value HostsTable::LastHardStateAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + return host->GetLastHardState(); } @@ -646,6 +654,9 @@ Value HostsTable::CurrentAttemptAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -659,19 +670,15 @@ Value HostsTable::LastNotificationAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - /* XXX Service -> Notifications, latest wins */ - double last_notification = 0; - BOOST_FOREACH(const Notification::Ptr& notification, hc->GetNotifications()) { - if (notification->GetLastNotification() > last_notification) - last_notification = notification->GetLastNotification(); - } - - return static_cast(last_notification); + return CompatUtility::GetServiceNotificationLastNotification(hc); } Value HostsTable::NextNotificationAccessor(const Value& row) @@ -679,19 +686,15 @@ Value HostsTable::NextNotificationAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - /* XXX Service -> Notifications, latest wins */ - double next_notification = 0; - BOOST_FOREACH(const Notification::Ptr& notification, hc->GetNotifications()) { - if (notification->GetNextNotification() < next_notification) - next_notification = notification->GetNextNotification(); - } - - return static_cast(next_notification); + return CompatUtility::GetServiceNotificationNextNotification(hc); } Value HostsTable::NextCheckAccessor(const Value& row) @@ -699,6 +702,9 @@ Value HostsTable::NextCheckAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -712,6 +718,9 @@ Value HostsTable::LastHardStateChangeAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -725,12 +734,15 @@ Value HostsTable::HasBeenCheckedAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - return (hc->HasBeenChecked() ? 1 : 0); + return CompatUtility::GetServiceHasBeenChecked(hc); } Value HostsTable::CurrentNotificationNumberAccessor(const Value& row) @@ -738,26 +750,24 @@ Value HostsTable::CurrentNotificationNumberAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - /* XXX Service -> Notifications, biggest wins */ - int notification_number = 0; - BOOST_FOREACH(const Notification::Ptr& notification, hc->GetNotifications()) { - if (notification->GetNotificationNumber() > notification_number) - notification_number = notification->GetNotificationNumber(); - } - - return notification_number; - + return CompatUtility::GetServiceNotificationNotificationNumber(hc); } Value HostsTable::TotalServicesAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + return host->GetTotalServices(); } @@ -766,12 +776,15 @@ Value HostsTable::ChecksEnabledAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - return (hc->GetEnableActiveChecks() ? 1 : 0); + return CompatUtility::GetServiceActiveChecksEnabled(hc); } Value HostsTable::NotificationsEnabledAccessor(const Value& row) @@ -779,12 +792,15 @@ Value HostsTable::NotificationsEnabledAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - return (hc->GetEnableNotifications() ? 1 : 0); + return CompatUtility::GetServiceNotificationsEnabled(hc); } Value HostsTable::AcknowledgedAccessor(const Value& row) @@ -792,15 +808,15 @@ Value HostsTable::AcknowledgedAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - /* important: lock acknowledgements */ - ObjectLock olock(hc); - - return (hc->IsAcknowledged() ? 1 : 0); + return CompatUtility::GetServiceIsAcknowledged(hc); } Value HostsTable::StateAccessor(const Value& row) @@ -808,6 +824,9 @@ Value HostsTable::StateAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + return host->GetState(); } @@ -816,6 +835,9 @@ Value HostsTable::StateTypeAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + return host->GetStateType(); } @@ -824,22 +846,15 @@ Value HostsTable::NoMoreNotificationsAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - /* XXX take the smallest notification_interval */ - double notification_interval = -1; - BOOST_FOREACH(const Notification::Ptr& notification, hc->GetNotifications()) { - if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval) - notification_interval = notification->GetNotificationInterval(); - } - - if (notification_interval == 0 && !hc->GetVolatile()) - return 1; - - return 0; + return CompatUtility::GetServiceNoMoreNotifications(hc); } Value HostsTable::LastCheckAccessor(const Value& row) @@ -847,6 +862,9 @@ Value HostsTable::LastCheckAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -860,6 +878,9 @@ Value HostsTable::LastStateChangeAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + return static_cast(host->GetLastStateChange()); } @@ -868,6 +889,9 @@ Value HostsTable::LastTimeUpAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + return static_cast(host->GetLastStateUp()); } @@ -876,6 +900,9 @@ Value HostsTable::LastTimeDownAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + return static_cast(host->GetLastStateDown()); } @@ -884,6 +911,9 @@ Value HostsTable::LastTimeUnreachableAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + return static_cast(host->GetLastStateUnreachable()); } @@ -892,6 +922,9 @@ Value HostsTable::IsFlappingAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -905,6 +938,9 @@ Value HostsTable::ScheduledDowntimeDepthAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -919,12 +955,15 @@ Value HostsTable::ActiveChecksEnabledAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - return (hc->GetEnableActiveChecks() ? 1 : 0); + return CompatUtility::GetServiceActiveChecksEnabled(hc); } Value HostsTable::CheckOptionsAccessor(const Value& row) @@ -938,6 +977,9 @@ Value HostsTable::ModifiedAttributesAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -957,12 +999,15 @@ Value HostsTable::CheckIntervalAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - return (hc->GetCheckInterval() / 60.0); + return CompatUtility::GetServiceCheckInterval(hc); } Value HostsTable::RetryIntervalAccessor(const Value& row) @@ -970,12 +1015,15 @@ Value HostsTable::RetryIntervalAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - return (hc->GetRetryInterval() / 60.0); + return CompatUtility::GetServiceRetryInterval(hc); } Value HostsTable::NotificationIntervalAccessor(const Value& row) @@ -983,22 +1031,15 @@ Value HostsTable::NotificationIntervalAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - /* XXX take the smallest notification_interval */ - double notification_interval = -1; - BOOST_FOREACH(const Notification::Ptr& notification, hc->GetNotifications()) { - if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval) - notification_interval = notification->GetNotificationInterval(); - } - - if (notification_interval == -1) - notification_interval = 60; - - return (notification_interval / 60.0); + return CompatUtility::GetServiceNotificationNotificationInterval(hc); } Value HostsTable::LowFlapThresholdAccessor(const Value& row) @@ -1006,12 +1047,15 @@ Value HostsTable::LowFlapThresholdAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - return hc->GetFlappingThreshold(); + return CompatUtility::GetServiceLowFlapThreshold(hc); } Value HostsTable::HighFlapThresholdAccessor(const Value& row) @@ -1019,18 +1063,24 @@ Value HostsTable::HighFlapThresholdAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - return hc->GetFlappingThreshold(); + return CompatUtility::GetServiceHighFlapThreshold(hc); } Value HostsTable::X2dAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Dictionary::Ptr custom = host->GetCustom(); if (!custom) @@ -1051,6 +1101,9 @@ Value HostsTable::Y2dAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Dictionary::Ptr custom = host->GetCustom(); if (!custom) @@ -1072,6 +1125,9 @@ Value HostsTable::LatencyAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -1085,6 +1141,9 @@ Value HostsTable::ExecutionTimeAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -1098,12 +1157,15 @@ Value HostsTable::PercentStateChangeAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - return hc->GetFlappingCurrent(); + return CompatUtility::GetServicePercentStateChange(hc); } Value HostsTable::InNotificationPeriodAccessor(const Value& row) @@ -1111,23 +1173,15 @@ Value HostsTable::InNotificationPeriodAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - BOOST_FOREACH(const Notification::Ptr& notification, hc->GetNotifications()) { - ObjectLock olock(notification); - - TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod(); - - /* XXX first notification wins */ - if (timeperiod) - return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0); - } - - /* none set means always notified */ - return 1; + return CompatUtility::GetServiceInNotificationPeriod(hc); } Value HostsTable::InCheckPeriodAccessor(const Value& row) @@ -1135,18 +1189,15 @@ Value HostsTable::InCheckPeriodAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - TimePeriod::Ptr timeperiod = hc->GetCheckPeriod(); - - /* none set means always checked */ - if (!timeperiod) - return 1; - - return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0); + return CompatUtility::GetServiceInCheckPeriod(hc); } Value HostsTable::ContactsAccessor(const Value& row) @@ -1154,6 +1205,9 @@ Value HostsTable::ContactsAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -1173,6 +1227,9 @@ Value HostsTable::DowntimesAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -1205,6 +1262,9 @@ Value HostsTable::DowntimesWithInfoAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -1241,6 +1301,9 @@ Value HostsTable::CommentsAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -1273,6 +1336,9 @@ Value HostsTable::CommentsWithInfoAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -1309,6 +1375,9 @@ Value HostsTable::CommentsWithExtraInfoAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -1346,6 +1415,9 @@ Value HostsTable::CustomVariableNamesAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Dictionary::Ptr customvars; { @@ -1371,6 +1443,9 @@ Value HostsTable::CustomVariableValuesAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Dictionary::Ptr customvars; { @@ -1396,6 +1471,9 @@ Value HostsTable::CustomVariablesAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Dictionary::Ptr customvars; { @@ -1424,6 +1502,9 @@ Value HostsTable::ParentsAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Array::Ptr parents = make_shared(); BOOST_FOREACH(const Host::Ptr& parent, host->GetParentHosts()) { @@ -1437,6 +1518,9 @@ Value HostsTable::ChildsAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Array::Ptr childs = make_shared(); BOOST_FOREACH(const Host::Ptr& child, host->GetChildHosts()) { @@ -1451,6 +1535,9 @@ Value HostsTable::NumServicesAccessor(const Value& row) /* duplicate of TotalServices */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + return host->GetTotalServices(); } @@ -1458,6 +1545,9 @@ Value HostsTable::WorstServiceStateAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Value worst_service = StateOK; BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { @@ -1472,6 +1562,9 @@ Value HostsTable::NumServicesOkAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + int num_services = 0; BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { @@ -1486,6 +1579,9 @@ Value HostsTable::NumServicesWarnAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + int num_services = 0; BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { @@ -1500,6 +1596,9 @@ Value HostsTable::NumServicesCritAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + int num_services = 0; BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { @@ -1514,6 +1613,9 @@ Value HostsTable::NumServicesUnknownAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + int num_services = 0; BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { @@ -1528,6 +1630,9 @@ Value HostsTable::NumServicesPendingAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + int num_services = 0; BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { @@ -1542,6 +1647,9 @@ Value HostsTable::WorstServiceHardStateAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Value worst_service = StateOK; BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { @@ -1558,6 +1666,9 @@ Value HostsTable::NumServicesHardOkAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + int num_services = 0; BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { @@ -1572,6 +1683,9 @@ Value HostsTable::NumServicesHardWarnAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + int num_services = 0; BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { @@ -1586,6 +1700,9 @@ Value HostsTable::NumServicesHardCritAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + int num_services = 0; BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { @@ -1600,6 +1717,9 @@ Value HostsTable::NumServicesHardUnknownAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + int num_services = 0; BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { @@ -1615,6 +1735,9 @@ Value HostsTable::HardStateAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -1633,21 +1756,24 @@ Value HostsTable::StalenessAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) return Empty; - if (hc->HasBeenChecked() && hc->GetLastCheck() > 0) - return (Utility::GetTime() - hc->GetLastCheck()) / (hc->GetCheckInterval() * 3600); - - return Empty; + return CompatUtility::GetServiceStaleness(hc); } Value HostsTable::GroupsAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Array::Ptr groups = host->GetGroups(); if (!groups) @@ -1661,6 +1787,9 @@ Value HostsTable::ContactGroupsAccessor(const Value& row) /* use hostcheck service */ Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Service::Ptr hc = host->GetCheckService(); if (!hc) @@ -1679,6 +1808,9 @@ Value HostsTable::ServicesAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Array::Ptr services = make_shared(); BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { @@ -1692,6 +1824,9 @@ Value HostsTable::ServicesWithStateAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Array::Ptr services = make_shared(); BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { @@ -1710,6 +1845,9 @@ Value HostsTable::ServicesWithInfoAccessor(const Value& row) { Host::Ptr host = static_cast(row); + if (!host) + return Empty; + Array::Ptr services = make_shared(); BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) { @@ -1722,10 +1860,8 @@ Value HostsTable::ServicesWithInfoAccessor(const Value& row) String output; CheckResult::Ptr cr = service->GetLastCheckResult(); - if (cr) { - std::pair output_bag = CompatUtility::GetCheckResultOutput(cr); - output = output_bag.first; - } + if (cr) + output = CompatUtility::GetCheckResultOutput(cr); svc_add->Add(output); services->Add(svc_add); diff --git a/components/livestatus/servicestable.cpp b/components/livestatus/servicestable.cpp index db1bec349..eb7650315 100644 --- a/components/livestatus/servicestable.cpp +++ b/components/livestatus/servicestable.cpp @@ -233,10 +233,8 @@ Value ServicesTable::PluginOutputAccessor(const Value& row) String output; CheckResult::Ptr cr = service->GetLastCheckResult(); - if (cr) { - std::pair output_bag = CompatUtility::GetCheckResultOutput(cr); - output = output_bag.first; - } + if (cr) + output = CompatUtility::GetCheckResultOutput(cr); return output; } @@ -251,10 +249,8 @@ Value ServicesTable::LongPluginOutputAccessor(const Value& row) String long_output; CheckResult::Ptr cr = service->GetLastCheckResult(); - if (cr) { - std::pair output_bag = CompatUtility::GetCheckResultOutput(cr); - long_output = output_bag.second; - } + if (cr) + long_output = CompatUtility::GetCheckResultLongOutput(cr); return long_output; } @@ -282,17 +278,7 @@ Value ServicesTable::NotificationPeriodAccessor(const Value& row) if (!service) return Empty; - BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { - ObjectLock olock(notification); - - TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod(); - - /* XXX first notification wins */ - if (timeperiod) - return timeperiod->GetName(); - } - - return Empty; + return CompatUtility::GetServiceNotificationNotificationPeriod(service); } Value ServicesTable::CheckPeriodAccessor(const Value& row) @@ -302,12 +288,7 @@ Value ServicesTable::CheckPeriodAccessor(const Value& row) if (!service) return Empty; - TimePeriod::Ptr timeperiod = service->GetCheckPeriod(); - - if (!timeperiod) - return Empty; - - return timeperiod->GetName(); + return CompatUtility::GetServiceCheckPeriod(service); } Value ServicesTable::NotesAccessor(const Value& row) @@ -317,12 +298,7 @@ Value ServicesTable::NotesAccessor(const Value& row) if (!service) return Empty; - Dictionary::Ptr custom = service->GetCustom(); - - if (!custom) - return Empty; - - return custom->Get("notes"); + return CompatUtility::GetCustomAttributeConfig(service, "notes"); } Value ServicesTable::NotesExpandedAccessor(const Value& row) @@ -332,17 +308,12 @@ Value ServicesTable::NotesExpandedAccessor(const Value& row) if (!service) return Empty; - Dictionary::Ptr custom = service->GetCustom(); - - if (!custom) - return Empty; - std::vector resolvers; resolvers.push_back(service); resolvers.push_back(service->GetHost()); resolvers.push_back(IcingaApplication::GetInstance()); - Value value = custom->Get("notes"); + Value value = CompatUtility::GetCustomAttributeConfig(service, "notes"); return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd); } @@ -354,12 +325,7 @@ Value ServicesTable::NotesUrlAccessor(const Value& row) if (!service) return Empty; - Dictionary::Ptr custom = service->GetCustom(); - - if (!custom) - return Empty; - - return custom->Get("notes_url"); + return CompatUtility::GetCustomAttributeConfig(service, "notes_url"); } Value ServicesTable::NotesUrlExpandedAccessor(const Value& row) @@ -369,17 +335,12 @@ Value ServicesTable::NotesUrlExpandedAccessor(const Value& row) if (!service) return Empty; - Dictionary::Ptr custom = service->GetCustom(); - - if (!custom) - return Empty; - std::vector resolvers; resolvers.push_back(service); resolvers.push_back(service->GetHost()); resolvers.push_back(IcingaApplication::GetInstance()); - Value value = custom->Get("notes_url"); + Value value = CompatUtility::GetCustomAttributeConfig(service, "notes_url"); return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd); } @@ -391,12 +352,7 @@ Value ServicesTable::ActionUrlAccessor(const Value& row) if (!service) return Empty; - Dictionary::Ptr custom = service->GetCustom(); - - if (!custom) - return Empty; - - return custom->Get("action_url"); + return CompatUtility::GetCustomAttributeConfig(service, "action_url"); } Value ServicesTable::ActionUrlExpandedAccessor(const Value& row) @@ -406,17 +362,12 @@ Value ServicesTable::ActionUrlExpandedAccessor(const Value& row) if (!service) return Empty; - Dictionary::Ptr custom = service->GetCustom(); - - if (!custom) - return Empty; - std::vector resolvers; resolvers.push_back(service); resolvers.push_back(service->GetHost()); resolvers.push_back(IcingaApplication::GetInstance()); - Value value = custom->Get("action_url"); + Value value = CompatUtility::GetCustomAttributeConfig(service, "action_url"); return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd); } @@ -428,12 +379,7 @@ Value ServicesTable::IconImageAccessor(const Value& row) if (!service) return Empty; - Dictionary::Ptr custom = service->GetCustom(); - - if (!custom) - return Empty; - - return custom->Get("icon_image"); + return CompatUtility::GetCustomAttributeConfig(service, "icon_image"); } Value ServicesTable::IconImageExpandedAccessor(const Value& row) @@ -443,17 +389,12 @@ Value ServicesTable::IconImageExpandedAccessor(const Value& row) if (!service) return Empty; - Dictionary::Ptr custom = service->GetCustom(); - - if (!custom) - return Empty; - std::vector resolvers; resolvers.push_back(service); resolvers.push_back(service->GetHost()); resolvers.push_back(IcingaApplication::GetInstance()); - Value value = custom->Get("icon_image"); + Value value = CompatUtility::GetCustomAttributeConfig(service, "icon_image"); return MacroProcessor::ResolveMacros(value, resolvers, CheckResult::Ptr(), Utility::EscapeShellCmd); } @@ -465,12 +406,7 @@ Value ServicesTable::IconImageAltAccessor(const Value& row) if (!service) return Empty; - Dictionary::Ptr custom = service->GetCustom(); - - if (!custom) - return Empty; - - return custom->Get("icon_image_alt"); + return CompatUtility::GetCustomAttributeConfig(service, "icon_image_alt"); } Value ServicesTable::MaxCheckAttemptsAccessor(const Value& row) @@ -500,7 +436,7 @@ Value ServicesTable::StateAccessor(const Value& row) if (!service) return Empty; - return service->GetState(); + return CompatUtility::GetServiceCurrentState(service); } Value ServicesTable::HasBeenCheckedAccessor(const Value& row) @@ -510,7 +446,7 @@ Value ServicesTable::HasBeenCheckedAccessor(const Value& row) if (!service) return Empty; - return (service->HasBeenChecked() ? 1 : 0); + return CompatUtility::GetServiceHasBeenChecked(service); } Value ServicesTable::LastStateAccessor(const Value& row) @@ -550,7 +486,7 @@ Value ServicesTable::CheckTypeAccessor(const Value& row) if (!service) return Empty; - return (service->GetEnableActiveChecks() ? 0 : 1); + return CompatUtility::GetServiceCheckType(service); } Value ServicesTable::AcknowledgedAccessor(const Value& row) @@ -560,10 +496,8 @@ Value ServicesTable::AcknowledgedAccessor(const Value& row) if (!service) return Empty; - /* important: lock acknowledgements */ - ObjectLock olock(service); - return (service->IsAcknowledged() ? 1 : 0); + return CompatUtility::GetServiceIsAcknowledged(service); } Value ServicesTable::AcknowledgementTypeAccessor(const Value& row) @@ -586,17 +520,7 @@ Value ServicesTable::NoMoreNotificationsAccessor(const Value& row) if (!service) return Empty; - /* XXX take the smallest notification_interval */ - double notification_interval = -1; - BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { - if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval) - notification_interval = notification->GetNotificationInterval(); - } - - if (notification_interval == 0 && !service->GetVolatile()) - return 1; - - return 0; + return CompatUtility::GetServiceNoMoreNotifications(service); } Value ServicesTable::LastTimeOkAccessor(const Value& row) @@ -666,14 +590,7 @@ Value ServicesTable::LastNotificationAccessor(const Value& row) if (!service) return Empty; - /* XXX Service -> Notifications, latest wins */ - double last_notification = 0; - BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { - if (notification->GetLastNotification() > last_notification) - last_notification = notification->GetLastNotification(); - } - - return static_cast(last_notification); + return CompatUtility::GetServiceNotificationLastNotification(service); } Value ServicesTable::NextNotificationAccessor(const Value& row) @@ -683,14 +600,7 @@ Value ServicesTable::NextNotificationAccessor(const Value& row) if (!service) return Empty; - /* XXX Service -> Notifications, latest wins */ - double next_notification = 0; - BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { - if (notification->GetNextNotification() < next_notification) - next_notification = notification->GetNextNotification(); - } - - return static_cast(next_notification); + return CompatUtility::GetServiceNotificationNextNotification(service); } Value ServicesTable::CurrentNotificationNumberAccessor(const Value& row) @@ -700,14 +610,7 @@ Value ServicesTable::CurrentNotificationNumberAccessor(const Value& row) if (!service) return Empty; - /* XXX Service -> Notifications, biggest wins */ - int notification_number = 0; - BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { - if (notification->GetNotificationNumber() > notification_number) - notification_number = notification->GetNotificationNumber(); - } - - return notification_number; + return CompatUtility::GetServiceNotificationNotificationNumber(service); } Value ServicesTable::LastStateChangeAccessor(const Value& row) @@ -757,7 +660,7 @@ Value ServicesTable::ChecksEnabledAccessor(const Value& row) if (!service) return Empty; - return (service->GetEnableActiveChecks() ? 1 : 0); + return CompatUtility::GetServiceActiveChecksEnabled(service); } Value ServicesTable::AcceptPassiveChecksAccessor(const Value& row) @@ -767,7 +670,7 @@ Value ServicesTable::AcceptPassiveChecksAccessor(const Value& row) if (!service) return Empty; - return (service->GetEnablePassiveChecks() ? 1 : 0); + return CompatUtility::GetServicePassiveChecksEnabled(service); } Value ServicesTable::EventHandlerEnabledAccessor(const Value& row) @@ -777,12 +680,7 @@ Value ServicesTable::EventHandlerEnabledAccessor(const Value& row) if (!service) return Empty; - EventCommand::Ptr eventcommand = service->GetEventCommand(); - - if (eventcommand) - return 1; - - return 0; + return CompatUtility::GetServiceEventHandlerEnabled(service); } Value ServicesTable::NotificationsEnabledAccessor(const Value& row) @@ -792,7 +690,7 @@ Value ServicesTable::NotificationsEnabledAccessor(const Value& row) if (!service) return Empty; - return (service->GetEnableNotifications() ? 1 : 0); + return CompatUtility::GetServiceNotificationsEnabled(service); } Value ServicesTable::ActiveChecksEnabledAccessor(const Value& row) @@ -802,7 +700,7 @@ Value ServicesTable::ActiveChecksEnabledAccessor(const Value& row) if (!service) return Empty; - return (service->GetEnableActiveChecks() ? 1 : 0); + return CompatUtility::GetServiceActiveChecksEnabled(service); } Value ServicesTable::CheckOptionsAccessor(const Value& row) @@ -818,13 +716,17 @@ Value ServicesTable::FlapDetectionEnabledAccessor(const Value& row) if (!service) return Empty; - return (service->GetEnableFlapping() ? 1 : 0); + return CompatUtility::GetServiceFlapDetectionEnabled(service); } Value ServicesTable::CheckFreshnessAccessor(const Value& row) { - /* always enabled */ - return 1; + Service::Ptr service = static_cast(row); + + if (!service) + return Empty; + + return CompatUtility::GetServiceFreshnessChecksEnabled(service); } Value ServicesTable::ModifiedAttributesAccessor(const Value& row) @@ -850,10 +752,7 @@ Value ServicesTable::StalenessAccessor(const Value& row) if (!service) return Empty; - if (service->HasBeenChecked() && service->GetLastCheck() > 0) - return (Utility::GetTime() - service->GetLastCheck()) / (service->GetCheckInterval() * 3600); - - return Empty; + return CompatUtility::GetServiceStaleness(service); } Value ServicesTable::CheckIntervalAccessor(const Value& row) @@ -863,7 +762,7 @@ Value ServicesTable::CheckIntervalAccessor(const Value& row) if (!service) return Empty; - return (service->GetCheckInterval() / 60.0); + return CompatUtility::GetServiceCheckInterval(service); } Value ServicesTable::RetryIntervalAccessor(const Value& row) @@ -873,7 +772,7 @@ Value ServicesTable::RetryIntervalAccessor(const Value& row) if (!service) return Empty; - return (service->GetRetryInterval() / 60.0); + return CompatUtility::GetServiceRetryInterval(service); } Value ServicesTable::NotificationIntervalAccessor(const Value& row) @@ -883,17 +782,7 @@ Value ServicesTable::NotificationIntervalAccessor(const Value& row) if (!service) return Empty; - /* XXX take the smallest notification_interval */ - double notification_interval = -1; - BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { - if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval) - notification_interval = notification->GetNotificationInterval(); - } - - if (notification_interval == -1) - notification_interval = 60; - - return (notification_interval / 60.0); + return CompatUtility::GetServiceNotificationNotificationInterval(service); } Value ServicesTable::LowFlapThresholdAccessor(const Value& row) @@ -903,7 +792,7 @@ Value ServicesTable::LowFlapThresholdAccessor(const Value& row) if (!service) return Empty; - return service->GetFlappingThreshold(); + return CompatUtility::GetServiceLowFlapThreshold(service); } Value ServicesTable::HighFlapThresholdAccessor(const Value& row) @@ -913,7 +802,7 @@ Value ServicesTable::HighFlapThresholdAccessor(const Value& row) if (!service) return Empty; - return service->GetFlappingThreshold(); + return CompatUtility::GetServiceHighFlapThreshold(service); } Value ServicesTable::LatencyAccessor(const Value& row) @@ -943,7 +832,7 @@ Value ServicesTable::PercentStateChangeAccessor(const Value& row) if (!service) return Empty; - return service->GetFlappingCurrent(); + return CompatUtility::GetServicePercentStateChange(service); } Value ServicesTable::InCheckPeriodAccessor(const Value& row) @@ -953,13 +842,7 @@ Value ServicesTable::InCheckPeriodAccessor(const Value& row) if (!service) return Empty; - TimePeriod::Ptr timeperiod = service->GetCheckPeriod(); - - /* none set means always checked */ - if (!timeperiod) - return 1; - - return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0); + return CompatUtility::GetServiceInCheckPeriod(service); } Value ServicesTable::InNotificationPeriodAccessor(const Value& row) @@ -969,18 +852,7 @@ Value ServicesTable::InNotificationPeriodAccessor(const Value& row) if (!service) return Empty; - BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { - ObjectLock olock(notification); - - TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod(); - - /* XXX first notification wins */ - if (timeperiod) - return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0); - } - - /* none set means always notified */ - return 1; + return CompatUtility::GetServiceInNotificationPeriod(service); } Value ServicesTable::ContactsAccessor(const Value& row) diff --git a/lib/db_ido/commanddbobject.cpp b/lib/db_ido/commanddbobject.cpp index 675dd0f9e..0d142f5b2 100644 --- a/lib/db_ido/commanddbobject.cpp +++ b/lib/db_ido/commanddbobject.cpp @@ -40,11 +40,7 @@ Dictionary::Ptr CommandDbObject::GetConfigFields(void) const Dictionary::Ptr fields = make_shared(); Command::Ptr command = static_pointer_cast(GetObject()); - Dictionary::Ptr attrs; - - attrs = CompatUtility::GetCommandConfigAttributes(command); - - fields->Set("command_line", attrs->Get("command_line")); + fields->Set("command_line", CompatUtility::GetCommandLine(command)); return fields; } diff --git a/lib/db_ido/hostdbobject.cpp b/lib/db_ido/hostdbobject.cpp index b916687c3..d236f6f19 100644 --- a/lib/db_ido/hostdbobject.cpp +++ b/lib/db_ido/hostdbobject.cpp @@ -45,76 +45,81 @@ Dictionary::Ptr HostDbObject::GetConfigFields(void) const Service::Ptr service = host->GetCheckService(); - Dictionary::Ptr attrs; - - { - ObjectLock olock(host); - attrs = CompatUtility::GetHostConfigAttributes(host); - } - - fields->Set("alias", attrs->Get("alias")); + fields->Set("alias", CompatUtility::GetHostAlias(host)); fields->Set("display_name", host->GetDisplayName()); - - fields->Set("address", attrs->Get("address")); - fields->Set("address6", attrs->Get("address6")); + fields->Set("address", CompatUtility::GetHostAddress(host)); + fields->Set("address6", CompatUtility::GetHostAddress6(host)); if (service) { fields->Set("check_command_object_id", service->GetCheckCommand()); fields->Set("check_command_args", Empty); fields->Set("eventhandler_command_object_id", service->GetEventCommand()); fields->Set("eventhandler_command_args", Empty); + fields->Set("notification_timeperiod_object_id", Notification::GetByName(CompatUtility::GetServiceNotificationNotificationPeriod(service))); + fields->Set("check_timeperiod_object_id", service->GetCheckPeriod()); + fields->Set("failure_prediction_options", Empty); + fields->Set("check_interval", CompatUtility::GetServiceCheckInterval(service)); + fields->Set("retry_interval", CompatUtility::GetServiceRetryInterval(service)); + fields->Set("max_check_attempts", service->GetMaxCheckAttempts()); + + fields->Set("first_notification_delay", Empty); + + fields->Set("notification_interval", CompatUtility::GetServiceNotificationNotificationInterval(service)); + /* requires host check service */ + fields->Set("notify_on_down", CompatUtility::GetHostNotifyOnDown(host)); + fields->Set("notify_on_unreachable", CompatUtility::GetHostNotifyOnDown(host)); + + fields->Set("notify_on_recovery", CompatUtility::GetServiceNotifyOnRecovery(service)); + fields->Set("notify_on_flapping", CompatUtility::GetServiceNotifyOnFlapping(service)); + fields->Set("notify_on_downtime", CompatUtility::GetServiceNotifyOnDowntime(service)); + + fields->Set("stalk_on_up", Empty); + fields->Set("stalk_on_down", Empty); + fields->Set("stalk_on_unreachable", Empty); + + fields->Set("flap_detection_enabled", CompatUtility::GetServiceFlapDetectionEnabled(service)); + fields->Set("flap_detection_on_up", Empty); + fields->Set("flap_detection_on_down", Empty); + fields->Set("flap_detection_on_unreachable", Empty); + fields->Set("low_flap_threshold", CompatUtility::GetServiceLowFlapThreshold(service)); + fields->Set("high_flap_threshold", CompatUtility::GetServiceHighFlapThreshold(service)); } - fields->Set("notification_timeperiod_object_id", Notification::GetByName(attrs->Get("notification_period"))); + fields->Set("process_performance_data", 0); - if (service) - fields->Set("check_timeperiod_object_id", service->GetCheckPeriod()); + if (service) { + fields->Set("freshness_checks_enabled", CompatUtility::GetServiceFreshnessChecksEnabled(service)); + fields->Set("freshness_threshold", CompatUtility::GetServiceFreshnessThreshold(service)); + fields->Set("passive_checks_enabled", CompatUtility::GetServicePassiveChecksEnabled(service)); + fields->Set("event_handler_enabled", CompatUtility::GetServiceEventHandlerEnabled(service)); + fields->Set("active_checks_enabled", CompatUtility::GetServiceActiveChecksEnabled(service)); + } - fields->Set("failure_prediction_options", Empty); - - fields->Set("check_interval", attrs->Get("check_interval")); - fields->Set("retry_interval", attrs->Get("retry_interval")); - fields->Set("max_check_attempts", attrs->Get("max_check_attempts")); - - fields->Set("first_notification_delay", Empty); - fields->Set("notification_interval", attrs->Get("notification_interval")); - fields->Set("notify_on_down", attrs->Get("notify_on_down")); - fields->Set("notify_on_unreachable", attrs->Get("notify_on_unreachable")); - fields->Set("notify_on_recovery", attrs->Get("notify_on_recovery")); - fields->Set("notify_on_flapping", attrs->Get("notify_on_flapping")); - fields->Set("notify_on_downtime", attrs->Get("notify_on_downtime")); - - fields->Set("stalk_on_up", Empty); - fields->Set("stalk_on_down", Empty); - fields->Set("stalk_on_unreachable", Empty); - - fields->Set("flap_detection_enabled", attrs->Get("flap_detection_enabled")); - fields->Set("flap_detection_on_up", Empty); - fields->Set("flap_detection_on_down", Empty); - fields->Set("flap_detection_on_unreachable", Empty); - fields->Set("low_flap_threshold", attrs->Get("low_flap_threshold")); - fields->Set("high_flap_threshold", attrs->Get("high_flap_threshold")); - fields->Set("process_performance_data", attrs->Get("process_performance_data")); - fields->Set("freshness_checks_enabled", attrs->Get("check_freshness")); - fields->Set("freshness_threshold", Empty); - fields->Set("passive_checks_enabled", attrs->Get("passive_checks_enabled")); - fields->Set("event_handler_enabled", attrs->Get("event_handler_enabled")); - fields->Set("active_checks_enabled", attrs->Get("active_checks_enabled")); fields->Set("retain_status_information", 1); fields->Set("retain_nonstatus_information", 1); - fields->Set("notifications_enabled", attrs->Get("notifications_enabled")); + + if (service) + fields->Set("notifications_enabled", CompatUtility::GetServiceNotificationsEnabled(service)); + fields->Set("obsess_over_host", 0); fields->Set("failure_prediction_enabled", 0); - fields->Set("notes", attrs->Get("notes")); - fields->Set("notes_url", attrs->Get("notes_url")); - fields->Set("action_url", attrs->Get("action_url")); - fields->Set("icon_image", attrs->Get("icon_image")); - fields->Set("icon_image_alt", attrs->Get("icon_image_alt")); - fields->Set("statusmap_image", attrs->Get("statusmap_image")); - fields->Set("have_2d_coords", attrs->Get("have_2d_coords")); - fields->Set("x_2d", attrs->Get("x_2d")); - fields->Set("y_2d", attrs->Get("y_2d")); + fields->Set("notes", CompatUtility::GetCustomAttributeConfig(host, "notes")); + fields->Set("notes_url", CompatUtility::GetCustomAttributeConfig(host, "notes_url")); + fields->Set("action_url", CompatUtility::GetCustomAttributeConfig(host, "action_url")); + fields->Set("icon_image", CompatUtility::GetCustomAttributeConfig(host, "icon_image")); + fields->Set("icon_image_alt", CompatUtility::GetCustomAttributeConfig(host, "icon_image_alt")); + fields->Set("statusmap_image", CompatUtility::GetCustomAttributeConfig(host, "statusmap_image")); + + Host2dCoords coords = CompatUtility::GetHost2dCoords(host); + + fields->Set("have_2d_coords", coords.have_2d_coords); + + if (coords.have_2d_coords) { + fields->Set("x_2d", coords.x_2d); + fields->Set("y_2d", coords.y_2d); + } + /* deprecated in 1.x */ fields->Set("have_3d_coords", 0); @@ -127,55 +132,61 @@ Dictionary::Ptr HostDbObject::GetStatusFields(void) const Host::Ptr host = static_pointer_cast(GetObject()); Service::Ptr service = host->GetCheckService(); - Dictionary::Ptr attrs; - /* fetch service status, or dump a pending hoststatus */ if (service) { - ObjectLock olock(service); - attrs = CompatUtility::GetServiceStatusAttributes(service, CompatTypeHost); - - fields->Set("output", attrs->Get("plugin_output")); - fields->Set("long_output", attrs->Get("long_plugin_output")); - fields->Set("perfdata", attrs->Get("performance_data")); - fields->Set("check_source", attrs->Get("check_source")); - fields->Set("current_state", attrs->Get("current_state")); - fields->Set("has_been_checked", attrs->Get("has_been_checked")); - fields->Set("should_be_scheduled", attrs->Get("should_be_scheduled")); - fields->Set("current_check_attempt", attrs->Get("current_attempt")); - fields->Set("max_check_attempts", attrs->Get("max_attempts")); - fields->Set("last_check", DbValue::FromTimestamp(attrs->Get("last_check"))); - fields->Set("next_check", DbValue::FromTimestamp(attrs->Get("next_check"))); - fields->Set("check_type", attrs->Get("check_type")); - fields->Set("last_state_change", DbValue::FromTimestamp(attrs->Get("last_state_change"))); - fields->Set("last_hard_state_change", DbValue::FromTimestamp(attrs->Get("last_hard_state_change"))); - fields->Set("last_time_up", DbValue::FromTimestamp(attrs->Get("last_time_up"))); - fields->Set("last_time_down", DbValue::FromTimestamp(attrs->Get("last_time_down"))); - fields->Set("last_time_unreachable", DbValue::FromTimestamp(attrs->Get("last_time_unreachable"))); - fields->Set("state_type", attrs->Get("state_type")); - fields->Set("last_notification", DbValue::FromTimestamp(attrs->Get("last_notification"))); - fields->Set("next_notification", DbValue::FromTimestamp(attrs->Get("next_notification"))); + CheckResult::Ptr cr = service->GetLastCheckResult(); + + if (cr) { + fields->Set("output", CompatUtility::GetCheckResultOutput(cr)); + fields->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr)); + fields->Set("perfdata", CompatUtility::GetCheckResultPerfdata(cr)); + fields->Set("check_source", cr->GetCheckSource()); + } + + fields->Set("current_state", host->GetState()); + fields->Set("has_been_checked", CompatUtility::GetServiceHasBeenChecked(service)); + fields->Set("should_be_scheduled", CompatUtility::GetServiceShouldBeScheduled(service)); + fields->Set("current_check_attempt", service->GetCheckAttempt()); + fields->Set("max_check_attempts", service->GetMaxCheckAttempts()); + + if (cr) + fields->Set("last_check", DbValue::FromTimestamp(cr->GetScheduleEnd())); + + fields->Set("next_check", DbValue::FromTimestamp(service->GetNextCheck())); + fields->Set("check_type", CompatUtility::GetServiceCheckType(service)); + fields->Set("last_state_change", DbValue::FromTimestamp(service->GetLastStateChange())); + fields->Set("last_hard_state_change", DbValue::FromTimestamp(service->GetLastHardStateChange())); + fields->Set("last_time_up", DbValue::FromTimestamp(static_cast(host->GetLastStateUp()))); + fields->Set("last_time_down", DbValue::FromTimestamp(static_cast(host->GetLastStateDown()))); + fields->Set("last_time_unreachable", DbValue::FromTimestamp(static_cast(host->GetLastStateUnreachable()))); + fields->Set("state_type", service->GetStateType()); + fields->Set("last_notification", DbValue::FromTimestamp(CompatUtility::GetServiceNotificationLastNotification(service))); + fields->Set("next_notification", DbValue::FromTimestamp(CompatUtility::GetServiceNotificationNextNotification(service))); fields->Set("no_more_notifications", Empty); - fields->Set("notifications_enabled", attrs->Get("notifications_enabled")); - fields->Set("problem_has_been_acknowledged", attrs->Get("problem_has_been_acknowledged")); - fields->Set("acknowledgement_type", attrs->Get("acknowledgement_type")); - fields->Set("current_notification_number", attrs->Get("current_notification_number")); - fields->Set("passive_checks_enabled", attrs->Get("passive_checks_enabled")); - fields->Set("active_checks_enabled", attrs->Get("active_checks_enabled")); - fields->Set("eventhandler_enabled", attrs->Get("eventhandler_enabled")); - fields->Set("flap_detection_enabled", attrs->Get("flap_detection_enabled")); - fields->Set("is_flapping", attrs->Get("is_flapping")); - fields->Set("percent_state_change", attrs->Get("percent_state_change")); - fields->Set("latency", attrs->Get("check_latency")); - fields->Set("execution_time", attrs->Get("check_execution_time")); - fields->Set("scheduled_downtime_depth", attrs->Get("scheduled_downtime_depth")); + fields->Set("notifications_enabled", CompatUtility::GetServiceNotificationsEnabled(service)); + fields->Set("problem_has_been_acknowledged", CompatUtility::GetServiceProblemHasBeenAcknowledged(service)); + fields->Set("acknowledgement_type", CompatUtility::GetServiceAcknowledgementType(service)); + fields->Set("current_notification_number", CompatUtility::GetServiceNotificationNotificationNumber(service)); + fields->Set("passive_checks_enabled", CompatUtility::GetServicePassiveChecksEnabled(service)); + fields->Set("active_checks_enabled", CompatUtility::GetServiceActiveChecksEnabled(service)); + fields->Set("event_handler_enabled", CompatUtility::GetServiceEventHandlerEnabled(service)); + fields->Set("flap_detection_enabled", CompatUtility::GetServiceFlapDetectionEnabled(service)); + fields->Set("is_flapping", CompatUtility::GetServiceIsFlapping(service)); + fields->Set("percent_state_change", CompatUtility::GetServicePercentStateChange(service)); + + if (cr) { + fields->Set("latency", Service::CalculateLatency(cr)); + fields->Set("execution_time", Service::CalculateExecutionTime(cr)); + } + fields->Set("scheduled_downtime_depth", service->GetDowntimeDepth()); fields->Set("failure_prediction_enabled", Empty); - fields->Set("process_performance_data", attrs->Get("process_performance_data")); + fields->Set("process_performance_data", 0); /* this is a host which does not process any perf data */ fields->Set("obsess_over_host", Empty); - fields->Set("modified_host_attributes", attrs->Get("modified_attributes")); - fields->Set("event_handler", attrs->Get("event_handler")); - fields->Set("check_command", attrs->Get("check_command")); - fields->Set("normal_check_interval", attrs->Get("check_interval")); - fields->Set("retry_check_interval", attrs->Get("retry_interval")); + fields->Set("modified_host_attributes", service->GetModifiedAttributes()); + fields->Set("event_handler", CompatUtility::GetServiceEventHandler(service)); + fields->Set("check_command", CompatUtility::GetServiceCheckCommand(service)); + fields->Set("normal_check_interval", CompatUtility::GetServiceCheckInterval(service)); + fields->Set("retry_check_interval", CompatUtility::GetServiceRetryInterval(service)); fields->Set("check_timeperiod_object_id", service->GetCheckPeriod()); } else { diff --git a/lib/db_ido/servicedbobject.cpp b/lib/db_ido/servicedbobject.cpp index d90d44897..a260342ac 100644 --- a/lib/db_ido/servicedbobject.cpp +++ b/lib/db_ido/servicedbobject.cpp @@ -79,64 +79,56 @@ Dictionary::Ptr ServiceDbObject::GetConfigFields(void) const { Dictionary::Ptr fields = make_shared(); Service::Ptr service = static_pointer_cast(GetObject()); - Host::Ptr host = service->GetHost(); - Dictionary::Ptr attrs; - - { - ObjectLock olock(service); - attrs = CompatUtility::GetServiceConfigAttributes(service); - } - fields->Set("host_object_id", host); fields->Set("display_name", service->GetDisplayName()); fields->Set("check_command_object_id", service->GetCheckCommand()); fields->Set("check_command_args", Empty); fields->Set("eventhandler_command_object_id", service->GetEventCommand()); fields->Set("eventhandler_command_args", Empty); - fields->Set("notification_timeperiod_object_id", Notification::GetByName(attrs->Get("notification_period"))); + fields->Set("notification_timeperiod_object_id", Notification::GetByName(CompatUtility::GetServiceNotificationNotificationPeriod(service))); fields->Set("check_timeperiod_object_id", service->GetCheckPeriod()); fields->Set("failure_prediction_options", Empty); - fields->Set("check_interval", attrs->Get("check_interval")); - fields->Set("retry_interval", attrs->Get("retry_interval")); - fields->Set("max_check_attempts", attrs->Get("max_check_attempts")); + fields->Set("check_interval", CompatUtility::GetServiceCheckInterval(service)); + fields->Set("retry_interval", CompatUtility::GetServiceRetryInterval(service)); + fields->Set("max_check_attempts", service->GetMaxCheckAttempts()); fields->Set("first_notification_delay", Empty); - fields->Set("notification_interval", attrs->Get("notification_interval")); - fields->Set("notify_on_warning", attrs->Get("notify_on_warning")); - fields->Set("notify_on_unknown", attrs->Get("notify_on_unknown")); - fields->Set("notify_on_critical", attrs->Get("notify_on_critical")); - fields->Set("notify_on_recovery", attrs->Get("notify_on_recovery")); - fields->Set("notify_on_flapping", attrs->Get("notify_on_flapping")); - fields->Set("notify_on_downtime", attrs->Get("notify_on_downtime")); + fields->Set("notification_interval", CompatUtility::GetServiceNotificationNotificationInterval(service)); + fields->Set("notify_on_warning", CompatUtility::GetServiceNotifyOnWarning(service)); + fields->Set("notify_on_unknown", CompatUtility::GetServiceNotifyOnUnknown(service)); + fields->Set("notify_on_critical", CompatUtility::GetServiceNotifyOnCritical(service)); + fields->Set("notify_on_recovery", CompatUtility::GetServiceNotifyOnRecovery(service)); + fields->Set("notify_on_flapping", CompatUtility::GetServiceNotifyOnFlapping(service)); + fields->Set("notify_on_downtime", CompatUtility::GetServiceNotifyOnDowntime(service)); fields->Set("stalk_on_ok", 0); fields->Set("stalk_on_warning", 0); fields->Set("stalk_on_unknown", 0); fields->Set("stalk_on_critical", 0); - fields->Set("is_volatile", attrs->Get("is_volatile")); - fields->Set("flap_detection_enabled", attrs->Get("flap_detection_enabled")); + fields->Set("is_volatile", CompatUtility::GetServiceIsVolatile(service)); + fields->Set("flap_detection_enabled", CompatUtility::GetServiceFlapDetectionEnabled(service)); fields->Set("flap_detection_on_ok", Empty); fields->Set("flap_detection_on_warning", Empty); fields->Set("flap_detection_on_unknown", Empty); fields->Set("flap_detection_on_critical", Empty); - fields->Set("low_flap_threshold", attrs->Get("low_flap_threshold")); - fields->Set("high_flap_threshold", attrs->Get("high_flap_threshold")); - fields->Set("process_performance_data", attrs->Get("process_performance_data")); - fields->Set("freshness_checks_enabled", attrs->Get("freshness_checks_enabled")); - fields->Set("freshness_threshold", Empty); - fields->Set("passive_checks_enabled", attrs->Get("passive_checks_enabled")); - fields->Set("event_handler_enabled", attrs->Get("event_handler_enabled")); - fields->Set("active_checks_enabled", attrs->Get("active_checks_enabled")); + fields->Set("low_flap_threshold", CompatUtility::GetServiceLowFlapThreshold(service)); + fields->Set("high_flap_threshold", CompatUtility::GetServiceHighFlapThreshold(service)); + fields->Set("process_performance_data", CompatUtility::GetServiceProcessPerformanceData(service)); + fields->Set("freshness_checks_enabled", CompatUtility::GetServiceFreshnessChecksEnabled(service)); + fields->Set("freshness_threshold", CompatUtility::GetServiceFreshnessThreshold(service)); + fields->Set("passive_checks_enabled", CompatUtility::GetServicePassiveChecksEnabled(service)); + fields->Set("event_handler_enabled", CompatUtility::GetServiceEventHandlerEnabled(service)); + fields->Set("active_checks_enabled", CompatUtility::GetServiceActiveChecksEnabled(service)); fields->Set("retain_status_information", Empty); fields->Set("retain_nonstatus_information", Empty); - fields->Set("notifications_enabled", attrs->Get("notifications_enabled")); + fields->Set("notifications_enabled", CompatUtility::GetServiceNotificationsEnabled(service)); fields->Set("obsess_over_service", Empty); fields->Set("failure_prediction_enabled", Empty); - fields->Set("notes", attrs->Get("notes")); - fields->Set("notes_url", attrs->Get("notes_url")); - fields->Set("action_url", attrs->Get("action_url")); - fields->Set("icon_image", attrs->Get("icon_image")); - fields->Set("icon_image_alt", attrs->Get("icon_image_alt")); + fields->Set("notes", CompatUtility::GetCustomAttributeConfig(service, "notes")); + fields->Set("notes_url", CompatUtility::GetCustomAttributeConfig(service, "notes_url")); + fields->Set("action_url", CompatUtility::GetCustomAttributeConfig(service, "action_url")); + fields->Set("icon_image", CompatUtility::GetCustomAttributeConfig(service, "icon_image")); + fields->Set("icon_image_alt", CompatUtility::GetCustomAttributeConfig(service, "icon_image_alt")); return fields; } @@ -145,55 +137,60 @@ Dictionary::Ptr ServiceDbObject::GetStatusFields(void) const { Dictionary::Ptr fields = make_shared(); Service::Ptr service = static_pointer_cast(GetObject()); - Dictionary::Ptr attrs; + CheckResult::Ptr cr = service->GetLastCheckResult(); - { - ObjectLock olock(service); - attrs = CompatUtility::GetServiceStatusAttributes(service, CompatTypeService); + if (cr) { + fields->Set("output", CompatUtility::GetCheckResultOutput(cr)); + fields->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr)); + fields->Set("perfdata", CompatUtility::GetCheckResultPerfdata(cr)); + fields->Set("check_source", cr->GetCheckSource()); } - fields->Set("output", attrs->Get("plugin_output")); - fields->Set("long_output", attrs->Get("long_plugin_output")); - fields->Set("perfdata", attrs->Get("performance_data")); - fields->Set("check_source", attrs->Get("check_source")); - fields->Set("current_state", attrs->Get("current_state")); - fields->Set("has_been_checked", attrs->Get("has_been_checked")); - fields->Set("should_be_scheduled", attrs->Get("should_be_scheduled")); - fields->Set("current_check_attempt", attrs->Get("current_attempt")); - fields->Set("max_check_attempts", attrs->Get("max_attempts")); - fields->Set("last_check", DbValue::FromTimestamp(attrs->Get("last_check"))); - fields->Set("next_check", DbValue::FromTimestamp(attrs->Get("next_check"))); - fields->Set("check_type", attrs->Get("check_type")); - fields->Set("last_state_change", DbValue::FromTimestamp(attrs->Get("last_state_change"))); - fields->Set("last_hard_state_change", DbValue::FromTimestamp(attrs->Get("last_hard_state_change"))); - fields->Set("last_time_ok", DbValue::FromTimestamp(attrs->Get("last_time_ok"))); - fields->Set("last_time_warning", DbValue::FromTimestamp(attrs->Get("last_time_warn"))); - fields->Set("last_time_critical", DbValue::FromTimestamp(attrs->Get("last_time_critical"))); - fields->Set("last_time_unknown", DbValue::FromTimestamp(attrs->Get("last_time_unknown"))); - fields->Set("state_type", attrs->Get("state_type")); - fields->Set("last_notification", DbValue::FromTimestamp(attrs->Get("last_notification"))); - fields->Set("next_notification", DbValue::FromTimestamp(attrs->Get("next_notification"))); + fields->Set("current_state", CompatUtility::GetServiceCurrentState(service)); + fields->Set("has_been_checked", CompatUtility::GetServiceHasBeenChecked(service)); + fields->Set("should_be_scheduled", CompatUtility::GetServiceShouldBeScheduled(service)); + fields->Set("current_check_attempt", service->GetCheckAttempt()); + fields->Set("max_check_attempts", service->GetMaxCheckAttempts()); + + if (cr) + fields->Set("last_check", DbValue::FromTimestamp(cr->GetScheduleEnd())); + + fields->Set("next_check", DbValue::FromTimestamp(service->GetNextCheck())); + fields->Set("check_type", CompatUtility::GetServiceCheckType(service)); + fields->Set("last_state_change", DbValue::FromTimestamp(service->GetLastStateChange())); + fields->Set("last_hard_state_change", DbValue::FromTimestamp(service->GetLastHardStateChange())); + fields->Set("last_time_ok", DbValue::FromTimestamp(static_cast(service->GetLastStateOK()))); + fields->Set("last_time_warning", DbValue::FromTimestamp(static_cast(service->GetLastStateWarning()))); + fields->Set("last_time_critical", DbValue::FromTimestamp(static_cast(service->GetLastStateCritical()))); + fields->Set("last_time_unknown", DbValue::FromTimestamp(static_cast(service->GetLastStateUnknown()))); + fields->Set("state_type", service->GetStateType()); + fields->Set("last_notification", DbValue::FromTimestamp(CompatUtility::GetServiceNotificationLastNotification(service))); + fields->Set("next_notification", DbValue::FromTimestamp(CompatUtility::GetServiceNotificationNextNotification(service))); fields->Set("no_more_notifications", Empty); - fields->Set("notifications_enabled", attrs->Get("notifications_enabled")); - fields->Set("problem_has_been_acknowledged", attrs->Get("problem_has_been_acknowledged")); - fields->Set("acknowledgement_type", attrs->Get("acknowledgement_type")); - fields->Set("current_notification_number", attrs->Get("current_notification_number")); - fields->Set("passive_checks_enabled", attrs->Get("passive_checks_enabled")); - fields->Set("active_checks_enabled", attrs->Get("active_checks_enabled")); - fields->Set("event_handler_enabled", attrs->Get("event_handler_enabled")); - fields->Set("flap_detection_enabled", attrs->Get("flap_detection_enabled")); - fields->Set("is_flapping", attrs->Get("is_flapping")); - fields->Set("percent_state_change", attrs->Get("percent_state_change")); - fields->Set("latency", attrs->Get("check_latency")); - fields->Set("execution_time", attrs->Get("check_execution_time")); - fields->Set("scheduled_downtime_depth", attrs->Get("scheduled_downtime_depth")); - fields->Set("process_performance_data", attrs->Get("process_performance_data")); - fields->Set("event_handler", attrs->Get("event_handler")); - fields->Set("check_command", attrs->Get("check_command")); - fields->Set("normal_check_interval", attrs->Get("check_interval")); - fields->Set("retry_check_interval", attrs->Get("retry_interval")); + fields->Set("notifications_enabled", CompatUtility::GetServiceNotificationsEnabled(service)); + fields->Set("problem_has_been_acknowledged", CompatUtility::GetServiceProblemHasBeenAcknowledged(service)); + fields->Set("acknowledgement_type", CompatUtility::GetServiceAcknowledgementType(service)); + fields->Set("current_notification_number", CompatUtility::GetServiceNotificationNotificationNumber(service)); + fields->Set("passive_checks_enabled", CompatUtility::GetServicePassiveChecksEnabled(service)); + fields->Set("active_checks_enabled", CompatUtility::GetServiceActiveChecksEnabled(service)); + fields->Set("event_handler_enabled", CompatUtility::GetServiceEventHandlerEnabled(service)); + fields->Set("flap_detection_enabled", CompatUtility::GetServiceFlapDetectionEnabled(service)); + fields->Set("is_flapping", CompatUtility::GetServiceIsFlapping(service)); + fields->Set("percent_state_change", CompatUtility::GetServicePercentStateChange(service)); + + if (cr) { + fields->Set("latency", Service::CalculateLatency(cr)); + fields->Set("execution_time", Service::CalculateExecutionTime(cr)); + } + + fields->Set("scheduled_downtime_depth", service->GetDowntimeDepth()); + fields->Set("process_performance_data", CompatUtility::GetServiceProcessPerformanceData(service)); + fields->Set("event_handler", CompatUtility::GetServiceEventHandler(service)); + fields->Set("check_command", CompatUtility::GetServiceCheckCommand(service)); + fields->Set("normal_check_interval", CompatUtility::GetServiceCheckInterval(service)); + fields->Set("retry_check_interval", CompatUtility::GetServiceRetryInterval(service)); fields->Set("check_timeperiod_object_id", service->GetCheckPeriod()); - fields->Set("modified_service_attributes", attrs->Get("modified_attributes")); + fields->Set("modified_service_attributes", service->GetModifiedAttributes()); return fields; } @@ -739,7 +736,7 @@ void ServiceDbObject::AddAcknowledgementHistory(const Service::Ptr& service, con fields1->Set("entry_time_usec", time_bag.second); fields1->Set("acknowledgement_type", type); fields1->Set("object_id", service); - fields1->Set("state", service->GetState()); + fields1->Set("state", CompatUtility::GetServiceCurrentState(service)); fields1->Set("author_name", author); fields1->Set("comment_data", comment); fields1->Set("is_sticky", type == AcknowledgementSticky ? 1 : 0); @@ -813,12 +810,11 @@ void ServiceDbObject::AddNotificationHistory(const Service::Ptr& service, const fields1->Set("start_time_usec", time_bag.second); fields1->Set("end_time", DbValue::FromTimestamp(time_bag.first)); fields1->Set("end_time_usec", time_bag.second); - fields1->Set("state", service->GetState()); + fields1->Set("state", CompatUtility::GetServiceCurrentState(service)); if (cr) { - std::pair output_bag = CompatUtility::GetCheckResultOutput(cr); - fields1->Set("output", output_bag.first); - fields1->Set("long_output", output_bag.second); + fields1->Set("output", CompatUtility::GetCheckResultOutput(cr)); + fields1->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr)); } fields1->Set("escalated", 0); @@ -858,7 +854,7 @@ void ServiceDbObject::AddStateChangeHistory(const Service::Ptr& service, const C fields1->Set("state_time_usec", time_bag.second); fields1->Set("object_id", service); fields1->Set("state_change", 1); /* service */ - fields1->Set("state", service->GetState()); + fields1->Set("state", CompatUtility::GetServiceCurrentState(service)); fields1->Set("state_type", service->GetStateType()); fields1->Set("current_check_attempt", service->GetCheckAttempt()); fields1->Set("max_check_attempts", service->GetMaxCheckAttempts()); @@ -866,9 +862,8 @@ void ServiceDbObject::AddStateChangeHistory(const Service::Ptr& service, const C fields1->Set("last_hard_state", service->GetLastHardState()); if (cr) { - std::pair output_bag = CompatUtility::GetCheckResultOutput(cr); - fields1->Set("output", output_bag.first); - fields1->Set("long_output", output_bag.second); + fields1->Set("output", CompatUtility::GetCheckResultOutput(cr)); + fields1->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr)); } fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */ @@ -937,10 +932,8 @@ void ServiceDbObject::AddCheckResultLogHistory(const Service::Ptr& service, cons String output; - if (cr) { - std::pair output_bag = CompatUtility::GetCheckResultOutput(cr); - output = output_bag.first; - } + if (cr) + output = CompatUtility::GetCheckResultOutput(cr); std::ostringstream msgbuf; msgbuf << "SERVICE ALERT: " @@ -1076,10 +1069,8 @@ void ServiceDbObject::AddNotificationSentLogHistory(const Service::Ptr& service, String output; - if (cr) { - std::pair output_bag = CompatUtility::GetCheckResultOutput(cr); - output = output_bag.first; - } + if (cr) + output = CompatUtility::GetCheckResultOutput(cr); std::ostringstream msgbuf; msgbuf << "SERVICE NOTIFICATION: " @@ -1251,6 +1242,9 @@ void ServiceDbObject::AddFlappingHistory(const Service::Ptr& service, FlappingSt /* servicechecks */ void ServiceDbObject::AddServiceCheckHistory(const Service::Ptr& service, const CheckResult::Ptr &cr) { + if (!cr) + return; + Host::Ptr host = service->GetHost(); Log(LogDebug, "db_ido", "add service check history for '" + service->GetName() + "'"); @@ -1261,38 +1255,33 @@ void ServiceDbObject::AddServiceCheckHistory(const Service::Ptr& service, const query1.Category = DbCatCheck; Dictionary::Ptr fields1 = make_shared(); - Dictionary::Ptr attrs; - - { - ObjectLock olock(service); - attrs = CompatUtility::GetServiceStatusAttributes(service, CompatTypeService); - } + double execution_time = Service::CalculateExecutionTime(cr); - fields1->Set("check_type", attrs->Get("check_type")); - fields1->Set("current_check_attempt", attrs->Get("current_attempt")); - fields1->Set("max_check_attempts", attrs->Get("max_attempts")); - fields1->Set("state", attrs->Get("current_state")); - fields1->Set("state_type", attrs->Get("state_type")); + fields1->Set("check_type", CompatUtility::GetServiceCheckType(service)); + fields1->Set("current_check_attempt", service->GetCheckAttempt()); + fields1->Set("max_check_attempts", service->GetMaxCheckAttempts()); + fields1->Set("state", CompatUtility::GetServiceCurrentState(service)); + fields1->Set("state_type", service->GetStateType()); double now = Utility::GetTime(); std::pair time_bag = CompatUtility::ConvertTimestamp(now); - double end = now + attrs->Get("check_execution_time"); - std::pair time_bag_end = CompatUtility::ConvertTimestamp(now); + double end = now + execution_time; + std::pair time_bag_end = CompatUtility::ConvertTimestamp(end); fields1->Set("start_time", DbValue::FromTimestamp(time_bag.first)); fields1->Set("start_time_usec", time_bag.second); - fields1->Set("end_time", DbValue::FromTimestamp(time_bag.first)); - fields1->Set("end_time_usec", time_bag.second); + fields1->Set("end_time", DbValue::FromTimestamp(time_bag_end.first)); + fields1->Set("end_time_usec", time_bag_end.second); fields1->Set("command_object_id", service->GetCheckCommand()); fields1->Set("command_args", Empty); fields1->Set("command_line", cr->GetCommand()); - fields1->Set("execution_time", attrs->Get("check_execution_time")); - fields1->Set("latency", attrs->Get("check_latency")); + fields1->Set("execution_time", execution_time); + fields1->Set("latency", Service::CalculateLatency(cr)); fields1->Set("return_code", cr->GetExitStatus()); - fields1->Set("output", attrs->Get("plugin_output")); - fields1->Set("long_output", attrs->Get("long_plugin_output")); - fields1->Set("perfdata", attrs->Get("performance_data")); + fields1->Set("output", CompatUtility::GetCheckResultOutput(cr)); + fields1->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr)); + fields1->Set("perfdata", CompatUtility::GetCheckResultPerfdata(cr)); fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */ @@ -1331,7 +1320,7 @@ void ServiceDbObject::AddEventHandlerHistory(const Service::Ptr& service) fields1->Set("eventhandler_type", 1); /* service */ fields1->Set("object_id", service); - fields1->Set("state", service->GetState()); + fields1->Set("state", CompatUtility::GetServiceCurrentState(service)); fields1->Set("state_type", service->GetStateType()); fields1->Set("start_time", DbValue::FromTimestamp(time_bag.first)); diff --git a/lib/icinga/compatutility.cpp b/lib/icinga/compatutility.cpp index 8796e668a..7a42e53df 100644 --- a/lib/icinga/compatutility.cpp +++ b/lib/icinga/compatutility.cpp @@ -21,6 +21,7 @@ #include "icinga/checkcommand.h" #include "icinga/eventcommand.h" #include "icinga/pluginutility.h" +#include "base/utility.h" #include "base/dynamictype.h" #include "base/objectlock.h" #include "base/debug.h" @@ -33,454 +34,667 @@ using namespace icinga; -Dictionary::Ptr CompatUtility::GetHostConfigAttributes(const Host::Ptr& host) +/* command */ +String CompatUtility::GetCommandLine(const Command::Ptr& command) { - Dictionary::Ptr attr = make_shared(); - Dictionary::Ptr service_attr = make_shared(); + Value commandLine = command->GetCommandLine(); + + String commandline; + if (commandLine.IsObjectType()) { + Array::Ptr args = commandLine; + ObjectLock olock(args); + String arg; + BOOST_FOREACH(arg, args) { + // This is obviously incorrect for non-trivial cases. + commandline = " \"" + EscapeString(arg) + "\""; + } + } else if (!commandLine.IsEmpty()) { + commandline = EscapeString(Convert::ToString(commandLine)); + } else { + commandline = ""; + } + + return commandline; +} + +/* host */ +String CompatUtility::GetHostAlias(const Host::Ptr& host) +{ ASSERT(host->OwnsLock()); - /* host config attributes */ - Dictionary::Ptr custom; - Dictionary::Ptr macros; - std::vector notification_options; + if (!host->GetDisplayName().IsEmpty()) + return host->GetName(); + else + return host->GetDisplayName(); +} + +String CompatUtility::GetHostAddress(const Host::Ptr& host) +{ + ASSERT(host->OwnsLock()); - /* dicts */ - macros = host->GetMacros(); - custom = host->GetCustom(); + Dictionary::Ptr macros = host->GetMacros(); + + String address; if (macros) { - attr->Set("address", macros->Get("address")); - attr->Set("address6", macros->Get("address6")); + address = macros->Get("address"); } + return address; +} + +String CompatUtility::GetHostAddress6(const Host::Ptr& host) +{ + ASSERT(host->OwnsLock()); + + Dictionary::Ptr macros = host->GetMacros(); + + String address6; + + if (macros) { + address6 = macros->Get("address6"); + } + + return address6; +} + +Host2dCoords CompatUtility::GetHost2dCoords(const Host::Ptr& host) +{ + ASSERT(host->OwnsLock()); + + Dictionary::Ptr custom = host->GetCustom(); + Host2dCoords bag; + if (custom) { - attr->Set("notes", custom->Get("notes")); - attr->Set("notes_url", custom->Get("notes_url")); - attr->Set("action_url", custom->Get("action_url")); - attr->Set("icon_image", custom->Get("icon_image")); - attr->Set("icon_image_alt", custom->Get("icon_image_alt")); - - attr->Set("statusmap_image", custom->Get("statusmap_image")); - attr->Set("2d_coords", custom->Get("2d_coords")); - - if (!custom->Get("2d_coords").IsEmpty()) { - std::vector tokens; - String coords = custom->Get("2d_coords"); - boost::algorithm::split(tokens, coords, boost::is_any_of(",")); - - if (tokens.size() == 2) { - attr->Set("have_2d_coords", 1); - attr->Set("x_2d", tokens[0]); - attr->Set("y_2d", tokens[1]); - } - else - attr->Set("have_2d_coords", 0); + bag.have_2d_coords = (custom->Get("x_2d") && custom->Get("y_2d") ? 1 : 0); + + if (bag.have_2d_coords == 1) { + bag.x_2d = custom->Get("x_2d"); + bag.y_2d = custom->Get("y_2d"); } - else - attr->Set("have_2d_coords", 0); - - /* deprecated in 1.x, but empty */ - attr->Set("vrml_image", Empty); - attr->Set("3d_coords", Empty); - attr->Set("have_3d_coords", 0); - attr->Set("x_3d", Empty); - attr->Set("y_3d", Empty); - attr->Set("z_3d", Empty); + } else { + bag.have_2d_coords = 0; } - /* alias */ - if (!host->GetDisplayName().IsEmpty()) - attr->Set("alias", host->GetName()); - else - attr->Set("alias", host->GetDisplayName()); + return bag; +} + +int CompatUtility::GetHostNotifyOnDown(const Host::Ptr& host) +{ + ASSERT(host->OwnsLock()); - /* get additonal attributes from check service */ Service::Ptr service = host->GetCheckService(); - if (service) { - unsigned long notification_type_filter = 0; - unsigned long notification_state_filter = 0; + if (!service) + return 0; - ObjectLock olock(service); + unsigned long notification_state_filter = GetServiceNotificationStateFilter(service); - BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { - if (notification->GetNotificationTypeFilter()) - notification_type_filter = notification->GetNotificationTypeFilter(); + if (notification_state_filter & (1<GetNotificationStateFilter()) - notification_state_filter = notification->GetNotificationStateFilter(); - } + return 0; +} - /* notification state filters */ - if (notification_state_filter & (1<Set("notify_on_down", 1); - notification_options.push_back("d"); - } +int CompatUtility::GetHostNotifyOnUnreachable(const Host::Ptr& host) +{ + ASSERT(host->OwnsLock()); - /* notification type filters */ - if (notification_type_filter & (1<Set("notify_on_recovery", 1); - notification_options.push_back("r"); - } - if (notification_type_filter & (1<Set("notify_on_flapping", 1); - notification_options.push_back("f"); - } - if (notification_type_filter & (1<Set("notify_on_downtime", 1); - notification_options.push_back("s"); - } + Service::Ptr service = host->GetCheckService(); - service_attr = CompatUtility::GetServiceConfigAttributes(service); - - attr->Set("check_period", service_attr->Get("check_period")); - attr->Set("check_interval", service_attr->Get("check_interval")); - attr->Set("retry_interval", service_attr->Get("retry_interval")); - attr->Set("max_check_attempts", service_attr->Get("max_check_attempts")); - attr->Set("active_checks_enabled", service_attr->Get("active_checks_enabled")); - attr->Set("passive_checks_enabled", service_attr->Get("passive_checks_enabled")); - attr->Set("flap_detection_enabled", service_attr->Get("flap_detection_enabled")); - attr->Set("low_flap_threshold", service_attr->Get("low_flap_threshold")); - attr->Set("high_flap_threshold", service_attr->Get("high_flap_threshold")); - attr->Set("notifications_enabled", service_attr->Get("notifications_enabled")); - attr->Set("eventhandler_enabled", service_attr->Get("eventhandler_enabled")); - attr->Set("is_volatile", service_attr->Get("is_volatile")); - attr->Set("notifications_enabled", service_attr->Get("notifications_enabled")); - attr->Set("notification_options", boost::algorithm::join(notification_options, ",")); - attr->Set("notification_interval", service_attr->Get("notification_interval")); - attr->Set("process_performance_data", service_attr->Get("process_performance_data")); - attr->Set("notification_period", service_attr->Get("notification_period")); - attr->Set("check_freshness", service_attr->Get("check_freshness")); - attr->Set("check_command", service_attr->Get("check_command")); - attr->Set("event_handler", service_attr->Get("event_handler")); - } + if (!service) + return 0; + + unsigned long notification_state_filter = GetServiceNotificationStateFilter(service); + + if (notification_state_filter & (1<(); + ASSERT(service->OwnsLock()); + + int state = service->GetState(); + + if (state > StateUnknown) + return StateUnknown; + return state; +} + +int CompatUtility::GetServiceShouldBeScheduled(const Service::Ptr& service) +{ ASSERT(service->OwnsLock()); - String raw_output; - String output; - String long_output; - String perfdata; - String check_source; - double schedule_end = -1; + return (service->GetEnableActiveChecks() ? 1 : 0); +} + +int CompatUtility::GetServiceCheckType(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return (service->GetEnableActiveChecks() ? 0 : 1); +} + +double CompatUtility::GetServiceCheckInterval(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return service->GetCheckInterval() / 60.0; +} + +double CompatUtility::GetServiceRetryInterval(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return service->GetRetryInterval() / 60.0; +} + +String CompatUtility::GetServiceCheckPeriod(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); - String check_period_str; TimePeriod::Ptr check_period = service->GetCheckPeriod(); if (check_period) - check_period_str = check_period->GetName(); + return check_period->GetName(); else - check_period_str = "24x7"; + return "24x7"; +} + +int CompatUtility::GetServiceHasBeenChecked(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return (service->GetLastCheckResult() ? 1 : 0); +} + + +int CompatUtility::GetServiceProblemHasBeenAcknowledged(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return (service->GetAcknowledgement() != AcknowledgementNone ? 1 : 0); +} + +int CompatUtility::GetServiceAcknowledgementType(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return static_cast(service->GetAcknowledgement()); +} + +int CompatUtility::GetServicePassiveChecksEnabled(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return (service->GetEnablePassiveChecks() ? 1 : 0); +} - CheckResult::Ptr cr = service->GetLastCheckResult(); +int CompatUtility::GetServiceActiveChecksEnabled(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return (service->GetEnableActiveChecks() ? 1 : 0); +} + +int CompatUtility::GetServiceEventHandlerEnabled(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return (service->GetEventCommand() ? 1 : 0); +} + +int CompatUtility::GetServiceFlapDetectionEnabled(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return (service->GetEnableFlapping() ? 1 : 0); +} + +int CompatUtility::GetServiceIsFlapping(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return (service->IsFlapping() ? 1 : 0); +} + +String CompatUtility::GetServicePercentStateChange(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return Convert::ToString(service->GetFlappingCurrent()); +} + +int CompatUtility::GetServiceProcessPerformanceData(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return (service->GetEnablePerfdata() ? 1 : 0); +} + +String CompatUtility::GetServiceEventHandler(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + String event_command_str; + EventCommand::Ptr eventcommand = service->GetEventCommand(); + + if (eventcommand) + event_command_str = eventcommand->GetName(); + + return event_command_str; +} + +String CompatUtility::GetServiceCheckCommand(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + String check_command_str; + CheckCommand::Ptr checkcommand = service->GetCheckCommand(); + + if (checkcommand) + check_command_str = checkcommand->GetName(); + + return check_command_str; +} + +int CompatUtility::GetServiceIsVolatile(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return (service->GetVolatile() ? 1 : 0); +} + +double CompatUtility::GetServiceLowFlapThreshold(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return service->GetFlappingThreshold(); +} - if (cr) { - std::pair output_bag = GetCheckResultOutput(cr); - output = output_bag.first; - long_output = output_bag.second; +double CompatUtility::GetServiceHighFlapThreshold(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return service->GetFlappingThreshold(); +} + +int CompatUtility::GetServiceFreshnessChecksEnabled(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return (service->GetCheckInterval() > 0 ? 1 : 0); +} + +int CompatUtility::GetServiceFreshnessThreshold(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); - check_source = cr->GetCheckSource(); + return static_cast(service->GetCheckInterval()); +} + +double CompatUtility::GetServiceStaleness(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + if (service->HasBeenChecked() && service->GetLastCheck() > 0) + return (Utility::GetTime() - service->GetLastCheck()) / (service->GetCheckInterval() * 3600); + + return 0.0; +} + +int CompatUtility::GetServiceIsAcknowledged(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return (service->IsAcknowledged() ? 1 : 0); +} + +int CompatUtility::GetServiceNoMoreNotifications(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + if (CompatUtility::GetServiceNotificationNotificationInterval(service) == 0 && !service->GetVolatile()) + return 1; + + return 0; +} - perfdata = GetCheckResultPerfdata(cr); - schedule_end = cr->GetScheduleEnd(); +int CompatUtility::GetServiceInCheckPeriod(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + TimePeriod::Ptr timeperiod = service->GetCheckPeriod(); + + /* none set means always checked */ + if (!timeperiod) + return 1; + + return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0); +} + +int CompatUtility::GetServiceInNotificationPeriod(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { + ObjectLock olock(notification); + + TimePeriod::Ptr timeperiod = notification->GetNotificationPeriod(); + + /* first notification wins */ + if (timeperiod) + return (timeperiod->IsInside(Utility::GetTime()) ? 1 : 0); } - int state = service->GetState(); + /* none set means always notified */ + return 1; +} - if (state > StateUnknown) - state = StateUnknown; +//fixme structs +/* custom attr */ +Dictionary::Ptr CompatUtility::GetCustomVariableConfig(const DynamicObject::Ptr& object) +{ + ASSERT(service->OwnsLock()); - if (type == CompatTypeHost) { - if (state == StateOK || state == StateWarning) - state = 0; /* UP */ - else - state = 1; /* DOWN */ + Dictionary::Ptr custom; - Host::Ptr host = service->GetHost(); + if (object->GetType() == DynamicType::GetByName("Host")) { + custom = static_pointer_cast(object)->GetCustom(); + } else if (object->GetType() == DynamicType::GetByName("Service")) { + custom = static_pointer_cast(object)->GetCustom(); + } else if (object->GetType() == DynamicType::GetByName("User")) { + custom = static_pointer_cast(object)->GetCustom(); + } else { + Log(LogDebug, "icinga", "unknown object type for custom vars"); + return Dictionary::Ptr(); + } - ASSERT(host); + Dictionary::Ptr customvars = make_shared(); - if (!host->IsReachable()) - state = 2; /* UNREACHABLE */ + if (!custom) + return Dictionary::Ptr(); - attr->Set("last_time_up", host->GetLastStateUp()); - attr->Set("last_time_down", host->GetLastStateDown()); - attr->Set("last_time_unreachable", host->GetLastStateUnreachable()); + ObjectLock olock(custom); + String key; + Value value; + BOOST_FOREACH(const Dictionary::Pair& kv, custom) { + if (kv.first == "notes" || + kv.first == "action_url" || + kv.first == "notes_url" || + kv.first == "icon_image" || + kv.first == "icon_image_alt" || + kv.first == "statusmap_image" || + kv.first == "2d_coords") + continue; + + customvars->Set(key, value); + } + + return customvars; +} + +String CompatUtility::GetCustomAttributeConfig(const DynamicObject::Ptr& object, const String& name) +{ + ASSERT(service->OwnsLock()); + + Dictionary::Ptr custom; + + if (object->GetType() == DynamicType::GetByName("Host")) { + custom = static_pointer_cast(object)->GetCustom(); + } else if (object->GetType() == DynamicType::GetByName("Service")) { + custom = static_pointer_cast(object)->GetCustom(); + } else if (object->GetType() == DynamicType::GetByName("User")) { + custom = static_pointer_cast(object)->GetCustom(); } else { - attr->Set("last_time_ok", service->GetLastStateOK()); - attr->Set("last_time_warn", service->GetLastStateWarning()); - attr->Set("last_time_critical", service->GetLastStateCritical()); - attr->Set("last_time_unknown", service->GetLastStateUnknown()); + Log(LogDebug, "icinga", "unknown object type for custom attributes"); + return Empty; } - double last_notification = 0; - double next_notification = 0; - int notification_number = 0; + if (!custom) + return Empty; + + return custom->Get(name); +} + +/* notifications */ +int CompatUtility::GetServiceNotificationsEnabled(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + return (service->GetEnableNotifications() ? 1 : 0); +} + +int CompatUtility::GetServiceNotificationLastNotification(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + double last_notification = 0.0; BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { if (notification->GetLastNotification() > last_notification) last_notification = notification->GetLastNotification(); + } + + return static_cast(last_notification); +} + +int CompatUtility::GetServiceNotificationNextNotification(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + double next_notification = 0.0; + BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { if (notification->GetNextNotification() < next_notification) next_notification = notification->GetNextNotification(); + } + + return static_cast(next_notification); +} + +int CompatUtility::GetServiceNotificationNotificationNumber(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + int notification_number = 0; + BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { if (notification->GetNotificationNumber() > notification_number) notification_number = notification->GetNotificationNumber(); } - CheckCommand::Ptr checkcommand = service->GetCheckCommand(); - if (checkcommand) - attr->Set("check_command", "check_" + checkcommand->GetName()); + return notification_number; +} - EventCommand::Ptr eventcommand = service->GetEventCommand(); - if (eventcommand) - attr->Set("event_handler", "event_" + eventcommand->GetName()); - - attr->Set("check_period", check_period_str); - attr->Set("check_interval", service->GetCheckInterval() / 60.0); - attr->Set("retry_interval", service->GetRetryInterval() / 60.0); - attr->Set("has_been_checked", (service->GetLastCheckResult() ? 1 : 0)); - attr->Set("should_be_scheduled", 1); - attr->Set("check_execution_time", Service::CalculateExecutionTime(cr)); - attr->Set("check_latency", Service::CalculateLatency(cr)); - attr->Set("current_state", state); - attr->Set("state_type", service->GetStateType()); - attr->Set("plugin_output", output); - attr->Set("long_plugin_output", long_output); - attr->Set("performance_data", perfdata); - attr->Set("check_source", check_source); - attr->Set("check_type", (service->GetEnableActiveChecks() ? 0 : 1)); - attr->Set("last_check", schedule_end); - attr->Set("next_check", service->GetNextCheck()); - attr->Set("current_attempt", service->GetCheckAttempt()); - attr->Set("max_attempts", service->GetMaxCheckAttempts()); - attr->Set("last_state_change", service->GetLastStateChange()); - attr->Set("last_hard_state_change", service->GetLastHardStateChange()); - attr->Set("last_update", static_cast(time(NULL))); - attr->Set("process_performance_data", 1); /* always enabled */ - attr->Set("freshness_checks_enabled", 1); /* always enabled */ - attr->Set("notifications_enabled", (service->GetEnableNotifications() ? 1 : 0)); - attr->Set("event_handler_enabled", 1); /* always enabled */ - attr->Set("active_checks_enabled", (service->GetEnableActiveChecks() ? 1 : 0)); - attr->Set("passive_checks_enabled", (service->GetEnablePassiveChecks() ? 1 : 0)); - attr->Set("flap_detection_enabled", (service->GetEnableFlapping() ? 1 : 0)); - attr->Set("is_flapping", (service->IsFlapping() ? 1 : 0)); - attr->Set("percent_state_change", Convert::ToString(service->GetFlappingCurrent())); - attr->Set("problem_has_been_acknowledged", (service->GetAcknowledgement() != AcknowledgementNone ? 1 : 0)); - attr->Set("acknowledgement_type", static_cast(service->GetAcknowledgement())); - attr->Set("acknowledgement_end_time", service->GetAcknowledgementExpiry()); - attr->Set("scheduled_downtime_depth", (service->IsInDowntime() ? 1 : 0)); - attr->Set("last_notification", last_notification); - attr->Set("next_notification", next_notification); - attr->Set("current_notification_number", notification_number); - attr->Set("modified_attributes", service->GetModifiedAttributes()); - - return attr; -} - -Dictionary::Ptr CompatUtility::GetServiceConfigAttributes(const Service::Ptr& service) -{ - Dictionary::Ptr attr = make_shared(); - - ASSERT(service->OwnsLock()); - - Host::Ptr host = service->GetHost(); - - String check_period_str; - TimePeriod::Ptr check_period = service->GetCheckPeriod(); - if (check_period) - check_period_str = check_period->GetName(); - else - check_period_str = "24x7"; +double CompatUtility::GetServiceNotificationNotificationInterval(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); double notification_interval = -1; - String notification_period; - unsigned long notification_type_filter = 0; - unsigned long notification_state_filter = 0; BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { if (notification_interval == -1 || notification->GetNotificationInterval() < notification_interval) notification_interval = notification->GetNotificationInterval(); + } - if (notification->GetNotificationPeriod()) - notification_period = notification->GetNotificationPeriod()->GetName(); + if (notification_interval == -1) + notification_interval = 60; - if (notification->GetNotificationTypeFilter()) - notification_type_filter = notification->GetNotificationTypeFilter(); + return notification_interval / 60.0; +} - if (notification->GetNotificationStateFilter()) - notification_state_filter = notification->GetNotificationStateFilter(); +String CompatUtility::GetServiceNotificationNotificationPeriod(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + TimePeriod::Ptr notification_period; + + BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { - Log(LogDebug, "icinga", "notification_type_filter: " + Convert::ToString(notification_type_filter) + " notification_state_filter: " + Convert::ToString(notification_state_filter)); + if (notification->GetNotificationPeriod()) + notification_period = notification->GetNotificationPeriod(); } + if (!notification_period) + return Empty; - if (notification_interval == -1) - notification_interval = 60; + return notification_period->GetName(); +} - String check_command_str; - String event_command_str; - CheckCommand::Ptr checkcommand = service->GetCheckCommand(); - EventCommand::Ptr eventcommand = service->GetEventCommand(); +String CompatUtility::GetServiceNotificationNotificationOptions(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); - if (checkcommand) - check_command_str = checkcommand->GetName(); + bool is_hostcheck = (service->GetHost()->GetCheckService() == service); - if (eventcommand) - event_command_str = eventcommand->GetName(); + unsigned long notification_type_filter = 0; + unsigned long notification_state_filter = 0; - Dictionary::Ptr custom; - Dictionary::Ptr macros; - std::vector notification_options; + BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { + if (notification->GetNotificationTypeFilter()) + notification_type_filter = notification->GetNotificationTypeFilter(); + + if (notification->GetNotificationStateFilter()) + notification_state_filter = notification->GetNotificationStateFilter(); + } - /* dicts */ - custom = service->GetCustom(); - macros = service->GetMacros(); + std::vector notification_options; /* notification state filters */ if (notification_state_filter & (1<Set("notify_on_warning", 1); notification_options.push_back("w"); } if (notification_state_filter & (1<Set("notify_on_unknown", 1); notification_options.push_back("u"); } if (notification_state_filter & (1<Set("notify_on_critical", 1); notification_options.push_back("c"); } /* notification type filters */ if (notification_type_filter & (1<Set("notify_on_recovery", 1); notification_options.push_back("r"); } if (notification_type_filter & (1<Set("notify_on_flapping", 1); notification_options.push_back("f"); } if (notification_type_filter & (1<Set("notify_on_downtime", 1); notification_options.push_back("s"); } - attr->Set("check_period", check_period_str); - attr->Set("check_interval", service->GetCheckInterval() / 60.0); - attr->Set("retry_interval", service->GetRetryInterval() / 60.0); - attr->Set("max_check_attempts", service->GetMaxCheckAttempts()); - attr->Set("active_checks_enabled", (service->GetEnableActiveChecks() ? 1 : 0)); - attr->Set("passive_checks_enabled", (service->GetEnablePassiveChecks() ? 1 : 0)); - attr->Set("flap_detection_enabled", (service->GetEnableFlapping() ? 1 : 0)); - attr->Set("low_flap_threshold", service->GetFlappingThreshold()); - attr->Set("high_flap_threshold", service->GetFlappingThreshold()); - attr->Set("notifications_enabled", (service->GetEnableNotifications() ? 1 : 0)); - attr->Set("eventhandler_enabled", 1); /* always 1 */ - attr->Set("is_volatile", (service->GetVolatile() ? 1 : 0)); - attr->Set("notifications_enabled", (service->GetEnableNotifications() ? 1 : 0)); - attr->Set("notification_options", boost::algorithm::join(notification_options, ",")); - attr->Set("notification_interval", notification_interval / 60.0); - attr->Set("process_performance_data", 1); /* always 1 */ - attr->Set("notification_period", notification_period); - attr->Set("check_freshness", 1); /* always 1 */ - attr->Set("check_command", check_command_str); - attr->Set("event_handler", event_command_str); - - /* custom attr */ - if (custom) { - attr->Set("notes", custom->Get("notes")); - attr->Set("notes_url", custom->Get("notes_url")); - attr->Set("action_url", custom->Get("action_url")); - attr->Set("icon_image", custom->Get("icon_image")); - attr->Set("icon_image_alt", custom->Get("icon_image_alt")); - } + return boost::algorithm::join(notification_options, ","); +} - return attr; +int CompatUtility::GetServiceNotificationTypeFilter(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + unsigned long notification_type_filter = 0; + BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { + ObjectLock olock(notification); + + if (notification->GetNotificationTypeFilter()) + notification_type_filter = notification->GetNotificationTypeFilter(); + } + + return notification_type_filter; } -Dictionary::Ptr CompatUtility::GetCommandConfigAttributes(const Command::Ptr& command) +int CompatUtility::GetServiceNotificationStateFilter(const Service::Ptr& service) { - Dictionary::Ptr attr = make_shared(); + ASSERT(service->OwnsLock()); - Value commandLine = command->GetCommandLine(); + unsigned long notification_state_filter = 0; - String commandline; - if (commandLine.IsObjectType()) { - Array::Ptr args = commandLine; + BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { + ObjectLock olock(notification); - ObjectLock olock(args); - String arg; - BOOST_FOREACH(arg, args) { - // This is obviously incorrect for non-trivial cases. - commandline = " \"" + EscapeString(arg) + "\""; - } - } else if (!commandLine.IsEmpty()) { - commandline = EscapeString(Convert::ToString(commandLine)); - } else { - commandline = ""; + if (notification->GetNotificationStateFilter()) + notification_state_filter = notification->GetNotificationStateFilter(); } - attr->Set("command_line", commandline); + return notification_state_filter; +} + +int CompatUtility::GetServiceNotifyOnWarning(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); - return attr; + if (GetServiceNotificationStateFilter(service) & (1<OwnsLock()); + + if (GetServiceNotificationStateFilter(service) & (1<OwnsLock()); - if (object->GetType() == DynamicType::GetByName("Host")) { - custom = static_pointer_cast(object)->GetCustom(); - } else if (object->GetType() == DynamicType::GetByName("Service")) { - custom = static_pointer_cast(object)->GetCustom(); - } else if (object->GetType() == DynamicType::GetByName("User")) { - custom = static_pointer_cast(object)->GetCustom(); - } else { - Log(LogDebug, "icinga", "unknown object type for custom vars"); - return Dictionary::Ptr(); - } + if (GetServiceNotificationStateFilter(service) & (1<(); + return 0; +} - if (!custom) - return Dictionary::Ptr(); +int CompatUtility::GetServiceNotifyOnRecovery(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); - ObjectLock olock(custom); - String key; - Value value; - BOOST_FOREACH(const Dictionary::Pair& kv, custom) { - if (kv.first == "notes" || - kv.first == "action_url" || - kv.first == "notes_url" || - kv.first == "icon_image" || - kv.first == "icon_image_alt" || - kv.first == "statusmap_image" || - kv.first == "2d_coords") - continue; + if (GetServiceNotificationTypeFilter(service) & (1<Set(key, value); - } + return 0; +} - return customvars; +int CompatUtility::GetServiceNotifyOnFlapping(const Service::Ptr& service) +{ + ASSERT(service->OwnsLock()); + + unsigned long notification_type_filter = GetServiceNotificationTypeFilter(service); + + if (notification_type_filter & (1<OwnsLock()); + + unsigned long notification_type_filter = GetServiceNotificationTypeFilter(service); + + if (notification_type_filter & (1< CompatUtility::GetServiceNotificationUsers(const Service::Ptr& service) { + ASSERT(service->OwnsLock()); + /* Service -> Notifications -> (Users + UserGroups -> Users) */ std::set allUsers; std::set users; @@ -503,6 +717,8 @@ std::set CompatUtility::GetServiceNotificationUsers(const Service::Pt std::set CompatUtility::GetServiceNotificationUserGroups(const Service::Ptr& service) { + ASSERT(service->OwnsLock()); + std::set usergroups; /* Service -> Notifications -> UserGroups */ BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { @@ -516,12 +732,11 @@ std::set CompatUtility::GetServiceNotificationUserGroups(const S return usergroups; } -std::pair CompatUtility::GetCheckResultOutput(const CheckResult::Ptr& cr) +String CompatUtility::GetCheckResultOutput(const CheckResult::Ptr& cr) { if (!cr) - return std::make_pair(Empty, Empty); + return Empty; - String long_output; String output; String raw_output = cr->GetOutput(); @@ -534,16 +749,33 @@ std::pair CompatUtility::GetCheckResultOutput(const CheckResult: size_t line_end = raw_output.Find("\n"); - output = raw_output.SubStr(0, line_end); + return raw_output.SubStr(0, line_end); +} + +String CompatUtility::GetCheckResultLongOutput(const CheckResult::Ptr& cr) +{ + if (!cr) + return Empty; + + String long_output; + String output; + + String raw_output = cr->GetOutput(); + + /* + * replace semi-colons with colons in output + * semi-colon is used as delimiter in various interfaces + */ + boost::algorithm::replace_all(raw_output, ";", ":"); + + size_t line_end = raw_output.Find("\n"); if (line_end > 0 && line_end != String::NPos) { long_output = raw_output.SubStr(line_end+1, raw_output.GetLength()); - long_output = EscapeString(long_output); + return EscapeString(long_output); } - output = EscapeString(output); - - return std::make_pair(output, long_output); + return Empty; } String CompatUtility::GetCheckResultPerfdata(const CheckResult::Ptr& cr) @@ -880,7 +1112,7 @@ int CompatUtility::MapExternalCommandType(const String& name) if (name == "SET_SVC_NOTIFICATION_NUMBER") return 143; if (name == "CHANGE_HOST_CHECK_TIMEPERIOD") - return 144; + return 144; if (name == "CHANGE_SVC_CHECK_TIMEPERIOD") return 145; if (name == "PROCESS_FILE") diff --git a/lib/icinga/compatutility.h b/lib/icinga/compatutility.h index cd80cf597..fff9beadc 100644 --- a/lib/icinga/compatutility.h +++ b/lib/icinga/compatutility.h @@ -39,6 +39,12 @@ enum CompatObjectType CompatTypeHost }; +typedef struct { + int have_2d_coords; + String x_2d; + String y_2d; +} Host2dCoords; + /** * Compatibility utility functions. * @@ -47,21 +53,80 @@ enum CompatObjectType class I2_ICINGA_API CompatUtility { public: - static Dictionary::Ptr GetHostConfigAttributes(const Host::Ptr& host); - - static Dictionary::Ptr GetServiceStatusAttributes(const Service::Ptr& service, CompatObjectType type); - static Dictionary::Ptr GetServiceConfigAttributes(const Service::Ptr& service); - static Dictionary::Ptr GetCommandConfigAttributes(const Command::Ptr& command); - - static Dictionary::Ptr GetCustomVariableConfig(const DynamicObject::Ptr& object); + /* host */ + static String GetHostAlias(const Host::Ptr& host); + static String GetHostAddress(const Host::Ptr& host); + static String GetHostAddress6(const Host::Ptr& host); + static Host2dCoords GetHost2dCoords(const Host::Ptr& host); + static int GetHostNotifyOnDown(const Host::Ptr& host); + static int GetHostNotifyOnUnreachable(const Host::Ptr& host); + + /* service */ + static int GetServiceCurrentState(const Service::Ptr& service); + static int GetServiceShouldBeScheduled(const Service::Ptr& service); + static int GetServiceCheckType(const Service::Ptr& service); + static double GetServiceCheckInterval(const Service::Ptr& service); + static double GetServiceRetryInterval(const Service::Ptr& service); + static String GetServiceCheckPeriod(const Service::Ptr& service); + static int GetServiceHasBeenChecked(const Service::Ptr& service); + static int GetServiceProblemHasBeenAcknowledged(const Service::Ptr& service); + static int GetServiceAcknowledgementType(const Service::Ptr& service); + static int GetServicePassiveChecksEnabled(const Service::Ptr& service); + static int GetServiceActiveChecksEnabled(const Service::Ptr& service); + static int GetServiceEventHandlerEnabled(const Service::Ptr& service); + static int GetServiceFlapDetectionEnabled(const Service::Ptr& service); + static int GetServiceIsFlapping(const Service::Ptr& service); + static String GetServicePercentStateChange(const Service::Ptr& service); + static int GetServiceProcessPerformanceData(const Service::Ptr& service); + + static String GetServiceEventHandler(const Service::Ptr& service); + static String GetServiceCheckCommand(const Service::Ptr& service); + + static int GetServiceIsVolatile(const Service::Ptr& service); + static double GetServiceLowFlapThreshold(const Service::Ptr& service); + static double GetServiceHighFlapThreshold(const Service::Ptr& service); + static int GetServiceFreshnessChecksEnabled(const Service::Ptr& service); + static int GetServiceFreshnessThreshold(const Service::Ptr& service); + static double GetServiceStaleness(const Service::Ptr& service); + static int GetServiceIsAcknowledged(const Service::Ptr& service); + static int GetServiceNoMoreNotifications(const Service::Ptr& service); + static int GetServiceInCheckPeriod(const Service::Ptr& service); + static int GetServiceInNotificationPeriod(const Service::Ptr& service); + + /* notification */ + static int GetServiceNotificationsEnabled(const Service::Ptr& service); + static int GetServiceNotificationLastNotification(const Service::Ptr& service); + static int GetServiceNotificationNextNotification(const Service::Ptr& service); + static int GetServiceNotificationNotificationNumber(const Service::Ptr& service); + static double GetServiceNotificationNotificationInterval(const Service::Ptr& service); + static String GetServiceNotificationNotificationPeriod(const Service::Ptr& service); + static String GetServiceNotificationNotificationOptions(const Service::Ptr& service); + static int GetServiceNotificationTypeFilter(const Service::Ptr& service); + static int GetServiceNotificationStateFilter(const Service::Ptr& service); + static int GetServiceNotifyOnWarning(const Service::Ptr& service); + static int GetServiceNotifyOnCritical(const Service::Ptr& service); + static int GetServiceNotifyOnUnknown(const Service::Ptr& service); + static int GetServiceNotifyOnRecovery(const Service::Ptr& service); + static int GetServiceNotifyOnFlapping(const Service::Ptr& service); + static int GetServiceNotifyOnDowntime(const Service::Ptr& service); static std::set GetServiceNotificationUsers(const Service::Ptr& service); static std::set GetServiceNotificationUserGroups(const Service::Ptr& service); - static std::pair GetCheckResultOutput(const CheckResult::Ptr& cr); + /* command */ + static String GetCommandLine(const Command::Ptr& command); + + /* custom attribute */ + static String GetCustomAttributeConfig(const DynamicObject::Ptr& object, const String& name); + static Dictionary::Ptr GetCustomVariableConfig(const DynamicObject::Ptr& object); + + /* check result */ + static String GetCheckResultOutput(const CheckResult::Ptr& cr); + static String GetCheckResultLongOutput(const CheckResult::Ptr& cr); static String GetCheckResultPerfdata(const CheckResult::Ptr& cr); + /* misc */ static std::pair ConvertTimestamp(double time); static int MapNotificationReasonType(NotificationType type); -- 2.40.0