Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import sys
- import argparse
- from pyzabbix import ZabbixAPI
- zapi = ZabbixAPI(server='http://korolv.zabbix.local')
- zapi.login(user = 'Admin', password='zabbix')
- def createParser():
- parser = argparse.ArgumentParser(
- prog = 'update template',
- description = '''Script for detach
- templates and remove macros
- through Zabbix API.''',
- usage='detach_templates.py [arguments] [value]',
- epilog = '''(c) i-Free 2018.''')
- parser.add_argument(
- '-t', '--template', dest='template',
- help = 'Specify a template')
- parser.add_argument(
- '-m1', '--macros1', dest='macros1',
- help = 'Specify a first macros1')
- parser.add_argument(
- '-m2', '--macros2', dest='macros2',
- help = 'Specify a second macros2')
- parser.add_argument(
- '-H', '--hosts', type=str, dest='host',
- help = 'Specify a list of hosts')
- return parser
- def detach_templates(current_host, templateIDs):
- detachIDs = zapi.host.update(hostid=current_host, templates_clear=templateIDs,output = 'extend')
- print(' Template was removed from host: ' + current_host)
- def macros_delete(current_macroid):
- current_macros_delete = zapi.usermacro.deletehostmacro(current_macroid)
- print(" Macros " + current_macroid + " was deleted")
- def get_macrosIDs(current_host, macros_pattern, templateIDs):
- macrosIDs = zapi.usermacro.get(hostids=current_host, search={'macro':macros_pattern}, output = 'extend')
- detach_templates(current_host, templateIDs)
- for macnum in range(0,len(macrosIDs)):
- current_macroid = macrosIDs[macnum]['hostmacroid']
- print(' Macros' + macrosIDs[macnum]['macro'] + " was found on " + current_host)
- macros_delete(current_macroid)
- def get_hosts(templateIDs):
- hostIDs = zapi.host.get(search={'host':HOST_NAME_PATTERN,'templateids':templateIDs}, output = 'shorten')
- for host in range(0,len(hostIDs)):
- current_host = hostIDs[host]['hostid']
- print(" Host " + hostIDs[host]['hostid'] + " was found.")
- if MACROS_PATTERN1 and MACROS_PATTERN2:
- get_macrosIDs(current_host, MACROS_PATTERN1, templateIDs)
- get_macrosIDs(current_host, MACROS_PATTERN2, templateIDs)
- else:
- get_macrosIDs(current_host, MACROS_PATTERN1, templateIDs)
- def get_templateIDs(template):
- templateIDs = zapi.template.get(search={'host':template}, output = 'shorten')
- get_hosts(templateIDs)
- if __name__ == "__main__":
- parser = createParser()
- args = parser.parse_args()
- HOST_NAME_PATTERN = args.host
- TEMPLATE = args.template
- MACROS_PATTERN1 = args.macros1
- MACROS_PATTERN2 = args.macros2
- print('Logs:')
- get_templateIDs(TEMPLATE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement