Ad Widget

Collapse

How create template for get data from JSON

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Viewer
    Senior Member
    • Oct 2012
    • 131

    #1

    How create template for get data from JSON

    Hello, friends,
    I created bash script for get all needed data in one request.
    Data example:
    Code:
    [
    { "{#NAME}":"cat","{#TAIL}":"Y","{#AGE}":"12" },
    { "{#NAME}":"dog","{#TAIL}":"Y","{#AGE}":"44" },
    ...
    ]
    I use Userparam key, and I can get this data to Zabbix.

    How should I create template??
    it should be item prototypes from ITEM or from discovery rule?
    Please , help, I need example
    Last edited by Viewer; 22-02-2025, 09:51.
  • MRedbourne
    Senior Member
    • Feb 2023
    • 103

    #2
    Hey Viewer:

    I discuss some of this in one of my previous posts. See here. At a high level though this is a multi-step process.

    1. You need a (parent) item that calls your User Parameter key.
    2. Your data needs to be in a key-value statement for JSON.
    3. Discovery Rule (LLD Macro)
    4. Item Prototype(s)

    In your example, your JSON is misconfigured. Do not put the LLD macros in the source JSON, put the name or key of the value you're specifying. Eg:
    Code:
    [
      {
        "Type": "cat",
        "hasTail": "Y",
        "age": 12
      },
      {
        "Type": "dog",
        "hasTail": "Y",
        "age": 44
      }
    ]
    Let's say this information is in the UserParameter called "animals".

    1. Create a regular item in your Zabbix template with the following configuration:
    • Name: <Your Choice>
    • Type: Zabbix Agent (Active)
    • Key: animals
    • Type of Information: Text
    • Update Interval <your choice>
    2. Create a discovery rule with the following configuration:
    • Name: <Your choice>
    • Type: Dependent Item
    • Key: animals.discovery
    • Master Item: $ItemInStep1
    • LLD Macros:
      • {#TYPE} -> $.type
    3. Create item prototypes for any item you wish to track, per type of animal:
    • Name: {#TYPE}: Has Tail
    • Type: Dependent Item
    • Key: animals.discovery.hasTail[{#TYPE}]
    • Information: Text
    • Master Item: $ItemInStep1
    • Pre-processing
      • JSONPath -> $..[?(@.Type == "{#TYPE}")].hasTail.first()
      • Trim -> []"
    You may need to play with the JSONPaths a little. That should create dynamic items if you ever add more types of animals into your monitoring, as long as the JSONPaths remain consistent.

    Comment

    • Viewer
      Senior Member
      • Oct 2012
      • 131

      #3
      Originally posted by MRedbourne
      Hey Viewer:

      In your example, your JSON is misconfigured. Do not put the LLD macros in the source JSON, put the name or key of the value you're specifying.
      Thank you for your help! My main mistake - wrong JSON format. I shaked LLD and JSON.

      Comment

      • cyber
        Senior Member
        Zabbix Certified SpecialistZabbix Certified Professional
        • Dec 2006
        • 4807

        #4
        LLD macros in source data is OK. All built-in discovery items return such data. Then you do not need to define LLD macros by yourself..

        Comment

        • yurtesen
          Senior Member
          • Aug 2008
          • 130

          #5
          cyber I have a question. Assuming the JSON data is like the OP originally put:

          Code:
          [
          { "{#NAME}":"cat","{#TAIL}":"Y","{#AGE}":"12" },
          { "{#NAME}":"dog","{#TAIL}":"Y","{#AGE}":"44" },
          ...
          ]
          OK now we do not have to define LLD macros, but how would you setup the item prototypes? Wouldn't you end up with some JSONPATH like where you need to match weirdly? Eg. if we want to get AGE in this item prototype
          Code:
          @.{#NAME} == "{#NAME}" && @.{#TAIL} == "{#TAIL}"
          Yes, this example is perhaps not good as there can be many dogs with tail

          Comment

          • cyber
            Senior Member
            Zabbix Certified SpecialistZabbix Certified Professional
            • Dec 2006
            • 4807

            #6
            jsonpath has always been my archenemy..
            So you only want age of those dogs with tail? Maybe override is easier... skip those where {#TAIL} <> "y"

            Theres always an option, that I did not catch the question..:P

            Comment

            • troffasky
              Senior Member
              • Jul 2008
              • 567

              #7
              I have had success with getting AI to write my JSONpath queries for me.

              Comment

              Working...