• tx, rx 값을 1초마다 출력 → 소수 둘째자리까지 출력
  • 1초마다 나타나는 tx와 rx데이터를 그래프로 출력 필요 → 그래프 사용 용도에 대한 고민은 필요
  • 데이터 분석에 사용하기 위한 기초 자료로 사용 예정
  • tx, rx 값 뿐만아니라 CPU, MEMORY, DISK 등도 확장 가능
  • 1초간 tx, rx 확인하는 python3 예제 코드

    #! /bin/python3
    #-*- coding: utf-8 -*-
    from subprocess import Popen
    from subprocess import os
    from subprocess import PIPE
    from datetime import datetime
    import time
    from os import sys
    import signal
    
    def common_cmd(command, args):
        cmd = command + args
          p = Popen(cmd, shell=True, stdout=PIPE)
           (ret, err) = p.communicate()
           return ret
    
    def ifconfig_cmd():
        cmd_args = "| grep 'UP mode' | awk -F ':' '{print $2}'"
        ifconfig_ports = common_cmd('ip link ', cmd_args)
        ifconfig_ports = ifconfig_ports.decode()
        return ifconfig_ports
    
    def rx_cal():
        rx_cat_cmd_args = '/sys/class/net/'
        rx_cat_cmd_args = rx_cat_cmd_args + NIC_PORT +'/statistics/rx_bytes'
        rx_bs = common_cmd('cat ', rx_cat_cmd_args)
        rx_bs = rx_bs.decode()
        return rx_bs
    
    def tx_cal():
        tx_cat_cmd_args = '/sys/class/net/'
        tx_cat_cmd_args = tx_cat_cmd_args + NIC_PORT +'/statistics/tx_bytes'
        tx_bs = common_cmd('cat ', tx_cat_cmd_args)
        tx_bs = tx_bs.decode()
        return tx_bs
    
    def handler(signum, frame):
        print("\n"+"*" * 50 + "CTRL+Z" + "*" * 50)
        print_condition(max_rx, max_tx)
        sys.exit()
    
    def print_ms(rx_max, tx_max):
        now = str(datetime.now())
        now = now.split('.')[0]
        print("%s -> rx : %.2f MB/s\t\t\t\t\t tx : %.2f MB/s" %(now, rx_max, tx_max))
    
    def print_gs(rx_max, tx_max):
        now = str(datetime.now())
        now = now.split('.')[0]
        print("%s -> rx : %.2f GB/s\t\t\t\t\t tx : %.2f GB/s" %(now, rx_max, tx_max))
    
    def print_rx_gs(rx_max, tx_max):
        now = str(datetime.now())
        now = now.split('.')[0]
        print("%s -> rx : %.2f GB/s\t\t\t\t\t tx : %.2f MB/s" %(now, rx_max, tx_max))
    
    def print_tx_gs(rx_max, tx_max):
        now = str(datetime.now())
        now = now.split('.')[0]
        print("%s -> rx : %.2f MB/s\t\t\t\t\t tx : %.2f GB/s" %(now, rx_max, tx_max))
    
    def print_condition(max_rx, max_tx):
        if ((max_rx/1024) >=1):
            if ((max_tx/1024) >=1):
                g_rx_max = max_rx / 1024
                g_tx_max = max_tx / 1024
                print_gs(g_rx_max, g_tx_max)
            else:
                g_rx_max = max_rx / 1024
                print_rx_gs(g_rx_max, max_tx)
        else:
            if ((max_tx/1024) >=1):
                g_tx_max = max_tx / 1024
                print_tx_gs(max_rx, g_tx_max)
            else:
                print_ms(max_rx, max_tx)
    
    def byte_sum(lists):
        sum = lists[0] + lists[1] + lists[2] + lists[3] + lists[4] + lists[5] + lists[6] + lists[7]
        return sum
    
    if __name__ == "__main__":
        port_list = []
        ports = ifconfig_cmd()
        port_list = ports.split('\n')
        for index, value in enumerate(port_list):
            if (value != port_list[len(port_list)-1]):
                print("[%i] : %s\t" % (index, value), end = '')
        print("[99] : exit\n", end = '')
        print("\n위에 있는 NIC 포트는 사용 중인 것입니다.")
        try:
            NIC_PORT = input("원하는nic 포트를 입력하세요 : ")
            if (NIC_PORT == "exit" or NIC_PORT == "99"):
                exit()
            max_rx = 0
            max_tx = 0
            print("1초 간격으로 출력하겠습니다.")
    
            while True:
                rx_bs1 = rx_cal()
                tx_bs1 = tx_cal()
    
                time.sleep(1)
    
                rx_bs2 = rx_cal()
                tx_bs2 = tx_cal()
    
                rx_bs = int(rx_bs2) - int(rx_bs1)
                tx_bs = int(tx_bs2) - int(tx_bs1)
    
                rx_ms = float((rx_bs /1024 / 1024))
                tx_ms = float((tx_bs /1024 / 1024))
    
                rx_max = rx_ms * 8
                tx_max = tx_ms * 8
    
                print_condition(rx_max, tx_max)
    
                if (rx_max > max_rx):
                    max_rx = rx_max
    
                if (tx_max > max_tx):
                    max_tx = tx_max
    
                signal.signal(signal.SIGTSTP, handler)
    
        except KeyboardInterrupt:
            print("\n"+"*" * 50 + "CTRL+C" + "*" * 50)
            print_condition(max_rx, max_tx)
            # 종료
            sys.exit()

  • 1초간 tx, rx 확인하는 python3 실행 결과


+ Recent posts