删除主机

#!/usr/bin/python
# encoding: utf-8
#filename: host_delete.py
#author: gaohaixiang
#writetime:20190713

import zabbix_base_api  # zabbix-API-自定义库
import time

z_api_con = zabbix_base_api.zabbix_base_api(url='http://192.168.73.9/api_jsonrpc.php')

def getHostid(method,ip,authid):
    data = {
        "output": ["hostid", "host"],
        "filter": {
            "ip": ip
        },
        "selectInterfaces": ["ip"],
        "selectParentTemplates": ["name"]
    }

    responses = z_api_con.json_data(method, data, authid)
    return responses

def hostDelete(method,hostid,authid):
    data = [
        hostid
    ]
    
    responses = z_api_con.json_data(method, data, authid)
    return responses

def main_all(authid):
    file = open("iplist.txt", "r")
    ff = open("host-delete.txt", "a+")
    lists = file.readlines()
    for list in lists:
        ip = list.strip("\n")
        hostid_result = getHostid("host.get", ip, authid)["result"]
        # print (hostid_result)
        if hostid_result:
            hostid = hostid_result[0]["hostid"]
            print (hostid)

            hostdelete = hostDelete("host.delete",hostid,authid)
            print (hostdelete)
            ff.writelines(ip+"\n")

    ff.close()
    file.close()

if __name__ == "__main__":
    starttime = time.time()
    print ("Process is running...")
    authid = z_api_con.authid('gaohaixiang', 'gaohaixiang')
    main_all(authid)
    z_api_con.login_out(authid)
    endtime = time.time()
    print (endtime-starttime)
上一篇:Django 路由视图FBV/CBV


下一篇:FBV与CBV及上传文件示例