目前我的zabbix版本是3.4.
之前看书上的zabbix版本是2.2的。
这里的登录验证令牌是user.authenticate
目前3.4的版本使用的user.login的方式。
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.1.101/zabbix/api_jsonrpc.php
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 22 Jan 2019 09:03:46 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.6.34
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: POST
Access-Control-Max-Age: 1000
{"jsonrpc":"2.0","result":"bfc159bf9502255dab7fb5b60d32ff0b","id":0}
让我们仔细看看请求对象。它具有以下属性:
jsonrpc - API使用的JSON-RPC协议的版本; Zabbix API实现JSON-RPC版本2.0;
method - 调用的API方法;
params - 将被传递给API方法的参数;
id - 请求的任意标识符;
auth -用户认证令牌; 因为我们还没有一个,它的设置null。
使用这样的方式登录并获取身份验证令牌,方便后面我们需要做的检查。
下面这里我们引用我们刚刚获取的验证令牌
curl -i -X POST -H 'Content-Type:application/json' -d '{"jsonrpc": "2.0","method":"host.get","params":{"output":["hostid","host"],"selectInterfaces":["interfaceid","ip"]},"id":2,"auth":"bfc159bf9502255dab7fb5b60d32ff0b"}' http://192.168.1.101/zabbix/api_jsonrpc.php
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 22 Jan 2019 09:21:40 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.6.34
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: POST
Access-Control-Max-Age: 1000
{"jsonrpc":"2.0","result":[{"hostid":"10084","host":"Zabbix server","interfaces":[{"interfaceid":"1","ip":"127.0.0.1"}]},{"hostid":"10293","host":"192.168.1.114","interfaces":[{"interfaceid":"2","ip":"192.168.1.114"}]},{"hostid":"10294","host":"192.168.1.113","interfaces":[{"interfaceid":"3","ip":"192.168.1.113"}]},{"hostid":"10295","host":"192.168.1.116","interfaces":[{"interfaceid":"4","ip":"127.0.0.1"}]},{"hostid":"10299","host":"192.168.1.201","interfaces":[{"interfaceid":"6","ip":"192.168.1.201"}]},{"hostid":"10306","host":"192.168.1.202","interfaces":[{"interfaceid":"7","ip":"192.168.1.202"}]},{"hostid":"10309","host":"192.168.1.115","interfaces":[{"interfaceid":"8","ip":"192.168.1.115"}]},{"hostid":"10310","host":"192.168.1.118","interfaces":[{"interfaceid":"9","ip":"192.168.1.118"}]},{"hostid":"10312","host":"192.168.1.1","interfaces":[{"interfaceid":"10","ip":"192.168.1.1"}]},{"hostid":"10313","host":"192.168.1.121","interfaces":[{"interfaceid":"11","ip":"192.168.1.121"}]},{"hostid":"10314","host":"192.168.2.40","interfaces":[{"interfaceid":"12","ip":"192.168.2.40"
这里我们将会获得我们目前主机上的监控主机的ip。
可以看出来。method表示这次api的请求操作动作(方法)。params方法传入的参数,若无参数,则传入[ ]。id调用标识符,用于表示一次远程调试过程。
仅供参考