前言:
以下内容根据zabbix 3.2官方文档总结:https://www.zabbix.com/documentation/3.2/manual/api
此文档只是简单的介绍API的基本使用,关于一些完整功能或高级功能将在后续的博文中展现
调用Zabbix api使用的是HTTP的POST请求方式
其中请求的头部为:
1
|
{
"Content-Type"
:
"application/json-rpc"
}
|
请求的URL为:
1
|
http:
/
/
zabbix.aek.com
/
api_jsonrpc.php
|
如果url中包含目录需要把目录写上,如http://zabbix.aek.com/zabbix/api_jsonrpc.php,最后就是请求的数据(data),比如获取Token,创建主机,获取主机ID,获取模板ID,获取组ID,删除主机等。
接下以以Python调用 API为例,例出用法案例:
测试连接是否成功
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #!/usr/bin/python 2 #coding:utf:8 3 4 import requests 5 import json 6 7 url = "http://zabbix.aek.com/zabbix/api_jsonrpc.php" 8 header = {"Content-Type": "application/json-rpc"} 9 data = {"jsonrpc":"2.0","method":"apiinfo.version","id":1,"auth":None,"params":{}} 10 request = requests.post(url=url, headers=header, data=json.dumps(data)) 11 print(request.content)
上面的data用来测试是否可连接成功,官方文档中auth的值为null,但在python中null用None表示,所以需要改成None。
成功后返回值大概如下:会显示zabbix版本
1 {"jsonrpc":"2.0","result":"3.2.3","id":1}
获取Token
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #!/usr/bin/python 2 #coding:utf:8 3 4 import requests 5 import json 6 7 url = "http://zabbix.aek.com/zabbix/api_jsonrpc.php" 8 header = {"Content-Type": "application/json-rpc"} 9 data = {"jsonrpc": "2.0", 10 "method": "user.login", 11 "params": { 12 "user": "Admin", 13 "password": "zabbix" 14 }, 15 "id": 1, 16 "auth": None 17 } 18 request = requests.post(url=url, headers=header, data=json.dumps(data)) 19 print(request.content)
成功后返回值如下:会返回一个Token
1 {"jsonrpc":"2.0","result":"391277f070a3ebc5b2afe9cf466cb74c","id":1}
最后以一个完整的Python脚本通过zabbix API获取所有主机的信息为例:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #!/usr/bin/python 2 #coding:utf:8 3 4 import requests 5 import json 6 7 class Zabbix: 8 9 def __init__(self, url, header, username, password): 10 11 self.url = url 12 self.header = header 13 self.username = username 14 self.password = password 15 16 def getToken(self): 17 #获取Token并返回字符Token字符串 18 19 data = {"jsonrpc": "2.0", 20 "method": "user.login", 21 "params": { 22 "user": self.username, 23 "password": self.password 24 }, 25 "id": 1, 26 "auth": None 27 } 28 token = requests.post(url=self.url, headers=self.header, data=json.dumps(data)) 29 return json.loads(token.content)["result"] 30 31 def getAllHost(self): 32 #获取所有主机信息 33 34 data = {"jsonrpc": "2.0", 35 "method": "host.get", 36 "params": { 37 "output": [ 38 "hostid", 39 "host", 40 ], 41 "selectGroups": "extend", 42 "selectInterfaces": [ 43 "interfaceid", 44 "ip" 45 ] 46 }, 47 "id": 2, 48 "auth": self.getToken() 49 } 50 hosts = requests.post(url=self.url, headers=self.header, data=json.dumps(data)) 51 return json.loads(hosts.content)["result"] 52 53 54 if __name__ == "__main__": 55 header = {"Content-Type": "application/json-rpc"} 56 url="http://zabbix.aek.com/zabbix/api_jsonrpc.php" 57 test = Zabbix(url=url, header=header, username="Admin", password="zabbix") 58 print(test.getAllHost())