Judging server unreachable from Python script, to Zabbix alarm

Source: Internet
Author: User
Tags python script

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.