Ad Widget

Collapse

Adding new tag via API to a host created via host.create, resulting not successful

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • verde
    Member
    • Jul 2021
    • 39

    #1

    Adding new tag via API to a host created via host.create, resulting not successful

    Hi, I have a host created via API (host.create) including 5 initial tags.
    I'm trying to add a new tag for a host, but the API host.update replaces all the 5 old tags with the new one. So instead of having 6 tags, the host ended up with only 1.
    Any suggestions to add a new tag via APIs?

    zabbix_server (Zabbix) 6.2.1

    Call:
    url='http://'+ zbxServer +'/zabbix/api_jsonrpc.php';
    Code:
    host.update:
     
    data={
     "jsonrpc": "2.0",
     "method": "host.update",
     "params": {
     "hostid": hostid,
     "tags": [ {
       "tag": "deactivated",
       "value": deactivationDate
       }]
     },
     "auth": token,
     "id": 1
    }
    Also, I tried to use host.get with "selectTags": "extend", but it brings the tags with an "automatic": "0" in each tag, i.e:
    Code:
    host.get result:
     
      "tags": [
    {
    "tag": "type",
    "value": "server",
    "automatic": "0"
    },
    ...
    ...
    {
    "tag": "service",
    "value": "active",
    "automatic": "0"
    }
    ]

    when I added the new tag to the existing tags returned by host.get, and try to host.update it I'm receiving the error:

    Code:
    Reading : [{'tag": "type","value": "server", "automatic": "0"},.....{"tag":"service","value":"active","autom atic":"0"}]
    
    Appending: [{'tag": "type","value": "server", "automatic": "0"},.....{"tag":"service","value":"active","autom atic":"0"},
    {"tag": "deactivated", "value": "2022-08-26 20:51:53"}
      ]
    
    host.update result:
    
    Error: {'jsonrpc': '2.0',
    'error':{'code': -32602, 'message': 'Invalid params.', 'data': 'Invalid parameter "/1/tags/1": unexpected parameter "automatic".'},
    'id': 1}
    ​
    Thanks​
    Last edited by verde; 31-08-2022, 18:26.
  • cyber
    Senior Member
    Zabbix Certified SpecialistZabbix Certified Professional
    • Dec 2006
    • 4807

    #2
    What happens if you skip that "automatic": "0" part when inserting? use only type and value...

    Comment

    • dimir
      Zabbix developer
      • Apr 2011
      • 1080

      #3
      Unfortunately there is currently now way to "add" host tags to the existing ones. When adding you must also list all the current ones.

      Comment

      • verde
        Member
        • Jul 2021
        • 39

        #4
        Thanks for the feedback. I just confirmed that to add a new tag via APIs on 6.2.1, it's needed to read first the current tags.
        So, after reading the tags, taking out "automatic": "0" from each one, and appending the new tag/value, host.update worked.
        Last edited by verde; 05-09-2022, 18:23.

        Comment

        • purval.patel
          Junior Member
          • Feb 2023
          • 1

          #5
          Yes it is possible. please check the link.

          Comment

          • cyber
            Senior Member
            Zabbix Certified SpecialistZabbix Certified Professional
            • Dec 2006
            • 4807

            #6
            Originally posted by purval.patel
            Yes it is possible. please check the link.
            No it is still not possible to add directly... you still have to get list of existing ones and then update to include all, old and new... Just as told before and also implemented...​

            Comment


            • purval.patel
              purval.patel commented
              Editing a comment
              yes you were right, need to include all old and new. I have tried with the python code. and its working.

              from zabbix_api import ZabbixAPI
              import sys

              # Connect to the Zabbix API
              zapi = ZabbixAPI(server="http://zabbix-server-ip/zabbix")
              zapi.login("username", "password")

              # Get all hosts from the Zabbix server
              hosts = zapi.host.get({"output": ["host"]})

              # Print the host names
              for host in hosts:
              # print(host["host"])
              hostis=host["host"]
              # Get the ID of the host you want to update
              hosts = zapi.host.get({"filter": {"host": hostis}})
              if hosts:
              host_id = hosts[0]["hostid"]
              else:
              print("Host not found")

              print(hostis,host_id)
              # Get the existing tags for the host
              existing_tags = zapi.host.get({"hostids": host_id, "selectTags": "extend"})
              existing_tags = existing_tags[0]["tags"]

              # Add the new tag to the list of existing tags
              new_tag = {"tag": "tag1", "value": "value1"}
              existing_tags.append(new_tag)

              # Update the host with the new list of tags
              zapi.host.update(
              {
              "hostid": host_id,
              "tags": existing_tags,
              }
              )
          Working...