如何通过使用API获得支持zabbix的主机

2024-04-19 05:16:48 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码,它可以得到所有主机。我只需要启用主机。在

import requests
import csv
import json

url = 'https://xxxx.zabbix.com/api_jsonrpc.php'
post_data = {
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "filter":{
            "with_monitored_items": True},
        "output":
            ["host"],
        "selectInterfaces":
            ["ip"]},
    "id": 1,
    "auth": "xxxxxxxxxxxxxxxxxx"}

post_header = {'Content-Type': 'application/json-rpc'}


ret = requests.post(url,
                    data=json.dumps(post_data),
                    headers=post_header,
                    verify=False)
data = ret.json()['result']
# print(data)
parsed_result = [{'host': i['host'], 'eth0': i['interfaces'][0]['ip'],
                  'type': 'vm', 'status': 'online'} for i in data]
print(parsed_result)
with open("data.csv", "w") as file:
    csv_file = csv.writer(file)
    header = ['hostname', 'eth0', 'type', 'status']
    data_rows = [(i['host'], i['eth0'], i['type'], i['status']) for i in parsed_result]  # NOQA
    csv_file.writerow(header)
    csv_file.writerows(data_rows)

我该怎么办?我是扎比克斯医生: https://www.zabbix.com/documentation/3.0/manual/api/reference/host/get

我在网上找了很久。但没用。请帮助或尝试给出一些想法如何实现这一点。在

提前谢谢。在


Tags: csvimportjsonhosturldatatypestatus