From 177e1a9000e42433acbd31c62450436f5d8d4853 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Wed, 16 Oct 2013 11:46:54 +0200 Subject: [PATCH] Implement modified attributes for enable_active_checks and enable_passive_checks. --- components/compat/statusdatawriter.cpp | 3 ++- lib/icinga/compatutility.cpp | 1 + lib/icinga/externalcommandprocessor.cpp | 22 +++++++++++++++++ lib/icinga/externalcommandprocessor.h | 1 + lib/icinga/icinga-type.conf | 3 +++ lib/icinga/service-check.cpp | 20 +++++++++------- lib/icinga/service.cpp | 32 +++++++++++++++++++++++-- lib/icinga/service.h | 31 ++++++++++++++++++++++++ 8 files changed, 102 insertions(+), 11 deletions(-) diff --git a/components/compat/statusdatawriter.cpp b/components/compat/statusdatawriter.cpp index cd1315a5e..fbb0d4601 100644 --- a/components/compat/statusdatawriter.cpp +++ b/components/compat/statusdatawriter.cpp @@ -413,7 +413,8 @@ void StatusDataWriter::DumpServiceStatusAttrs(std::ostream& fp, const Service::P << "\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" << "current_notification_number=" << attrs->Get("current_notification_number") << "\n" + << "\t" << "modified_attributes=" << attrs->Get("modified_attributes") << "\n"; } void StatusDataWriter::DumpServiceStatus(std::ostream& fp, const Service::Ptr& service) diff --git a/lib/icinga/compatutility.cpp b/lib/icinga/compatutility.cpp index 5cd77c2f7..a0d0389a9 100644 --- a/lib/icinga/compatutility.cpp +++ b/lib/icinga/compatutility.cpp @@ -284,6 +284,7 @@ Dictionary::Ptr CompatUtility::GetServiceStatusAttributes(const Service::Ptr& se 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; } diff --git a/lib/icinga/externalcommandprocessor.cpp b/lib/icinga/externalcommandprocessor.cpp index 4c6c32d2b..5abdfa1fc 100644 --- a/lib/icinga/externalcommandprocessor.cpp +++ b/lib/icinga/externalcommandprocessor.cpp @@ -180,6 +180,7 @@ void ExternalCommandProcessor::Initialize(void) RegisterCommand("DISABLE_PERFORMANCE_DATA", &ExternalCommandProcessor::DisablePerformanceData); RegisterCommand("START_EXECUTING_SVC_CHECKS", &ExternalCommandProcessor::StartExecutingSvcChecks); RegisterCommand("STOP_EXECUTING_SVC_CHECKS", &ExternalCommandProcessor::StopExecutingSvcChecks); + RegisterCommand("CHANGE_SVC_MODATTR", &ExternalCommandProcessor::ChangeSvcModAttr); } void ExternalCommandProcessor::RegisterCommand(const String& command, const ExternalCommandProcessor::Callback& callback) @@ -1815,3 +1816,24 @@ void ExternalCommandProcessor::StopExecutingSvcChecks(double time, const std::ve IcingaApplication::GetInstance()->SetEnableChecks(false); } + +void ExternalCommandProcessor::ChangeSvcModAttr(double time, const std::vector& arguments) +{ + if (arguments.size() < 3) + BOOST_THROW_EXCEPTION(std::invalid_argument("Expected 3 arguments.")); + + Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]); + + if (!service) + BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot update modified attributes for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'")); + + int modifiedAttributes = Convert::ToLong(arguments[2]); + + Log(LogInformation, "icinga", "Updating modified attributes for service '" + arguments[1] + "'"); + + { + ObjectLock olock(service); + + service->SetModifiedAttributes(modifiedAttributes); + } +} \ No newline at end of file diff --git a/lib/icinga/externalcommandprocessor.h b/lib/icinga/externalcommandprocessor.h index 515bcc353..03e9fd303 100644 --- a/lib/icinga/externalcommandprocessor.h +++ b/lib/icinga/externalcommandprocessor.h @@ -131,6 +131,7 @@ private: static void DisablePerformanceData(double time, const std::vector& arguments); static void StartExecutingSvcChecks(double time, const std::vector& arguments); static void StopExecutingSvcChecks(double time, const std::vector& arguments); + static void ChangeSvcModAttr(double time, const std::vector& arguments); }; } diff --git a/lib/icinga/icinga-type.conf b/lib/icinga/icinga-type.conf index 0a95ff167..816481293 100644 --- a/lib/icinga/icinga-type.conf +++ b/lib/icinga/icinga-type.conf @@ -76,6 +76,9 @@ type Service { %attribute number "check_interval", %attribute number "retry_interval", + %attribute number "enable_active_checks", + %attribute number "enable_passive_checks", + %attribute name(EventCommand) "event_command", %attribute number "enable_flapping", diff --git a/lib/icinga/service-check.cpp b/lib/icinga/service-check.cpp index 60c6b7938..09fd12d5c 100644 --- a/lib/icinga/service-check.cpp +++ b/lib/icinga/service-check.cpp @@ -397,30 +397,34 @@ double Service::GetLastHardStateChange(void) const bool Service::GetEnableActiveChecks(void) const { - if (m_EnableActiveChecks.IsEmpty()) - return true; - else + if (!m_OverrideEnableActiveChecks.IsEmpty()) + return m_OverrideEnableActiveChecks; + else if (!m_EnableActiveChecks.IsEmpty()) return m_EnableActiveChecks; + else + return true; } void Service::SetEnableActiveChecks(bool enabled, const String& authority) { - m_EnableActiveChecks = enabled ? 1 : 0; + m_OverrideEnableActiveChecks = enabled ? 1 : 0; Utility::QueueAsyncCallback(boost::bind(boost::ref(OnEnableActiveChecksChanged), GetSelf(), enabled, authority)); } bool Service::GetEnablePassiveChecks(void) const { - if (m_EnablePassiveChecks.IsEmpty()) - return true; - else + if (!m_OverrideEnablePassiveChecks.IsEmpty()) + return m_OverrideEnablePassiveChecks; + if (!m_EnablePassiveChecks.IsEmpty()) return m_EnablePassiveChecks; + else + return true; } void Service::SetEnablePassiveChecks(bool enabled, const String& authority) { - m_EnablePassiveChecks = enabled ? 1 : 0; + m_OverrideEnablePassiveChecks = enabled ? 1 : 0; Utility::QueueAsyncCallback(boost::bind(boost::ref(OnEnablePassiveChecksChanged), GetSelf(), enabled, authority)); } diff --git a/lib/icinga/service.cpp b/lib/icinga/service.cpp index 0bf0a68a8..46eca8cf8 100644 --- a/lib/icinga/service.cpp +++ b/lib/icinga/service.cpp @@ -333,6 +333,30 @@ bool Service::GetEnablePerfdata(void) const return m_EnablePerfdata; } +int Service::GetModifiedAttributes(void) const +{ + int attrs = 0; + + if (!m_OverrideEnableActiveChecks.IsEmpty()) + attrs |= ModAttrActiveChecksEnabled; + + if (!m_OverrideEnablePassiveChecks.IsEmpty()) + attrs |= ModAttrPassiveChecksEnabled; + + // TODO: finish + + return attrs; +} + +void Service::SetModifiedAttributes(int flags) +{ + if ((flags & ModAttrActiveChecksEnabled) == 0) + m_OverrideEnableActiveChecks = Empty; + + if ((flags & ModAttrPassiveChecksEnabled) == 0) + m_OverrideEnablePassiveChecks = Empty; +} + bool Service::ResolveMacro(const String& macro, const Dictionary::Ptr& cr, String *result) const { if (macro == "SERVICEDESC") { @@ -467,6 +491,8 @@ void Service::InternalSerialize(const Dictionary::Ptr& bag, int attributeTypes) bag->Set("enable_flapping", m_EnableFlapping); bag->Set("enable_perfdata", m_EnablePerfdata); bag->Set("enable_event_handlers", m_EnableEventHandlers); + bag->Set("override_enable_active_checks", m_OverrideEnableActiveChecks); + bag->Set("override_enable_passive_checks", m_OverrideEnablePassiveChecks); } } @@ -491,6 +517,8 @@ void Service::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes m_HostName = bag->Get("host"); m_FlappingThreshold = bag->Get("flapping_threshold"); m_NotificationDescriptions = bag->Get("notifications"); + m_EnableActiveChecks = bag->Get("enable_active_checks"); + m_EnablePassiveChecks = bag->Get("enable_passive_checks"); } if (attributeTypes & Attribute_State) { @@ -512,8 +540,6 @@ void Service::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes m_LastStateUnknown = bag->Get("last_state_unknown"); m_LastStateUnreachable = bag->Get("last_state_unreachable"); m_LastInDowntime = bag->Get("last_in_downtime"); - m_EnableActiveChecks = bag->Get("enable_active_checks"); - m_EnablePassiveChecks = bag->Get("enable_passive_checks"); m_ForceNextCheck = bag->Get("force_next_check"); m_Acknowledgement = bag->Get("acknowledgement"); m_AcknowledgementExpiry = bag->Get("acknowledgement_expiry"); @@ -527,5 +553,7 @@ void Service::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes m_EnableFlapping = bag->Get("enable_flapping"); m_EnablePerfdata = bag->Get("enable_perfdata"); m_EnableEventHandlers = bag->Get("enable_event_handlers"); + m_OverrideEnableActiveChecks = bag->Get("override_enable_active_checks"); + m_OverrideEnablePassiveChecks = bag->Get("override_enable_passive_checks"); } } diff --git a/lib/icinga/service.h b/lib/icinga/service.h index 1d61444da..caba86fd4 100644 --- a/lib/icinga/service.h +++ b/lib/icinga/service.h @@ -96,6 +96,32 @@ enum DowntimeChangedType DowntimeChangedDeleted = 2 }; +/** + * Modified attributes. + * + * @ingroup icinga + */ +enum ModifiedAttributeType +{ + ModAttrNotificationsEnabled = 1, + ModAttrActiveChecksEnabled = 2, + ModAttrPassiveChecksEnabled = 4, + ModAttrEventHandlerEnabled = 8, + ModAttrFlapDetectionEnabled = 16, + ModAttrFailurePredictionEnabled = 32, + ModAttrPerformanceDataEnabled = 64, + ModAttrObsessiveHandlerEnabled = 128, + ModAttrEventHandlerCommand = 256, + ModAttrCheckCommand = 512, + ModAttrNormalCheckInterval = 1024, + ModAttrRetryCheckInterval = 2048, + ModAttrMaxCheckAttempts = 4096, + ModAttrFreshnessChecksEnabled = 8192, + ModAttrCheckTimeperiod = 16384, + ModAttrCustomVariable = 32768, + ModAttrNotificationTimeperiod = 65536 +}; + class CheckCommand; class EventCommand; @@ -223,6 +249,9 @@ public: void ExecuteCheck(void); void ProcessCheckResult(const Dictionary::Ptr& cr, const String& authority = String()); + int GetModifiedAttributes(void) const; + void SetModifiedAttributes(int flags); + static double CalculateExecutionTime(const Dictionary::Ptr& cr); static double CalculateLatency(const Dictionary::Ptr& cr); @@ -383,7 +412,9 @@ private: Value m_LastStateUnreachable; bool m_LastInDowntime; Value m_EnableActiveChecks; + Value m_OverrideEnableActiveChecks; Value m_EnablePassiveChecks; + Value m_OverrideEnablePassiveChecks; Value m_ForceNextCheck; bool m_CheckRunning; -- 2.40.0