zabbix 監控的API開發技巧

2024年2月6日 19点热度 0人点赞

一:數據流程

二:如何獲取組ID

點擊任一模板查看組ID

group id

三:基於 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']

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']