[리눅스 쉘 스크립트]CentOS : Network Monitoring
1. 용도 및 목적
별도 프로그램 없이 간단하게 네트워크 사용량 보기 위한 쉘(가상머신 또는 bonding 같은 가상 디바이스에서는 인터페이스 스피드를 못받아오며 추가 입력 필요)
2. 환경 : CentOS 6.9 64bit, /bin/bash
3. 쉘 스크립트
#vi networkmonitor.sh
----------------------------------------------
#!/bin/bash
######################################
#
# Network Monitoring
# by Ru
######################################
NODE=`hostname`
echo '------interfaces with status UP------'
#상태가 up인 네트워크 인터페이스 출력
ip link show |grep "state UP" |cut -d ":" -f2 |grep -v bond*
echo '---------------------------------------'
echo -n 'Choose from interfaces with status UP! : '
#상태가 up인 네트워크 중 사용률 보고 싶은 인터페이스 입력
read NIC
#NICSpeed=`ethtool $NIC |grep Speed |awk '{print $2}'`
NICSpeed=`cat /sys/class/net/$NIC/speed `
#NIC Speed 가 수집 안될 경 우 수동 입력 숫자만
echo $NICSpeed |grep 0 && echo "NICSpeed loading Success" || echo -n "Loading $NIC information failed - NICSpeed Enter(100/1000/10000)Mbps : " ;read NICSpeed
#NICSpeedSet=`ethtool $NIC |grep Speed |awk '{print $2}' |cut -d"M" -f1 |cut -b2-5`
#NICSpeedSet=`cat /sys/class/net/$NIC/speed |cut -b2-5`
NICSpeedSet=`echo $NICSpeed |cut -b2-5`
while true
do
#초단위
DATE=`date +%Y%m%d%H%M%S`
#RXTX byte 1차 수집
RX1=`cat /sys/class/net/$NIC/statistics/rx_bytes`
TX1=`cat /sys/class/net/$NIC/statistics/tx_bytes`
sleep 1
#RXTX byte 2차 수집
RX2=`cat /sys/class/net/$NIC/statistics/rx_bytes`
TX2=`cat /sys/class/net/$NIC/statistics/tx_bytes`
#RXTX error 수집
RXerror=`cat /sys/class/net/$NIC/statistics/rx_errors`
TXerror=`cat /sys/class/net/$NIC/statistics/tx_errors`
#RXTX drop 수집
RXdrop=`cat /sys/class/net/$NIC/statistics/rx_dropped`
TXdrop=`cat /sys/class/net/$NIC/statistics/tx_dropped`
#RXTX overrun collision 수집
RXover=`cat /sys/class/net/$NIC/statistics/rx_over_errors`
Collisions=`cat /sys/class/net/$NIC/statistics/collisions`
#RXTX byte 1초간 계산
TX_BYTE=`expr $TX2 - $TX1`
RX_BYTE=`expr $RX2 - $RX1`
#RXTX byte M단위 변경 20190725
TX_MBYTE=`expr $TX_BYTE / 1000000`
RX_MBYTE=`expr $RX_BYTE / 1000000`
#RXTX byte 를 bit 로 변환
TX_BIT=`expr $TX_BYTE \* 8`
RX_BIT=`expr $RX_BYTE \* 8`
#RXTX bit M단위 변경 20190725
TX_MBIT=`expr $TX_BIT / 1000000`
RX_MBIT=`expr $RX_BIT / 1000000`
#RXTX bit 합
#RXTX_BIT=`expr $TX_BIT + $RX_BIT`
#RXTX bit 합을 NIC MAX bps 로 나눔
#RXTX_Percent=`expr $RXTX_BIT / 10000$NICSpeedSet`
#RXTX bit 분리 Percent NIC MAX bps 20190725
RX_Percent=`expr $RX_BIT / 10000$NICSpeedSet`
TX_Percent=`expr $TX_BIT / 10000$NICSpeedSet`
clear
#아래는 원하는 자료 별로 출력 배치
echo "################################################################"
echo "#### $NODE Server DATE : $DATE"
echo "#### Network Monitoring"
echo "#### by Ru "
echo "################################################################"
echo -e "Byte per second\t [$NIC] TX: $TX_MBYTE MB/s\tRX: $RX_MBYTE MB/s"
echo -e "bit per second\t [$NIC] TX: $TX_MBIT Mb/s\tRX: $RX_MBIT Mb/s"
echo -e "NIC MAX Speed\t [$NIC] $NICSpeed Mbps"
# echo -e "Network Trrafic\t [$NIC] $RXTX_BIT bps\tUsage:$RXTX_Percent%"
echo -e "Network Trrafic RX\t [$NIC] $RX_BIT bps\tUsage:$RX_Percent%"
echo -e "Network Trrafic TX\t [$NIC] $TX_BIT bps\tUsage:$TX_Percent%"
if [ $TX_Percent -le 5 ];then
echo -e "TX Progres bar \t |#-------------------|"
elif [ $TX_Percent -le 10 ];then
echo -e "TX Progres bar \t |##------------------|"
elif [ $TX_Percent -le 15 ];then
echo -e "TX Progres bar \t |###-----------------|"
elif [ $TX_Percent -le 20 ];then
echo -e "TX Progres bar \t |####----------------|"
elif [ $TX_Percent -le 25 ];then
echo -e "TX Progres bar \t |#####---------------|"
elif [ $TX_Percent -le 30 ];then
echo -e "TX Progres bar \t |######--------------|"
elif [ $TX_Percent -le 35 ];then
echo -e "TX Progres bar \t |#######-------------|"
elif [ $TX_Percent -le 40 ];then
echo -e "TX Progres bar \t |########------------|"
elif [ $TX_Percent -le 45 ];then
echo -e "TX Progres bar \t |#########-----------|"
elif [ $TX_Percent -le 50 ];then
echo -e "TX Progres bar \t |##########----------|"
elif [ $TX_Percent -le 55 ];then
echo -e "\033[32mTX Progres bar \t |###########---------|\033[0m"
elif [ $TX_Percent -le 60 ];then
echo -e "\033[32mTX Progres bar \t |############--------|\033[0m"
elif [ $TX_Percent -le 65 ];then
echo -e "\033[32mTX Progres bar \t |#############-------|\033[0m"
elif [ $TX_Percent -le 70 ];then
echo -e "\033[32mTX Progres bar \t |##############------|\033[0m"
elif [ $TX_Percent -le 75 ];then
echo -e "\033[32mTX Progres bar \t |###############-----|\033[0m"
elif [ $TX_Percent -le 80 ];then
echo -e "\033[32mTX Progres bar \t |################----|\033[0m"
elif [ $TX_Percent -le 85 ];then
echo -e "\033[32mTX Progres bar \t |#################---|\033[0m"
elif [ $TX_Percent -le 90 ];then
echo -e "\033[32mTX Progres bar \t |##################--|\033[0m"
elif [ $TX_Percent -le 95 ];then
echo -e "\033[32mTX Progres bar \t |###################-|\033[0m"
elif [ $TX_Percent -le 110 ];then
echo -e "\033[32mTX Progres bar \t |####################|\033[0m"
fi
if [ $RX_Percent -le 5 ];then
echo -e "RX Progres bar \t |#-------------------|"
elif [ $RX_Percent -le 10 ];then
echo -e "RX Progres bar \t |##------------------|"
elif [ $RX_Percent -le 15 ];then
echo -e "RX Progres bar \t |###-----------------|"
elif [ $RX_Percent -le 20 ];then
echo -e "RX Progres bar \t |####----------------|"
elif [ $RX_Percent -le 25 ];then
echo -e "RX Progres bar \t |#####---------------|"
elif [ $RX_Percent -le 30 ];then
echo -e "RX Progres bar \t |######--------------|"
elif [ $RX_Percent -le 35 ];then
echo -e "RX Progres bar \t |#######-------------|"
elif [ $RX_Percent -le 40 ];then
echo -e "RX Progres bar \t |########------------|"
elif [ $RX_Percent -le 45 ];then
echo -e "RX Progres bar \t |#########-----------|"
elif [ $RX_Percent -le 50 ];then
echo -e "\033[32mRX Progres bar \t |##########----------|\033[0m"
elif [ $RX_Percent -le 55 ];then
echo -e "\033[32mRX Progres bar \t |###########---------|\033[0m"
elif [ $RX_Percent -le 60 ];then
echo -e "\033[32mRX Progres bar \t |############--------|\033[0m"
elif [ $RX_Percent -le 65 ];then
echo -e "\033[32mRX Progres bar \t |#############-------|\033[0m"
elif [ $RX_Percent -le 70 ];then
echo -e "\033[32mRX Progres bar \t |##############------|\033[0m"
elif [ $RX_Percent -le 75 ];then
echo -e "\033[32mRX Progres bar \t |###############-----|\033[0m"
elif [ $RX_Percent -le 80 ];then
echo -e "\033[32mRX Progres bar \t |################----|\033[0m"
elif [ $RX_Percent -le 85 ];then
echo -e "\033[32mRX Progres bar \t |#################---|\033[0m"
elif [ $RX_Percent -le 90 ];then
echo -e "\033[32mRX Progres bar \t |##################--|\033[0m"
elif [ $RX_Percent -le 95 ];then
echo -e "\033[32mRX Progres bar \t |###################-|\033[0m"
elif [ $RX_Percent -le 110 ];then
echo -e "\033[32mRX Progres bar \t |####################|bomb\033[0m"
fi
echo -e ""
echo -e "Network Error\t [$NIC] TX : $TXerror\tRX: $RXerror"
echo -e "Network Drop\t [$NIC] TX : $TXdrop\tRX: $RXdrop"
echo -e "RXoverrun\t [$NIC] RX : $RXover"
echo -e "TXCollisions\t [$NIC] TX : $Collisions"
echo "################################################################"
done
----------------------------------------------