Operations 8 min read

Automate Zabbix Monitoring: Fetch Host Metrics and Export to CSV with Python

This guide demonstrates how to use Zabbix's API with Python to retrieve host information, item IDs, historical and trend data, process the metrics, and automatically write them into an Excel/CSV file, enabling scheduled monitoring reports.

Ops Development Stories
Ops Development Stories
Ops Development Stories
Automate Zabbix Monitoring: Fetch Host Metrics and Export to CSV with Python

Implementation Approach

Mainly using Zabbix's API to fetch and process data.

Zabbix provides a rich API to obtain host information, item IDs, and trend and history data.

First retrieve all hosts in a host group by group ID, including host name and IP address.

Iterate over each host ID and, within the loop, request the item ID based on the monitoring item key.

Use the obtained item IDs to fetch historical data and trend data respectively.

Store the history and trend values in a dictionary, then add each dictionary to a list.

Write the list information to an Excel/CSV file and schedule the script with a timed task.

Define Time Interval for Data Retrieval

<code>x=(datetime.datetime.now()-datetime.timedelta(minutes=120)).strftime("%Y-%m-%d %H:%M:%S")
y=(datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")
def timestamp(x,y):
    p=time.strptime(x,"%Y-%m-%d %H:%M:%S")
    starttime = str(int(time.mktime(p)))
    q=time.strptime(y,"%Y-%m-%d %H:%M:%S")
    endtime= str(int(time.mktime(q)))
    return starttime,endtime</code>

Get Host Information by Host Group ID

<code>def get_hosts(groupids,auth):
    data ={
            "jsonrpc": "2.0",
             "method": "host.get",
             "params": {
             "output": [ "name"],
             "groupids": groupids,
             "filter":{
                 "status": "0"
             },
             "selectInterfaces": [   
                        "ip"
                    ],
            },
            "auth": auth,  # the auth id is what auth script returns, remember it is string
            "id": 1
        }
    gethost=requests.post(url=ApiUrl,headers=header,json=data)
    return json.loads(gethost.content)["result"]</code>

Build Loop Using Host Info to Retrieve Monitoring Item Data

Fetch Historical Data

<code>host=[]
    print(hosts)
    for i in hosts:
        item1=[]
        item2=[]
        #print(i)
        dic1={}
        for j in ['vfs.fs.size[C:,total]','vm.memory.size[total]','system.cpu.num']:
            data={
                "jsonrpc": "2.0",
                "method": "item.get",
                "params": {
                    "output": [
                        "itemid"

                    ],
                    "search": {
                        "key_": j  
                    },
                    "hostids": i['hostid']
                },
                "auth":auth,
                "id": 1
            }
            getitem=requests.post(url=ApiUrl,headers=header,json=data)
            item=json.loads(getitem.content)['result']

            hisdata={
                "jsonrpc":"2.0",
                "method":"history.get",
                "params":{
                    "output":"extend",                    
                    "time_from":timestamp[0],
                    #"time_till":timestamp[1],
                    "history":0,
                    "sortfield": "clock",
                    "sortorder": "DESC",
                    "itemids": '%s' %(item[0]['itemid']),
                    "limit":1
                },
                "auth": auth,
                "id":1
                }
            gethist=requests.post(url=ApiUrl,headers=header,json=hisdata)
            hist=json.loads(gethist.content)['result']
            item1.append(hist)</code>

Fetch Trend Data

<code>for j in ['vfs.fs.size[C:,used]','vm.memory.size[used]','system.cpu.load']:
            data={
                "jsonrpc": "2.0",
                "method": "item.get",
                "params": {
                    "output": [
                        "itemid"

                    ],
                    "search": {
                        "key_": j  
                    },
                    "hostids": i['hostid']
                },
                "auth":auth,
                "id": 1
            }
            getitem=requests.post(url=ApiUrl,headers=header,json=data)
            item=json.loads(getitem.content)['result']

            trendata={
                "jsonrpc":"2.0",
                "method":"trend.get",
                "params":{
                    "output": [
                        "itemid",
                        "value_max",
                        "value_avg"
                    ],                    
                    "time_from":timestamp[0],
                    "time_till":timestamp[1],
                    "itemids": '%s' %(item[0]['itemid']),
                    "limit":1
                },
                "auth": auth,
                "id":1
                }
            gettrend=requests.post(url=ApiUrl,headers=header,json=trendata)
            trend=json.loads(gettrend.content)['result']
            item2.append(trend)</code>

Process Retrieved Data and Export to CSV

<code>dic1['Hostname']=i['name']
        dic1['IP']=i['interfaces'][0]['ip']
        dic1['磁盘C:Total(B)']=round(float(item1[0][0]['value'])/1024**3,2)
        dic1['磁盘最大C:Used(B)']=round(float(item2[0][0]['value_max'])/1024**3,2)
        dic1['内存Total(B)']=round(float(item1[1][0]['value'])/1024**3,2)
        dic1['内存最大Used(B)']=round(float(item2[1][0]['value_max'])/1024**3,2)
        dic1['内存平均used(B)']=round(float(item2[1][0]['value_avg'])/1024**3,2)
        dic1['CPU负载最大值']=item2[2][0]['value_max']
        dic1['CPU负载平均值']=item2[2][0]['value_avg']
        dic1['CPU 核数']=item1[2][0]['value']
        x = time.localtime(int(item1[2][0]['clock']))
        item1[2][0]['clock'] = time.strftime("%Y-%m-%d %H:%M:%S", x)
        dic1['clock']=item1[2][0]['clock']
        host.append(dic1)  
        print(item)
    print(host)
    return host       
def writecsv(getitem1):
    with open('data.csv','w',encoding='utf-8-sig') as f:
        #f.write(codecs.BOM_UTF8)
        writer = csv.DictWriter(f,csvheader)
        writer.writeheader()
        for row in getitem1:
            writer.writerow(row)</code>

Resulting Output

Full code is available on GitHub: https://github.com/sunsharing-note/zabbix/blob/master/xunjian_auto.py

Zabbix API documentation: https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/history/get

Feel free to discuss.

MonitoringPythonAutomationAPICSVZabbix
Ops Development Stories
Written by

Ops Development Stories

Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.