Ad Widget

Collapse

Zabbix API add a host to a hostgroup

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hvaithianathan
    Junior Member
    • Jun 2014
    • 3

    #1

    Zabbix API add a host to a hostgroup

    Hi,

    I am trying to add a host to a hostgroup using the python API

    I get the groupid like this

    group_id = zapi.hostgroup.get(output='extend',filter={"name":["group_name"]})
    print group_id[0]['groupid']
    19

    and the hostID like this,
    hostids=zapi.host.get(output='extend',filter={"hos t":["server_name"]})
    print hostids[0]['hostid']
    10122

    Now I want to add the host with hostiD 10122 to the group with ID 19.
    Somehow update isn't working for me. Can the ppl in the forum help please
  • hvaithianathan
    Junior Member
    • Jun 2014
    • 3

    #2
    Tried both the host and hostgroup update but its not working for me,

    >>> test=zapi.hostgroup.update(groupid=19,hostid=10122 )
    >>> print test
    {u'groupids': [19]}


    >>> test=zapi.host.update(hostid=10122,groupid=19)
    >>> print test
    {u'hostids': [u'10122']}

    but the host is not assigned to the hostgroup (19). I have checked that the hostgroup.exists() and hostgroup.iswriteable() (similarly for the hosts) and its all True.

    What am I doing wrongly here... Please help me.

    ------------------
    Update:
    --------

    I think I have been doing the zabbix update syntax wrongly. I corrected it like below,

    zapi.hostgroup.update(hostid=10122,groups={"groupi d":19})

    For which I am getting, Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python2.6/site-packages/pyzabbix/__init__.py", line 140, in fn
    args or kwargs
    File "/usr/lib/python2.6/site-packages/pyzabbix/__init__.py", line 117, in do_request
    raise ZabbixAPIException(msg, response_json['error']['code'])
    pyzabbix.ZabbixAPIException: ("Error -32500: Application error., No permissions to referred object or it does not exist! while sending {'params': {'hostid': 10122, 'groups': {'groupid': 19}}, 'jsonrpc': '2.0', 'method': 'hostgroup.update', 'auth': u'd971827d490709cbb6d519208708d4f5', 'id': 107}", -32500)

    Is this some kind of a bug? Looks like its a permission error and my user is a super zabbix admin. Pls let me know how to solve this.
    -------------------------

    The api version I am using is 2.2.4
    print "Connected to Zabbix API Version %s" % zapi.api_version() Connected to Zabbix API Version 2.2.4
    -------------------------

    Also I am a super zabbix admin and both the host and hostgroup are writeable

    >>> zapi.host.iswritable(hostid=10122)
    True
    >>> zapi.hostgroup.iswritable(groupid=19)
    True

    I am out of options now
    -------------------------
    Last edited by hvaithianathan; 17-07-2014, 19:24.

    Comment

    Working...