pip install py-zabbix
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']
{u'jsonrpc': u'2.0', u'result': u'3.0.2', u'id': u'1'}
Version: 3.0.2
#Get List of available groups groups = z.hostgroup.get(output=['itemid','name']) for group in groups: print group['groupid'],group['name']
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
#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']
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--
#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']
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:
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
DC1_--172.16.1.4--, 1 , 99.0758766296 DC2_--172.16.1.5--, 1 , 97.0989181683 LocalDB_--172.16.1.12--, 2 , 98.9930628704
Source: https://habr.com/ru/post/325876/
All Articles