Linux打印网卡RX和TX信息

急着测试,宿便改了改,加了TX信息,全局变量太弱了,啥时候改成yield

#!/usr/bin/env python
######################################
#nic_traffic.pl => nic_status.py
#HuiLi
#2015-07-07
######################################
import sys
import time
import os
import re
import copy
#import threading

class Status(object):
    def __init__(self):
        self.color = {
            'purple': '\033[1;35;2m',
            'red': '\033[1;31;2m',
            'green': '\033[1;32;2m',
            'default': '\033[0;0m'
        }
        self.freshtime = 1
        self.maxnic = 4
        self.width = 10
        self.G = 1024 * 1024 * 1024
        self.M = 1024 * 1024
        self.K = 1024
        self.commlen = 120

    def __printf(self, string):
        sys.stdout.write(string)
        sys.stdout.flush()

    def __purple(self, string):
        return '%s%s%s' %(self.color['purple'], string, self.color['default'])

    def __red(self, string):
        return '%s%s%s' %(self.color['red'], string, self.color['default'])

    def __green(self, string):
        return '%s%s%s' %(self.color['green'], string, self.color['default'])
	
    def __theme(self, front, end, num, bool):
        num = self.__int2str(num)
        if bool == 'wrong':
            self.__printf('| ' + front + self.__red(num) + end + '\n')
        elif bool == 'right':
            self.__printf('| ' + front + self.__green(num) + end + '	')
        else:
            print 'color flag error'
            sys.exit(0)
	
    def __gettime(self):
        nowtime = time.strftime('%Y-%m-%d %H:%M:%S')
        return nowtime

    def __parser(self, nic):
        data = {}
        pattern_nic = '%s:' %nic
        try:
            nicpf = open('/proc/net/dev', 'r')
        except IOError, e:
            sys.exit(0)
        for eachLine in nicpf:
            eachLine = eachLine.strip()
            info_nic = re.search(pattern_nic, eachLine)
            if info_nic is not None:
                nic_status = eachLine.split()
                data['rx_bytes'] = int(nic_status[1]) * 8
                data['rx_packets'] = int(nic_status[2])
                data['rx_dropped'] = int(nic_status[4])
                data['tx_bytes'] = int(nic_status[9]) * 8
                data['tx_packets'] = int(nic_status[10])
                data['tx_dropped'] = int(nic_status[12])
                return data
	
    def __int2str(self, num):
        if num > self.G:
            num = '%.1f G' %(num * 1.0 / self.G)
        elif num > self.M:
            num = '%.1f M' %(num * 1.0 / self.M)
        elif num > self.K:
            num = '%.1f K' %(num * 1.0 / self.K)
        else:
            num = '%.1f  ' %(num * 1.0)
        return num

    def __show(self, nic, b, p, d, flag):
        self.__printf(self.__gettime())
        self.__printf(self.__purple(nic.center(self.width)))
        self.__theme(flag + ' bps:	', '	bits/s', b, 'right')
        self.__theme(flag + ' packets:	', '	pkts/s', p, 'right')
        self.__theme(flag + ' dropped:	', '	pkts/s', d, 'wrong')
	
    def process(self, nic, number):
        self.__count(nic, number)

    def __count(self, nic, number):
        global rx_b0, rx_b1, rx_b2, rx_b3, rx_p0, rx_p1, rx_p2, rx_p3, rx_d0, rx_d1, rx_d2, rx_d3 
        global tx_b0, tx_b1, tx_b2, tx_b3, tx_p0, tx_p1, tx_p2, tx_p3, tx_d0, tx_d1, tx_d2, tx_d3 
        dict = self.__parser(nic)
        if number == 0:
            self.__show(nic, dict['rx_bytes'] - rx_b0, dict['rx_packets'] - rx_p0, dict['rx_dropped'] - rx_d0, 'rx')
            rx_b0, rx_p0, rx_d0 = dict['rx_bytes'], dict['rx_packets'], dict['rx_dropped']
            self.__show(nic, dict['tx_bytes'] - tx_b0, dict['tx_packets'] - tx_p0, dict['tx_dropped'] - tx_d0, 'tx')
            tx_b0, tx_p0, tx_d0 = dict['tx_bytes'], dict['tx_packets'], dict['tx_dropped']
        elif number == 1:
            self.__show(nic, dict['rx_bytes'] - rx_b1, dict['rx_packets'] - rx_p1, dict['rx_dropped'] - rx_d1, 'rx')
            rx_b1, rx_p1, rx_d1 = dict['rx_bytes'], dict['rx_packets'], dict['rx_dropped']
            self.__show(nic, dict['tx_bytes'] - tx_b1, dict['tx_packets'] - tx_p1, dict['tx_dropped'] - tx_d1, 'tx')
            tx_b1, tx_p1, tx_d1 = dict['tx_bytes'], dict['tx_packets'], dict['tx_dropped']
        elif number == 2:
            self.__show(nic, dict['rx_bytes'] - rx_b2, dict['rx_packets'] - rx_p2, dict['rx_dropped'] - rx_d2, 'rx')
            rx_b2, rx_p2, rx_d2 = dict['rx_bytes'], dict['rx_packets'], dict['rx_dropped']
            self.__show(nic, dict['tx_bytes'] - tx_b2, dict['tx_packets'] - tx_p2, dict['tx_dropped'] - tx_d2, 'tx')
            tx_b2, tx_p2, tx_d2 = dict['tx_bytes'], dict['tx_packets'], dict['tx_dropped']
        elif number == 3:
            self.__show(nic, dict['rx_bytes'] - rx_b3, dict['rx_packets'] - rx_p3, dict['rx_dropped'] - rx_d3, 'rx')
            rx_b3, rx_p3, rx_d3 = dict['rx_bytes'], dict['rx_packets'], dict['rx_dropped']
            self.__show(nic, dict['tx_bytes'] - tx_b3, dict['tx_packets'] - tx_p3, dict['tx_dropped'] - tx_d3, 'tx')
            tx_b3, tx_p3, tx_d3 = dict['tx_bytes'], dict['tx_packets'], dict['tx_dropped']
        else:
            print 'Sorry, nic argv more than maxnic !'
            sys.exit(0)

rx_b0 = rx_b1 = rx_b2 = rx_b3 = 0
rx_p0 = rx_p1 = rx_p2 = rx_p3 = 0
rx_d0 = rx_d1 = rx_d2 = rx_d3 = 0
tx_b0 = tx_b1 = tx_b2 = tx_b3 = 0
tx_p0 = tx_p1 = tx_p2 = tx_p3 = 0
tx_d0 = tx_d1 = tx_d2 = tx_d3 = 0

if __name__ == '__main__':
    status = Status()
    while 1:
        number = 0
        print '#' * status.commlen
        for nic in sys.argv[1:]:
            #t = threading.Thread(target = status.process(nic, number))
            #t.start()
            status.process(nic, number)
            number = (number + 1) % status.maxnic 
        time.sleep(status.freshtime)

发表回复