Ad Widget

Collapse

How to use a value from JSON in LLD Macro/Variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MMol
    Junior Member
    • Nov 2021
    • 7

    #1

    How to use a value from JSON in LLD Macro/Variable

    We are trying to monitor a few things related to Microsoft Graph. For this we need to POST for a Bearer token first. This works in an item:

    https://login.microsoftonline.com/{$TENANT.ID}/oauth2/v2.0/token

    This returns the following:

    Code:
    {"token_type":"Bearer","expires_in":3599,"ext_expi res_in":3599,"access_token":"TOKEN_STRING_HERE"}
    Then we created a discovery rule with a preprocessing rule:

    Code:
    JSONPath : $.access_token
    This returns the full Bearer token as value for this, but how can we use this as a variable/macro?
    As the LLD Macro function needs a JSON path this doesn't work, there is just a string which is the token.

    If we remove the preprocessing rule, and create a LLD Macro:

    Code:
    {#BEARER} : $.access_token
    Then Zabbix returns
    Code:
    Cannot find the "data" array in the received JSON object.
    When discovering SNMP, we can use {#SNMPVALUE} which returns the value of the discovery. Can it be as simple as that for HTTP agent?

    Any solution would be appreciated!
  • Hamardaban
    Senior Member
    Zabbix Certified SpecialistZabbix Certified Professional
    • May 2019
    • 2713

    #2
    There is a special items type for such requests:
    https://www.zabbix.com/documentation...emtypes/script
    In it, you can do a multi-pass, step-by-step receiving of the necessary data using JS.

    Comment

    • MMol
      Junior Member
      • Nov 2021
      • 7

      #3
      Originally posted by Hamardaban
      There is a special items type for such requests:
      https://www.zabbix.com/documentation...emtypes/script
      In it, you can do a multi-pass, step-by-step receiving of the necessary data using JS.
      If I read this correctly, this uses variables/parameter but does not create them from the result of the script or API call.

      Comment

      • Hamardaban
        Senior Member
        Zabbix Certified SpecialistZabbix Certified Professional
        • May 2019
        • 2713

        #4
        Using this element, you get a token at the first step and use it in the next request. After that, you can do anything with the received data. In particular, if the result is json -use it further in LD.

        Comment

        • MMol
          Junior Member
          • Nov 2021
          • 7

          #5
          Originally posted by Hamardaban
          Using this element, you get a token at the first step and use it in the next request. After that, you can do anything with the received data. In particular, if the result is json -use it further in LD.
          Alright, so as how I understand you are suggesting the script gets the token AND the JSON data in the same script. After that I can process the JSON data as I like?

          Is JavaScript the only supported language in type "Script"? Or can I also work with Python?

          Comment

        • MMol
          Junior Member
          • Nov 2021
          • 7

          #6
          Originally posted by Hamardaban
          There is a special items type for such requests:
          https://www.zabbix.com/documentation...emtypes/script
          In it, you can do a multi-pass, step-by-step receiving of the necessary data using JS.
          Do you know any examples? As the link you send only shows how to do HTTP GET, we also need POST. But when I try to Google they all use other methods like JS Fetch, jQuery or XHR. These do not seem to work inside Zabbix.

          I need a little help where to start on learning to use JavaScript.

          Comment


          • lptarik
            lptarik commented
            Editing a comment
            Hello did you solve your issue?
            Thanks

          • MMol
            MMol commented
            Editing a comment
            Solved it by using a script for an item, and sub items that discover from this data. Have a look at the documentation. It's actually pretty straight forward. I did not know any JS
        Working...