From 91cd8e1edd9f28894799f59f55ae0c849e35e693 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Thu, 9 Nov 2023 14:03:48 +0100 Subject: [PATCH] Truncate several macros if a notification command line gets too big to run Namely $host.output$, $service.output$ and $notification.comment$. --- lib/icinga/pluginutility.cpp | 5 +++-- lib/icinga/pluginutility.hpp | 3 ++- lib/methods/pluginnotificationtask.cpp | 20 +++++++++++++++++++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/icinga/pluginutility.cpp b/lib/icinga/pluginutility.cpp index 4dc46f754d5..9fe09c5e238 100644 --- a/lib/icinga/pluginutility.cpp +++ b/lib/icinga/pluginutility.cpp @@ -16,7 +16,8 @@ using namespace icinga; void PluginUtility::ExecuteCommand(const Command::Ptr& commandObj, const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const MacroProcessor::ResolverList& macroResolvers, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros, int timeout, - const std::function& callback) + const std::function& callback, + const Array::Ptr& safeToTruncate) { Value raw_command = commandObj->GetCommandLine(); Dictionary::Ptr raw_arguments = commandObj->GetArguments(); @@ -74,7 +75,7 @@ void PluginUtility::ExecuteCommand(const Command::Ptr& commandObj, const Checkab if (resolvedMacros && !useResolvedMacros) return; - Process::Ptr process = new Process(Process::PrepareCommand(command), envMacros); + Process::Ptr process = new Process(Process::PrepareCommand(command), envMacros, safeToTruncate); process->SetTimeout(timeout); process->SetAdjustPriority(true); diff --git a/lib/icinga/pluginutility.hpp b/lib/icinga/pluginutility.hpp index 3f6a8444a22..95bed575b3e 100644 --- a/lib/icinga/pluginutility.hpp +++ b/lib/icinga/pluginutility.hpp @@ -25,7 +25,8 @@ class PluginUtility static void ExecuteCommand(const Command::Ptr& commandObj, const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const MacroProcessor::ResolverList& macroResolvers, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros, int timeout, - const std::function& callback = std::function()); + const std::function& callback = std::function(), + const Array::Ptr& safeToTruncate = nullptr); static ServiceState ExitStatusToState(int exitStatus); static std::pair ParseCheckOutput(const String& output); diff --git a/lib/methods/pluginnotificationtask.cpp b/lib/methods/pluginnotificationtask.cpp index a20c971a1f3..1013cdcb678 100644 --- a/lib/methods/pluginnotificationtask.cpp +++ b/lib/methods/pluginnotificationtask.cpp @@ -62,8 +62,26 @@ void PluginNotificationTask::ScriptFunc(const Notification::Ptr& notification, callback = [checkable](const Value& commandline, const ProcessResult& pr) { ProcessFinishedHandler(checkable, commandline, pr); }; } + Array::Ptr safeToTruncate = new Array(); + + auto hostCr (host->GetLastCheckResult()); + + if (hostCr) { + safeToTruncate->Add(hostCr->GetOutput()); + } + + if (service) { + auto cr (service->GetLastCheckResult()); + + if (cr) { + safeToTruncate->Add(cr->GetOutput()); + } + } + + safeToTruncate->Add(comment); + PluginUtility::ExecuteCommand(commandObj, checkable, cr, resolvers, - resolvedMacros, useResolvedMacros, timeout, callback); + resolvedMacros, useResolvedMacros, timeout, callback, safeToTruncate); } void PluginNotificationTask::ProcessFinishedHandler(const Checkable::Ptr& checkable, const Value& commandLine, const ProcessResult& pr)