网卡流量监控脚本

来源:互联网 发布:知乎上传图片 编辑:程序博客网 时间:2024/05/01 19:20

1、\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

#!/bin/bash

eth=eth0
RXpre=$(ifconfig ${eth} | grep bytes | awk  '{print $2}'| awk -F":" '{print $2}')
TXpre=$(ifconfig ${eth} | grep bytes | awk '{print $6}' | awk -F":" '{print $2}')
sleep 1
RXnext=$(ifconfig ${eth} | grep bytes | awk  '{print $2}'| awk -F":" '{print $2}')
TXnext=$(ifconfig ${eth} | grep bytes | awk '{print $6}' | awk -F":" '{print $2}')


echo RX ----- TX

echo "$(((${RXnext}-${RXpre})/1024))KB/s   $(((${TXnext}-${TXpre})/1024))KB/s"


2、\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

#!/bin/bash
while [ "1" ]
do
eth=$1
RXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
TXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
sleep 1
RXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
TXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
clear
echo  -e  "\t RX `date +%k:%M:%S` TX"
RX=$((${RXnext}-${RXpre}))
TX=$((${TXnext}-${TXpre}))


if [[ $RX -lt 1024 ]];then
RX="${RX}B/s"
elif [[ $RX -gt 1048576 ]];then
RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
else
RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
fi


if [[ $TX -lt 1024 ]];then
TX="${TX}B/s"
elif [[ $TX -gt 1048576 ]];then
TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
else
TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
fi


echo -e "$eth \t $RX   $TX "
done

3、\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

#!/bin/bash
# osdba 2008.10.22 monitor the interface's network traffic.
#   Zeuslion 2009.08.29.
if [ $# -ne 3 ];then
   echo Useage : $0 interface interval count
   echo Example: $0 eth0 2 10
   exit
fi
eth=$1
count=$3
interval=$2
inbytesfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $2}')
if [ -z "$inbytesfirst" ];then
    echo The network interface $eth is not exits!
    exit 1;
fi
outbytesfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $10}')
inpacketsfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $3}')
outpacketsfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $11}')
sleep $interval"s"
i=0
while [ "$i" -lt "$count" ]
do
   inbytesend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $2}')
   outbytesend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $10}')
   inpacketsend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $3}')
   outpacketsend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $11}')


   bytesin=$(($inbytesend-$inbytesfirst))
   bytesout=$(($outbytesend-$outbytesfirst))
   packetsin=$(($inpacketsend-$inpacketsfirst))
   packetsout=$(($outpacketsend-$outpacketsfirst))


   if [ "$bytesin" -lt "0" ];then
      bytesin=$((4294967295-$inbytesfirst+$inbytesend))
      #echo bytesin $bytesin $inbytesfirst $inbytesend
   fi
   if [ "$bytesout" -lt "0" ];then
      bytesout=$((4294967295-$outbytesfirst+$outbytesend))
      #echo bytesout $bytesout $outbytesfirst $outbytesend
   fi
   if [ "$packetsin" -lt "0" ];then
      packetsin=$((4294967295-$inpacketsfirst+$inpacketsend))
      #echo packetsin $packetsin $inpacketsfirst $inpacketsend
   fi
   if [ "$packetsout" -lt "0" ];then
      packetsout=$((4294967295-$outpacketsfirst+$outpacketsend))
      #echo packetsout $packetsout $outpacketsfirst $outpacketsend
   fi


   bytesin=$(($bytesin/$interval))
   bytesout=$(($bytesout/$interval))
   packetsin=$(($packetsin/$interval))
   packetsout=$(($packetsout/$interval))


   sumbytesin=$(($sumbytesin+$bytesin))
   sumbytesout=$(($sumbytesout+$bytesout))
   sumpacketsin=$(($sumpacketsin+$packetsin))
   sumpacketsout=$(($sumpacketsout+$packetsout))


   if [ $(($i%20)) -eq 0 ];then
      echo " ifname   | in_kbits/s out_kbits/s | in_kBytes/s out_kBytes/s | in_packets/s out_packets/s"
      echo "--------- | ---------- ----------- | ----------- ------------ | ------------ -------------"
   fi
   echo $eth $bytesin $bytesout $packetsin $packetsout |awk '{printf("%9s | %10d %11d | %11d %12d | %12d %13d\n",$1,$2/128,$3/128,$2/1024,$3/1024,$4,$5)}'
   inbytesfirst=$inbytesend
   outbytesfirst=$outbytesend
   inpacketsfirst=$inpacketsend
   outpacketsfirst=$outpacketsend
   
   i=$(($i+1))
   sleep $interval"s"
done


sumbytesin=$(($sumbytesin/$i))
sumbytesout=$(($sumbytesout/$i))
sumpacketsin=$(($sumpacketsin/$i))
sumpacketsout=$(($sumpacketsout/$i))


echo "--------- | ---------- ----------- | ----------- ------------ | ------------ -------------"
echo Average $sumbytesin $sumbytesout $sumpacketsin $sumpacketsout |awk '{printf("%9s | %10d %11d | %11d %12d | %12d %13d\n",$1,$2/128,$3/128,$2/1024,$3/1024,$4,$5)}'
输出示例:
[root@localhost nat]# ./ifstat.sh eth1 2 5
ifname   | in_kbits/s out_kbits/s | in_kBytes/s out_kBytes/s | in_packets/s out_packets/s
--------- | ---------- ----------- | ----------- ------------ | ------------ -------------
     eth1 |     204743      112824 |       25592        14103 |        38459         38172
     eth1 |     212353      111547 |       26544        13943 |        39025         38152
     eth1 |     204263      114007 |       25532        14250 |        38802         38233
     eth1 |     208357      111877 |       26044        13984 |        38706         38123
     eth1 |     215614      113400 |       26951        14175 |        39950         39375
