diff --git a/clients/client-linux.py b/clients/client-linux.py index cf46ff3..b4ad4b7 100755 --- a/clients/client-linux.py +++ b/clients/client-linux.py @@ -161,47 +161,56 @@ lostRate = { '189': 0.0, '10086': 0.0 } -def _ping_thread(host, mark): - output = os.popen('ping -O %s &' % host) - lostCount = 0 - allCount = 0 +def _ping_thread(host, mark, port): + lostPacket = 0 + allPacket = 0 startTime = time.time() - output.readline() + while True: - buffer = output.readline() - if len(buffer) == 0: - return - if 'ttl' not in buffer: - lostCount += 1 - allCount += 1 - if allCount > 100: - lostRate[mark] = float(lostCount) / allCount + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.settimeout(1) + try: + s.connect((host, port)) + except: + lostPacket += 1 + finally: + allPacket += 1 + s.close() + + if allPacket > 100: + lostRate[mark] = float(lostPacket) / allPacket + endTime = time.time() - if endTime-startTime > 3600: - lostCount = 0 - allCount = 0 + if endTime - startTime > 3600: + lostPacket = 0 + allPacket = 0 startTime = endTime + time.sleep(1) + def get_packetLostRate(): t1 = threading.Thread( target=_ping_thread, kwargs={ 'host': 'cu.tz.cloudcpp.com', - 'mark': '10010' + 'mark': '10010', + 'port': 443 } ) t2 = threading.Thread( target=_ping_thread, kwargs={ 'host': 'ct.tz.cloudcpp.com', - 'mark': '189' + 'mark': '189', + 'port': 443 } ) t3 = threading.Thread( target=_ping_thread, kwargs={ 'host': 'cm.tz.cloudcpp.com', - 'mark': '10086' + 'mark': '10086', + 'port': 443 } ) t1.setDaemon(True) diff --git a/clients/client-psutil.py b/clients/client-psutil.py index 24de503..f054426 100755 --- a/clients/client-psutil.py +++ b/clients/client-psutil.py @@ -129,48 +129,56 @@ lostRate = { '189': 0.0, '10086': 0.0 } -def _ping_thread(host, mark): - output = os.popen('ping -O %s &' % host if 'linux' in sys.platform else 'ping %s -t &' % host) - lostCount = 0 - allCount = 0 +def _ping_thread(host, mark, port): + lostPacket = 0 + allPacket = 0 startTime = time.time() - output.readline() - output.readline() + while True: - buffer = output.readline() - if len(buffer) == 0: - return - if 'TTL' not in buffer.upper(): - lostCount += 1 - allCount += 1 - if allCount > 100: - lostRate[mark] = float(lostCount) / allCount + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.settimeout(1) + try: + s.connect((host, port)) + except: + lostPacket += 1 + finally: + allPacket += 1 + s.close() + + if allPacket > 100: + lostRate[mark] = float(lostPacket) / allPacket + endTime = time.time() - if endTime-startTime > 3600: - lostCount = 0 - allCount = 0 + if endTime - startTime > 3600: + lostPacket = 0 + allPacket = 0 startTime = endTime + time.sleep(1) + def get_packetLostRate(): t1 = threading.Thread( target=_ping_thread, kwargs={ 'host': 'cu.tz.cloudcpp.com', - 'mark': '10010' + 'mark': '10010', + 'port': 443 } ) t2 = threading.Thread( target=_ping_thread, kwargs={ 'host': 'ct.tz.cloudcpp.com', - 'mark': '189' + 'mark': '189', + 'port': 443 } ) t3 = threading.Thread( target=_ping_thread, kwargs={ 'host': 'cm.tz.cloudcpp.com', - 'mark': '10086' + 'mark': '10086', + 'port': 443 } ) t1.setDaemon(True)