Ad Widget

Collapse

Discovering hosts and attaching items from single REST API call

Collapse
This topic has been answered.
X
X
 
  • Time
  • Show
Clear All
new posts
  • -max
    Junior Member
    • Dec 2024
    • 5

    #1

    Discovering hosts and attaching items from single REST API call

    What I got already:

    I have a template attached to few hosts with an item whose value is a json document representing an array of hosts with name, properties and sub elements like this like this:
    Code:
    [
        {
            "Name": "A",
            "Available": true,
            "Items": [
                {
                    "ItemName": "z",
                    "Active": true
                },
                {
                    "ItemName": "y",
                    "Active": true
                }
            ]
        },
        {
            "Name": "B",
            "Available": false,
            "Items": [
                {
                    "ItemName": "x",
                    "Active": true
                }
            ]
        },
        {
            "Name": "C",
            "Available": false,
            "Items": [
                {
                    "ItemName": "w",
                    "Active": false
                },
                {
                    "ItemName": "u",
                    "Active": false
                }
            ]
        }
    ]
    I can retrieve the json just fine from zabbix, and I've added a discovery rule with host prototype to generate hosts (one host per item in the array) with their own template (different from the parent host which caused the discovery).

    To achieve this, I have added a dependent item as discovery rule with a preprocessing javascript snippet which extracts the fields into macros:

    Code:
    output = JSON.parse(value).map(function(item){
        return {
            "{#NAME}": item.Name,
            "{#AVAILABLE}": item.Available
        }})
    return JSON.stringify({"data": output})
    And the host prototype simply has {#NAME} as its host name. The hosts are properly generated which is great, but so far they have no item configured.

    What I'm trying to achieve:

    I want my generated host to have an item "Available" whose value is {#AVAILABLE} from the json (without having to query a second time the REST endpoint ideally).
    I can add items in the template assigned to the host prototype but I don't know how to refer to that {#AVAILABLE} macro.

    Is there a non convoluted way to achieve this?
    Ultimately the algorithm should be:
    1) query REST endpoint
    2) create any missing host based on host prototype
    3) for each host, update items value based on content of json response
  • Answer selected by -max at 16-12-2024, 14:08.
    -max
    Junior Member
    • Dec 2024
    • 5

    I managed to find a way

    1) define macros in the host (key = host macro with $, value = LLD macro with #)
    2) refer to them in items use calculated item with formula to refer to the host level macro (with $)

    Comment

    • Brambo
      Senior Member
      • Jul 2023
      • 245

      #2
      Item values are set by a template or host item.
      I doesn't make sense to do this from host prototype.
      What you can do is make a template where you have an item which uses {HOST.HOST} or something simalar as macro in a jsonpath to gather that information.

      PS you don't have to make LLD macro in the json file, just use the build in LLD macro functionality of you LLD rule to create them.

      Comment

      • -max
        Junior Member
        • Dec 2024
        • 5

        #3
        Thanks for the insight.

        Originally posted by Brambo
        What you can do is make a template where you have an item which uses {HOST.HOST} or something simalar as macro in a jsonpath to gather that information.

        Could you expand on that? What item type should I be using for the item in the template?

        Comment

        • -max
          Junior Member
          • Dec 2024
          • 5

          #4
          It does not look like Zabbix is accepting any macros referring to the JSON object I extracted in the main item that performs the HTTP GET on the endpoint ; that item lives in a different template from the template that is assigned to discovered hosts.

          Comment

          • Brambo
            Senior Member
            • Jul 2023
            • 245

            #5
            Originally posted by -max
            Thanks for the insight.




            Could you expand on that? What item type should I be using for the item in the template?
            In simple terms:
            Host prototype hostname is filled in by LLDmacro,
            You assign template on the host prototype
            In that template your data gathering item uses {HOST.HOST} macro (if applicable) as part of path where to gather info.

            Comment

            • -max
              Junior Member
              • Dec 2024
              • 5

              #6
              Originally posted by Brambo

              In simple terms:
              Host prototype hostname is filled in by LLDmacro,
              You assign template on the host prototype
              In that template your data gathering item uses {HOST.HOST} macro (if applicable) as part of path where to gather info.
              Thanks for the follow up Brambo.
              I guess I still struggle to understand how to configure such item in the template assigned to the host prototype. How would you configure it?

              Click image for larger version

Name:	image.png
Views:	207
Size:	48.3 KB
ID:	495760

              Comment

              • -max
                Junior Member
                • Dec 2024
                • 5

                #7
                I managed to find a way

                1) define macros in the host (key = host macro with $, value = LLD macro with #)
                2) refer to them in items use calculated item with formula to refer to the host level macro (with $)

                Comment

                Working...