Linux shell环境下Zabbix Api的使用
程序员文章站
2022-07-09 16:49:55
在linux shell环境下直接调用就可以,根据官网所述:在访问zabbix中的任何数据之前,你需要登录并获取身份验证令牌。这可以使用该 user.login 方法完成。...
在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
附官网链接:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
推荐阅读