From 33e5a839dbbeef87023637a1633013c6c669d96f Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Wed, 17 Jul 2013 16:38:31 +0200 Subject: [PATCH] livestatus: fix check_command_expanded for hosts table refs #4372 --- components/livestatus/hoststable.cpp | 42 ++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/components/livestatus/hoststable.cpp b/components/livestatus/hoststable.cpp index ef7495174..27437617b 100644 --- a/components/livestatus/hoststable.cpp +++ b/components/livestatus/hoststable.cpp @@ -215,16 +215,48 @@ Value HostsTable::CheckCommandAccessor(const Value& row) Value HostsTable::CheckCommandExpandedAccessor(const Value& row) { /* use hostcheck service */ - Service::Ptr hc = static_cast(row)->GetHostCheckService(); + Host::Ptr host = static_cast(row); + Service::Ptr hc = host->GetHostCheckService(); if (!hc) return Empty; - CheckCommand::Ptr checkcommand = hc->GetCheckCommand(); - if (checkcommand) - return checkcommand->GetName(); /* this is the name without '!' args */ + CheckCommand::Ptr commandObj = hc->GetCheckCommand(); - return Empty; + if (!commandObj) + return Empty; + + Value raw_command = commandObj->GetCommandLine(); + + std::vector resolvers; + resolvers.push_back(commandObj); + resolvers.push_back(hc); + resolvers.push_back(host); + resolvers.push_back(IcingaApplication::GetInstance()); + + Value commandLine = MacroProcessor::ResolveMacros(raw_command, resolvers, Dictionary::Ptr(), Utility::EscapeShellCmd); + + String buf; + if (commandLine.IsObjectType()) { + Array::Ptr args = commandLine; + + ObjectLock olock(args); + String arg; + BOOST_FOREACH(arg, args) { + // This is obviously incorrect for non-trivial cases. + String argitem = " \"" + arg + "\""; + boost::algorithm::replace_all(argitem, "\n", "\\n"); + buf += argitem; + } + } else if (!commandLine.IsEmpty()) { + String args = Convert::ToString(commandLine); + boost::algorithm::replace_all(args, "\n", "\\n"); + buf += args; + } else { + buf += ""; + } + + return buf; } Value HostsTable::EventHandlerAccessor(const Value& row) -- 2.40.0