Authored by root

Add zabbix.py to decomission hosts

  1 +#!/usr/bin/python
  2 +# -*- coding: UTF-8 -*-
  3 +# author tiexin.yang@yoho.cn
  4 +
  5 +"""
  6 +删除zabbix configura里指定ip的host
  7 +此操作不可回滚,确认ip无误后再执行
  8 +
  9 +"""
  10 +
  11 +import json,sys,argparse
  12 +from zabbix_api import ZabbixAPI
  13 +server = "http://zabbix.yohops.com"
  14 +username = "yohoops"
  15 +password = "rknTpkVa@6ItBP"
  16 +zapi = ZabbixAPI(server=server, path="", log_level=0)
  17 +zapi.login(username, password)
  18 +
  19 +def get_args():
  20 + parser = argparse.ArgumentParser()
  21 + parser.add_argument("-H", "--hosts", help="host ips")
  22 + # 解析所传入的参数
  23 + args = parser.parse_args()
  24 + if not args.hosts:
  25 + args.hosts = raw_input('hosts(ip): ')
  26 + return args
  27 +
  28 +def get_host_id(hosts):
  29 + get_host_id = zapi.host.get(
  30 + {
  31 + "output": "hostid",
  32 + "filter": {
  33 + "host": hosts.split(",")
  34 + }
  35 + }
  36 + )
  37 + host_id = []
  38 + host_id.append([host["hostid"] for host in get_host_id])
  39 + return host_id[0]
  40 +
  41 +def delete_host(hosts_id):
  42 + if hosts_id:
  43 + hosts_delete = zapi.host.delete(hosts_id)
  44 + return "host delete success!"
  45 + else:
  46 + return "host not found!"
  47 +
  48 +if __name__ == "__main__":
  49 + args = get_args()
  50 + hosts_id = get_host_id(args.hosts)
  51 + print delete_host(hosts_id)