调用zabbix 分组api

Posted 箐惜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调用zabbix 分组api相关的知识,希望对你有一定的参考价值。

调用zabbix 分组api,获取分组中主机host信息,并分类保存,

#!/usr/bin/env python
#coding:utf8
import requests
import json
import os
#定义url头部信息
headers = Content-Type: application/json-rpc
#zabbix地址
server_ip = 10.37.149.109 #zabbix url url = http://%s/zabbix/api_jsonrpc.php %server_ip #获取token def getToken(username,passwd): #url = http://%s/zabbix/api_jsonrpc.php%server_ip #headers = Content-Type: application/json-rpc username = admin passwd = zabbix data = "jsonrpc": "2.0", "method": "user.login", "params": "user": username, "password": passwd , "id": 0 request = requests.post(url=url,headers=headers,data=json.dumps(data)) dict = json.loads(request.text) return dict[result] # 获取groupID def getGroupid(token_num): group = data = "jsonrpc": "2.0", "method":"hostgroup.get", "params": "output":["groupid","name"] , "auth":token_num, "id": 0 request = requests.post(url=url,headers=headers,data=json.dumps(data)) dict = json.loads(request.content) for i in dict[result]: groupid = i[groupid] name = i[name] group[name] = groupid return group # 获取group中的hosts信息 def getHost(token_num,group): dict_group = for k in group.keys(): groupid = group[k] data = "jsonrpc":"2.0", "method":"host.get", "params": "output":["hostid","name"], "selectInterfaces": [ "interfaceid", "ip" ], "groupids":groupid , "auth":token_num, "id":1, request = requests.post(url=url,headers=headers,data=json.dumps(data)) hosts = json.loads(request.content) hosts1 = hosts[result] ip_lists = getProc(hosts1) dict_group[k]=ip_lists return dict_group # host ip排序 def getProc(hosts): list = hosts ip_lists = [ ] if list: for i in list: host = i[name] inter = i[interfaces] for j in inter: ip = j[ip] ip_lists.append(ip) ip_lists.sort(lambda x,y: cmp(.join( [ i.rjust(3, 0) for i in x.split(.)] ), .join( [ i.rjust(3, 0) for i in y.split(.)] ) )) return ip_lists # 进行ip组分类(同网段为一组) def splitIp(ip): ip_split = ip.split(.) ip_class = ip_split[0:-1] return ip_class # 进行ip组分类, def getIp(dict_group): dicGroupIp = dicIp = ip_l = [] for group,ip_list in dict_group.items(): for ip in ip_list: ip_class = splitIp(ip) ip_group =..join(ip_class) if ip_group not in dicIp: ip_l.append(ip) dicIp[ip_group] = ip_l else: dicIp[ip_group].append(ip) ip_l = [ ] dicGroupIp[group] = dicIp dicIp = return dicGroupIp # 分别创建group组文件将ip地址写入。 def writrIpfile(dicGroupIp): for group_class,group_dic in dicGroupIp.items(): ip_path = os.path.join(/python/zabbix2,group_class ) with open(ip_path,w)as fd: for ip_class,ip_list in group_dic.items(): ip_class = [ + ip_class + ] #with open(ip_path,a)as fd: fd.write(ip_class + \\n) for i in ip_list: fd.write(i + \\n) if __name__ == "__main__": #server_ip = zmonitor.enn.cn username = admin passwd = zabbix token_num = getToken(username,passwd) group = getGroupid(token_num) dict_group = getHost(token_num,group) dicIp = getIp(dict_group) writrIpfile(dicIp)

 

以上是关于调用zabbix 分组api的主要内容,如果未能解决你的问题,请参考以下文章

onActivityResult 未在 Android API 23 的片段上调用

python调用zabbix api 获取主机cpumemdisk

导航到另一个片段时触发 API 调用

调用zabbix的api接口导出主机配置信息

zabbix批量添加主机监控-zabbix api调用

Zabbix 调用API 批量添加主机等