mirror of
https://github.com//cppla/ServerStatus
synced 2025-06-02 21:59:51 +08:00
修复两个严重的bug,原计算方式会导致网速奇高。
This commit is contained in:
parent
3ab51d34a4
commit
2f7acc3564
@ -3,7 +3,7 @@
|
||||
# Update by : https://github.com/cppla/ServerStatus
|
||||
# 支持Python版本:2.7 to 3.7
|
||||
# 支持操作系统: Linux, OSX, FreeBSD, OpenBSD and NetBSD, both 32-bit and 64-bit architectures
|
||||
# 时间: 20200401
|
||||
# 时间: 20200407
|
||||
# 说明: 默认情况下修改server和user就可以了。丢包率监测方向可以自定义,例如:CU = "www.facebook.com"。
|
||||
|
||||
SERVER = "127.0.0.1"
|
||||
@ -31,12 +31,9 @@ import collections
|
||||
import threading
|
||||
|
||||
def get_uptime():
|
||||
f = open('/proc/uptime', 'r')
|
||||
uptime = f.readline()
|
||||
f.close()
|
||||
uptime = uptime.split('.', 2)
|
||||
time = int(uptime[0])
|
||||
return int(time)
|
||||
with open('/proc/uptime', 'r') as f:
|
||||
uptime = f.readline().split('.', 2)
|
||||
return int(uptime[0])
|
||||
|
||||
def get_memory():
|
||||
re_parser = re.compile(r'^(?P<key>\S*):\s*(?P<value>\d*)\s*kB')
|
||||
@ -61,12 +58,11 @@ def get_hdd():
|
||||
return int(size), int(used)
|
||||
|
||||
def get_time():
|
||||
stat_file = open("/proc/stat", "r")
|
||||
time_list = stat_file.readline().split(' ')[2:6]
|
||||
stat_file.close()
|
||||
for i in range(len(time_list)) :
|
||||
time_list[i] = int(time_list[i])
|
||||
return time_list
|
||||
with open("/proc/stat", "r") as f:
|
||||
time_list = f.readline().split(' ')[2:6]
|
||||
for i in range(len(time_list)) :
|
||||
time_list[i] = int(time_list[i])
|
||||
return time_list
|
||||
|
||||
def delta_time():
|
||||
x = get_time()
|
||||
@ -84,10 +80,23 @@ def get_cpu():
|
||||
result = 100-(t[len(t)-1]*100.00/st)
|
||||
return round(result, 1)
|
||||
|
||||
traffic_clock = time.time()
|
||||
traffic_diff = 0
|
||||
def TCLOCK(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
global traffic_clock, traffic_diff
|
||||
now_clock = time.time()
|
||||
traffic_diff = now_clock - traffic_clock
|
||||
traffic_clock = now_clock
|
||||
return func(*args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
class Traffic:
|
||||
def __init__(self):
|
||||
self.rx = collections.deque(maxlen=10)
|
||||
self.tx = collections.deque(maxlen=10)
|
||||
|
||||
@TCLOCK
|
||||
def get(self):
|
||||
f = open('/proc/net/dev', 'r')
|
||||
net_dev = f.readlines()
|
||||
@ -114,8 +123,8 @@ class Traffic:
|
||||
avgrx += self.rx[x+1] - self.rx[x]
|
||||
avgtx += self.tx[x+1] - self.tx[x]
|
||||
|
||||
avgrx = int(avgrx / l / INTERVAL)
|
||||
avgtx = int(avgtx / l / INTERVAL)
|
||||
avgrx = int(avgrx / l / traffic_diff)
|
||||
avgtx = int(avgtx / l / traffic_diff)
|
||||
|
||||
return avgrx, avgtx
|
||||
|
||||
@ -278,7 +287,7 @@ if __name__ == '__main__':
|
||||
INTERVAL = int(argc.split('INTERVAL=')[-1])
|
||||
socket.setdefaulttimeout(30)
|
||||
get_packetLostRate()
|
||||
while 1:
|
||||
while True:
|
||||
try:
|
||||
print("Connecting...")
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
@ -310,7 +319,7 @@ if __name__ == '__main__':
|
||||
|
||||
traffic = Traffic()
|
||||
traffic.get()
|
||||
while 1:
|
||||
while True:
|
||||
CPU = get_cpu()
|
||||
NetRx, NetTx = traffic.get()
|
||||
NET_IN, NET_OUT = liuliang()
|
||||
|
@ -4,7 +4,7 @@
|
||||
# 依赖于psutil跨平台库
|
||||
# 支持Python版本:2.7 to 3.7
|
||||
# 支持操作系统: Linux, Windows, OSX, Sun Solaris, FreeBSD, OpenBSD and NetBSD, both 32-bit and 64-bit architectures
|
||||
# 时间: 20200401
|
||||
# 时间: 20200407
|
||||
# 说明: 默认情况下修改server和user就可以了。丢包率监测方向可以自定义,例如:CU = "www.facebook.com"。
|
||||
|
||||
SERVER = "127.0.0.1"
|
||||
@ -58,10 +58,23 @@ def get_hdd():
|
||||
def get_cpu():
|
||||
return psutil.cpu_percent(interval=INTERVAL)
|
||||
|
||||
traffic_clock = time.time()
|
||||
traffic_diff = 0
|
||||
def TCLOCK(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
global traffic_clock, traffic_diff
|
||||
now_clock = time.time()
|
||||
traffic_diff = now_clock - traffic_clock
|
||||
traffic_clock = now_clock
|
||||
return func(*args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
class Traffic:
|
||||
def __init__(self):
|
||||
self.rx = collections.deque(maxlen=10)
|
||||
self.tx = collections.deque(maxlen=10)
|
||||
|
||||
@TCLOCK
|
||||
def get(self):
|
||||
avgrx = 0; avgtx = 0
|
||||
for name, stats in psutil.net_io_counters(pernic=True).items():
|
||||
@ -82,8 +95,8 @@ class Traffic:
|
||||
avgrx += self.rx[x+1] - self.rx[x]
|
||||
avgtx += self.tx[x+1] - self.tx[x]
|
||||
|
||||
avgrx = int(avgrx / l / INTERVAL)
|
||||
avgtx = int(avgtx / l / INTERVAL)
|
||||
avgrx = int(avgrx / l / traffic_diff)
|
||||
avgtx = int(avgtx / l / traffic_diff)
|
||||
|
||||
return avgrx, avgtx
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user