1

I try get JSON with top 5 process by memory.

This JSON i want send to Zabbix and draw top 5 process by memory.

I get top 5 process by memory by command:

ps axho comm --sort -rss | head -5
node
mongod
kubelet
dockerd
systemd-journal

How convert bash output to JSON with key {#PROCNAME} for get this structure:

{
  "data": [
    {
      "{#PROCNAME}": "node"
    },
    {
      "{#PROCNAME}": "mongod"
    },
    {
      "{#PROCNAME}": "kubelet"
    },
    {
      "{#PROCNAME}": "dockerd"
    },
    {
      "{#PROCNAME}": "systemd-journal"
    }
  ]
}

https://www.zabbix.com/documentation/current/manual/config/macros/lld_macros

There is a type of macro used within the low-level discovery (LLD) function:

{#MACRO} 
6

If your jq has the inputs function, and assuming {#PROCNAME} is just a string, you can use the following:

ps axho comm --sort -rss | head -5 | jq -Rn '{data: [inputs|{"#PROCNAME":.}]}'

The inputs functions lets jq read all input string. The rest is decoration to get the wanted format.

The option -R gets raw string as input. The option -n feeds jq input with null entry. That way inputs gets all strings at once.

  • Thanks! Changed: ps axho comm --sort -rss | head -5 | jq -Rn '{data: [inputs|{"{#PROCNAME}":.}]}' – Anton Patsev Feb 26 at 13:54
  • @AntonPatsev Yes you're right. It was a typo... – oliv Feb 26 at 14:04
1

actually, you could achieve it with just standard unix cli:

bash $ echo { \"data\": [  $(ps axho comm --sort -rss | head -5 | xargs -L1 -I% echo { \"{#PROCNAME}\": \"%\" } | paste -s -d, -) ] }
{ "data": [ { "{#PROCNAME}": "node" },{ "{#PROCNAME}": "mongod" },{ "{#PROCNAME}": "kubelet" },{ "{#PROCNAME}": "dockerd" },{ "{#PROCNAME}": "systemd-journal" } ] }
bash $ 
  • the resulting output is a valid JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.