Advertisement
syntax53

Zabbix API Updates

Feb 7th, 2019
3,133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import logging
  5. from pyzabbix import ZabbixAPI
  6.  
  7. URL = 'http://127.0.0.1/zabbix'
  8.  
  9. DEBUG=False
  10. if DEBUG:
  11.         stream = logging.StreamHandler(sys.stdout)
  12.         stream.setLevel(logging.DEBUG)
  13.         log = logging.getLogger('pyzabbix')
  14.         log.addHandler(stream)
  15.         log.setLevel(logging.DEBUG)
  16.  
  17. # init the API and Authenticate
  18. zapi = ZabbixAPI(URL, timeout=5)
  19. zapi.login("YOUR_API_USERNAME", "YOUR_API_PASSWORD")
  20.  
  21. #template ids
  22. # 10327 = Cisco Meraki Access Points
  23. # 10250 = Template Net HP Enterprise Switch SNMPv2
  24. # 10227 = Template Net HP Comware HH3C SNMPv2
  25. # 10852 = ZBX-DELL-POWERCONNECT-HARDWARE
  26. # 10923 = 3com 4500 extras
  27. # 10204 = Template Module Generic SNMPv2
  28. # 10223 = Template Net D-Link DES_DGS Switch SNMPv2
  29. # 10226 = Template Net Network Generic Device SNMPv2
  30.  
  31. #UPDATE VISIBLE HOST NAMES FROM INVENTORY NAME FOR SWITCHES AND APs
  32. for hosts in zapi.host.get(output='extend', templateids=[10327, 10250, 10227, 10852, 10923, 10204, 10223, 10226], selectInventory=['name']):
  33.         #print hosts['hostid'], "-", hosts['inventory']['name']
  34.         inv_name = hosts['inventory']['name']
  35.         inv_name = inv_name.strip().replace("_", " ")
  36.         if (hosts['name'] != inv_name and inv_name != ''):
  37.                 print hosts['name'], "=>", inv_name
  38.                 try:
  39.                         zapi.host.update(
  40.                                 hostid=hosts['hostid'],
  41.                                 name=inv_name
  42.                         )
  43.                 except:
  44.                         print 'Error updating host', hosts['hostid'], '=>', hosts['inventory']['name'], ' ...reverting name'
  45.                         try:
  46.                                 #name=hosts['host']
  47.                                 zapi.host.update(
  48.                                         hostid=hosts['hostid'],
  49.                                         name=''
  50.                                 )
  51.                         except:
  52.                                 print 'ERROR REVERTING NAME'
  53.  
  54. # RETRIEVE ALL SWITCHES
  55. all_switch_hostids = zapi.host.get(output=['hostid'], templateids=[10250, 10227, 10852, 10923, 10204, 10223, 10226])
  56. if all_switch_hostids:
  57.         list_host_ids = []
  58.         for id in all_switch_hostids: list_host_ids.append(int(id['hostid']))
  59.  
  60. if list_host_ids:
  61.         #DISABLE VLAN INTERFACE POLLERS
  62.         for items in zapi.item.get(output='extend', hostids=list_host_ids, searchWildcardsEnabled=True, search={'name':'Int*VLAN*:*'}, filter={'status':0}):
  63.                 #print items
  64.                 print 'Disabling', items['name'], 'on host', items['hostid']
  65.                 zapi.item.update(
  66.                         itemid=items['itemid'],
  67.                         status=1
  68.                 )
  69.  
  70.         #DISABLE INTERFACE LINK DOWN TRIGGERS
  71.         for triggers in zapi.trigger.get(output='extend', hostids=list_host_ids, searchWildcardsEnabled=True, search={'description':'Int*Link Down'}, filter={'status':0}):
  72.                 print 'Disabling', triggers['description']
  73.                 zapi.trigger.update(
  74.                         triggerid=triggers['triggerid'],
  75.                         status=1
  76.                 )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement