一:數據流程
![](https://news.xinpengboligang.com/upload/keji/23d7f492618c14caa7299a92197ff5a8.jpeg)
二:如何獲取組ID
![](https://news.xinpengboligang.com/upload/keji/a7f56955d3a3d1c741074ce6ed4f4593.jpeg)
點擊任一模板查看組ID
group id
![](https://news.xinpengboligang.com/upload/keji/ab921eadc5b67b5e29823f06f2729c63.jpeg)
![](https://news.xinpengboligang.com/upload/keji/bbbfeab86061339528cbc694c5f3f449.jpeg)
三:基於 Curl 的 Zabbix API 調用
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "shiye.meng",
"password": "xxx"
},
"id": 1
}' http://193.112.170.13/api_jsonrpc.php
四:api開發實例
1.獲取登錄密鑰
2.執行方法操作
1.獲取登錄密鑰
#!/usr/bin/env python2.7
#coding=utf8
import json
import urllib2
#base url and required header
username = 'shiye.meng'
password = 'xxx'
api_url = "http://150.109.46.176/api_jsonrpc.php"
header = {"Content-Type":"application/json"}
#username and password
data = json.dumps(
{
"jsonrpc":"2.0",
"method":"user.login",
"params":{
"user":username,
"password":password
},
"id":0
})
#create object request
request=urllib2.Request(api_url,data)
for key in header:
request.add_header(key,header[key])
#auth and get authid
try:
result=urllib2.urlopen(request)
except URLError as e:
print "auth fail"
else:
response=json.loads(result.read())
result.close()
print "auth ok",response['result']
print response
2.執行方法操作
#!/usr/bin/env python2.7
#coding=utf8
import json
import urllib2
#base url and required header
username = 'shiye.meng'
password = 'xxx'
api_url = "http://150.109.46.176/api_jsonrpc.php"
header = {"Content-Type":"application/json"}
#username and password
data = json.dumps(
{
"jsonrpc":"2.0",
"method":"host.get",
"params":{
"output":["hostid","name"],
"filter":{"host":""}
},
"auth":"f2a7cb232fddb30e14fc544c6b09099e",
"id":1
})
#create object request
request=urllib2.Request(api_url,data)
for key in header:
request.add_header(key,header[key])
#auth and get authid
try:
result=urllib2.urlopen(request)
except URLError as e:
print "auth fail"
else:
response=json.loads(result.read())
result.close()
# print response
print "HostNumber:",len(response['result'])
for host in response['result']:
print "HostID:",host['hostid'],"HostName:",host['name']
![](https://news.xinpengboligang.com/upload/keji/6dbcc1a7d161d13f4acf5632d0650844.jpeg)
3.獲取維護列表
#!/usr/bin/env python2.7
#coding=utf8
import datetime
import time
import json
import urllib2
import sys
reload(sys)
sys.setdefaultencoding('utf8')
#time value
def timestamp_datetime(value):
format='%Y-%m-%d %H:%M:%S'
value = time.localtime(float(value))
dt = time.strftime(format, value)
return dt
def datetime_timestamp(dt):
time.strptime(dt, '%Y-%m-%d %H:%M:%S')
s = time.mktime(time.strptime(dt, '%Y-%m-%d %H:%M:%S'))
return int(s)
#base url and required header
username = 'shiye.meng'
password = 'xxx'
api_url = "http://150.109.46.176/api_jsonrpc.php"
header = {"Content-Type":"application/json"}
#username and password
data = json.dumps(
{
"jsonrpc":"2.0",
"method":"maintenance.get",
"params":{
"output":"extend",
},
"auth":"f2a7cb232fddb30e14fc544c6b09099e",
"id":2
})
#create object request
request=urllib2.Request(api_url,data)
for key in header:
request.add_header(key,header[key])
#auth and get authid
try:
result=urllib2.urlopen(request)
except URLError as e:
print "fail"
else:
response=json.loads(result.read())
result.close()
#print json.dumps(response,indent=4)
for mt in response['result']:
#print "StartTime:",mt['active_till'],"Name:",mt['name'].decode('utf-8').encode('gb2312')
print "StartTime:",timestamp_datetime(mt['active_till']),"EndTime:",timestamp_datetime(mt['active_since']),"Name:",mt['name']
![](https://news.xinpengboligang.com/upload/keji/3a0bdad8c8925fca6b48896cc0ddcf26.jpeg)