【zabbix怎么批量查询主机是否可用】有时候我们想要查看自己的设备是否可用,当查询几个还好,但是如果要查询大量设备呢,一个个去看,有点得不偿失,费时费力,因此最好的方法就是调用我们的API去批量查询问题的所在
文章插图
import requestsimport jsonimport osimport pandas as pdfrom tqdm import tqdmimport reip = 'xxx.xxx.xxx.xxx'#这里是你的zabbix-ip地址user = "xxxxxx"#这里是你的zabbix的用户名称password = "xxxxxx" #这里是你的zabbix的登录密码class ZabbixApi:def __init__(self, ip, user, password):self.url = 'http://' + ip + '/zabbix/api_jsonrpc.php'# 'http://192.168.1.20/zabbix/api_jsonrpc.php'self.headers = {"Content-Type": "application/json",}self.user = userself.password = passwordself.auth = self.__login()def __login(self):'''zabbix登陆获取auth:return: auth#样例bdc64373690ab9660982e0bafe1967dd'''data = http://www.kingceram.com/post/json.dumps({"jsonrpc": "2.0","method": "user.login","params": {"user": self.user,"password": self.password},"id": 10,# "auth": 'none'})try:response = requests.post(url=self.url, headers=self.headers, data=http://www.kingceram.com/post/data, timeout=2)# {"jsonrpc":"2.0","result":"bdc64373690ab9660982e0bafe1967dd","id":10}auth = response.json().get('result', '')return authexcept Exception as e:print("\033[0;31;0m Login error check: IP,USER,PASSWORD\033[0m")raise Exceptiondef host_get(self, hostname=''):'''获取主机 。不输入hostname 则获取所有:param hostName: zabbix主机名不允许重复 。(IP允许重复) 。#host_get(hostname='gateway1'):return: {'jsonrpc': '2.0', 'result': [], 'id': 21}:return: {'jsonrpc': '2.0', 'result': [{'hostid': '10277', ... , 'host': 'gateway', ...}], 'id': 21}'''if hostname == '':# print("no hostname and find all host")data = http://www.kingceram.com/post/json.dumps({"jsonrpc": "2.0","method": "host.get","params": {"output": ["hostid","host"],"selectInterfaces": ["interfaceid","ip"]},"id": 20,"auth": self.auth})else:data = http://www.kingceram.com/post/json.dumps({"jsonrpc": "2.0","method": "host.get","params": {"output": "extend","filter": {"host": hostname},},"id": 21,"auth": self.auth})try:response = requests.get(url=self.url, headers=self.headers, data=http://www.kingceram.com/post/data, timeout=2)# hosts_count=len(response.json().get('result',''))# print('l',hosts)return response.text# len(ret.get('result'))为1时获取到,否则未获取到 。except Exception as e:print("\033[0;31;0m HOST GET ERROR\033[0m")raise Exceptiondef host_error(self, hostname):data = http://www.kingceram.com/post/json.dumps({"jsonrpc": "2.0","method": "host.get","params": {"filter": {"host": [hostname,]}},"auth": self.auth,"id": 1})list_name = []list_error = []try:response = requests.post(url=self.url, headers=self.headers, data=http://www.kingceram.com/post/data, timeout=2)coment = response.json()error = coment['result'][0]['snmp_error']if len(error)> 0:list_error.append(error)list_name.append(hostname)return list_name, list_errorexcept Exception as e:raise Exceptionif __name__ == '__main__':zabbix = ZabbixApi(ip, user, password)print("...login success...")#df1 = pd.read_excel('LFL China Equipment List.20210423 - 副本.xls', sheet_name='Network List').loc[:,# ['City', 'DC Name', 'Device Type', 'Hostname', 'IP']]list_1 = ['MTCL-IDC-FG101F-2','MTGZ-16F-WLC01','MTGZ-16F-WLC01']list_name = []list_error = []#这里传的是主机的名称,返回的是主机的名称和错误信息for i in list_1:a, b = zabbix.host_error(i.strip(' ').replace('#', '').replace('(', '-').replace(')', '').replace(' ', ''))if len(a) > 0 and len(b) > 0:list_name.append(a[0])list_error.append(b[0])df = pd.DataFrame()df['设备名'] = list_namedf['报错信息'] = list_errordf.to_excel('不可用的设备.xlsx')
- 移动wifi显示限制服务器,涨姿势|笔记本电脑连WiFi时显示受限怎么办?
- gpt人工智能详细介绍
- php怎么优化搜索,导航型搜索关键词如何优化
- 网站优化基础教程:怎么做好关键词优化?
- 前台js把后台发过来的Base64码转换成图片,批量转换换行符\n
- 怎么把图片压缩到200K?教你把照片缩小到指定大小的方法
- 盆栽桔子树叶子发黄怎么办
- 打瓜怎么施肥
- 地栽桔子树怎么养
- 耐冬怎么施肥