主机监控和资产管理应该是一套完整的系统,但当公司还不具规模或系统建设不完善。两套数据相互独立。可能会漏掉部分主机监控,出现故障就显得尤为尴尬(如磁盘满了,未及时报警这类低级错误)。因此我们需要定期检查主机监控是否遗漏。
这里有个问题是,我们在 zabbix 中监控的主机可能并不是以 ip 作为主机名进行监控的,而是主机名。直接通过 zabbx api 获取到的是主机名。需要添加一个过滤参数:“selectInterfaces” 将 ip 打印出来,与现有ip 资产列表比对更加方便。实现如下:
#!/usr/bin/env python标签:python,ip,json,auth,zabbix,核对,监控,iplist From: https://blog.51cto.com/xikder/5769783
# -*- coding: utf-8 -*-
#检查ip是否在zabbix中, 用于监控核对。
import json
import requests
#zabbix api
url = 'http://xxx.com/api_jsonrpc.php'
header = {'Content-Type': 'application/json'}
##验证
auth = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "xxx",
"password": "xxx"
},
"id": 1,
"auth": None,
}
# 获取token
def getkey():
response = requests.post(url, data=json.dumps(auth), headers=header)
authid = json.loads(response.text)['result'] ### auth的id
return authid
def gethostip():
postdata = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ["host"],
#关键参数获取 ip
"selectInterfaces": [
"ip"
]
},
"auth": getkey(),
"id": 0,
}
r = requests.post(url, data=json.dumps(postdata), headers=header)
r = json.loads(r.text)['result']
global iplist
iplist = []
for i in r:
try:
iplist.append(i['interfaces'][0]['ip'])
except:
continue
if __name__ == "__main__":
gethostip()
with open("iplist.txt") as file:
for ip in file:
ip = ip.rstrip()
if ip not in iplist:
print ("%s" % ip + ":is not in zabbix")
else:
print ("%s" % ip + ":is in zabbix")