Ad Widget

Collapse

LLD Macros with JSON

Collapse
This topic has been answered.
X
X
 
  • Time
  • Show
Clear All
new posts
  • stnz
    Junior Member
    • Dec 2022
    • 4

    #1

    LLD Macros with JSON

    Hello,

    Been a day trying to get a simple discovery rule to work, creating a couple of items from a JSON file fetched by a HTTP Agent. I assume there's just something very simple and small wrong in the configuration, but.. Cannot figure out what.

    In short, I have an item which fetches the JSON file by HTTP and that works. I also have a discovery rule as a Dependant Item, and it only contains one LLD Macro: {#APNAME} -> $..name.

    The JSON looks like this.

    Code:
    [{
      "AP01 Upstairs": {
        "name": "AP01 Upstairs",
        "mac": "dfgdfg",
        "type": "uap",
        "intraclients": 2,
        "guestclients": 1,
        "clients": 3
      },
      "AP02 Lobby": {
        "name": "AP02 Lobby",
        "mac": "dfgdfg",
        "type": "uap",
        "intraclients": 2,
        "guestclients": 3,
        "clients": 5
      },
      "Total-AP": {
        "name": "Total-AP",
        "clients": 41,
        "intraclients": 19,
        "guestclients": 22,
        "mac": "none",
        "type": "none"
      }
    }]​
    If I test with a preprocessing rule (JSONPath, $..name) in the discovery rule I get expected (?) results, it returns ["AP01 Upstairs","AP02 Lobby","Total-AP"]. Naturally the preprocessing rule is not left there, just used for testing and the {#APNAME} LLD macro is the only thing left in the discovery rule.

    And now to the problem.. If I create a Item Prototype as Dependant Item the LLD Macro {#APNAME} is not resolved to invidual APs as expected, but to all three.

    So basically the configuration:
    Name: {{#APNAME} Clients
    Key: unifi.ap.clients[{#APNAME}]
    Type: Numeric
    Preprocessing: JSONPath -> $.items[?(@.name == "{#UNIFIAPNAME}")].clients (Not sure if correct now, regsub will be added to remove the extra brackets etc)

    Gives me item named:
    AP Names Discovery: AP User Count: {["AP01 Upstairs","AP02 Lobby","Total-AP"] Clients

    That doesn't naturally work.. And the keys etc are similarly named. So, it looks like the macro does not resolve to invidual items but to the list of items. Any ideas how t
  • Answer selected by stnz at 29-12-2022, 19:49.
    Markku
    Senior Member
    Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
    • Sep 2018
    • 1782

    This is my best shot at this

    In the master item:
    - set preprosessing rule: JSONPath = $[*].*
    ​(it flattens the JSON as you have "duplicate" levels there)

    In the discovery rule:
    - set LLD macro: {#APNAME} = $..name.first()
    (.first() removes the list notation)

    In the item prototype:
    - set first preprosessing rule: JSONPath = $.[?(@.name == "{#APNAME}")].first()
    - set second preprocessing rule: JSONPath = $.clients

    The preprocessing rule in the item prototype did not accept .clients so using them in steps seems to help.

    Markku

    Comment

    • stnz
      Junior Member
      • Dec 2022
      • 4

      #2
      Hmm, seems like I cannot edit the post and it was cut a bit short. Anyway, would be happy of any ideas how to get the LLD macro to actually resolve only to a single item. If I add .first() to the LLD macro path it works and resolves to the first item properly, but that doesn't really help as we would need them all.

      Comment

      • Markku
        Senior Member
        Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
        • Sep 2018
        • 1782

        #3
        When testing with https://jsonpath.com/:

        $..name returns ["AP01 Upstairs", "AP02 Lobby", "Total-AP"]​

        $.[?(@.name == "AP01 Upstairs")].clients returns [3]

        Thus, based on https://www.zabbix.com/documentation..._functionality, when your LLD macro {#APNAME} is populated from the list provided by $..name, then the item prototype should use $.[?(@.name == "{#APNAME}")].clients.first() to get the number of clients connected.

        Markku
        Last edited by Markku; 29-12-2022, 17:24.

        Comment


        • Markku
          Markku commented
          Editing a comment
          ... but I guess something is still missing here
      • stnz
        Junior Member
        • Dec 2022
        • 4

        #4
        Originally posted by Markku
        When testing with https://jsonpath.com/:

        $..name returns ["AP01 Upstairs", "AP02 Lobby", "Total-AP"]​

        $.[?(@.name == "AP01 Upstairs")].clients returns [3]

        Thus, based on https://www.zabbix.com/documentation..._functionality, when your LLD macro {#APNAME} is populated from the list provided by $..name, then the item prototype should use $.[?(@.name == "{#APNAME}")].clients.first() to get the number of clients connected.

        Markku
        Yea.. In theory it should work like that and the JSONPaths look correct.

        Still, my {#APNAME} in the item prototype is resolving to ["AP01 Upstairs", "AP02 Lobby", "Total-AP"]​, not a single AP like expected. And that's kinda the problem..

        It's Zabbix 6.0.8 btw.
        Last edited by stnz; 29-12-2022, 17:45.

        Comment

        • Markku
          Senior Member
          Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
          • Sep 2018
          • 1782

          #5
          This is my best shot at this

          In the master item:
          - set preprosessing rule: JSONPath = $[*].*
          ​(it flattens the JSON as you have "duplicate" levels there)

          In the discovery rule:
          - set LLD macro: {#APNAME} = $..name.first()
          (.first() removes the list notation)

          In the item prototype:
          - set first preprosessing rule: JSONPath = $.[?(@.name == "{#APNAME}")].first()
          - set second preprocessing rule: JSONPath = $.clients

          The preprocessing rule in the item prototype did not accept .clients so using them in steps seems to help.

          Markku

          Comment

          • Markku
            Senior Member
            Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
            • Sep 2018
            • 1782

            #6
            Screenshots to prove this

            Click image for larger versionName:	image.pngViews:	0Size:	57.6 KBID:	456777

            Latest data:
            Click image for larger versionName:	image.pngViews:	0Size:	34.8 KBID:	456778

            Markku

            Comment

            • stnz
              Junior Member
              • Dec 2022
              • 4

              #7
              Oo, it actually seems to work. Thank you.

              Not sure what helped, but maybe the first preprocessing rule on the master item? The item preprocessing rule did not work with proposed two steps, but works fine like this with only one step:

              $.[?(@.name == "{#APNAME}")].clients.first()​

              Comment

              • Markku
                Senior Member
                Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
                • Sep 2018
                • 1782

                #8
                Oops I failed to say that I tested with 6.4.0beta5 only. Yeah, I might have missed to try some steps again after the master item changes.

                Markku

                Comment

                • fvoprsal
                  Junior Member
                  • Jan 2023
                  • 1

                  #9
                  Hi, I'm dealing with maybe a pretty similar problem. Could I ask you to post a template to Zabbix so I can derive your functional setup? Thank you very much

                  Comment

                  Working...