--------- | ---------- ----------- | ----------- ------------ | ------------ -------------
Average |     209066      112731 |       26133        14091 |        38988         38411

4、\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

!/bin/bash
#  osdba 2008.10.22 monitor the interface's network traffic.


#显示正确的信息,一般以绿色显示
function OkMsg()
{
        echo -e "\033[32m$*\033[m"
}


#显示错误的信息,一般以红色带闪烁显示
function ErrMsg()
{
        echo -e "\033[0;31;5m$*\033[m"
}


#显示提示的信息,一般以黄色显示
function TipMsg()
{
        echo -e "\033[33m$*\033[m"
}
WRITELOG()
{
        OkMsg "网络流量异常"
}


CHARKFLOW()
{
        inbytesfirst=$(cat /proc/net/dev |tr ':' ' '|awk  '/'$eth'/{print $2}')
        if [ -z "$inbytesfirst" ];then
                ErrMsg "The network interface $eth is not exits!"
                return 1000
        fi
        sleep $interval"s"
        inbytesend=$(cat /proc/net/dev |tr ':' ' '|awk  '/'$eth'/{print $2}')
        sumbytesin=$((($inbytesend-$inbytesfirst)/$interval))
        #echo "ifname" "in_bytes/s"|awk '{printf("%10s %16s\n",$1,$2)}'
        OkMsg " ifname   in_Mbytes/s "
        OkMsg "--------- -----------"
        OkMsg $eth $sumbytesin |awk '{printf("%9s %11.1d \n",$1,($2/1024)/1024)}'
        return  $(($(($sumbytesin/1024))/1024))
}
################################################################
#执行部分
################################################################
if [ $# -ne 4 ];then
   echo example: $0 "网卡" "统计时间:S" "下限流量值:M" "连续低于下限多少次写日志"
   echo example: $0 eth0 1 10 1 3
   exit
fi
eth=$1
interval=$2
FailCount=0
while true
do
FailCount=0
CHARKFLOW
FLOW=$?
if [ $FLOW -lt $3 ];then
        FailCount=$[FailCount+1]
        while [ $FailCount -lt $4 ]
        do
                CHARKFLOW
                FLOW=$?
                if [ $FLOW -lt $3 ];then
                        FailCount=$[FailCount+1]
                else
                        FailCount=0
                        break
                fi
                if [ $FailCount -ge $4 ];then
                        WRITELOG
                fi
        done
fi
done

5、\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

#!/bin/bash
#  osdba 2008.10.22 monitor the interface's network traffic.
if [ $# -ne 3 ];then
   echo example: $0 eth0 1 10
   exit
fi
eth=$1
count=$3
interval=$2
inbytesfirst=$(cat /proc/net/dev |tr ':' ' '|awk  '/'$eth'/{print $2}')
if [ -z "$inbytesfirst" ];then
    echo The network interface $eth is not exits!
    exit 1;
fi
outbytesfirst=$(cat /proc/net/dev |tr ':' ' '|awk  '/'$eth'/{print $10}')
inpacketsfirst=$(cat /proc/net/dev |tr ':' ' '|awk  '/'$eth'/{print $3}')
outpacketsfirst=$(cat /proc/net/dev |tr ':' ' '|awk  '/'$eth'/{print $11}')
sleep $interval"s"
i=0
while [ "$i" -lt "$count" ]
do
   inbytesend=$(cat /proc/net/dev |tr ':' ' '|awk  '/'$eth'/{print $2}')
   outbytesend=$(cat /proc/net/dev |tr ':' ' '|awk  '/'$eth'/{print $10}')
   inpacketsend=$(cat /proc/net/dev |tr ':' ' '|awk  '/'$eth'/{print $3}')
   outpacketsend=$(cat /proc/net/dev |tr ':' ' '|awk  '/'$eth'/{print $11}')
   sumbytesin=$((($inbytesend-$inbytesfirst)/$interval))
   sumbytesout=$((($outbytesend-$outbytesfirst)/$interval))
   sumpacketsin=$((($inpacketsend-$inpacketsfirst)/$interval))
   sumpacketsout=$((($outpacketsend-$outpacketsfirst)/$interval))
   
   sumbytes=$(($sumbytesin+$sumbytesout))
   sumpackets=$(($sumpacketsin+$sumpacketsout))
   if [ $(($i%20)) -eq 0 ];then
       #echo "ifname" "in_bytes/s" "out_bytes/s" "total_bytes/s" |awk '{printf("%10s %16s %16s %16s\n",$1,$2,$3,$4)}'
    echo " ifname   in_kbytes/s out_kbytes/s all_kbytes/s in_packets/s out_packets/s all_packets/s"
    echo "--------- ----------- ------------ ------------ ------------ ------------- -------------"
   fi
   echo $eth $sumbytesin $sumbytesout $sumbytes $sumpacketsin $sumpacketsout $sumpackets |awk '{printf("%9s %11.1d %12.1d %12.1d %12s %13s %13s\n",$1,$2/1024,$3/1024,$4/1024,$5,$6,$7)}'
   inbytesfirst=$inbytesend
   outbytesfirst=$outbytesend
   inpacketsfirst=$inpacketsend
   outpacketsfirst=$outpacketsend 
   i=$(($i+1))
   sleep $interval"s"
done



          

原创粉丝点击