Ad Widget

Collapse

Getting value of context macro assigned to host

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stevene1900
    Junior Member
    • Jun 2018
    • 9

    #1

    Getting value of context macro assigned to host

    Context macro can be assigned at global level, templates and at host. There are inheritance rules that determine which value is assigned to host for context macro, if it is defined at multiple places.

    Does anyone have a method to figure out what a particular macro is resolved to at the host level based on the inheritance rules?

    I'd like to get his using a scirpt: usermacro.get method in API is not sufficient because it will only provide what is assigned at host level and not what is inherited. It would be good to be able to pull the "Inherited and host macros" using the api.

    Thanks.
  • CesarAcevedo
    Junior Member
    • Oct 2023
    • 1

    #2
    Did you find a way to get it? (for usermacro.get in inherited macros)

    Comment


    • stevene1900
      stevene1900 commented
      Editing a comment
      I ended up creating a multi step process to get macro I am interested in, which includes inherited macros. It is a bit convoluted. This is the approach:

      Basically, create a sorted array based on level of the macro and name of the macro.

      1. Get all host level macros and assign level of "0"
      2. Get all macros from all assigned templates and assign level equal to ("1-" + templateid).
      3. Get all global macros and assign level of "2".

      So now this is all sorted based on level.
      --------the python function for the sort: json_data.sort(key=lambda x: [int(y) for y in x['overall_level'].split('-')])

      I then have a function get_macro(macro_name). It processes from the very top of list where first level is 0...the function iterates through each macro until it finds a name match. On the first name match, that macro is then returned.

      The thing about this is it is tailored to my environment because we only use first level templates, although it can probably be modified to work with second, third, ... levels.

      These are the rules on how Zabbix resolves the macros, which I think my solution addresses.

      -host level macros (checked first)
      -macros defined for first level templates of the host (i.e., templates linked directly to the host), sorted by template ID
      -macros defined for second level templates of the host, sorted by template ID
      -macros defined for third level templates of the host, sorted by template ID, etc.
      -global macros (checked last)

      I hope that made sense.

      I'd still love to get the inherited macros directly from Zabbix api rather than doing this custom method.

      Thanks.
  • PavelZ
    Senior Member
    • Dec 2024
    • 162

    #3
    There is documentation https://www.zabbix.com/documentation.../usermacro/get
    There is a parameter inherited = 1

    but it still doesn't work
    Maybe someone will share a working code?

    Comment

    • stevene1900
      Junior Member
      • Jun 2018
      • 9

      #4
      Hello, it seems the "inherited" parameter name is a bit misleading. The Description says: "If set to true return only host prototype user macros inherited from a template." So it seems to only return host protype user macros inherited form a template (?)

      Comment

      • PavelZ
        Senior Member
        • Dec 2024
        • 162

        #5
        I also realized that this parameter refers to the host prototype.
        If you need macros from the template, you need to request these values ​separately by specifying the hostids, which is also the template id.​

        Comment

        Working...