Ad Widget

Collapse

Templates and Host Macros

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bforpc
    Junior Member
    • Jan 2024
    • 15

    #1

    Templates and Host Macros

    Hi,

    we have a running zabbix with lot of hosts and so on.
    What i dont understand:
    If i create a template Item with an macro variable,it will get the variable data from the host later. So far, so good.

    But if i want to execute the same template item on the host, but only with another macro variable, this is not possible?
    Example:

    Template-> check whatever {$PARAM}
    Host->Macro->{$PARAM} == "foo"
    now i want the same (template item-) check for the same host but with
    Host->Macro->{$PARAM} == "bar"

    Bfo
  • dimir
    Zabbix developer
    • Apr 2011
    • 1080

    #2
    Of course it will. When a template is attached to the host all the template things are actually copied and the reference is saved.

    The macros are substituted in hierarchy. When Zabbix faces macro it will look for it's value in the following order:
    1. host macro (the one the item belongs to, ignoring the template)
    2. if not found - template macro (if the item comes from template)
    3. if not found - global macro (Administration -> Macros)
    HTH

    Comment

    • bforpc
      Junior Member
      • Jan 2024
      • 15

      #3
      Hi dimir,

      thanks for you answer. All of them is understand. What i mean is:
      I want to use "one" Template Item on one host multiple times, but with different Variables (macros).

      From a programmer's point of view, I would solve this by creating a macro that is an "array", but that is probably not intended.
      For example, I want to create macros "{$VAR1}", "{$VAR2}", "{$VAR3}" ...
      and in this example use a single template item, which I add to the host 3 times, each with one of the "{$VAR[]}"​

      Bfo

      Comment

      • dimir
        Zabbix developer
        • Apr 2011
        • 1080

        #4
        Sorry, I still don't follow. Could you describe one complete use case?

        Or, perhaps what you want is Context User Macros?

        Comment

        • bforpc
          Junior Member
          • Jan 2024
          • 15

          #5
          Dir dimir,

          Ok, very simple example:
          I have a template with an item, who runs a shell script with the parameter "foo" (this param is stored on the host as macro variable) and gets back 0|1 - this is easy and already working.
          On the same host, i need to use from the same template the same item, but with param "bar" (which should also be stored on the host as macro variable).
          And this i need 5-10 times with differnt macro variables, alwayys the same item template, but always with different param.
          Last but not least also each item should have a trigger.

          How can i do that?

          bfo

          Comment

          • ISiroshtan
            Senior Member
            • Nov 2019
            • 324

            #6
            Create a discovery that runs some script passing into it your array from macro (in any form that you will be able to process). In script just read the array and return it as JSON array, like [{"Name": "foo"}, {"Name": "bar"}] + add LLD Macro definition like "{#NAME} -> $.Name". Create an item prototype with how you have it right now and in key as param use {#Name}. Now Zabbix will automatically create/delete items based on your macro array

            Not sure it's the best way, and not tested it out as dont have my lab handy, but should be doable like described above with some minor tweaks
            Last edited by ISiroshtan; 12-02-2024, 19:55.

            Comment

            • bforpc
              Junior Member
              • Jan 2024
              • 15

              #7
              Hi ISiroshtan,

              I'm new in zabbix with discovery "things". Can you please explain in more detail, how to do that?
              The (bash-) scripting and returning json data should be no problem.

              Bfo

              Comment

              • ISiroshtan
                Senior Member
                • Nov 2019
                • 324

                #8
                You create an LLD discovery (doc), the type of discovery you set to external script. The logic of key is same as when you execute external script in item, as param/argument you pass your Macro containing array. As result script should return JSON array (doc section of how it's expected). Also if you using normal names in your JSON (like "Name" and not "{#NAME}") - configure mapping of JSON elements to LLD Macro (section in doc).

                Then create an Item prototype (doc) of external_check type pointing to your original script and having {#NAME} as argument/param. it's effectively a template that will be used to create new Item for each element of JSON array returned to Discovery.
                Then in similar way create a trigger prototype (if alerting is needed) (doc)

                Also I would use the {#NAME} LLD macro somewhere in in both itemprototype and triggerprotorype names to be able to distinguish them from each other not only by key.

                Sorry if I'm not the best at explaining things, I do hope it gives you enough information to form a general idea...

                Comment

                • bforpc
                  Junior Member
                  • Jan 2024
                  • 15

                  #9
                  Dear ISiroshtan ... this sound like "hard stuff" ... (i never ever had to do with discover rules and LLD and so on). Everything was configured by hand - step by step.
                  I try it in the next days, but unshure if it will be successfull ;-)
                  Thank you for your message.

                  Bfo

                  Comment

                  Working...