我是靠谱客的博主 儒雅烤鸡,最近开发中收集的这篇文章主要介绍Curl读取Zabbix API的TrubleShooting,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一般curl可以通过POST直接读取zabbix API接口来执行脚本,进行批量配置和获取相关信息

 curl -i -X POST -H 'Content-Type:application/json' -d '{"jsonrpc":"2.0","method":"user.login","params":{"user":"admin","password":"xxxx"},"auth":null,"id":0}' http://10.40.x.x/zabbix/api_jsonrpc.php
HTTP/1.1 200 OK
Date: Fri, 09 Aug 2019 08:17:05 GMT
Server: Apache/2.4.29 (Ubuntu)
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":"b0535ba016be1f2a380ebf3096bca711","id":0}

当我们通过curl读取API发生失败时,需要了解其原因,便于排查问题,这时可以增加参数"-v",这样在执行读取时就会输出具体的过程,如下是读取成功,显示HTTP/1.1 200 OK,并得了 “result”:"07eb9198785e8f4c09028ddae6b41f2b

curl -i -v -X POST -H ‘Content-Type:application/json’ -d ‘{“jsonrpc”:“2.0”,“method”:“user.login”,“params”:{“user”:“admin”,“password”:“xxxx”},“auth”:null,“id”:0}’ http://10.40.x.x/zabbix/api_jsonrpc.php

Note: Unnecessary use of -X or --request, POST is already inferred.
*
Trying 10.40.x.x...
* TCP_NODELAY set
* Connected to 10.40.x.x (10.40.x.x) port 80 (#0)
> POST /zabbix/api_jsonrpc.php HTTP/1.1
> Host: 10.40.x.x
> User-Agent: curl/7.58.0
> Accept: */*
> Content-Type:application/json
> Content-Length: 107
>
* upload completely sent off: 107 out of 107 bytes
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Date: Fri, 09 Aug 2019 06:47:26 GMT
Date: Fri, 09 Aug 2019 06:47:26 GMT
< Server: Apache/2.4.29 (Ubuntu)
Server: Apache/2.4.29 (Ubuntu)
< Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
< Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Headers: Content-Type
< Access-Control-Allow-Methods: POST
Access-Control-Allow-Methods: POST
< Access-Control-Max-Age: 1000
Access-Control-Max-Age: 1000
< Content-Length: 68
Content-Length: 68
< Content-Type: application/json
Content-Type: application/json
<
* Connection #0 to host 10.40.x.x left intact
{"jsonrpc":"2.0","result":"07eb9198785e8f4c09028ddae6b41f2b","id":0}

如下为读取失败时,会得到提示HTTP/1.1 404 Not Found,最后也未得到result,而是Closing connection 0


curl -i -v -X POST -H 'Content-Type:application/json' -d '{"jsonrpc":"2.0","method":"user.login","params":{"user":"admin","password":"xxxx"},"auth":null,"id":0}' http://10.41.x.x/zabbix/api_jsonrpc.php
Note: Unnecessary use of -X or --request, POST is already inferred.
*
Trying 10.41.x.x...
* TCP_NODELAY set
* Connected to 10.41.x.x (10.41.x.x) port 80 (#0)
> POST /zabbix/api_jsonrpc.php HTTP/1.1
> Host: 10.40.x.x
> User-Agent: curl/7.58.0
> Accept: */*
> Content-Type:application/json
> Content-Length: 107
>
* upload completely sent off: 107 out of 107 bytes
< HTTP/1.1 404 Not Found
HTTP/1.1 404 Not Found
< Date: Fri, 09 Aug 2019 06:50:58 GMT
Date: Fri, 09 Aug 2019 06:50:58 GMT
< Server: Apache/2.2.15 (CentOS)
Server: Apache/2.2.15 (CentOS)
< Content-Length: 298
Content-Length: 298
< Connection: close
Connection: close
< Content-Type: text/html; charset=iso-8859-1
Content-Type: text/html; charset=iso-8859-1
<
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /zabbix/api_jsonrpc.php was not found on this server.</p>
<hr>
<address>Apache/2.2.15 (CentOS) Server at 10.41.x.x Port 80</address>
</body></html>
* Closing connection 0

最后

以上就是儒雅烤鸡为你收集整理的Curl读取Zabbix API的TrubleShooting的全部内容,希望文章能够帮你解决Curl读取Zabbix API的TrubleShooting所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部