- 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
来源: http://www.bubuko.com/infodetail-3214919.html