知识的价值不在于占有,而在于使用。这篇文章主要讲述python 调用zabbix api实现查询主机信息,输出所有主机ip相关的知识,希望能为你提供帮助。
之前发现搜索出来的主机调用zabbix api信息都不是那么明确,后来通过zabbix官方文档,查到想要的api信息,随后写一篇自己这次项目中用到的api。
#!/usr/bin/env python #coding:utf8 import requests import jsonheaders = Content-Type: application/json-rpc server_ip = 10.37.149.109url = 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-rpcusername = admin passwd = zabbix data = "https://www.songbingjia.com/android/jsonrpc": "2.0", "method": "user.login", "params": "user": username, "password": passwd , "id": 0request = requests.post(url=url,headers=headers,data=https://www.songbingjia.com/android/json.dumps(data)) dict = json.loads(request.text) return dict[result] # 从api获取主机信息, def getHosts(token_num): data ="https://www.songbingjia.com/android/jsonrpc": "2.0", "method": "host.get", "params": "output": [ "hostid", "host" ], "selectInterfaces": [ "interfaceid", "ip" ] , "id": 2, "auth": token_num,request = requests.post(url=url,headers=headers,data=https://www.songbingjia.com/android/json.dumps(data)) dict = json.loads(request.content) #print dict[result] return dict[result] #整理信息,输出想要的信息,组合成字典,我这边提出ip。 def getProc(data): dict = list = data for i in list: host = i[host] inter = i[interfaces] for j in inter: ip = j[ip] dict[host] = ipreturn dict#排序ip列表 def getData(dict): data = dict ip_list = [ ] for key in data.keys():ip =data[key] ip_list.append(ip) ip_list = list(set(ip_list)) ip_list.sort() return ip_list #整理输出ip def getGroup(ip_list): ip_group = ips = ip_list for i in ips: print iif __name__ =="__main__":#server_ip = 10.37.149.109 username = admin passwd = zabbix token_num = getToken(username,passwd) data = https://www.songbingjia.com/android/getHosts(token_num) hosts = getProc(data) ip_list = getData(hosts) getGroup(ip_list)
【python 调用zabbix api实现查询主机信息,输出所有主机ip】
推荐阅读
- day1-python简介+安装
- 调用zabbix 分组api
- Spug监控
- Total Commander快捷键
- oeasy教您玩转vim - 76 - # Session会话
- 解决ubuntu下编译报错/usr/include/linux/errno.h:1:10: fatal error: asm/errno.h #yyds干货盘点#
- day5-python数据类型
- XP开机出现桌面蓝屏代码0x00000019的几种处理方案以及如何排除问题
- xp系统不能安装itunes的总是提示更改版本如何处理?