|
| 1 | +#!/usr/bin/env python |
| 2 | +#coding:utf8 |
| 3 | + |
| 4 | +''' |
| 5 | +Created on 03.06.2015 |
| 6 | +''' |
| 7 | + |
| 8 | +import optparse |
| 9 | +import sys |
| 10 | +import traceback |
| 11 | +from getpass import getpass |
| 12 | +from core import ZabbixAPI |
| 13 | + |
| 14 | +def get_options(): |
| 15 | + usage = "usage: %prog [options]" |
| 16 | + OptionParser = optparse.OptionParser |
| 17 | + parser = OptionParser(usage) |
| 18 | + |
| 19 | + parser.add_option("-s","--server",action="store",type="string",\ |
| 20 | + dest="server",help="(REQUIRED)Zabbix Server URL.") |
| 21 | + parser.add_option("-u", "--username", action="store", type="string",\ |
| 22 | + dest="username",help="(REQUIRED)Username (Will prompt if not given).") |
| 23 | + parser.add_option("-p", "--password", action="store", type="string",\ |
| 24 | + dest="password",help="(REQUIRED)Password (Will prompt if not given).") |
| 25 | + parser.add_option("-H","--hostname",action="store",type="string",\ |
| 26 | + dest="hostname",help="(REQUIRED)hostname for hosts.") |
| 27 | + parser.add_option("-t","--templates",action="store",type="string",\ |
| 28 | + dest="templates",default="",help="Templates to unlink and clear from the given hosts.If you want to use multiple groups,separate them with a ','.") |
| 29 | + parser.add_option("-g","--groups",action="store",type="string",\ |
| 30 | + dest="groups",default="",help="Host groups to remove the given hosts from.If you want to use multiple groups,separate them with a ','.") |
| 31 | + parser.add_option("-f","--file",dest="filename",\ |
| 32 | + metavar="FILE",help="Load values from input file. Specify - for standard input Each line of file contains whitespace delimited: <hostname>4space<templates>4space<groups>") |
| 33 | + |
| 34 | + options,args = parser.parse_args() |
| 35 | + |
| 36 | + if not options.server: |
| 37 | + options.server = raw_input('server http:') |
| 38 | + |
| 39 | + if not options.username: |
| 40 | + options.username = raw_input('Username:') |
| 41 | + |
| 42 | + if not options.password: |
| 43 | + options.password = getpass() |
| 44 | + |
| 45 | + return options, args |
| 46 | + |
| 47 | +def errmsg(msg): |
| 48 | + sys.stderr.write(msg + "\n") |
| 49 | + sys.exit(-1) |
| 50 | + |
| 51 | +def get_list(lists, key): |
| 52 | + l = [] |
| 53 | + for i in lists: |
| 54 | + l.append(i[key]) |
| 55 | + return l |
| 56 | + |
| 57 | +def get_api(**kwargs): |
| 58 | + try: |
| 59 | + print zapi.host.massremove(kwargs) |
| 60 | + except Exception as e: |
| 61 | + print str(e) |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + options, args = get_options() |
| 66 | + |
| 67 | + zapi = ZabbixAPI(options.server,options.username, options.password) |
| 68 | + |
| 69 | + file = options.filename |
| 70 | + |
| 71 | + if file: |
| 72 | + with open(file,"r") as f: |
| 73 | + content = f.readlines() |
| 74 | + for i in content: |
| 75 | + l = i.split(" ") |
| 76 | + hostname = l[0].rstrip() |
| 77 | + hostid=zapi.host.get({"filter":{"host":hostname}})[0]["hostid"] |
| 78 | + try: |
| 79 | + templates = l[1].rstrip() |
| 80 | + templates_id = zapi.template.get({"output": "templateid","filter": {"host":templates.split(",")}}) |
| 81 | + templateids = get_list(templates_id, "templateid") |
| 82 | + except: |
| 83 | + templateids = "" |
| 84 | + try: |
| 85 | + groups = l[2].rstrip() |
| 86 | + groups_id = zapi.hostgroup.get({"output": "groupid","filter": {"name":groups.split(",")}}) |
| 87 | + groupids = get_list(groups_id, "groupid") |
| 88 | + except: |
| 89 | + groupids = "" |
| 90 | + if templateids and groupids: |
| 91 | + get_api(templateids_clear=templateids, groupids=groupids, hostids=hostid) |
| 92 | + elif templateids and not groupids: |
| 93 | + get_api(templateids_clear=templateids, hostids=hostid) |
| 94 | + elif not templateids and groupids: |
| 95 | + get_api(groupids=groupids, hostids=hostid) |
| 96 | + else: |
| 97 | + templates = options.templates |
| 98 | + hostname = options.hostname |
| 99 | + groups = options.groups |
| 100 | + hostid = zapi.host.get({"filter":{"host":hostname}})[0]["hostid"] |
| 101 | + if templates: |
| 102 | + templates_id = zapi.template.get({"output": "templateid","filter": {"host":templates.split(",")}}) |
| 103 | + templateids = get_list(templates_id, "templateid") |
| 104 | + else: |
| 105 | + templateids = "" |
| 106 | + if groups: |
| 107 | + groups_id = zapi.hostgroup.get({"output": "groupid","filter": {"name":groups.split(",")}}) |
| 108 | + groupids = get_list(groups_id, "groupid") |
| 109 | + else: |
| 110 | + groupids = "" |
| 111 | + if templateids and groupids: |
| 112 | + get_api(templateids_clear=templateids, groupids=groupids, hostids=hostid) |
| 113 | + elif templateids and not groupids: |
| 114 | + get_api(templateids_clear=templateids, hostids=hostid) |
| 115 | + elif not templateids and groupids: |
| 116 | + get_api(groupids=groupids, hostids=hostid) |
0 commit comments