大网数据广告活动
服务器运维问答

Using Zabbix to record JSON output from REST api? is it possible

· 52CE 我爱测

I'm trying to use Zabbix (5.0) to capture the response from a REST API. If I paste the following into a webbrowser (or wget / curl etc) http://{username}:{password}@10.1.2.3:8443/api/metadata/ I get the response I expect: {"items…

问题原文

原帖链接 · IGGt · CC BY-SA 4.0

I'm trying to use Zabbix (5.0) to capture the response from a REST API.

If I paste the following into a webbrowser (or wget / curl etc)

http://{username}:{password}@10.1.2.3:8443/api/metadata/

I get the response I expect:

{"items":[{"name":"this_is_my_name"}]}

I am trying to capture the value of name into zabbix.

I have set up a web scenario against my template, and in the steps section I have:

Name: test_name
URL: http://{username}:{password}@10.1.2.3:8443/api/metadata/
Raw Data
Retrieve Mode: Body
Timeout: 15s
Required String: $.items:["name"]
Required Status Code: 200

Then looking at the latest data for it I see I have a response conde of 200, but an error of:

required pattern "$.items:["name"]" was not found on http://{username}:{password}@10.1.2.3:8443/api/metadata/

I have tried all manner of variations in the Required String field, and always the same result.

Have I missed something, or misunderstood something. Does Zabbix even do what I am trying to do?

已采纳解答

原帖链接 · Gerald Schneider · CC BY-SA 4.0

A web scenario can't evaluate JSON, it can only check plain text with a regular expression.

You could set Required string to this_is_my_name, but that would just check if the string occurs in the answer from the server, no matter where. Depending on the rest of your result and your use case this might work, or it might not.

If you want to get a specific field from the JSON you have to configure an item of the type HTTP agent:

Do NOT check Convert to JSON, it might be tempting, but it is meant to convert non-JSON answers to JSON. You already have JSON, so you don't need it.

Next, add a prepocessing step of the type JSONPath. The correct path for your example would be:

$.items[:1].name

The result from JSON will be ["this_is_my_name"], not just this_is_my_name, so you need the regular expression \["(.+)"\] to retrieve the plain value from the result.

You can test it directly:

Store your item as Text, and then you can add a trigger to it.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-11-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。