From 6428fc61238a378c9e4acabcd7d4e7e1901f2959 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 26 Nov 2013 13:43:56 +0100 Subject: [PATCH] Implement override_check_period. Refs #4746 --- lib/icinga/externalcommandprocessor.cpp | 52 +++++++++++++++++++++++++ lib/icinga/externalcommandprocessor.h | 2 + lib/icinga/service-check.cpp | 14 ++++++- lib/icinga/service.cpp | 6 +++ lib/icinga/service.h | 1 + lib/icinga/service.ti | 1 + 6 files changed, 75 insertions(+), 1 deletion(-) diff --git a/lib/icinga/externalcommandprocessor.cpp b/lib/icinga/externalcommandprocessor.cpp index 197c4553d..7b3fd46b2 100644 --- a/lib/icinga/externalcommandprocessor.cpp +++ b/lib/icinga/externalcommandprocessor.cpp @@ -197,6 +197,8 @@ void ExternalCommandProcessor::Initialize(void) RegisterCommand("CHANGE_SVC_CHECK_COMMAND", &ExternalCommandProcessor::ChangeSvcCheckCommand); RegisterCommand("CHANGE_MAX_HOST_CHECK_ATTEMPTS", &ExternalCommandProcessor::ChangeMaxHostCheckAttempts); RegisterCommand("CHANGE_MAX_SVC_CHECK_ATTEMPTS", &ExternalCommandProcessor::ChangeMaxSvcCheckAttempts); + RegisterCommand("CHANGE_HOST_CHECK_TIMEPERIOD", &ExternalCommandProcessor::ChangeHostCheckTimeperiod); + RegisterCommand("CHANGE_SVC_CHECK_TIMEPERIOD", &ExternalCommandProcessor::ChangeSvcCheckTimeperiod); } void ExternalCommandProcessor::RegisterCommand(const String& command, const ExternalCommandProcessor::Callback& callback) @@ -2167,3 +2169,53 @@ void ExternalCommandProcessor::ChangeMaxSvcCheckAttempts(double time, const std: service->SetMaxCheckAttempts(attempts); } } + +void ExternalCommandProcessor::ChangeHostCheckTimeperiod(double time, const std::vector& arguments) +{ + if (arguments.size() < 2) + BOOST_THROW_EXCEPTION(std::invalid_argument("Expected 2 arguments.")); + + Host::Ptr host = Host::GetByName(arguments[0]); + + if (!host) + BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot change check period for non-existent host '" + arguments[0] + "'")); + + Service::Ptr hc = host->GetCheckService(); + + TimePeriod::Ptr tp = TimePeriod::GetByName(arguments[2]); + + if (!tp) + BOOST_THROW_EXCEPTION(std::invalid_argument("Time period '" + arguments[1] + "' does not exist.")); + + Log(LogInformation, "icinga", "Changing check period for host '" + arguments[0] + "' to '" + arguments[1] + "'"); + + { + ObjectLock olock(hc); + + hc->SetCheckPeriod(tp); + } +} + +void ExternalCommandProcessor::ChangeSvcCheckTimeperiod(double time, const std::vector& arguments) +{ + if (arguments.size() < 2) + 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 change check period for non-existent service '" + arguments[1] + "' on host '" + arguments[0] + "'")); + + TimePeriod::Ptr tp = TimePeriod::GetByName(arguments[2]); + + if (!tp) + BOOST_THROW_EXCEPTION(std::invalid_argument("Time period '" + arguments[2] + "' does not exist.")); + + Log(LogInformation, "icinga", "Changing check period for service '" + arguments[1] + "' to '" + arguments[2] + "'"); + + { + ObjectLock olock(service); + + service->SetCheckPeriod(tp); + } +} \ No newline at end of file diff --git a/lib/icinga/externalcommandprocessor.h b/lib/icinga/externalcommandprocessor.h index 66bdc6742..371eef800 100644 --- a/lib/icinga/externalcommandprocessor.h +++ b/lib/icinga/externalcommandprocessor.h @@ -147,6 +147,8 @@ private: static void ChangeSvcCheckCommand(double time, const std::vector& arguments); static void ChangeMaxHostCheckAttempts(double time, const std::vector& arguments); static void ChangeMaxSvcCheckAttempts(double time, const std::vector& arguments); + static void ChangeHostCheckTimeperiod(double time, const std::vector& arguments); + static void ChangeSvcCheckTimeperiod(double time, const std::vector& arguments); }; } diff --git a/lib/icinga/service-check.cpp b/lib/icinga/service-check.cpp index 9fce8433a..e2f68b468 100644 --- a/lib/icinga/service-check.cpp +++ b/lib/icinga/service-check.cpp @@ -64,7 +64,19 @@ void Service::SetCheckCommand(const CheckCommand::Ptr& command) TimePeriod::Ptr Service::GetCheckPeriod(void) const { - return TimePeriod::GetByName(GetCheckPeriodRaw()); + String tp; + + if (!GetOverrideCheckPeriod().IsEmpty()) + tp = GetOverrideCheckPeriod(); + else + tp = GetCheckPeriodRaw(); + + return TimePeriod::GetByName(tp); +} + +void Service::SetCheckPeriod(const TimePeriod::Ptr& tp) +{ + SetOverrideCheckPeriod(tp->GetName()); } double Service::GetCheckInterval(void) const diff --git a/lib/icinga/service.cpp b/lib/icinga/service.cpp index a4f5e2909..4c909118b 100644 --- a/lib/icinga/service.cpp +++ b/lib/icinga/service.cpp @@ -310,6 +310,9 @@ int Service::GetModifiedAttributes(void) const if (!GetOverrideMaxCheckAttempts().IsEmpty()) attrs |= ModAttrMaxCheckAttempts; + if (!GetOverrideCheckPeriod().IsEmpty()) + attrs |= ModAttrCheckTimeperiod; + // TODO: finish return attrs; @@ -349,6 +352,9 @@ void Service::SetModifiedAttributes(int flags) if ((flags & ModAttrMaxCheckAttempts) == 0) SetOverrideMaxCheckAttempts(Empty); + + if ((flags & ModAttrCheckTimeperiod) == 0) + SetOverrideCheckPeriod(Empty); } bool Service::ResolveMacro(const String& macro, const CheckResult::Ptr& cr, String *result) const diff --git a/lib/icinga/service.h b/lib/icinga/service.h index 36d2f173a..1680e2221 100644 --- a/lib/icinga/service.h +++ b/lib/icinga/service.h @@ -112,6 +112,7 @@ public: void SetCheckCommand(const shared_ptr& command); TimePeriod::Ptr GetCheckPeriod(void) const; + void SetCheckPeriod(const TimePeriod::Ptr& tp); double GetCheckInterval(void) const; void SetCheckInterval(double interval); diff --git a/lib/icinga/service.ti b/lib/icinga/service.ti index 4da129a24..ee693c706 100644 --- a/lib/icinga/service.ti +++ b/lib/icinga/service.ti @@ -139,6 +139,7 @@ class Service : DynamicObject [state] Value override_event_command; [state] Value override_check_command; [state] Value override_max_check_attempts; + [state] Value override_check_period; }; }