龙空技术网

zabbix 监控的API开发技巧

lisanmengmeng 104

前言:

此刻同学们对“php调用第三方api接口实例”大致比较着重,看官们都需要了解一些“php调用第三方api接口实例”的相关资讯。那么小编在网摘上搜集了一些关于“php调用第三方api接口实例””的相关文章,希望朋友们能喜欢,我们一起来了解一下吧!

一:数据流程

二:如何获取组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

}'

四: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 = ";

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 = ";

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 = ";

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

标签: #php调用第三方api接口实例