「Zabbix」- Web API @20210402
当有大批量数据需要导入 Zabbix 时,或者需要管理 Zabbix 服务时,可以通过 Zabbix 的 WebAPI 进行进行批量操作,而不是手动添加。
通过接口操作 Zabbix 的基本流程
Zabbix Documentation 4.4 / 19. API
第一步、测试接口是否可用(可选)
该步骤是可选的,目的是测试接口是否可用:
# curl -s --header 'Content-Type: application/json-rpc' http://zabbix.example.com/api_jsonrpc.php | jq { "jsonrpc": "2.0", "error": { "code": -32600, "message": "Invalid Request.", "data": "The received JSON is not a valid JSON-RPC Request." }, "id": null }
第二步、用户登录,以获取凭证信息
如下为请求信息:
POST http://company.com/zabbix/api_jsonrpc.php HTTP/1.1 Content-Type: application/json-rpc { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "zabbix" }, "id": 1, "auth": null }
响应信息如下:
{ "jsonrpc": "2.0", "result": "0424bd59b807674191e7d77572075f33", "id": 1 }
第三步、使用凭证信息请求数据接口
这里我们使用主机接口作为示例,获取所有主机信息
如下为请求信息:
{ "jsonrpc": "2.0", "method": "host.get", "params": { "output": [ "hostid", "host" ], "selectInterfaces": [ "interfaceid", "ip" ] }, "id": 2, "auth": "0424bd59b807674191e7d77572075f33" }
响应信息如下:
{ "jsonrpc": "2.0", "result": [ { "hostid": "10084", "host": "Zabbix server", "interfaces": [ { "interfaceid": "1", "ip": "127.0.0.1" } ] } ], "id": 2 }
第四步、阅读官方文档,获取其他数据
阅读官方 Zabbix Documentation 4.4 / Method reference 以获取其他数据。
相关链接
批量导入主机
相关文章
「Zabbix」- Number of CPUs [no data](未解决)
「Zabbix 3.2」- 使用源码安装
「Zabbix」- 中文字体“乱码”(无中文字体)
「Zabbix」- 基本概念
「Zabbix」- Debain 8.2 and Zabbix 4.0
「Zabbix」- please increase CacheSize configuration parameter
「Zabbix」- 安装(CentOS)
参考文献
Zabbix Documentation 4.4 / 19. API