游走的鱼

导航

python3 对zabbix5.0 批量新增聚合图形

背景:主机较多,要添加的聚合图形也很多,通过python3来批量新增

代码如下:

#!/bin/env python
import json
import
urllib.request import argparse import re import requests import sys re_digits = re.compile(r'(\d+)') def emb_numbers(s): pieces=re_digits.split(s) pieces[1::2]=map(int,pieces[1::2]) return pieces def sort_strings_with_emb_numbers2(alist): return sorted(alist, key=emb_numbers) def requestjson(url, values): head = {'Content-Type': 'application/json-rpc'} request_auth = requests.post(url,headers=head,data=json.dumps(values)) output = request_auth.json() return output def authenticate(url, username, password): values = {'jsonrpc': '2.0', 'method': 'user.login', 'params': { 'user': username, 'password': password }, 'id': '1', 'auth': None } output = requestjson(url, values) return output['result'] #通过主机组获取对应主机名,并按照主机名排序,保证新增聚合图形按照主机名字排序 def gethosts(groupname, url, auth): host_list = {} values = {'jsonrpc': '2.0', 'method': 'hostgroup.get', 'params': { 'output': 'extend', 'filter': {'name': groupname}, 'selectHosts': ['name'] }, 'auth': auth, 'id': '2' } output = requestjson(url, values) print(output) for host in output['result'][0]['hosts']: host_list[host['name']]=(host['hostid']) hosts_sort = [] for host in sort_strings_with_emb_numbers2(host_list.keys()): hosts_sort.append(host_list[host]) return hosts_sort #通过主机名获取对应监控项的graphid def getgraphs(host_list, name_list, url, auth, columns, graphtype=0, dynamic=0): if (graphtype == 0): selecttype = ['graphid'] select = 'selectGraphs' if (graphtype == 1): selecttype = ['itemid', 'value_type'] select = 'selectItems' graphs = [] for host in host_list: values = ({'jsonrpc': '2.0', 'method': 'graph.get', 'params': { select: [selecttype, 'name'], 'output': ['graphid', 'name'], 'hostids': host, 'filter': {'name': name_list}, 'sortfield': 'name' }, 'auth': auth, 'id': '3' }) output = requestjson(url, values) bb=output['result'] if (graphtype == 0): for i in bb: graphs.append(i['graphid']) if (graphtype == 1): for i in bb: if int(i['value_type']) in (0, 3): graphs.append(i['itemid']) print('----',graphs) graph_list = [] x = 0 y = 0 for graph in graphs: graph_list.append({ 'resourcetype': graphtype, 'resourceid': graph, 'width': '500', 'height': '100', 'x': str(x), 'y': str(y), 'colspan': '1', 'rowspan': '1', }) x += 1 if x == int(columns): x = 0 y += 1 return graph_list #创建聚合图形 def screencreate(url, auth, screen_name, graphids, columns): columns = int(columns) #print('----',len(graphids)) if len(graphids) % columns == 0: vsize = len(graphids) / columns else: vsize = (len(graphids) / columns) + 1 values = {'jsonrpc': '2.0', 'method': 'screen.create', 'params': [{ 'name': screen_name, 'hsize': columns, # 'vsize': vsize, # 'screenitems': [] }], 'auth': auth, 'id': 2 } for i in graphids: values['params'][0]['screenitems'].append(i) output = requestjson(url, values) print(output) def main(groupname,screen_name): url = 'http://localhost/api_jsonrpc.php' username = 'admin' password = 'admin' #监控项图形名称 graphname=["CPU utilization","Memory utilization","Interface eth0: Network traffic",\ "CPU jumps","Memory usage","Processes","vda: Disk average waiting time",\ "vda: Disk read/write rates","vda: Disk utilization and queue","Swap usage"] #有几个监控项图形名称写几 columns=len(graphname) auth = authenticate(url, username, password) host_list = gethosts(groupname, url, auth) graph_ids = getgraphs(host_list, graphname, url, auth, columns) screencreate(url, auth, screen_name, graph_ids, columns) if __name__ == '__main__': #写入要添加聚合图形的主机组,groupname是主机组名称,screen_name是创建聚合图形的名称 hlist=["reco-admin","reco-apiup","reco-filter","reco-tools","reco-airec"]

for h in hlist: main(groupname=h,screen_name=h)

 

posted on 2021-09-22 20:42  游走的鱼  阅读(149)  评论(0编辑  收藏  举报