📜 ⬆️ ⬇️

Using Zabbix API. When there is not enough standard statistics

There was a task to get some statistics from Zabbix, I share the experience of getting data from the Zabbix database through the API using Python tools.



Pieces of code will be for Python 2.7

There is a ready py-zabbix library for working with zabbix-api, documentation on it is available here , but there are not many examples. The official Zabbix API manual.
')
So, after the standard installation:

pip install py-zabbix 

We are trying to connect to the Zabbix server:

 from pyzabbix import ZabbixAPI z = ZabbixAPI('https://172.16.1.10', user='user1', password='pass1') answer = z.do_request('apiinfo.version') print "Version:",answer['result'] 

The format of the response from the server is JSON:

 {u'jsonrpc': u'2.0', u'result': u'3.0.2', u'id': u'1'} 

The script prints the contents of the result field:

 Version: 3.0.2 

Now you can take to solving the problem of interest. The task is to get the average value of Disk Idle Time from all virtual machines per week (Mon-Fri) during working hours (from 10:00 to 19:00) for a certain week. I do not want to focus on the relevance of these parameters, but simply share my experience with the Zabbix API using the example of this specific task.

So, virtual machines in Zabbix are in a separate group, first we get a list of available groups using the hostgroup.get method:

 #Get List of available groups groups = z.hostgroup.get(output=['itemid','name']) for group in groups: print group['groupid'],group['name'] 

With the output parameter you can determine which fields the API returns:

 38 _Local Domains 53 _Local NAS 23 _Local Servers Linux 27 _Local Servers Virtual Linux 25 _Local Servers Virtual Windows 24 _Local Servers Windows 35 _Local Switches 

You can then get a list of hosts in a specific group using the host.get method:

 #Get List of hosts in the group hosts = z.host.get(groupids=25, output=['hostid','name']) for host in hosts: print host['hostid'],host['name'] 

The groupids parameter specifies the group ID:

 10197 DC1_--172.16.1.4-- 10204 DC2_--172.16.1.5-- 10637 LocalDB_--172.16.1.12-- 10686 WSUS_--172.16.1.16-- 10708 Jira_--172.16.1.24-- 

To get a list of items for a specific host, use the item.get method:

 #Get List of items on the host items = z.item.get(hostids=10637, output=['itemid','name']) for item in items: print item['itemid'],item['name'] 

Result:

 525617 ICMP ping 525618 ICMP loss 525619 ICMP response time 940205 Input Microsoft Hyper-V Network Adapter #2 940206 Output Microsoft Hyper-V Network Adapter #2 990808 Disk Idle time on C: 990809 Disk Idle time on D: 

As can be seen from the answer, the selected host has 2 disks, you need to display the minimum value from several. To access data on items, use the history.get method. The following code does not claim to be optimal, I just started learning Python, but in general, the script coped with the task.

For the history.get method, you need to define the following parameters:


Script collecting statistics:

 from pyzabbix import ZabbixAPI import time import sys z = ZabbixAPI('https://172.16.1.10', user='user1', password='pass1') groupid = 25 #Local Servers Virtual Windows hosts = z.host.get(groupids=groupid , output=['hostid','name']) #   host_names = [host['name'] for host in hosts] #  host_ids = [host['hostid'] for host in hosts] nameindex = 0 #, -    increment = 60*60*24 for host_id in host_ids: # search    items,       items = z.do_request('item.get',{'hostids':[host_id],'output': ['itemid','name'],'search':{'name': 'Idle time'}}) #   disk_ids = [item['itemid'] for item in items['result']] #   -  num_disks = len(disk_ids) avg_list=[] #      for disk in disk_ids: #       time # ,     - 27  2017 ,  9:00  18:00 time_from = time.mktime((2017,3,27,9,0,0,0,0,0)) time_till = time.mktime((2017,3,27,18,0,0,0,0,0)) history_sum=0 history_len=0 #  5   27  31  for day in range(0,5): data = z.history.get(history = 0, itemids=disk, time_from=time_from, time_till=time_till) #      graph = [float(item['value']) for item in data] #   ,        if(len(graph)!=0): history_sum+=sum(graph) history_len+=len(graph) #    time_from += increment time_till += increment #   ,        if(history_len!=0): avg_list.append(history_sum/history_len) else: avg_list.append(0) #   ,    if(len(avg_list)>0): sys.stdout.write(host_names[nameindex]) print ',',num_disks,',',min(avg_list) nameindex+=1 

The result is a comma-separated host name, number of screws, and min idle time:

 DC1_--172.16.1.4--, 1 , 99.0758766296 DC2_--172.16.1.5--, 1 , 97.0989181683 LocalDB_--172.16.1.12--, 2 , 98.9930628704 

Thank you for attention.

Source: https://habr.com/ru/post/325876/


All Articles