python通過zabbix api獲取主機(jī)
zabbix強(qiáng)大地方在于有強(qiáng)大的api,zabbix 的api可以拿到zabbix大部分?jǐn)?shù)據(jù),目前我所需的數(shù)據(jù)基本可以通過api獲取,以下是通過zabbix api獲取的主機(jī)信息python代碼,其他數(shù)據(jù)也如此類推,api使用方法可參見官網(wǎng)文檔:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import json
import urllib2
from urllib2 import URLError
from login import zabbix_login
t=zabbix_login()
def hostid_get():
data = json.dumps(
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": "extend",
"groupids":14,
"filter":{"flags": "4" },
},
"auth":t.user_login(),
"id": 1,
})
request = urllib2.Request(t.url, data)
for key in t.header:
request.add_header(key, t.header[key])
try:
result = urllib2.urlopen(request)
except URLError as e:
if hasattr(e, 'reason'):
print 'zabbix server is faile'
print 'Reason: ', e.reason
elif hasattr(e, 'code'):
print 'zabbix server not request.'
print 'Error code: ', e.code
else:
response = json.loads(result.read())
result.close()
hostid=[]
hostname=[]
for host in response['result']:
hostid.append(host['hostid'])
hostname.append(host['name'])
return hostid,hostname
if __name__ == "__main__":
a,b=hostid_get()
i=0
n=len(b)
for i in range(n):
print a[i],b[i]
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python3使用matplotlib繪圖時(shí),坐標(biāo)軸刻度不從X軸、y軸兩端開始
這篇文章主要介紹了Python3使用matplotlib繪圖時(shí),坐標(biāo)軸刻度不從X軸、y軸兩端開始問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
python+selenium+autoit實(shí)現(xiàn)文件上傳功能
這篇文章主要介紹了python+selenium+autoit實(shí)現(xiàn)文件上傳功能,需要的朋友可以參考下2017-08-08
pygame 精靈的行走及二段跳的實(shí)現(xiàn)方法(必看篇)
下面小編就為大家?guī)?lái)一篇pygame 精靈的行走及二段跳的實(shí)現(xiàn)方法(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-07-07
pycharm 如何取消連按兩下shift出現(xiàn)的全局搜索
這篇文章主要介紹了pycharm 如何取消連按兩下shift出現(xiàn)的全局搜索?下面小編就為大家介紹一下解決方法,還等什么?一起跟隨小編過來(lái)看看吧2021-01-01
TensorFlow平臺(tái)下Python實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)
這篇文章主要為大家詳細(xì)介紹了TensorFlow平臺(tái)下Python實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03

