请求的方法参数

官方原文:

  • jsonrpc - the version of the JSON-RPC protocol used by the API; the Zabbix API implements JSON-RPC version 2.0;
  • method - the API method being called;
  • params - parameters that will be passed to the API method;
  • id - an arbitrary identifier of the request;
  • auth - a user authentication token; since we don’t have one yet, it’s set to null.
参数名称 值类型 说明 是否必须
jsonrpc str 接口版本
method str 请求方法
params json 请求方法参数
user str zabbix账号
password str zabbix密码
auth str 认证的key
id int 认证id

请求事例:

1
2
3
4
POST http://company.com/zabbix/api_jsonrpc.php HTTP/1.1
Content-Type: application/json-rpc

{"jsonrpc":"2.0","method":"apiinfo.version","id":1,"auth":null,"params":{}}

python获取token

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import json
import requests
import sys, argparse


class Zabbix_Api:
def __init__(self):
self.url = 'http://IP/zabbix/api_jsonrpc.php'
self.header = {"Content-Type": "application/json"}
self.id = 1
self.user="zabbix登录账号"
self.password="zabbix登录密码"

def json_obj(self,method,auth=True,params={}):
obj = {'jsonrpc': '2.0',
'method': method,
'params': params,
'auth': auth,
'id': self.id}
if not auth:
del obj["auth"]
return obj

def user_login(self):
data=self.json_obj(method="user.login",auth=False, params={"user": self.user, "password": self.password})
return json.loads(requests.post(url=self.url, headers=self.header, data=json.dumps(data)).text)["result"]

if __name__ == '__main__':
print(Zabbix_Api().user_login())

获取版本信息

1
2
3
4
5
6
7
def get_version(self):
data=self.json_obj(method="apiinfo.version",
params={
"output": []
},
auth=False)
return json.loads(requests.post(url=self.url, headers=self.header, data=json.dumps(data)).text)

host.get方法获取所有的主机ID

参数名称 值类型 说明 是否必须
jsonrpc str 接口版本
method str 请求方法
params json 请求方法参数
output array 输出格式
groupids str/array 主机组id
auth str 认证的key
id int 认证id

请求事例(参考上面类):

1
2
3
4
5
6
7
8
def get_host(self):
data=self.json_obj(method="host.get",
params={
"output": ["hostid", "name"]
},
auth=self.user_login())

return json.loads(requests.post(url=self.url, headers=self.header, data=json.dumps(data)).text)

itemsid.get方法获取单个主机下所有的监控项ID

参数名称 值类型 说明 是否必须
jsonrpc str 接口版本
method str 请求方法
params json 请求方法参数
output array 输出格式
groupids str/array 主机组id
auth str 认证的key
id int 认证id

请求事例:

1
2
3
4
5
6
7
8
9
10
11
def get_mem_total(self,hostid):
data=self.json_obj(method="item.get",
params={
"output": "extend",
"hostids": hostid,
"search": {
"key_": "vm.memory.size[total]"
}
},
auth=self.user_login())
return int(json.loads(requests.post(url=self.url, headers=self.header, data=json.dumps(data)).text)["result"][0]["lastvalue"])/1024/1024/1024

以上完整事例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

import json
import requests
import sys, argparse


class Zabbix_Api:
def __init__(self):
self.url = 'http://192.168.31.249/zabbix/api_jsonrpc.php'
self.header = {"Content-Type": "application/json"}
self.id = 1

def json_obj(self,method,auth=True,params={}):
obj = {'jsonrpc': '2.0',
'method': method,
'params': params,
'auth': auth,
'id': self.id}
if not auth:
del obj["auth"]
return obj

def user_login(self):
data=self.json_obj(method="user.login",auth=False, params={"user": "yunwei", "password": "yunwei"})
return json.loads(requests.post(url=self.url, headers=self.header, data=json.dumps(data)).text)["result"]

def get_host(self):
data=self.json_obj(method="host.get",
params={
"output": ["hostid", "name"]
},
auth=self.user_login())

return json.loads(requests.post(url=self.url, headers=self.header, data=json.dumps(data)).text)

def get_mem_total(self,hostid):
data=self.json_obj(method="item.get",
params={
"output": "extend",
"hostids": hostid,
"search": {
"key_": "vm.memory.size[total]"
}
},
auth=self.user_login())
return int(json.loads(requests.post(url=self.url, headers=self.header, data=json.dumps(data)).text)["result"][0]["lastvalue"])/1024/1024/1024


if __name__ == '__main__':
# for k in Zabbix_Api().get_host()["result"]:
# print(k["hostid"],k["name"],str(round(Zabbix_Api().get_mem(k["hostid"]),2))+"G")
print(Zabbix_Api().get_mem_total(10084))

以上只是简单举例,具体实践可参考官网文档(要永远相信官方文档是最好的文档):https://www.zabbix.com/documentation/3.0/manual/api

Comments

2018-07-29