add network packetLostRate

pull/55/head
ubuntu 2018-08-28 16:58:01 +08:00
parent 232b12814a
commit 00e71b08c4
1 changed files with 47 additions and 4 deletions

View File

@ -11,17 +11,15 @@ USER = "s01"
PASSWORD = "USER_DEFAULT_PASSWORD" PASSWORD = "USER_DEFAULT_PASSWORD"
INTERVAL = 1 #更新间隔 INTERVAL = 1 #更新间隔
import socket import socket
import time import time
import string
import math
import re import re
import os import os
import sys import sys
import json import json
import subprocess import subprocess
import collections import collections
import threading
def get_uptime(): def get_uptime():
f = open('/proc/uptime', 'r') f = open('/proc/uptime', 'r')
@ -129,7 +127,6 @@ def liuliang():
NET_OUT += int(netinfo[0][9]) NET_OUT += int(netinfo[0][9])
return NET_IN, NET_OUT return NET_IN, NET_OUT
# todo: 不确定是否要用多线程or多进程: 效率? 资源? 
def ip_status(): def ip_status():
object_check = ['www.10010.com', 'www.189.cn', 'www.10086.cn'] object_check = ['www.10010.com', 'www.189.cn', 'www.10086.cn']
ip_check = 0 ip_check = 0
@ -159,6 +156,52 @@ def get_network(ip_version):
pass pass
return False 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__': if __name__ == '__main__':
for argc in sys.argv: for argc in sys.argv:
if 'SERVER' in argc: if 'SERVER' in argc: