如何通过使用API​​获得zabbix主机

时间:2016-12-22 08:29:19

标签: python zabbix

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

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)

我该怎么办?这是zabbix doc: https://www.zabbix.com/documentation/3.0/manual/api/reference/host/get

我在网上搜索了很长时间。但没用。请帮助或尝试提供一些如何实现这一目标的想法。

提前致谢。

1 个答案:

答案 0 :(得分:2)

host过滤器中,添加status为0的过滤,可能是这样的:

"filter":{
        "with_monitored_items": True,
        "status": "0"},