]> granicus.if.org Git - icinga2/commitdiff
Add minimum version check to the built-in icinga command 5958/head
authorMichael Friedrich <michael.friedrich@icinga.com>
Mon, 8 Jan 2018 18:44:54 +0000 (19:44 +0100)
committerMichael Friedrich <michael.friedrich@icinga.com>
Mon, 8 Jan 2018 18:44:54 +0000 (19:44 +0100)
fixes #3998

doc/10-icinga-template-library.md
lib/methods/icingachecktask.cpp
lib/methods/icingachecktask.hpp

index 2e81203851d933517d525a2383f65f08dbc1fc1f..3fe35584c5f9a87c73ed884ce464cf8dfc70df5c 100644 (file)
@@ -74,9 +74,13 @@ plugin scripts.
 ### icinga <a id="itl-icinga"></a>
 
 Check command for the built-in `icinga` check. This check returns performance
-data for the current Icinga instance.
+data for the current Icinga instance and optionally allows for minimum version checks.
 
-The `icinga` check command does not support any vars.
+Custom attributes passed as [command parameters](03-monitoring-basics.md#command-passing-parameters):
+
+Name                   | Description
+-----------------------|---------------
+icinga\_min\_version   | **Optional.** Required minimum Icinga 2 version, e.g. `2.8.0`. If not satisfied, the state changes to `Critical`. Release packages only.
 
 ### cluster <a id="itl-icinga-cluster"></a>
 
index 70a757a4f5627f43e5a07ccbee3734001b965d5b..da46ef206f7dc9f24384cc96bc75ff309becefe3 100644 (file)
@@ -32,9 +32,25 @@ using namespace icinga;
 
 REGISTER_SCRIPTFUNCTION_NS(Internal, IcingaCheck, &IcingaCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros");
 
-void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr,
+void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
        const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
 {
+       CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
+
+       Host::Ptr host;
+       Service::Ptr service;
+       tie(host, service) = GetHostService(checkable);
+
+       MacroProcessor::ResolverList resolvers;
+       if (service)
+               resolvers.emplace_back("service", service);
+       resolvers.emplace_back("host", host);
+       resolvers.emplace_back("command", commandObj);
+       resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
+
+       String icingaMinVersion = MacroProcessor::ResolveMacros("$icinga_min_version$", resolvers, checkable->GetLastCheckResult(),
+               nullptr, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
+
        if (resolvedMacros && !useResolvedMacros)
                return;
 
@@ -130,17 +146,31 @@ void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResul
        perfdata->Add(new PerfdataValue("sum_bytes_sent_per_second", bytesSentPerSecond));
        perfdata->Add(new PerfdataValue("sum_bytes_received_per_second", bytesReceivedPerSecond));
 
-       cr->SetOutput("Icinga 2 has been running for " + Utility::FormatDuration(uptime) +
-               ". Version: " + Application::GetAppVersion());
        cr->SetPerformanceData(perfdata);
+       cr->SetState(ServiceOK);
+
+       String appVersion = Application::GetAppVersion();
+
+       String output = "Icinga 2 has been running for " + Utility::FormatDuration(uptime) +
+               ". Version: " + appVersion;
 
+       /* Indicate a warning if the last reload failed. */
        double lastReloadFailed = Application::GetLastReloadFailed();
 
        if (lastReloadFailed > 0) {
-               cr->SetOutput(cr->GetOutput() + "; Last reload attempt failed at " + Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", lastReloadFailed));
+               output += "; Last reload attempt failed at " + Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", lastReloadFailed);
                cr->SetState(ServiceWarning);
-       } else
-               cr->SetState(ServiceOK);
+       }
+
+       /* Return an error if the version is less than specified (optional). */
+       String parsedAppVersion = appVersion.SubStr(1,5);
+
+       if (!icingaMinVersion.IsEmpty() && Utility::CompareVersion(icingaMinVersion, parsedAppVersion) < 0) {
+               output += "; Minimum version " + icingaMinVersion + " is not installed.";
+               cr->SetState(ServiceCritical);
+       }
+
+       cr->SetOutput(output);
 
-       service->ProcessCheckResult(cr);
+       checkable->ProcessCheckResult(cr);
 }
index 2868f68402c9386340e43073bf3e214d64858085..24f9f232f03306306da1f0d9332fe3d53c7c4120 100644 (file)
@@ -34,7 +34,7 @@ namespace icinga
 class IcingaCheckTask
 {
 public:
-       static void ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr,
+       static void ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
                const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros);
 
 private: