From 00e71b08c416a619f37345bb4755a4236ff5d588 Mon Sep 17 00:00:00 2001 From: ubuntu Date: Tue, 28 Aug 2018 16:58:01 +0800 Subject: [PATCH] add network packetLostRate --- clients/client-linux.py | 51 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/clients/client-linux.py b/clients/client-linux.py index 64c3a34..c0d356e 100755 --- a/clients/client-linux.py +++ b/clients/client-linux.py @@ -11,17 +11,15 @@ USER = "s01" PASSWORD = "USER_DEFAULT_PASSWORD" INTERVAL = 1 #更新间隔 - import socket import time -import string -import math import re import os import sys import json import subprocess import collections +import threading def get_uptime(): f = open('/proc/uptime', 'r') @@ -129,7 +127,6 @@ def liuliang(): NET_OUT += int(netinfo[0][9]) return NET_IN, NET_OUT -# todo: 不确定是否要用多线程or多进程: 效率? 资源?  def ip_status(): object_check = ['www.10010.com', 'www.189.cn', 'www.10086.cn'] ip_check = 0 @@ -159,6 +156,52 @@ def get_network(ip_version): pass return False +lostRate = {} +def _ping_thread(host, mark): + output = os.popen('ping -O %s &' % host) + lostCount = 0 + allCount = 0 + startTime = time.time() + while True: + if 'ttl' not in output.readline(): + lostCount += 1 + allCount += 1 + lostRate[mark] = "%.4f" % (float(lostCount) / allCount) + endTime = time.time() + if endTime-startTime > 3600: + lostCount = 0 + allCount = 0 + startTime = endTime + +def get_packetLostRate(): + t1 = threading.Thread( + target=_ping_thread, + kwargs={ + 'host': 'www.10010.com', + 'mark': '10010' + } + ) + t2 = threading.Thread( + target=_ping_thread, + kwargs={ + 'host': 'www.189.cn', + 'mark': '189' + } + ) + t3 = threading.Thread( + target=_ping_thread, + kwargs={ + 'host': 'bj.10086.cn', + 'mark': '10086' + } + ) + t1.setDaemon(True) + t2.setDaemon(True) + t3.setDaemon(True) + t1.start() + t2.start() + t3.start() + if __name__ == '__main__': for argc in sys.argv: if 'SERVER' in argc: