Zabbix LLD发现规则的python脚本



我需要编写一个python脚本来监控sudoers文件的更改。我想使用LLD发现规则在Zabbix中实现这一点。据我所知,我需要在输出端使用JSON格式的数据。由于我只是在学习如何写脚本,我遇到了一个问题。我不知道如何实现这一点。我请求帮助。这是我的剧本。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
file_path = ["/etc/sudoers",
"/etc/sudoers.d/file1",
"/etc/sudoers.d/file2"]
import os.path
import json
for i in file_path:
a = os.path.isfile(i)
if a == True:
print(i)
JSON是一种与Python dict非常相似的格式。我的建议是创建一个Python dict,以JSON格式表示您想要的数据。然后,获取dict对象,并将其作为JSON字符串返回,以满足您的需要。这里有一个例子:
file_path = ['/etc/sudoers', '/etc/sudoers.d/file1', '/etc/sudoers.d/file2']
import os.path
import json
json_data = {}
for path in file_path:
json_data[path] = os.path.isfile(path)
>>> json.dumps(json_data)
'{"/etc/sudoers": false, "/etc/sudoers.d/file1": false, "/etc/sudoers.d/file2": false}'

也许有人会派上的用场

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import os.path
list_a = ["/etc/sudoers",
"/etc/sudoers.d/file1",
"/etc/sudoers.d/file2"]
list_b = []
for a in list_a:
b = os.path.isfile(a)
if b == True:
dict_files = {}
dict_files["#FILE_PATH"] = (a)
list_b.append(dict_files)
z = json.dumps(list_b)
print(z)

谢谢大家的帮助。谢谢我的同事亚历克斯的帮助!

最新更新