Ad Widget

Collapse

Can't delete host using the zabbix_api.py

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MaximVeksler
    Junior Member
    • Jun 2011
    • 9

    #1

    Can't delete host using the zabbix_api.py

    Hello,

    Trying to manipulate Zabbix using the unofficial python api for zabbix for host deletion and it fails.


    My code looks like this:

    Code:
    #!/usr/bin/python 
    
    #GRACE_PERIOD = 60 * 60 * 24 * 7
    GRACE_PERIOD = 2
    
    import time
    from zabbix_api import ZabbixAPI, Already_Exists
    z = ZabbixAPI(server="http://localhost/zabbix", log_level=6)
    z.login("admin", "zabbix")
    
    live_hosts = []
    for host in z.host.get({}):
     live_hosts.append(host['hostid'])
    
    status_items = z.item.get({ 'output' : 'extend', 'hostids' : live_hosts, 'filter' : { 'key_' : 'status' } })
    for status_item in status_items:
     if status_item['lastvalue'] == '2':
      last_event_time = int(status_item['lastclock'])
      now_time = int(time.time())
      elapsed_event_time = now_time - last_event_time
    
      if elapsed_event_time > GRACE_PERIOD:
       print "Will kill host " + status_item['hostid'] + " passed time: " + str(elapsed_event_time)
       z.host.delete( { 'params' : [{ "hostid" : int(status_item['hostid']) }] })
    and it prints the following:

    Code:
    20: url: http://localhost/zabbix/api_jsonrpc.php
    10: Trying to login with 'admin':'md5(5fce1b3e34b520afeffb37ce08c7cd66)'
    10: json_obj: {'params': {'password': 'zabbix', 'user': 'admin'}, 'jsonrpc': '2.0', 'method': 'user.authenticate', 'auth': '', 'id': 0}
    20: Sending: {"params": {"password": "zabbix", "user": "admin"}, "jsonrpc": "2.0", "method": "user.authenticate", "auth": "", "id": 0}
    10: Sending headers: {'Content-Type': 'application/json-rpc', 'User-Agent': 'python/zabbix_api'}
    20: Response Code: 200
    10: Response Body: {u'jsonrpc': u'2.0', u'result': u'e686ad782a427ce80c0f62920d259a4f', u'id': 0}
    10: json_obj: {'params': {}, 'jsonrpc': '2.0', 'method': 'host.get', 'auth': u'e686ad782a427ce80c0f62920d259a4f', 'id': 1}
    20: Sending: {"params": {}, "jsonrpc": "2.0", "method": "host.get", "auth": "e686ad782a427ce80c0f62920d259a4f", "id": 1}
    10: Sending headers: {'Content-Type': 'application/json-rpc', 'User-Agent': 'python/zabbix_api'}
    20: Response Code: 200
    10: Response Body: {u'jsonrpc': u'2.0', u'result': [{u'hostid': u'10047'}, {u'hostid': u'10017'}], u'id': 1}
    10: json_obj: {'params': {'filter': {'key_': 'status'}, 'output': 'extend', 'hostids': [u'10047', u'10017']}, 'jsonrpc': '2.0', 'method': 'item.get', 'auth': u'e686ad782a427ce80c0f62920d259a4f', 'id': 2}
    20: Sending: {"params": {"filter": {"key_": "status"}, "output": "extend", "hostids": ["10047", "10017"]}, "jsonrpc": "2.0", "method": "item.get", "auth": "e686ad782a427ce80c0f62920d259a4f", "id": 2}
    10: Sending headers: {'Content-Type': 'application/json-rpc', 'User-Agent': 'python/zabbix_api'}
    20: Response Code: 200
    10: Response Body: {u'jsonrpc': u'2.0', u'result': [{u'itemid': u'18465', u'username': u'', u'snmp_port': u'161', u'lastlogsize': u'0', u'trends': u'365', u'snmpv3_authpassphrase': u'', u'snmp_oid': u'', u'templateid': u'10061', u'snmpv3_securitylevel': u'0', u'multiplier': u'0', u'authtype': u'0', u'password': u'', u'logtimefmt': u'', u'mtime': u'0', u'delay': u'60', u'publickey': u'', u'params': u'', u'units': u'', u'formula': u'0', u'type': u'0', u'prevvalue': None, u'status': u'0', u'lastvalue': None, u'lastclock': None, u'snmp_community': u'', u'description': u'Host status', u'data_type': u'0', u'trapper_hosts': u'', u'snmpv3_securityname': u'', u'value_type': u'3', u'prevorgvalue': None, u'delta': u'0', u'delay_flex': u'', u'snmpv3_privpassphrase': u'', u'hostid': u'10017', u'key_': u'status', u'privatekey': u'', u'valuemapid': u'2', u'hosts': [{u'hostid': u'10017'}], u'error': u'', u'ipmi_sensor': u'', u'history': u'7'}, {u'itemid': u'22191', u'username': u'', u'snmp_port': u'161', u'lastlogsize': u'0', u'trends': u'365', u'snmpv3_authpassphrase': u'', u'snmp_oid': u'', u'templateid': u'10061', u'snmpv3_securitylevel': u'0', u'multiplier': u'0', u'authtype': u'0', u'password': u'', u'logtimefmt': u'', u'mtime': u'0', u'delay': u'60', u'publickey': u'', u'params': u'', u'units': u'', u'formula': u'0', u'type': u'0', u'prevvalue': u'0', u'status': u'0', u'lastvalue': u'2', u'lastclock': u'1307360174', u'snmp_community': u'', u'description': u'Host status', u'data_type': u'0', u'trapper_hosts': u'', u'snmpv3_securityname': u'', u'value_type': u'3', u'prevorgvalue': None, u'delta': u'0', u'delay_flex': u'', u'snmpv3_privpassphrase': u'', u'hostid': u'10047', u'key_': u'status', u'privatekey': u'', u'valuemapid': u'2', u'hosts': [{u'hostid': u'10047'}], u'error': u'', u'ipmi_sensor': u'', u'history': u'7'}], u'id': 2}
    Will kill host 10047 passed time: 562
    10: json_obj: {'params': {'params': [{'hostid': 10047}]}, 'jsonrpc': '2.0', 'method': 'host.delete', 'auth': u'e686ad782a427ce80c0f62920d259a4f', 'id': 3}
    20: Sending: {"params": {"params": [{"hostid": 10047}]}, "jsonrpc": "2.0", "method": "host.delete", "auth": "e686ad782a427ce80c0f62920d259a4f", "id": 3}
    10: Sending headers: {'Content-Type': 'application/json-rpc', 'User-Agent': 'python/zabbix_api'}
    20: Response Code: 200
    10: Response Body: {u'jsonrpc': u'2.0', u'result': True, u'id': 3}
    , the API confirms the operation (HTTP 200) but the host remains, I can see it both in the web front-end and in another execution of this script.

    I'm running Zabbix 1.8.5 from debian sid .deb packages on Ubuntu 10.10.

    What could be the problem with this? I've seen some bugs in Zabbix 1.8.2 but have upgraded to 1.8.5 both for the agent and the server, frontend.
  • MaximVeksler
    Junior Member
    • Jun 2011
    • 9

    #2
    Solved!

    I got the python API working incorrectly, after the fix - the script should look like this:

    Code:
    #!/usr/bin/python 
    
    #GRACE_PERIOD = 60 * 60 * 24 * 7
    GRACE_PERIOD = 2
    
    import time
    from zabbix_api import ZabbixAPI, Already_Exists
    z = ZabbixAPI(server="http://localhost/zabbix", log_level=6)
    z.login("admin", "zabbix")
    
    live_hosts = []
    for host in z.host.get({}):
     live_hosts.append(host['hostid'])
    
    status_items = z.item.get({ 'output' : 'extend', 'hostids' : live_hosts, 'filter' : { 'key_' : 'status' } })
    for status_item in status_items:
     if status_item['lastvalue'] == '2':
      last_event_time = int(status_item['lastclock'])
      now_time = int(time.time())
      elapsed_event_time = now_time - last_event_time
    
      if elapsed_event_time > GRACE_PERIOD:
       print "Will kill host " + status_item['hostid'] + " passed time: " + str(elapsed_event_time)
       z.host.delete( [{ "hostid" : int(status_item['hostid']) }] )

    And now the output is:

    Code:
    20: url: http://localhost/zabbix/api_jsonrpc.php
    10: Trying to login with 'admin':'md5(5fce1b3e34b520afeffb37ce08c7cd66)'
    10: json_obj: {'params': {'password': 'zabbix', 'user': 'admin'}, 'jsonrpc': '2.0', 'method': 'user.authenticate', 'auth': '', 'id': 0}
    20: Sending: {"params": {"password": "zabbix", "user": "admin"}, "jsonrpc": "2.0", "method": "user.authenticate", "auth": "", "id": 0}
    10: Sending headers: {'Content-Type': 'application/json-rpc', 'User-Agent': 'python/zabbix_api'}
    20: Response Code: 200
    10: Response Body: {u'jsonrpc': u'2.0', u'result': u'e107a24ace1f53635ca9fc66f5ea18a3', u'id': 0}
    10: json_obj: {'params': {}, 'jsonrpc': '2.0', 'method': 'host.get', 'auth': u'e107a24ace1f53635ca9fc66f5ea18a3', 'id': 1}
    20: Sending: {"params": {}, "jsonrpc": "2.0", "method": "host.get", "auth": "e107a24ace1f53635ca9fc66f5ea18a3", "id": 1}
    10: Sending headers: {'Content-Type': 'application/json-rpc', 'User-Agent': 'python/zabbix_api'}
    20: Response Code: 200
    10: Response Body: {u'jsonrpc': u'2.0', u'result': [{u'hostid': u'10047'}, {u'hostid': u'10017'}], u'id': 1}
    10: json_obj: {'params': {'filter': {'key_': 'status'}, 'output': 'extend', 'hostids': [u'10047', u'10017']}, 'jsonrpc': '2.0', 'method': 'item.get', 'auth': u'e107a24ace1f53635ca9fc66f5ea18a3', 'id': 2}
    20: Sending: {"params": {"filter": {"key_": "status"}, "output": "extend", "hostids": ["10047", "10017"]}, "jsonrpc": "2.0", "method": "item.get", "auth": "e107a24ace1f53635ca9fc66f5ea18a3", "id": 2}
    10: Sending headers: {'Content-Type': 'application/json-rpc', 'User-Agent': 'python/zabbix_api'}
    20: Response Code: 200
    10: Response Body: {u'jsonrpc': u'2.0', u'result': [{u'itemid': u'18465', u'username': u'', u'snmp_port': u'161', u'lastlogsize': u'0', u'trends': u'365', u'snmpv3_authpassphrase': u'', u'snmp_oid': u'', u'templateid': u'10061', u'snmpv3_securitylevel': u'0', u'multiplier': u'0', u'authtype': u'0', u'password': u'', u'logtimefmt': u'', u'mtime': u'0', u'delay': u'60', u'publickey': u'', u'params': u'', u'units': u'', u'formula': u'0', u'type': u'0', u'prevvalue': None, u'status': u'0', u'lastvalue': None, u'lastclock': None, u'snmp_community': u'', u'description': u'Host status', u'data_type': u'0', u'trapper_hosts': u'', u'snmpv3_securityname': u'', u'value_type': u'3', u'prevorgvalue': None, u'delta': u'0', u'delay_flex': u'', u'snmpv3_privpassphrase': u'', u'hostid': u'10017', u'key_': u'status', u'privatekey': u'', u'valuemapid': u'2', u'hosts': [{u'hostid': u'10017'}], u'error': u'', u'ipmi_sensor': u'', u'history': u'7'}, {u'itemid': u'22191', u'username': u'', u'snmp_port': u'161', u'lastlogsize': u'0', u'trends': u'365', u'snmpv3_authpassphrase': u'', u'snmp_oid': u'', u'templateid': u'10061', u'snmpv3_securitylevel': u'0', u'multiplier': u'0', u'authtype': u'0', u'password': u'', u'logtimefmt': u'', u'mtime': u'0', u'delay': u'60', u'publickey': u'', u'params': u'', u'units': u'', u'formula': u'0', u'type': u'0', u'prevvalue': u'0', u'status': u'0', u'lastvalue': u'2', u'lastclock': u'1307360174', u'snmp_community': u'', u'description': u'Host status', u'data_type': u'0', u'trapper_hosts': u'', u'snmpv3_securityname': u'', u'value_type': u'3', u'prevorgvalue': None, u'delta': u'0', u'delay_flex': u'', u'snmpv3_privpassphrase': u'', u'hostid': u'10047', u'key_': u'status', u'privatekey': u'', u'valuemapid': u'2', u'hosts': [{u'hostid': u'10047'}], u'error': u'', u'ipmi_sensor': u'', u'history': u'7'}], u'id': 2}
    Will kill host 10047 passed time: 1192
    10: json_obj: {'params': [{'hostid': 10047}], 'jsonrpc': '2.0', 'method': 'host.delete', 'auth': u'e107a24ace1f53635ca9fc66f5ea18a3', 'id': 3}
    20: Sending: {"params": [{"hostid": 10047}], "jsonrpc": "2.0", "method": "host.delete", "auth": "e107a24ace1f53635ca9fc66f5ea18a3", "id": 3}
    10: Sending headers: {'Content-Type': 'application/json-rpc', 'User-Agent': 'python/zabbix_api'}
    20: Response Code: 200
    10: Response Body: {u'jsonrpc': u'2.0', u'result': {u'hostids': [10047]}, u'id': 3}

    Comment

    • PratapSingh
      Junior Member
      • Apr 2016
      • 29

      #3
      I am replying on this very old thread. But I have very similar situation and tried the script given but its not working so just wanted to get some help.

      I see there is no value coming to "status_items".

      Also I am very poor to json/xml as well so understanding such key, value chain reaction is like solving some mystery to me .

      It is definitely due to version difference as I am using Zabbix 3. So any one have any idea about how to tweak this script.

      Thank you
      Pratap

      Comment

      Working...