|
| 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("-f","--file",dest="filename",\ |
| 28 | + metavar="FILE",help="Load values from input file. Specify - for standard input Each line of file contains whitespace delimited: <hostname>") |
| 29 | + |
| 30 | + options,args = parser.parse_args() |
| 31 | + |
| 32 | + if not options.server: |
| 33 | + options.server = raw_input('server http:') |
| 34 | + |
| 35 | + if not options.username: |
| 36 | + options.username = raw_input('Username:') |
| 37 | + |
| 38 | + if not options.password: |
| 39 | + options.password = getpass() |
| 40 | + |
| 41 | + return options, args |
| 42 | + |
| 43 | +def errmsg(msg): |
| 44 | + sys.stderr.write(msg + "\n") |
| 45 | + sys.exit(-1) |
| 46 | + |
| 47 | +if __name__ == "__main__": |
| 48 | + options, args = get_options() |
| 49 | + |
| 50 | + zapi = ZabbixAPI(options.server,options.username, options.password) |
| 51 | + |
| 52 | + hostname = options.hostname |
| 53 | + file = options.filename |
| 54 | + |
| 55 | + if file: |
| 56 | + with open(file,"r") as f: |
| 57 | + host_list = f.readlines() |
| 58 | + for hostname in host_list: |
| 59 | + hostname = hostname.rstrip() |
| 60 | + try: |
| 61 | + hostinfo = zapi.host.get({"filter":{"host":hostname},"output":"hostid", "selectGroups": "extend", "selectParentTemplates": ["templateid","name"]})[0] |
| 62 | + hostid = hostinfo["hostid"] |
| 63 | + host_group_list = [] |
| 64 | + host_template_list = [] |
| 65 | + for l in hostinfo["groups"]: |
| 66 | + host_group_list.append(l["name"]) |
| 67 | + for t in hostinfo["parentTemplates"]: |
| 68 | + host_template_list.append(t["name"]) |
| 69 | + #print "host %s exist, hostid : %s, group: %s, template: %s " % (hostname, hostid, host_group_list, host_template_list) |
| 70 | + print "host %s exist, hostid : %s, group: %s" % (hostname, hostid, host_group_list) |
| 71 | + except: |
| 72 | + print "host not exist: %s" %hostname |
| 73 | + else: |
| 74 | + try: |
| 75 | + hostinfo = zapi.host.get({"filter":{"host":hostname},"output":"hostid", "selectGroups": "extend", "selectParentTemplates": ["templateid","name"]})[0] |
| 76 | + hostid = hostinfo["hostid"] |
| 77 | + host_group_list = [] |
| 78 | + host_template_list = [] |
| 79 | + for l in hostinfo["groups"]: |
| 80 | + host_group_list.append(l["name"]) |
| 81 | + for t in hostinfo["parentTemplates"]: |
| 82 | + host_template_list.append(t["name"]) |
| 83 | + print "host %s exist, hostid : %s, group: %s, template: %s " % (hostname, hostid, host_group_list, host_template_list) |
| 84 | + except: |
| 85 | + print "host not exist: %s" %hostname |
0 commit comments