Search code examples
pythonzabbix

How get zabbix enabled host,Through the use of API


This is my code,it can get all host.I just need enabled host.

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)

What should I do?This is zabbix doc: https://www.zabbix.com/documentation/3.0/manual/api/reference/host/get

I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.

Thanks in advance.


Solution

  • In your host filter, add filtering for status of 0, possibly like so:

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