Ad Widget

Collapse

Using API to add/update/delete macros

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chubz
    Junior Member
    • Jul 2012
    • 11

    #1

    Using API to add/update/delete macros

    http://www.zabbix.com/documentation/...pi/host/update
    ^ States that with the host.update in API, you can "Update host macros. Missing macros will be added, existing updated, others deleted."

    So there are 3 macros I have. And When I try to update one of them it says that that macro already exists. Also how would I go about deleting a macro? The documentation did not go into depth with this and I am curious.
    So far I have a work around the solution. For the time being I send a JSON object that just adds an empty macro, which deletes every other macro(why?). Then i send a second JSON object with all the macros I want(that's how I'm "updating" macros)

    Is there a cleaner method to do so?
  • wesmason
    Junior Member
    • Oct 2017
    • 6

    #2
    Yeah, this still seems to be under explained. I would appreciate a 'add', 'update', 'delete' section containing values to perform those actions against.

    Ex:

    Code:
    {
        "jsonrpc": "2.0",
        "method": "host.update",
        "params": {
            "hostid": "10126",
            "macros": {
                "add": {
                    "{$MACRO1}": "value",
                    "{$MACRO2}": "5"
                },
                "delete": [
                    "{$MACRO4}"
                ],
                "update": {
                    "{$MACRO3}": "new_value"
                }
            }
        },
        "auth": "038e1d7b1735c6a5436ee9eae095879e",
        "id": 1
    }

    Comment

    • Vlaad
      Junior Member
      • Mar 2014
      • 4

      #3
      there is usermacro.update method to work with macros. you need to know the macros ID only, hostID is not required.
      it works with hosts and templates

      use the usermacro.get method to search hostmacroid by name:
      Code:
      zbx.usermacro.get(
                  hostids = host_id,
                  filter = {
                      "macro": "{$THE.MACROS}",
                      },
                  )
      response example:
      Code:
      {
          "jsonrpc": "2.0",
          "result": [
              {
                  "macro": "{$THE.MACROS}",
                  "hostmacroid": "777",
                  "hostid": "10309",
                  "value": "MACROS.VALUE"
              }
          ],
          "id": "1"
      }
      then, update it:
      Code:
      zbx.usermacro.update(
                  hostmacroid = "777",
                  value = new_value,
              )
      response example:
      Code:
      {
          "jsonrpc": "2.0",
          "result": {
              "hostmacroids": [
                  "777"
              ]
          },
          "id": "1"
      }
      again, hostmacroid works with hosts and templates

      Comment

      • SergeiChechev
        Junior Member
        • May 2021
        • 8

        #4
        At the moment I get macros without an hostmacroids, is it something that has changed in the new zabix api? I use zabbix docker 6.0
        zabbix.get
        Code:
        [{ 10517 {$NEW_MACROS} 2} { 10517 {$TEST1} 33}]
        And when I try to create I get error too
        Code:
        Invalid parameter "/1": unexpected parameter "hostmacroids

        Comment

        • sandreev
          Junior Member
          • Aug 2022
          • 1

          #5
          Any updates on the issue? As for now,
          Code:
          usermacro.update
          is marked as deprecated, and in
          Code:
          host.update
          you can't update single macro - only overwrite entire array of macros, which is not appropriate if you have secret values

          Comment

          Working...