Python教程

Python3监控IP丢包率

本文主要是介绍Python3监控IP丢包率,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
 
这里的代码是监控区域内的所有IP地址,故障数量达到某个值后就触发告警

import os
import time
import re
import subprocess
import threading
from queue import Queue


ip_queue = Queue()
abnormal_num = 0
call_num = 1
name = os.path.split('C:/Users/Administrator/Desktop/zone')[-1]    # 获取文件名用于告警通知是哪个zone的网络故障
with open('C:/Users/Administrator/Desktop/zone', 'r') as z_ip:
    for line in z_ip:
        z_file = line.strip()
        ip_queue.put(z_file)


def zone(name):
    while not ip_queue.empty():
        ip = ip_queue.get()
        status1 = subprocess.Popen('ping -n 5 {}'.format(ip), stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE, shell=True)
        res1 = str(status1.stdout.read(), encoding='gbk')
        regex1 = re.findall(r'\w*%\w*', res1)[0]  # 获取丢包率,百分比
        loss1 = int(re.findall(r'\d*', regex1)[0])     # 获取丢包率数字

        if loss1 >= 80:
            time.sleep(10)
            status2 = subprocess.Popen('ping -n 5 {}'.format(ip), stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE, shell=True)
            res2 = str(status2.stdout.read(), encoding='gbk')
            print(res2)                               # ping输出值
            regex2 = re.findall(r'\w*%\w*', res2)[0]  # 获取丢包率,百分比
            loss2 = int(re.findall(r'\d*', regex2)[0])     # 获取丢包率数字
            print('loss {}%'.format(loss2))

            if loss2 >= 80:
                global abnormal_num, call_num
                abnormal_num += 1
                down_time = time.strftime("%Y-%m-%d %H:%M:%S")
                print('down time: [{}]; zone: {}; ip: {}; abnormal num: {}'.format(down_time, name, ip, abnormal_num))
                if abnormal_num >= 3 and call_num == 1:   
                    call_num += 1  # 设置只触发一次告警
                    send_sms('down {}'.format(name))  # 这里就可以调用你编写的告警函数,我的代码未贴出来


if __name__ == '__main__':
    for t in range(10):
        z_th = threading.Thread(target=zone, args=(name,))
        z_th.start()
这篇关于Python3监控IP丢包率的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!