@@ -148,6 +148,32 @@ def get_cpu_model():
148148 return vendor
149149 return get_platform_cpu_arch ()
150150
151+ def get_os_name ():
152+ try :
153+ sysname = platform .system ().lower ()
154+ if sysname .startswith ('windows' ):
155+ return 'windows'
156+ if sysname .startswith ('darwin' ) or 'mac' in sysname :
157+ return 'darwin'
158+ if 'bsd' in sysname :
159+ return 'bsd'
160+ if sysname .startswith ('linux' ):
161+ os_name = 'linux'
162+ try :
163+ with open ('/etc/os-release' ) as f :
164+ for line in f :
165+ if line .startswith ('ID=' ):
166+ value = line .strip ().split ('=' , 1 )[1 ].strip ().strip ('"' )
167+ if value :
168+ os_name = value
169+ break
170+ except Exception :
171+ pass
172+ return os_name
173+ return sysname or 'unknown'
174+ except Exception :
175+ return 'unknown'
176+
151177def _get_net_io_counters ():
152178 with _net_io_counters_lock :
153179 return psutil .net_io_counters (pernic = True )
@@ -236,6 +262,22 @@ def get_network(ip_version):
236262}
237263monitorServer = {}
238264
265+ def update_net_speed (avgrx , avgtx , now_clock = None ):
266+ if now_clock is None :
267+ now_clock = time .monotonic ()
268+ previous_clock = netSpeed .get ("clock" , 0.0 )
269+ previous_rx = netSpeed .get ("avgrx" , 0 )
270+ previous_tx = netSpeed .get ("avgtx" , 0 )
271+ diff = now_clock - previous_clock
272+ initialized = previous_clock > 0 and diff > 0
273+ netSpeed ["diff" ] = diff if initialized else 0.0
274+ netSpeed ["clock" ] = now_clock
275+ netSpeed ["netrx" ] = int ((avgrx - previous_rx ) / diff ) if initialized and avgrx >= previous_rx else 0
276+ netSpeed ["nettx" ] = int ((avgtx - previous_tx ) / diff ) if initialized and avgtx >= previous_tx else 0
277+ netSpeed ["avgrx" ] = avgrx
278+ netSpeed ["avgtx" ] = avgtx
279+ return netSpeed ["netrx" ], netSpeed ["nettx" ]
280+
239281def _ping_thread (host , mark , port ):
240282 lostPacket = 0
241283 packet_queue = Queue (maxsize = PING_PACKET_HISTORY_LEN )
@@ -286,13 +328,7 @@ def _net_speed():
286328 continue
287329 avgrx += stats .bytes_recv
288330 avgtx += stats .bytes_sent
289- now_clock = time .time ()
290- netSpeed ["diff" ] = now_clock - netSpeed ["clock" ]
291- netSpeed ["clock" ] = now_clock
292- netSpeed ["netrx" ] = int ((avgrx - netSpeed ["avgrx" ]) / netSpeed ["diff" ])
293- netSpeed ["nettx" ] = int ((avgtx - netSpeed ["avgtx" ]) / netSpeed ["diff" ])
294- netSpeed ["avgrx" ] = avgrx
295- netSpeed ["avgtx" ] = avgtx
331+ update_net_speed (avgrx , avgtx )
296332 time .sleep (INTERVAL )
297333
298334def _disk_io ():
@@ -570,31 +606,7 @@ def byte_str(object):
570606 array ['tcp' ], array ['udp' ], array ['process' ], array ['thread' ] = tupd ()
571607 array ['io_read' ] = diskIO .get ("read" )
572608 array ['io_write' ] = diskIO .get ("write" )
573- # report OS (normalized)
574- try :
575- sysname = platform .system ().lower ()
576- if sysname .startswith ('windows' ):
577- os_name = 'windows'
578- elif sysname .startswith ('darwin' ) or 'mac' in sysname :
579- os_name = 'darwin'
580- elif 'bsd' in sysname :
581- os_name = 'bsd'
582- elif sysname .startswith ('linux' ):
583- # try distro from os-release
584- try :
585- with open ('/etc/os-release' ) as f :
586- for line in f :
587- if line .startswith ('ID=' ):
588- val = line .strip ().split ('=' ,1 )[1 ].strip ().strip ('"' )
589- if val : os_name = val
590- break
591- except Exception :
592- os_name = 'linux'
593- else :
594- os_name = sysname or 'unknown'
595- except Exception :
596- os_name = 'unknown'
597- array ['os' ] = os_name
609+ array ['os' ] = get_os_name ()
598610 items = []
599611 for _n , st in monitorServer .items ():
600612 key = str (_n )
0 commit comments