From 71ec1d2b958d5d06afe88555f056053c871677d4 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 22 Jan 2013 16:01:08 +0100 Subject: [PATCH] Implement enable_checks property for services. Fixes #3550 --- components/checker/checkercomponent.cpp | 14 ++++++++++ components/compat/compatcomponent.cpp | 4 +-- docs/icinga2-config.txt | 12 +++++++++ lib/icinga/externalcommand.cpp | 33 ++++++++++++++++++++++-- lib/icinga/externalcommand.h | 2 ++ lib/icinga/host.cpp | 4 +++ lib/icinga/service.cpp | 34 ++++++++++++++++++++++++- lib/icinga/service.h | 6 +++++ 8 files changed, 104 insertions(+), 5 deletions(-) diff --git a/components/checker/checkercomponent.cpp b/components/checker/checkercomponent.cpp index 98fc7f1f1..b35b51664 100644 --- a/components/checker/checkercomponent.cpp +++ b/components/checker/checkercomponent.cpp @@ -70,6 +70,20 @@ void CheckerComponent::CheckTimerHandler(void) idx.erase(it); + /* reschedule the service if checks are currently disabled + * for it and this is not a forced check */ + if (!service->GetEnableChecks()) { + if (!service->GetForceNextCheck()) { + service->UpdateNextCheck(); + + idx.insert(service); + + return; + } + + service->SetForceNextCheck(false); + } + Dictionary::Ptr cr = service->GetLastCheckResult(); if (cr) { diff --git a/components/compat/compatcomponent.cpp b/components/compat/compatcomponent.cpp index 85a709351..c777bea3f 100644 --- a/components/compat/compatcomponent.cpp +++ b/components/compat/compatcomponent.cpp @@ -285,7 +285,7 @@ void CompatComponent::DumpServiceStatus(ofstream& fp, const Service::Ptr& servic << "\t" << "last_state_change=" << service->GetLastStateChange() << "\n" << "\t" << "last_hard_state_change=" << service->GetLastHardStateChange() << "\n" << "\t" << "last_update=" << time(NULL) << "\n" - << "\t" << "active_checks_enabled=1" << "\n" + << "\t" << "active_checks_enabled=" << (service->GetEnableChecks() ? 1 : 0) <<"\n" << "\t" << "passive_checks_enabled=1" << "\n" << "\t" << "}" << "\n" << "\n"; @@ -300,7 +300,7 @@ void CompatComponent::DumpServiceObject(ofstream& fp, const Service::Ptr& servic << "\t" << "check_interval" << "\t" << service->GetCheckInterval() / 60.0 << "\n" << "\t" << "retry_interval" << "\t" << service->GetRetryInterval() / 60.0 << "\n" << "\t" << "max_check_attempts" << "\t" << 1 << "\n" - << "\t" << "active_checks_enabled" << "\t" << 1 << "\n" + << "\t" << "active_checks_enabled" << "\t" << (service->GetEnableChecks() ? 1 : 0) << "\n" << "\t" << "passive_checks_enabled" << "\t" << 1 << "\n" << "\t" << "}" << "\n" << "\n"; diff --git a/docs/icinga2-config.txt b/docs/icinga2-config.txt index 8873a0620..e3e5885bb 100644 --- a/docs/icinga2-config.txt +++ b/docs/icinga2-config.txt @@ -614,6 +614,12 @@ Attribute: checkers Optional. A list of remote endpoints that may check this service. Wildcards can be used here. +Attribute: enable_checks +^^^^^^^^^^^^^^^^^^^^^^^^ + +Optional. Whether active checks should be performed for this service. Defaults +to 1 (true). + Type: ServiceGroup ~~~~~~~~~~~~~~~~~~ @@ -747,6 +753,12 @@ Attribute: checkers Optional. Copied into inline service definitions. The host itself does not have any checks. +Attribute: enable_checks +^^^^^^^^^^^^^^^^^^^^^^^^ + +Optional. Copied into inline service definitions. The host itself does not have +any checks. + Type: HostGroup ~~~~~~~~~~~~~~~ diff --git a/lib/icinga/externalcommand.cpp b/lib/icinga/externalcommand.cpp index 7fafe7f2b..fc583aebb 100644 --- a/lib/icinga/externalcommand.cpp +++ b/lib/icinga/externalcommand.cpp @@ -31,6 +31,8 @@ void ExternalCommand::Execute(double time, const String& command, const vector& Service::Ptr service = Service::GetByName(arguments[1]); - // TODO: force checks (once we have time periods) - Logger::Write(LogInformation, "icinga", "Rescheduling next check for service '" + arguments[1] + "'"); + service->SetForceNextCheck(true); service->SetNextCheck(arguments[2].ToDouble()); } +void ExternalCommand::EnableSvcCheck(double time, const vector& arguments) +{ + if (arguments.size() < 2) + throw_exception(invalid_argument("Expected 2 arguments.")); + + if (!Service::Exists(arguments[1])) + throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist.")); + + Service::Ptr service = Service::GetByName(arguments[1]); + + Logger::Write(LogInformation, "icinga", "Enabling checks for service '" + arguments[1] + "'"); + service->SetEnableChecks(true); +} + +void ExternalCommand::DisableSvcCheck(double time, const vector& arguments) +{ + if (arguments.size() < 2) + throw_exception(invalid_argument("Expected 2 arguments.")); + + if (!Service::Exists(arguments[1])) + throw_exception(invalid_argument("The service '" + arguments[1] + "' does not exist.")); + + Service::Ptr service = Service::GetByName(arguments[1]); + + Logger::Write(LogInformation, "icinga", "Disabling checks for service '" + arguments[1] + "'"); + service->SetEnableChecks(false); +} + diff --git a/lib/icinga/externalcommand.h b/lib/icinga/externalcommand.h index f46601e5a..dea2f72c4 100644 --- a/lib/icinga/externalcommand.h +++ b/lib/icinga/externalcommand.h @@ -32,6 +32,8 @@ public: static void ProcessServiceCheckResult(double time, const vector& arguments); static void ScheduleSvcCheck(double time, const vector& arguments); static void ScheduleForcedSvcCheck(double time, const vector& arguments); + static void EnableSvcCheck(double time, const vector& arguments); + static void DisableSvcCheck(double time, const vector& arguments); private: typedef function& arguments)> Callback; diff --git a/lib/icinga/host.cpp b/lib/icinga/host.cpp index 7fafd7895..a289252d7 100644 --- a/lib/icinga/host.cpp +++ b/lib/icinga/host.cpp @@ -175,6 +175,10 @@ static void CopyServiceAttributes(const Host::Ptr& host, TDict serviceDesc, if (!hostchecks.IsEmpty()) builder->AddExpression("dependencies", OperatorPlus, Service::ResolveDependencies(host, hostchecks)); + + Value enableChecks = serviceDesc->Get("enable_checks"); + if (!enableChecks.IsEmpty()) + builder->AddExpression("enable_checks", OperatorSet, enableChecks); } void Host::ObjectCommittedHandler(const ConfigItem::Ptr& item) diff --git a/lib/icinga/service.cpp b/lib/icinga/service.cpp index 704da8854..ef9b94dbd 100644 --- a/lib/icinga/service.cpp +++ b/lib/icinga/service.cpp @@ -41,7 +41,9 @@ static AttributeDescription serviceAttributes[] = { { "state_type", Attribute_Replicated }, { "last_result", Attribute_Replicated }, { "last_state_change", Attribute_Replicated }, - { "last_hard_state_change", Attribute_Replicated } + { "last_hard_state_change", Attribute_Replicated }, + { "enable_checks", Attribute_Replicated }, + { "force_next_check", Attribute_Replicated } }; REGISTER_TYPE(Service, serviceAttributes); @@ -353,6 +355,36 @@ double Service::GetLastHardStateChange(void) const return value; } +bool Service::GetEnableChecks(void) const +{ + Value value = Get("enable_checks"); + + if (value.IsEmpty()) + return true; + + return static_cast(value); +} + +void Service::SetEnableChecks(bool enabled) +{ + Set("enable_checks", enabled ? 1 : 0); +} + +bool Service::GetForceNextCheck(void) const +{ + Value value = Get("force_next_check"); + + if (value.IsEmpty()) + return false; + + return static_cast(value); +} + +void Service::SetForceNextCheck(bool forced) +{ + Set("force_next_check", forced ? 1 : 0); +} + void Service::ApplyCheckResult(const Dictionary::Ptr& cr) { ServiceState old_state = GetState(); diff --git a/lib/icinga/service.h b/lib/icinga/service.h index 65331c289..99b143950 100644 --- a/lib/icinga/service.h +++ b/lib/icinga/service.h @@ -116,6 +116,12 @@ public: void SetLastHardStateChange(double ts); double GetLastHardStateChange(void) const; + bool GetEnableChecks(void) const; + void SetEnableChecks(bool enabled); + + bool GetForceNextCheck(void) const; + void SetForceNextCheck(bool forced); + void ApplyCheckResult(const Dictionary::Ptr& cr); void BeginExecuteCheck(const function& callback);