Next, I use Python to shut Down the Down hosts in Zabbix through py Zabbix API. The basic idea is
1. Obtain the IP of the previous group 2. Verify whether the host is Down through telnet 3. Shut down the host
Warning: the python script relies heavily on telnet connectivity results. If the network is unstable, the results will be directly affected. I have to think about the solution to this problem again
The following is the script's initial small damo. Mengxin must first test whether the script conforms to your production environment and then execute it in the formal environment
#!/bin/env python import telnetlib import pyzabbix from pyzabbix import ZabbixAPI import os web = input("Input Your Zabbix-Web address:") username = input("API User :") passwd = input("Password:") groupids = input("HostGroup1:15\nHostGroup2:16\nInput Your Group: ") #check Your Password try: zapi = ZabbixAPI(url=web, user=username, password=passwd) print("Certification success") zapi.do_request('user.logout') except: print("User authentication error!! Script exit") os._exit(0) #Get Host IP From Zabbix def ZabbixIP(): zapi = ZabbixAPI(url=web, user=username, password=passwd) #Number of output results obtained result1 = zapi.do_request("host.get", {"groupids":groupids, "output": ["name"], 'countOutput': True}) #Output results with IP result2 = zapi.do_request("host.get", {"groupids":groupids, "output": ["name"], "selectInterfaces": ["interfaces", "ip"]}) host_count = int(result1.get(u'result')) #Initialize the IPList and load the IP later IPList = [] for i in range(host_count): num = i #Output tuples separately VeeeHostList = result2.get(u'result')[num] # print(VeeeHostList) #Get a tuple containing ip eg: {interfaces:["ip":"0.0.0.0"]} HostIP = VeeeHostList.get('interfaces') #To the sequence in the tuple ["ip":"0.0.0.0"] IpTup = HostIP[0] Ip = IpTup.get(r"ip") #print(Ip) IPList.append(Ip) zapi.do_request('user.logout') return IPList #Do telnet to test whether the port is open. That is, check whether the server is online, and shut down the host if it is not online def telenetlib(): WrongIpList = [] RightIpList = [] for ip in IpGroup: try: tn = telnetlib.Telnet(host=ip,port=22,timeout=5) print("telnet The connection is blocked.") RightIpList.append(ip) except: print("there is sometime wrong with " + ip + "!!!") WrongIpList.append(ip) return WrongIpList,RightIpList def UpdateHost(): zapi = ZabbixAPI(url=web, user=username, password=passwd) for Winterface in IpWrongGroup: result3 = zapi.do_request("host.get",{"output":"hostid","selectInterfaces":["interfaces","ip"],"filter":{"ip":Winterface}}) result4 = str(result3.get(u'result')).split('\'') hostid = result4[3] zapi.do_request("host.update",{"hostid":hostid,"status":1}) for Rinterface in IpRightGroup: result3 = zapi.do_request("host.get",{"output":"hostid","selectInterfaces":["interfaces","ip"],"filter":{"ip":Rinterface}}) result4 = str(result3.get(u'result')).split('\'') hostid = result4[3] zapi.do_request("host.update",{"hostid":hostid,"status":0}) zapi.do_request('user.logout') IpGroup = ZabbixIP() IpWrongGroup,IpRightGroup = telenetlib() UpdateHost()
Excuse me, it's gone.
If you have any questions, you can contact the blogger, Telegram: @ tingbob