Using Zabbix to record JSON output from REST api? is it possible
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?
Solution 1:
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.