Advertisement
Guest User

demo.py

a guest
Feb 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import sys
  3. import argparse
  4. from pyzabbix import ZabbixAPI
  5.  
  6.  
  7. zapi = ZabbixAPI(server='http://korolv.zabbix.local')
  8. zapi.login(user = 'Admin', password='zabbix')
  9.  
  10. def createParser():
  11.     parser = argparse.ArgumentParser(
  12.         prog = 'update template',
  13.         description = '''Script for detach
  14.                         templates and remove macros
  15.                         through Zabbix API.''',
  16.         usage='detach_templates.py [arguments] [value]',
  17.         epilog = '''(c) i-Free 2018.''')
  18.      
  19.     parser.add_argument(
  20.         '-t', '--template',  dest='template',
  21.          help = 'Specify a template')
  22.  
  23.     parser.add_argument(
  24.         '-m1', '--macros1', dest='macros1',
  25.          help = 'Specify a first macros1')
  26.  
  27.     parser.add_argument(
  28.         '-m2', '--macros2', dest='macros2',                                                                          
  29.          help = 'Specify a second macros2')
  30.  
  31.     parser.add_argument(
  32.         '-H', '--hosts', type=str, dest='host',
  33.          help = 'Specify a list of hosts')
  34.  
  35.     return parser
  36.  
  37.  
  38. def detach_templates(current_host, templateIDs):
  39.     detachIDs = zapi.host.update(hostid=current_host, templates_clear=templateIDs,output = 'extend')
  40.     print('    Template was removed from host: ' + current_host)
  41.  
  42.  
  43. def macros_delete(current_macroid):
  44.     current_macros_delete = zapi.usermacro.deletehostmacro(current_macroid)
  45.     print("    Macros " + current_macroid + " was deleted")
  46.    
  47.  
  48.  
  49. def get_macrosIDs(current_host, macros_pattern, templateIDs):
  50.     macrosIDs = zapi.usermacro.get(hostids=current_host, search={'macro':macros_pattern}, output = 'extend')
  51.     detach_templates(current_host, templateIDs)
  52.     for macnum in range(0,len(macrosIDs)):
  53.         current_macroid = macrosIDs[macnum]['hostmacroid']
  54.         print('    Macros' + macrosIDs[macnum]['macro'] +  " was found on " + current_host)
  55.         macros_delete(current_macroid)
  56.    
  57.  
  58. def get_hosts(templateIDs):
  59.     hostIDs = zapi.host.get(search={'host':HOST_NAME_PATTERN,'templateids':templateIDs}, output = 'shorten')
  60.     for host in range(0,len(hostIDs)):
  61.         current_host = hostIDs[host]['hostid']
  62.         print("    Host " + hostIDs[host]['hostid'] + " was found.")
  63.         if MACROS_PATTERN1 and MACROS_PATTERN2:
  64.             get_macrosIDs(current_host, MACROS_PATTERN1, templateIDs)
  65.             get_macrosIDs(current_host, MACROS_PATTERN2, templateIDs)
  66.         else:
  67.             get_macrosIDs(current_host, MACROS_PATTERN1, templateIDs)
  68.            
  69. def get_templateIDs(template):
  70.     templateIDs = zapi.template.get(search={'host':template}, output = 'shorten')
  71.     get_hosts(templateIDs)
  72.  
  73.  
  74. if __name__ == "__main__":
  75.     parser = createParser()
  76.     args = parser.parse_args()
  77.     HOST_NAME_PATTERN = args.host
  78.     TEMPLATE = args.template
  79.     MACROS_PATTERN1 = args.macros1
  80.     MACROS_PATTERN2 = args.macros2
  81.     print('Logs:')
  82.     get_templateIDs(TEMPLATE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement