话不多说直接上脚本,自用的话替换一下脚本里面的API地址以及zabbix的用户名和密码即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
#!/usr/bin/env python # encoding=utf-8 import datetime import json import time import requests def getToken(url, post_headers, url_user, url_password): post_data = { "jsonrpc": "2.0", "method": "user.login", "params": { "user": url_user, "password": url_password }, "id": 1 } ret = requests.post(url, data=json.dumps(post_data), headers=post_headers) return json.loads(ret.text).get("result") def timestamp_to_string(timestamp): # 转换成localtime time_local = time.localtime(timestamp) # 转换成新的时间格式(2016-05-05 20:28:54) dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local) return dt def get_host_discovery_list(url, auth, post_headers,hostid): post_data = { "jsonrpc": "2.0", "method": "discoveryrule.get", "params": { "output": "extend", "hostids": hostid, }, "auth": auth, "id": 1 } ret = requests.post(url, data=json.dumps(post_data), headers=post_headers) ret_dict = json.loads(ret.text) return ret_dict def get_all_host_list(url, auth, post_headers): post_data = { "jsonrpc": "2.0", "method": "host.get", "params": {"output":["hostid", "name"], "filter": {"status": 0}}, "auth": auth, "id": 1 } ret = requests.post(url, data=json.dumps(post_data), headers=post_headers) ret_dict = json.loads(ret.text) return ret_dict if __name__ == '__main__': # zabbix监控系统接口地址 url = "http://xxxx.com/zabbix/api_jsonrpc.php" post_headers = {'Content-Type': 'application/json'} url_user = "" url_passwd = "" auth = getToken(url, post_headers, url_user, url_passwd) # 获取token值 # print(get_host_discovery_list(url,auth,post_headers)) all_host_list = get_all_host_list(url,auth,post_headers)['result'] # print(all_host_list) count = 0 error_host_list = [] error_msg_list = [] for host in all_host_list: host_id = host['hostid'] host_name = host['name'] print(count) count= count + 1 auto_discovery_list = get_host_discovery_list(url,auth,post_headers, host_id)['result'] for auto in auto_discovery_list: is_error = auto['error'] if is_error: if 'Unsupported item key' in is_error: error_host_list.append(host_name) error_msg_list.append(is_error) error_host_list_unique = set(error_host_list) for error in error_host_list_unique: print(error) |