Ad Widget

Collapse

Utilizing JSONPath to setup an LLD Macros

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • high-t
    Member
    • Dec 2014
    • 68

    #1

    Utilizing JSONPath to setup an LLD Macros

    Hi.

    Zabbix Version: 5.2.4.

    I'm querying AWS CLI to discover items.
    My item is as follows:
    Code:
    system.run["aws autoscaling describe-tags --region {$ZABBIX_ASG_REGION} --query 'Tags[?ResourceType==`auto-scaling-group` && Key==`{$ZABBIX_ASG_TAG}` && Value==`{$ZABBIX_ASG_VALUE}`]'.ResourceId --output json"]
    with a JSONPath of
    Code:
    $.[*]
    And it works. This is the reply I'm getting
    Code:
    [
    "name1-asg",
    "name2-asg"
    ]
    But when I set a Discovery Rule with the same key, same preprocessing,

    Click image for larger version  Name:	prep.png Views:	0 Size:	3.5 KB ID:	419394

    and the following LLD Macro settings:

    Click image for larger version  Name:	lld.png Views:	0 Size:	4.5 KB ID:	419395

    I'm getting nothing, and no Item Prototype is resolved to an item.

    What am I doing wrong?
    Last edited by high-t; 24-02-2021, 18:46.
  • high-t
    Member
    • Dec 2014
    • 68

    #2
    vso Highly appreciate if you can you kindly shed some light on this issue.
    Thank you!
    Last edited by high-t; 25-02-2021, 03:37.

    Comment

    • vso
      Zabbix developer
      • Aug 2016
      • 190

      #3
      Hi, maybe this can help:

      The problem is that LLD Processed each array separately, for example first takes:
      "name1-asg"

      But unfortunately currently JSONPath implementation does not allow selecting such value, could create feature request for that.

      Comment

      • high-t
        Member
        • Dec 2014
        • 68

        #4
        Originally posted by vso
        Hi, maybe this can help:

        The problem is that LLD Processed each array separately, for example first takes:
        "name1-asg"

        But unfortunately currently JSONPath implementation does not allow selecting such value, could create feature request for that.
        OK, Great input!
        So, If I understand your reply correctly, would you say the the solution is either to construct the JSON as such that each item will be located in its own array, or alternatively, include the LLD Macro name in the JSON?

        Comment

        • vso
          Zabbix developer
          • Aug 2016
          • 190

          #5
          JSON need to be in following format if possible:
          Code:
          [
          { "name":"name1-asg"},
          { "name":"name2-asg" }
          ]
          Then LLD worker will iterate over array, for first entry it will take:
          Code:
          { "name":"name1-asg"}
          And JSONPath $.name can be used with it, unfortunately currently not possible to work with just "name1-asg" but this could be implemented in future

          Comment

          • high-t
            Member
            • Dec 2014
            • 68

            #6
            Originally posted by vso
            JSON need to be in following format if possible:
            Code:
            [
            { "name":"name1-asg"},
            { "name":"name2-asg" }
            ]
            Then LLD worker will iterate over array, for first entry it will take:
            Code:
            { "name":"name1-asg"}
            And JSONPath $.name can be used with it, unfortunately currently not possible to work with just "name1-asg" but this could be implemented in future
            So just to make sure I understood you correctly: With your example json will zabbix create the items per each "name" value?

            Comment

            • vso
              Zabbix developer
              • Aug 2016
              • 190

              #7
              Originally posted by high-t

              So just to make sure I understood you correctly: With your example json will zabbix create the items per each "name" value?
              Yes, it will create item for each entry of array

              Comment

              • high-t
                Member
                • Dec 2014
                • 68

                #8
                Originally posted by vso

                Yes, it will create item for each entry of array
                Well, in this case I think I'll preprocess the data coming from the API call, using JavaScript to form a JSON document out of the returning data.
                Thank you very much for your help. As always, your input is pin point clear. Highly appreciated!

                Comment

                • high-t
                  Member
                  • Dec 2014
                  • 68

                  #9
                  So, just to wrap things up, here is the now-working solution:

                  I've amended the preprocessing of this discovery rule to the following:

                  Click image for larger version

Name:	prep.png
Views:	9748
Size:	7.6 KB
ID:	419538

                  with the JavaScript being the following:
                  Code:
                  //Convert array into a discovery-ready JSON object
                  var array = JSON.parse(value)
                  var len = array.length;
                  var x = 0
                  output = "{ \"data\" :["
                  for (; x < len - 1; x++){
                  output += "{\"asgname\": \"" + array[x] + "\"},"
                  }
                  output += "{\"asgname\": \"" + array[x] + "\"}"
                  output += "]}"
                  return output
                  which gives me the following result:
                  [
                  {"asgname": "name1"},
                  {"asgname": "name2"}
                  ]

                  I then set the LLD Macro:

                  Click image for larger version

Name:	lld.png
Views:	9731
Size:	4.6 KB
ID:	419539

                  Following the above, I'm now able to create a prototype objects.

                  Once again: Thank you vso for your help here!

                  Comment

                  • ballisticxlr
                    Junior Member
                    • Apr 2022
                    • 2

                    #10
                    Apologies for resurrecting such an old post but this dealing with JSON stuff is all a bit new for me. It appears like the message here is, yes JSON works but only if there is no serialzation.

                    So assuming that I begin with (pardon the goofy indenting, the forum doesn't seem to handle copy/paste of JSON cleanly)
                    Code:
                    {
                    "firstName": "John",
                    "lastName": "Smith",
                    "isAlive": true,
                    "age": 27,
                    "address": {
                    "streetAddress": "21 2nd Street",
                    "city": "New York",
                    "state": "NY",
                    "postalCode": "10021-3100"
                    },
                         "phoneNumbers": [
                         {
                         "type": "home",
                         "number": "212 555-1234"
                         },
                         {
                         "type": "office",
                         "number": "646 555-4567"
                         }
                    ],
                    "children": [
                    "Catherine",
                    "Thomas",
                    "Trevor"
                    ],
                    "spouse": null
                    }​
                    I would need to do something more like the following, essentially stripping the input JSON down to a json formatted list of key:value pairsbut one which does not include items of the type category:key:value:
                    Code:
                    [
                    {"firstName": "John"},
                    {"lastName": "Smith"},
                    {"isAlive": true},
                    {"age": 27},
                    {"address": "21 2nd Street"},
                    {"city": "New York"},
                    {"state": "NY"},
                    {"postalCode": "10021-3100"},
                    {"Home number": "212 555-1234"},
                    {"Office number": "646 555-4567"},
                    {"Child 1":"Catherine"},
                    {"Child 1":"Thomas"},
                    {"Child 1":"Trevor"},
                    {"spouse": null}
                    ]
                    I'm working with data that must for tedious reasons come out with more than a single level of nesting, so far it looks like 4 levels to begin with:
                    Code:
                    /subscriptions/subscriptionID/resourceGroups/resourceGroupName/providers/provider_name/item_name/item_value
                    I assume that this means that for each context, which would be:
                    Code:
                    /subscriptions/subscriptionID/resourceGroups/resourceGroupName/providers/provider_name
                    So in the end, I could create LLDP rules for each of the possible contexts by setting the JSON path to something like the following in the preprocessing rules:
                    Code:
                    JSONPath = $.subscriptions.subscriptionID.resourceGroups.resourceGroupName.providers.provider_name
                    LMK how badly I've misunderstood if I have indeed misunderstood.

                    Comment

                    Working...