Search code examples
zabbix

ZabbixAPI, retrieving information from a particular field within each host


I want to retrieve the percentage of disk space left for a particular diskX: from all hosts within a particular host group. I tried to work with the item.get() function but that returned an empty list.

zapi =ZabbixApi(server)

for t in zapi.item.get(groups = 'Type1',filter = {'name': 'Free Disk Space on X'},)

This^^ method using the item.get method. Gives me an empty list

I tried using the history.get method but that kept timing out

for t in groups:
  t2 += zapi.history.get(filter = {'name':'free Disk Space on E:(percentage)'},)

Anyone have any experience with Zabbix Api's to advice me on what I am doing wrong?

Thanks :)


Solution

  • Edited after more details regarding the request, see the comments.

    To avoid php timeouts you should split your requests and use time_from/time_till as Jan suggested.

    When using discovered items the item name obtained through the APIs will not expand the macros, there's a feature request about it.

    For example if you use a Windows Filesystem Discovery and your server has C: and D: drives, in Zabbix you will have two items with the same name ("Free disk space on $1 (percentage)"), while the discovered drive will be in the key_ field of each item, for instance:

    vfs.fs.size[C:,pfree]
    vfs.fs.size[D:,pfree]
    

    So, you will have to call the item get API filtering for the generic name (the $1), then get the history values only if the key_ contains your target drive name

    I've updated the sample script with a hostgroup filter and more verbose variables and output: edit out any non-needed field to simplify the output you need.

    from zabbix.api import ZabbixAPI
    import re
    import time
    import datetime
    
    zapi = ZabbixAPI(url=zabbixServer, user=zabbixUser, password=zabbixPass)
    
    # Static filters, implement argparse if needed
    itemFilter = {  "name" : "Free disk space on $1 (percentage)"  }
    hostgroupFilter = { "name": "Some HostGroup" }
    keyFilter = "C\:"
    
    # args.f and args.t supplied from cmd line - see argparse
    fromTimestamp = time.mktime(datetime.datetime.strptime(args.f, "%d/%m/%Y %H:%M").timetuple())
    tillTimestamp = time.mktime(datetime.datetime.strptime(args.t, "%d/%m/%Y %H:%M").timetuple())
    
    
    # Get only the host of the specified hostgroup
    hostGroup = zapi.hostgroup.get(filter=hostgroupFilter,output='extend')
    hosts =  zapi.host.get(groupids=hostGroup[0]['groupid'],output='extend')
    
    for host in hosts:
        items = zapi.item.get(filter=itemFilter, host=host['host'], output='extend' )
    
        for item in items:
            # Check if the item key contains the target object (in your example, if in contains C:)
            if re.search(keyFilter, item['key_']):
    
                values = zapi.history.get(itemids=item['itemid'], time_from=fromTimestamp, time_till=tillTimestamp, history=item['value_type'])
                for historyValue in values:
                    currentDate = datetime.datetime.fromtimestamp(int(historyValue['clock'])).strftime('%d/%m/%Y %H:%M:%S')
                    print "{}:{}({}) - {} {} Value: {}".format(
                      host['host'],
                      item['name'],
                      item['key_'],
                      historyValue['clock'],
                      currentDate, historyValue['value'])
    

    Sample output of 5 minutes, hostgroup with 3 windows server

    SRV01:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538128853 28/09/2018 12:00:53 Value: 63.3960
    SRV01:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538128914 28/09/2018 12:01:54 Value: 63.3960
    SRV01:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538128974 28/09/2018 12:02:54 Value: 63.3960
    SRV01:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538129034 28/09/2018 12:03:54 Value: 63.3960
    SRV01:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538129094 28/09/2018 12:04:54 Value: 63.3960
    SRV02:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538128824 28/09/2018 12:00:24 Value: 52.2341
    SRV02:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538128885 28/09/2018 12:01:25 Value: 52.2341
    SRV02:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538128944 28/09/2018 12:02:24 Value: 52.2341
    SRV02:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538129004 28/09/2018 12:03:24 Value: 52.2341
    SRV02:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538129065 28/09/2018 12:04:25 Value: 52.2341
    SRV03:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538128828 28/09/2018 12:00:28 Value: 33.2409
    SRV03:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538128888 28/09/2018 12:01:28 Value: 33.2409
    SRV03:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538128947 28/09/2018 12:02:27 Value: 33.2409
    SRV03:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538129008 28/09/2018 12:03:28 Value: 33.2409
    SRV03:Free disk space on $1 (percentage)(vfs.fs.size[C:,pfree]) - 1538129069 28/09/2018 12:04:29 Value: 33.2409