我尝试通过内存获取前5个进程的JSON。
我想将此JSON发送到Zabbix并按内存绘制前5个进程。
我通过命令按内存获得前5个进程:
ps axho comm --sort -rss | head -5
node
mongod
kubelet
dockerd
systemd-journal
如何使用键{#PROCNAME}将bash输出转换为JSON,以获得以下结构:
{
"data": [
{
"{#PROCNAME}": "node"
},
{
"{#PROCNAME}": "mongod"
},
{
"{#PROCNAME}": "kubelet"
},
{
"{#PROCNAME}": "dockerd"
},
{
"{#PROCNAME}": "systemd-journal"
}
]
}
https://www.zabbix.com/documentation/current/manual/config/macros/lld_macros
低层发现(LLD)功能中使用了一种宏:
{#MACRO}
If your
jq
has theinputs
function, and assuming{#PROCNAME}
is just a string, you can use the following:The
inputs
functions letsjq
read all input string. The rest is decoration to get the wanted format.The option
-R
gets raw string as input. The option-n
feedsjq
input withnull
entry. That wayinputs
gets all strings at once.实际上,您可以使用标准的unix cli来实现: