将解析变量的问题放入 curl 中,以将 JSON 发送到 splunk

将解析变量的问题放入 curl 中,以将 JSON 发送到 splunk

这里有一个简单的脚本,我打算定期运行:

statsout=`docker stats --no-stream --format "{{json .}}"`
while read line ; do
        echo "$line"
        curl -k "https://splunkpath/services/collector" -H "Authorization: Splunk token" -d '{"index":"index", "sourcetype":"source:server", "source":"source:script:dev", "event":'$line'}'
done <<< "$statsout"

如果我从 docker stats 中获取输出并手动将其输入到事件中,它会运行良好,但如果我尝试解析它,它会失败。这是一个示例行:

{"BlockIO":"0B / 0B","CPUPerc":"4.27%","Container":"f49b9b6e8534","ID":"f49b9b6e8534","MemPerc":"1.07%","MemUsage":"342MiB / 31.24GiB","Name":"nginx","NetIO":"410MB / 208MB","PIDs":"11"}

我尝试了所有可能的引用组合,检查了这里的 5-6 个答案,但我不知道还能尝试什么。有什么建议吗?我要么得到错误的数据格式,要么 bash 开始将行粘贴为 URL,即使没有任何迹象表明这一点。(echo 行只是为了让我看看它在做什么)。

有小费吗?

编辑:如果我将变量放在单引号之间,我会得到

curl: (3) <url> malformed
curl: (6) Could not resolve host: 0B","CPUPerc":"0.22%","Container":"f014e518c912","ID":"f014e518c912","MemPerc":"0.03%","MemUsage"; Unknown error
curl: (3) <url> malformed
curl: (6) Could not resolve host: 31.24GiB","Name":"redis","NetIO"; Unknown error

答案1

固定的:

curl -k "https://splunk-hec.intranet.mckinsey.com/services/collector" -H "Authorization: Splunk 5f1d28c8-a5be-48f6-9381-f9ed48deef78" -d '{"index":"converge_red_0", "sourcetype":"converge:server", "source":"converge:script:dev", "event":'"$line"'}'

“事件”:'“$line”'

相关内容