赞
踩
python获取zabbix指标信息时,用三方包py-zabbix(即from pyzabbix import ZabbixAPI)不能成功连接zabbix的api。所以选择用最原始的requests三方包实现
import requests
import json
headers = {
"Content-Type": "application/json-rpc",
}
2、先获取token,即在访问zabbix中的任何数据之前,需要登录并获取身份认证token。我们假设你想要以标准Zabbix Admin用户身份登录,那么,你的json请求将如下所示:
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1,
"auth": null
}
以上json请求是zabbix官方文档给出的,在python中将null替换成None。如果不能成功连接zabbix,需要将请求中的user修改为username,即完整的json请求如下:
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"username": "Admin",
"password": "zabbix"
},
"id": 1,
"auth": null
}
python实现过程如下:
data = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"username": "Admin",
"password": "zabbix"
},
"id": 1,
"auth": None
}
data = json.dumps(data)
response = requests.post(url, data=data, headers=headers)
token = json.loads(response.text)['result']
如果正确提供了凭据,API返回的响应将包含用户身份认证token:
{
"jsonrpc": "2.0",
"result": "0424bd59b807674191e7d77572075f33",
"id": 1
}
即result字段对应的结果就是token。
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"filter": {
"host": [
"Zabbix server",
"Linux server"
]
}
},
"auth": "0424bd59b807674191e7d77572075f33",
"id": 1
}
将其中的auth值用上述获取的token替换,filter部分按照自己的需求来设置,获取主机id的代码如下:
host_get_query = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"filter": {
'flags': [0, 4]
}
},
"auth": token,
"id": 1
}
response_host = requests.post(url, data=json.dumps(host_get_query), headers=headers)
host_result = json.loads(response_host.text)['result']
{
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"output": "extend",
"hostids": "10084",
"search": {
"key_": "system"
},
"sortfield": "name"
},
"auth": "0424bd59b807674191e7d77572075f33",
"id": 1
}
同样,auth对应的值用上述的token结果替换,hostids用步骤2获取的主机id替换,获取主机对应的指标的信息的代码如下所示:
item_ls = [] for host in host_result: param = { "output": ["itemid", "delay", "units"], "hostids": host["hostid"], "with_triggers": False, "filter": {'flags': [0, 4]}, "tags": [{"tag": "component", "value": "cpu"}, {"tag": "component", "value": "memory"}, {"tag": "component", "value": "network"}], # "search": { # "key_": "system.cpu" # 通配符搜索。 # }, "sortfield": "name" } item_get_query = { "jsonrpc": "2.0", "method": "item.get", "params": param, "auth": token, "id": 1 } response_item = requests.post(url, data=json.dumps(item_get_query), headers=headers) item_list = json.loads(response_item.text)['result'] item_ls.extend(item_list) print(len(item_ls))
代码中我只获取了指标的itemid,delay, units字段的信息,循环的将获取到的主机的所有监控指标满足flags是0和4的,以及tag的值为‘cpu’, “memory”, “network”的指标提取出来。
import requests import json url = r'http://zabbix.com.cn/api_jsonrpc.php' # 该处根据自己的实际情况修改。 headers = { "Content-Type": "application/json-rpc", } data = { "jsonrpc": "2.0", "method": "user.login", "params": { "username": "Admin", # 该处用户名根据实际用户名修改 "password": "zabbix" # 该处密码根据实际密码修改 }, "id": 1, "auth": None } data = json.dumps(data) response = requests.post(url, data=data, headers=headers) token = json.loads(response.text)['result'] host_get_query = { "jsonrpc": "2.0", "method": "host.get", "params": { "filter": { 'flags': [0, 4] } }, "auth": token, "id": 1 } response_host = requests.post(url, data=json.dumps(host_get_query), headers=headers) host_result = json.loads(response_host.text)['result'] item_ls = [] for host in host_result: param = { "output": ["itemid", "delay", "units"], "hostids": host["hostid"], "with_triggers": False, "filter": {'flags': [0, 4]}, "tags": [{"tag": "component", "value": "cpu"}, {"tag": "component", "value": "memory"}, {"tag": "component", "value": "network"}], # "search": { # "key_": "system.cpu" # 通配符搜索。 # }, "sortfield": "name" } item_get_query = { "jsonrpc": "2.0", "method": "item.get", "params": param, "auth": token, "id": 1 } response_item = requests.post(url, data=json.dumps(item_get_query), headers=headers) item_list = json.loads(response_item.text)['result'] item_ls.extend(item_list) print(len(item_ls))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。