pyhton通过zabbix api获取内存的值


import json
import urllib
from urllib import request

#这个程序是用来获取某个主机组下每个主机的七天之间可用内存的最大值
#history的类型,zabbix官网api有
#History object types to return. 要返回的历史对象类型。

# Possible values:可能的值
# 0 - numeric float;数字浮点数
# 1 - character;字符
# 2 - log; 日志
# 3 - numeric unsigned; 数字符号
# 4 - text.文本
#
# Default: 3.默认:3;

#需要修改主机组名、history类型、zabbix ip、用户名密码、时间、key
def auth(gurl):
header = {"Content-Type": "application/json"}
data = json.dumps( #json.dumps()用于将字典形式的数据转化为json格式的字符串
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 0
})
data2 = bytes(data,encoding='utf-8')
req = urllib.request.Request(url=gurl,data=data2,headers=header)
resp = request.urlopen(req)
resp1 = resp.readline().decode("utf-8") #resp1是str类型
resp2 = eval(resp1) #使用eval()将str类型转换成字典类型
auth = resp2["result"]
return auth
def get_host_id(*args):
header = {"Content-Type": "application/json"}
auth=args[0]
group=args[1]
H = {}
for i in group:
data = json.dumps(
{
"jsonrpc": "2.0",
"method": "host.get",
"selectHosts":"extend",
"params": {
"output": "extend",
"groupids":i #通过组机组名过滤
},
"auth": auth,
"id": 1
})
data2 = bytes(data, encoding='utf-8')
req = urllib.request.Request(url=gurl, data=data2, headers=header)
resp = request.urlopen(req)
resp1 = resp.readline().decode("utf-8")
resp2 = json.loads(resp1) # resp1是str类型,需要将其转换成字典
for i in resp2.get("result"):
hostid=i.get("hostid")
# print(hostid)
hostname=i.get("host")
# print(hostname)
# print("....................")
H[hostname]=hostid
#print(H)
return H


def get_item_id(auth,gurl,hostid):
header = {"Content-Type": "application/json"}
L=[]

for i in hostid:
data = json.dumps(
{
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"output": "extend",
"hostids":i, #这个是host的id号
"search":{
"key_":"vm.memory.size[available]"
}
},
"auth": auth,
"id": 1
})
data2 = bytes(data, encoding='utf-8')
req = urllib.request.Request(url=gurl, data=data2, headers=header)
resp = request.urlopen(req)
resp1 = resp.readline().decode("utf-8")
resp2=json.loads(resp1) #resp1是str类型,需要将其转换成字典
re=resp2.get("result")[0]
j={}
j[i]=re.get("itemid")
L.append(j)
return L #类似[{'10261': '28581'}, {'10332': '31492'}] 主机id:item id

def get_item_value(auth,gurl,itemid):
header = {"Content-Type": "application/json"}
data = json.dumps(
{
"jsonrpc": "2.0",
"method": "history.get",
"params": {
"output":"extend",
"history":3,
"sortfield": "clock",
"itemids": itemid,
# "limit":10
# "time_from":1569307030,
# "time_till":1569307554
# "time_from": 1569307030,
# "time_till": 1569307554
"time_from": 1568822400, #通过date -d $(date -d "-7 day" +%Y%m%d) +%s得到
"time_till": 1569488748 #通过date +%s得到
},
"id": 1,
"auth": auth
})
data2 = bytes(data, encoding='utf-8')
req = urllib.request.Request(url=gurl, data=data2, headers=header)
resp = request.urlopen(req)
resp1 = resp.readline().decode("utf-8")
#print(resp1)
resp2=eval(resp1)
result=resp2["result"]
#print(result)
l=[]
for m in result:
#print(m["value"])
l.append(m["value"])
return l
if __name__ == '__main__':
gurl = "http://10.103.22.41/zabbix/api_jsonrpc.php"
group=[31] #列表,存放需要查询的主机组的id号
a = auth(gurl)
L1=[a,group]
host_id=get_host_id(*L1) #获取指定主机组下的所有主机id,返回是一个字典--主机名:主机id
host_id_new={value:key for key,value in host_id.items()} #将字典的key和value互换
#print(host_id_new)
host=[]
for i in host_id.values():
host.append(i)

L2=get_item_id(a,gurl,host) #通过主机id获取itemid 类似[{'10261': '28581'}, {'10332': '31492'}] 主机id:item id
#print(L2)
for m in L2:
for n in m: #n是主机id m[n]是主机id对应的item id
#print(m[n])
hisroty_value=get_item_value(a,gurl,m[n]) #通过itemid获取历史数据
#print(n,max(hisroty_value))
max_value=int(min(hisroty_value))

print(host_id_new[n],round(max_value/1024/1024/1024,2)) #保留两位小数

#结果如下所示,是以主机名 监控项值的形式输出,可以写入到excel中

   #admin-web-1 0.29
   #auth-sign-1 0.12

posted @ 2019-09-27 09:06  Aslan-a  阅读(853)  评论(0编辑  收藏  举报