Search code examples
pythonzabbix

Output with zabbix-api value history


I am trying to capture the following sequence with the zabbix hostid -> itemid -> history API, but it has not returned anything to me. I need this script to return the last values ​​collected by ZABBIX, including item id + hostname

SCRIPT

from zabbix.api import ZabbixAPI
from datetime import datetime
import time

zapi = ZabbixAPI(url='http://192.168.1.250/zabbix', user='Admin', password='zabbix')

fromTimestamp = int(time.mktime(datetime.now().timetuple()))
tillTimestamp = int(fromTimestamp - 60 * 60 * 1)  # 1 hours

# Get only the host of the specified hostgroup
hosts =  zapi.host.get(groupids='15',output='extend')

for host in hosts:
    items = zapi.item.get(itemid='28689', host=host['host'], output='extend' )
    for item in items:
        values = zapi.history.get(itemids=item['itemid'], time_from=fromTimestamp, time_till=tillTimestamp, output='extend')        
        for historyValue in values:
            print(host['host'],item['itemid'],historyValue['value'])

OUTPUT

Nothing returns me

DESIRED OUTPUT

'host','28689','84'
'host','28689','82'
'host','28689','85'
'host','28689','83'

Solution

  • There are a few issues on your code (static itemid, missing params in the history.get etc...), I'll try to sum up everything.

    You are filtering by a static hostgroup id, so I assume that you have more than one host and you want the value of a specific item of each host, something like:

    • hostgroup: MyHostGroup
    • members: host01, host02, host03
    • item of interest: ICMP Loss

    The output should be something like:

    Timestamp  Hostname   ItemID   ICMP Loss
    xxxxxx1    host01     10011    0
    xxxxxx2    host01     10011    10
    xxxxxx3    host01     10011    10
    xxxxxx4    host01     10011    15
    
    xxxxxx1    host02     10026    100
    xxxxxx2    host02     10026    100
    xxxxxx3    host02     10026    100
    xxxxxx4    host02     10026    100
    
    xxxxxx1    host03     10088    0
    xxxxxx2    host03     10088    10
    xxxxxx3    host03     10088    0
    xxxxxx4    host03     10088    0
    

    A working python implementation:

    groupFilter = {'name': 'MyHostGroup'}
    itemFilter = {'name': 'ICMP Loss'}
    
    # Get the hostgroup id by its name 
    hostgroups = zapi.hostgroup.get(filter=groupFilter, output=['groupids', 'name'])
    
    # Get the hosts of the hostgroup by hostgroup id
    hosts = zapi.host.get(groupids=hostgroups[0]['groupid'])
    
    for host in (hosts):
        # Get the item info (not the values!) by item name AND host id
        items = zapi.item.get(filter=itemFilter, host=host['host'], output='extend', selectHosts=['host', 'name'])
    
        # for loop - for future fuzzy search, otherwise don't loop and use items[0] 
        for item in items:
            # Get item values
            values = zapi.history.get(itemids=item['itemid'], time_from=fromTimestamp, time_till=tillTimestamp, history=item['value_type'])
    
            for historyValue in values:
                print( ......... ) # format here your output, values are stored in historyValue['value']