add network packetLostRate

This commit is contained in:
ubuntu 2018-08-28 16:58:01 +08:00
parent 232b12814a
commit 00e71b08c4

@ -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: