Linux shell环境下Zabbix Api的使用
作者:zsx0728
今天小编就为大家分享一篇关于Linux shell环境下Zabbix Api的使用,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
在linux shell环境下直接调用就可以,根据官网所述:在访问Zabbix中的任何数据之前,你需要登录并获取身份验证令牌。这可以使用该 user.login 方法完成。
[root@localhost ~]# curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method":"user.login","params":{"user":"Admin","password":"zabbix"},"auth": null,"id":0}' http://192.168.149.129/zabbix/api_jsonrpc.php HTTP/1.1 200 OK Date: Fri, 26 Oct 2018 07:46:05 GMT Server: Apache/2.4.6 (CentOS) PHP/5.6.36 X-Powered-By: PHP/5.6.36 Access-Control-Allow-Origin: * Access-Control-Allow-Headers: Content-Type Access-Control-Allow-Methods: POST Access-Control-Max-Age: 1000 Content-Length: 68 Content-Type: application/json {"jsonrpc":"2.0","result":"77bfe44db3f5c016477110c5748ac3e1","id":0}
根据官网所述,返回了用户身份验证令牌"77bfe44db3f5c016477110c5748ac3e1",后面所有的api请求都需要带着它。
依葫芦画瓢又查询了一个,这次带着上面的身份验证令牌:
[root@localhost ~]# curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method":"host.get","params":{"output":["host"],"selectInterfaces":["ip"]},"auth": "77bfe44db3f5c016477110c5748ac3e1","id":4}' http://192.168.149.129/zabbix/api_jsonrpc.p hp HTTP/1.1 200 OK Date: Fri, 26 Oct 2018 08:17:23 GMT Server: Apache/2.4.6 (CentOS) PHP/5.6.36 X-Powered-By: PHP/5.6.36 Access-Control-Allow-Origin: * Access-Control-Allow-Headers: Content-Type Access-Control-Allow-Methods: POST Access-Control-Max-Age: 1000 Content-Length: 111 Content-Type: application/json {"jsonrpc":"2.0","result":[{"hostid":"10084","host":"Zabbix server","interfaces":[{"ip":"127.0.0.1"}]}],"id":4}
返回了主机名Zabbix server,主机Ip 127.0.0.1
实际上,API怎么用还是要以官网文档为准,比如apiinfo.version,官网写的很清楚:此方法可用于未经身份验证的用户,必须在发送 JSON-RPC 请求中不加“auth”参数的情况下调用。
示例如下:
[root@localhost ~]# curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method":"apiinfo.version","params":[],"id":5}' http://192.168.149.129/zabbix/api_ jsonrpc.php HTTP/1.1 200 OK Date: Fri, 26 Oct 2018 08:40:17 GMT Server: Apache/2.4.6 (CentOS) PHP/5.6.36 X-Powered-By: PHP/5.6.36 Access-Control-Allow-Origin: * Access-Control-Allow-Headers: Content-Type Access-Control-Allow-Methods: POST Access-Control-Max-Age: 1000 Content-Length: 42 Content-Type: application/json {"jsonrpc":"2.0","result":"3.4.14","id":5}
显示目标主机的Zabbix API 版本是3.4.14
附官网链接:
https://www.zabbix.com/documentation/3.4/zh/manual/api
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接