1. The python script gets the IP of the unreachable server:
Determine if the host can be reached with a simple ping command. The script based on the python3.3.6,python2.x version of the queue module should be queue.
#!/usr/bin/python#-*- coding: utf-8 -*-from __future__ import print_ Functionimport reimport subprocessimport threadingfrom queue import queuefrom queue import emptydef is_reachable (IP): "according to the return value of the ping command, Determines whether the IP can be ping-pass. If the return value is not 0, the output of the IP ' if subprocess.getstatusoutput (' ping -c 1 {0} '. Format (IP)) [0] != 0: print (ip,end= ', ') def Unreachable_ip (ip_queue): "Do not wait, remove the IP from the IP queue, call the Is_reachable function until the contents of the queue are empty" try: while True: ip = ip_queue.get_nowait () is_reachable (IP) except Empty: &Nbsp; passdef get_unreachable_ip (filename, ip_queue, threads): " Obtain the IP from the IPList file and join the queue. Create 5 threads call unreachable_ip function ' with open (filename, ' RT ') as fin: for line in fin: if line and not re.match (' # ', line): ip_queue.put (Line.split (' \ n ') [0]) for i in range (5): thr = threading. Thread (target=unreachable_ip, args= (Ip_queue,)) thr.start ( ) threads.append (THR) for thr In threads: thr.join () if __name__ == ' __main__ ': filename = '/etc/zabbix/scripts/ Iplist.txt ' ip_queue = queue () threads = []     GET_UNREACHABLE_IP (Filename, ip_queue, threads)
Judging server unreachable from Python script, to Zabbix alarm