python小脚本,linux下实时查看网卡流量与pps
个人博客地址
可自定义刷新频率,最低为1秒,运行结束后进行结果汇总
#! /usr/bin/env python #-*-coding:utf-8-*- from __future__ import division from ast import Raise from operator import invert import time,sys,os import signal class Traffic: def __init__(self,name,interval): signal.signal(signal.SIGINT, self.signal_handler) self.bps_in_max = 0 self.bps_out_max = 0 self.bps_in_min = 999999999999 self.bps_out_min = 999999999999 self.bps_in_sum = 0 self.bps_out_sum = 0 self.pps_in_max = 0 self.pps_out_max = 0 self.pps_in_min = 999999999999 self.pps_out_min = 999999999999 self.pps_in_sum = 0 self.pps_out_sum = 0 self.count = 0 self.name = name self.interval = int(interval) self.rxpath = '/sys/class/net/' + name + '/statistics/rx_bytes' self.txpath = '/sys/class/net/' + name + '/statistics/tx_bytes' self.ppsrxpath = '/sys/class/net/' + name + '/statistics/rx_packets' self.ppstxpath = '/sys/class/net/' + name + '/statistics/tx_packets' self.starttime = time.strftime('%Y-%m-%d %H:%M:%S') def init_ret(self): with open(self.rxpath,'r') as t: bpsrx = int(t.read().strip()) with open(self.txpath,'r') as t: bpstx = int(t.read().strip()) with open(self.ppsrxpath,'r') as t: ppsrx = int(t.read().strip()) with open(self.ppstxpath,'r') as t: ppstx = int(t.read().strip()) return bpsrx,bpstx,ppsrx,ppstx def run(self): init_rx,init_tx,initpps_rx,initpps_tx = self.init_ret() time.sleep(self.interval) while True: with open(self.rxpath,'r') as t: rx = int(t.read().strip()) with open(self.txpath,'r') as t: tx = int(t.read().strip()) with open(self.ppsrxpath,'r') as t: ppsrx = int(t.read().strip()) with open(self.ppstxpath,'r') as t: ppstx = int(t.read().strip()) rx_ret=round((rx-init_rx)*8/1024/1024,2) tx_ret=round((tx-init_tx)*8/1024/1024,2) ppsrx_ret = ppsrx-initpps_rx ppstx_ret = ppstx-initpps_tx self.bps_in_max = max(rx_ret,self.bps_in_max) self.bps_out_max = max(tx_ret,self.bps_out_max) self.bps_in_min = min(rx_ret,self.bps_in_min) self.bps_out_min = min(tx_ret,self.bps_out_min) self.bps_in_sum += rx_ret self.bps_out_sum += tx_ret self.pps_in_max = max(ppsrx_ret,self.pps_in_max) self.pps_out_max = max(ppstx_ret,self.pps_out_max) self.pps_in_min = min(ppsrx_ret,self.pps_in_min) self.pps_out_min = min(ppstx_ret,self.pps_out_min) self.pps_in_sum += ppsrx_ret self.pps_out_sum += ppstx_ret self.count+=1 bps_in_show = 'incoming: %sMbps'%rx_ret bps_out_show = 'outgoing: %sMbps'%tx_ret pps_in_show = 'incoming: %spps'%ppsrx_ret pps_out_show = 'outgoing: %spps'%ppstx_ret os.system('clear -x') print('-----------'+self.name+' '+str(self.interval)+' second interval bps monitoring'+'-----------') print(bps_in_show) print(bps_out_show) print('-----------'+self.name+' '+str(self.interval)+' second interval pps monitoring'+'-----------') print(pps_in_show) print(pps_out_show) time.sleep(self.interval) init_rx,init_tx,initpps_rx,initpps_tx=rx,tx,ppsrx,ppstx def signal_handler(self,*args, **kwargs): try: if self.count!=0: os.system('reset') print('---------------------------%s至%s----------------------------'%(self.starttime,time.strftime('%Y-%m-%d %H:%M:%S'))) print('------------%s bps------------'%self.name) print(' min max avg') print('incoming %sM %sM %sM'%(self.bps_in_min,self.bps_in_max,round(self.bps_in_sum/self.count,2))) print('outgoing %sM %sM %sM'%(self.bps_out_min,self.bps_out_max,round(self.bps_out_sum/self.count,2))) print('------------%s pps------------'%self.name) print(' min max avg') print('incoming %spps %spps %spps'%(self.pps_in_min,self.pps_in_max,round(self.pps_in_sum/self.count,2))) print('outgoing %spps %spps %spps'%(self.pps_out_min,self.pps_out_max,round(self.pps_out_sum/self.count,2))) except: os._exit(0) os._exit(0) if __name__ == '__main__': try: name = sys.argv[1] interval = int(sys.argv[2]) if interval < 1: raise ValueError('interval must gt 1') obj = Traffic(name,interval) obj.run() except Exception as e: print(e) print('argv:') print('NetworkName,interval') print('example:') print(' ./networkpps eth0 1')
已打包,可下载使用
链接:https://pan.baidu.com/s/1qSqAS6oCxO1n0QfTO_LyaA
提取码:7297
以驱魔为理想,为生计而奔波