使用带有序列密钥的python创建JSON,以实现LLD Zabbix发现顶级内存过程

时间:2019-02-26 15:44:05

标签: python json zabbix

我尝试使用带有序列密钥的python创建JSON,以实现LLD Zabbix发现最佳内存过程

JMSType in ('BOALF','BOD','SEL')

获取不被接受的JSON Zabbix(以下代码):

#!/usr/bin/python

import subprocess
import json

s = subprocess.Popen(["ps axho comm --sort -rss | head -5"], shell=True, stdout=subprocess.PIPE).stdout
service_states = s.read().splitlines()

count = 0
data = {"data":{}}
for i in service_states:
  count += 1
  key = "{#TOPMEMNAME" + str(count) + "}"
  data["data"][key] = i

json_data = json.dumps(data)
print(json_data)

下面的代码,Zabbix接受JSON。

{
  "data": {
    "{#TOPMEMNAME2}": "node",
    "{#TOPMEMNAME1}": "mongod",
    "{#TOPMEMNAME4}": "ffmpeg",
    "{#TOPMEMNAME3}": "kubelet",
    "{#TOPMEMNAME5}": "dockerd"
  }
}

如何更改python代码以获取JSON zabbix接受?

1 个答案:

答案 0 :(得分:0)

data = {"data":{}}
# should be changed to the line below
data = {"data":[]}

data["data"][key] = i
# should be changed to the line below
data["data"].append({key:i})