Ad Widget

Collapse

Zabbix extract JSON data as item value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • info@davidstein.cz
    Junior Member
    • Jun 2023
    • 4

    #1

    Zabbix extract JSON data as item value

    Hello, I want to ask for help. I tried a lot, including the Chatgpt. Many tests but I am still not able to crack this simple problem.

    I have script on server what return JSON Array like this:
    Code:
    [{"{#CAP}": "AP-07-1", "{#NOC}": 4}, {"{#CAP}": "AP-12-1", "{#NOC}": 4}, {"{#CAP}": "AP-08-1", "{#NOC}": 5}, {"{#CAP}": "cap8", "{#NOC}": 10}, {"{#CAP}": "AP-10-1", "{#NOC}": 4}, {"{#CAP}": "AP-09-1",
    "{#NOC}": 5}, {"{#CAP}": "Office-AP01-1", "{#NOC}": 2}]
    I create the host and Item "Capsman data" as the key I use
    Code:
    system.run[python3 /home/zabbix/zabbix_capsman_interfaces.py {$CAPS_HOST} {$CAPS_USER} {$CAPS_PASS}]
    I created the discovery rule "Cap interface discovery" and set it as a Dependent item on the previously created Item. As a key I use
    Code:
    capsman.discovery[{#CAP}]
    but I am not able to find a solution to create item prototypes right.

    If I create the Item prototype with name: Cap clients for {#CAP} it create the Items with valid name but I do not know what set to type and key to fill the #NOC as the Item value.

    The result should be to create Item with name "Cap clients for {#CAP}" and the item values should be the value from {#NOC}

    Thanks for any help.
  • cyberphantom
    Member
    • Dec 2021
    • 67

    #2
    Hello!

    I think I understand. Here's what I did:

    First, I modified the JSON generated by the script, then I created the item that outputs the script JSON response. I'm not sure if you can do this, but instead of using macro keys like "{#CAP}", I changed it to:
    Code:
    [{"CAP": "AP-07-1", "NOC": 4}, {"CAP": "AP-12-1", "NOC": 4}, {"CAP": "AP-08-1", "NOC": 5}, {"CAP": "cap8", "NOC": 10}, {"CAP": "AP-10-1", "NOC": 4}, {"CAP": "AP-09-1", "NOC": 5}, {"CAP": "Office-AP01-1", "NOC": 2}]
    Then, I did the same thing you already did: I created a discovery rule dependent on the master item (which outputs the script response) with the key disc_json. I added two LLD macros: {#CAP} with the JSONPath expression $..CAP.first() and {#NOC} with $..NOC.first().

    Next, I created an item prototype dependent on the master item with the name {#CAP} and key cap.[{#CAP}], and added the following JSONPath preprocessing: $..[?(@.CAP=="{#CAP}")].NOC.first().

    So it works.

    I hope this is helpful!

    Comment

    • info@davidstein.cz
      Junior Member
      • Jun 2023
      • 4

      #3
      Originally posted by cyberphantom
      Hello!

      I think I understand. Here's what I did:

      First, I modified the JSON generated by the script, then I created the item that outputs the script JSON response. I'm not sure if you can do this, but instead of using macro keys like "{#CAP}", I changed it to:
      Code:
      [{"CAP": "AP-07-1", "NOC": 4}, {"CAP": "AP-12-1", "NOC": 4}, {"CAP": "AP-08-1", "NOC": 5}, {"CAP": "cap8", "NOC": 10}, {"CAP": "AP-10-1", "NOC": 4}, {"CAP": "AP-09-1", "NOC": 5}, {"CAP": "Office-AP01-1", "NOC": 2}]
      Then, I did the same thing you already did: I created a discovery rule dependent on the master item (which outputs the script response) with the key disc_json. I added two LLD macros: {#CAP} with the JSONPath expression $..CAP.first() and {#NOC} with $..NOC.first().

      Next, I created an item prototype dependent on the master item with the name {#CAP} and key cap.[{#CAP}], and added the following JSONPath preprocessing: $..[?(@.CAP=="{#CAP}")].NOC.first().

      So it works.

      I hope this is helpful!
      Thanks, I will try. As I read the documentation if there is the {#MACRO} format in the JSON the Zabbix should have decoded it as the macro. It works as I can define the item prototype with name "Cap clients for {#CAP}" and it creates the correct item
      Cap clients for AP-01-2. I am still missing the simple thing of how to "say" to Zabbix: this item prototype value is this value from JSON.

      My thinking is as I understand documentation:
      The discovery list takes the "some data" in this case the JSON Array of objects, and for every member of the array, it tries to create the item prototypes, triggers prototypes etc. I am jus totally confused how to correctly set the Item Prototype Type, Key.

      Comment

      • info@davidstein.cz
        Junior Member
        • Jun 2023
        • 4

        #4
        If someone is interested, I created a tutorial for monitoring Mikrotik Capsman connected clients. Thanks for the help, I hope someone will find this tutorial useful.
        Hello, I want to share my solution for monitoring connected clients to CapMan interfaces on Mikrotik in the Zabbix monitoring system. First, for the Zabbix server part, to get data from Mikrotik…

        Comment

        Working...