首页 >> 大全

zabbix怎么批量查询主机是否可用

2023-10-03 大全 29 作者:考证青年

有时候我们想要查看自己的设备是否可用,当查询几个还好,但是如果要查询大量设备呢,一个个去看,有点得不偿失,费时费力,因此最好的方法就是调用我们的API去批量查询问题的所在

批量查看_主机使用批量交易什么意思_

import requests
import json
import os
import pandas as pd
from tqdm import tqdm
import 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 = 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=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 = json.dumps({"jsonrpc": "2.0","method": "host.get","params": {"output": ["hostid","host"],"selectInterfaces": ["interfaceid","ip"]},"id": 20,"auth": self.auth})else:data = 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=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 = 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=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')

最后生成一个错误列表的Excel文件,里面有你的主机名称和错误信息

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了