G
G
Galdar Turin2018-12-24 15:23:24
PHP
Galdar Turin, 2018-12-24 15:23:24

Zabbix. How to display data from JSON by key?

There is a key ( Trunk ) that stores the JSON output:

JSON
{
  "data": [{
    "{#TRUNKNAME}": "name1",
    "{#LINESNOW}": "15",
    "{#LINESMAX}": "32"
  }, {
    "{#TRUNKNAME}": "name2",
    "{#LINESNOW}": "10",
    "{#LINESMAX}": "32"
  }, {
    "{#TRUNKNAME}": "name3",
    "{#LINESNOW}": "25",
    "{#LINESMAX}": "100"
  }]
}


In the output, macros were immediately indicated ( {#TRUNKNAME} {#LINESNOW} {#LINESMAX} ), as well as macros were indicated in the "TEMPLATE" in which it is located ( Data element - Trunk ). I wanted values ​​to be displayed
when writing a macro
: LAST DATA tab - trunk[{#TRUNKNAME}{#LINESNOW}{#LINESMAX}] = Output - name1 15 32 .
I realized that it doesn't work that way. I need to display data in the "LAST VALUE" field. So comrades, help me, I can’t figure it out, I’ve already read a bunch of answers on the forum, I’ve been dripping in the Zabbix documentation. I can’t understand how to correctly form the script so that I get such a request in the "DATA ELEMENTS" with such an answer in the "LAST VALUE" field (example in pictures from iostat):
ZABBIX - latest data
5c20cf3f712ba596625804.png

How I'm trying to do it:
Data element - trunk[name1,linesnow,linesmax]
Last value - name1 15 32
...
Here's how I can form a JSON request for such a "DATA ELEMENT" or maybe it's something else, suddenly I'm reading it wrong or the solution completely different ...
If not difficult, describe in detail.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Galdar Turin, 2018-12-24
@Galdar

I solved my own question...

Everything turned out to be easier than I thought. Created a trunk
key on Zabbix agent with JSON code:
JSON
$array = [
 "data" => 
 [
  "name1" => [
   "linesnow" => "15",
   "linesmax" => "32"
  ]
 ]
];

Created a "TEMPLATE" for convenience, in which I created "DATA ELEMENTS"
Gave them unique names and keys.
Then I went to "DATA ELEMENT" => "PRE-PROCESSING". Selected JSON "Preprocessing Step" and followed the JSON Path instruction in the Zabbix documentation:
Извлечение значения или фрагмента с JSON данных с использованием простейшего наблора функций JSONPath функционала.
Примеры:
$.document.item.value извлечёт 10 с {"document":{"item":{"value": 10}}}
$.document.item извлечёт {"value": 10} с {"document":{"item":{"value": 10}}}
$['a document'].item.value извлечёт 10 с {"a document":{"item":{"value": 10}}}
$.document.items[1].value извлечёт 20 с {"document":{"items":[{"value": 10}, {"value": 20}]}}
Обратите внимание, что поддерживаются только прямые пути к одиночным объектам в обозначении точкой или квадратной скобкой.
В JSONPath обозначении точкой ($.a.b.c) можно использовать только буквенно-цифровые символы + символ подчёркивания . Если имя объекта JSON содержит другие символы, необходимо использовать обозначение квадратной скобкой ($['a']['b']['c']). Допускается смешивание обозначений ($.a['b'].c)
Извлечение нескольких значений не поддерживается.
Поддерживается начиная с 3.4.0.
Preprocessing
5c20f847ed346290058332.png
I hope thanks to this answer, someone will save their time!

M
Maxim Grishin, 2018-12-24
@vesper-bot

In general, zabbix is ​​optimized for numeric data, although it can work with strings as well. This JSON is more like optimized for discovery, the {#MACRO} macros are for populating data item prototypes, not data per se. I recommend looking in your API for the ability to extract the required linesnow and linesmax by the name or ID of the trunk, and create element prototypes based on the macro, the names and keys of which will contain the values ​​linesnow in one and linesmax in the other, and then a separate postprocessing (xs, by the way, how, in actually - zabbix can't concatenate strings! https://support.zabbix.com/browse/ZBXNEXT-1911the developers did not seem to understand the request, then someone suggested a user case that dealt with alerts in general, and the idea died out - you can try to push it) to collect the desired string from the elements.
You can parse JSON into components by pushing data into an element of the text type with a third-party script (or in 4.0 an HTTP agent appeared that allows you to parse JSON from an HTTP response), then create several dependent elements from it by concatenating the preprocessing operations of the result of this JSON, specifying JSON first Path, and then write triggers and so on.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question