統計ppp接口流量

 
由於特殊原因需要統計***網絡接口的流量,寫個腳本。
                            
 
#!/bin/bash
#pptpd流量監控
#zhaoyun 2012-06-14
while true
do
        #判斷有幾個登陸的接口
        row=$(ip add |grep global |grep ppp |wc -l)
        for ((i=1;i<=$row;i++))
        do
                PPPX=$(ip add |grep global |grep ppp |awk '{print $4,$7}' |awk '{print $2}'|head -n$i |tail -1 )
                ip=`ip add |grep global |grep ppp |awk '{print $4,$7}' |awk -F/ '{print $1" "$2}' |awk '{print $1}' |head -n$i |tail -1`
                FLOWA=/tmp/.flow.$PPPX.a
                RESULT=`ifconfig $PPPX |grep "RX byte" |awk '{print $2"    "$6}' |awk -Fbytes: '{print "INPUT  "$2"OUTPUT  "$3}' `
                echo " $RESULT ">$FLOWA
        done
        sleep 1
        for ((i1=1;i1<=$row;i1++))
        do
                PPPX=$(ip add |grep global |grep ppp |awk '{print $4,$7}' |awk '{print $2}'|head -n$i |tail -1 )
                ip=`ip add |grep global |grep ppp |awk '{print $4,$7}' |awk -F/ '{print $1" "$2}' |awk '{print $1}' |head -n$i |tail -1`
                FLOWB=/tmp/.flow.$PPPX.b
                RESULT=`ifconfig $PPPX |grep "RX byte" |awk '{print $2"    "$6}' |awk -Fbytes: '{print "INPUT  "$2"OUTPUT  "$3}' `
                echo "$RESULT ">$FLOWB
                INPUTB=`awk '{print $2}' $FLOWB`
                OUTPUTB=`awk '{print $4}' $FLOWB`
                INPUTA=`awk '{print $2}' $FLOWA`
                OUTPUTA=`awk '{print $4}' $FLOWA`
                INPUTKB=$(($INPUTB-$INPUTA))
                OUTPUTKB=$(($OUTPUTB-$OUTPUTA))
                a=$(echo $INPUTKB  |grep '-' &>/dev/null; echo $?)
                b=$(echo $OUTPUTKB |grep '-' &>/dev/null; echo $?)
                if [ ! $a -eq 0 ] && [ ! $b -eq 0 ] ; then
                   if [ ! $INPUTKB -eq 0  ] && [ ! $OUTPUTKB -eq 0 ] ; then
                      echo "$ip $PPPX $INPUTKB $OUTPUTKB" >> Flow.log
                   fi
                fi
              unset INPUTKB OUTPUTKB OUTPUTB INPUTB INPUTA OUTPUTA
        done
done
生成報表
 
#!/bin/bash
#zhaoyun
#2012-06-14
#生成統計信息
  echo
  echo "   |------------------------------------"
  echo "   |ip地址          輸入流量   輸出流量"
row=$(awk '{print $1}' Flow.log |sort -u |wc -l)
for ((i=1;i<=$row;i++))
do
      >flow.log
      ip=$(awk '{print $1}' Flow.log |sort -u |head -n$i |tail -1)
      grep $ip Flow.log > tmp.flow
      file_row=$(awk 'END {print NR}' tmp.flow)
      for ((i1=1;i1<=$file_row;i1++))
      do
           result=$(head -n$i1 tmp.flow |tail -n1 |awk '{print $1}')
           if [ $result = $ip ] ; then
               result1=$(head -n$i1 tmp.flow |tail -n1 )
               echo $result1 >> flow.log
           fi
      done
   input=$(awk '{print $3}' flow.log | awk '{t+=$1} END {print  t}')
  output=$(awk '{print $4}' flow.log | awk '{t+=$1} END {print  t}')
  if [ $input -gt 1024 ] ; then
     #KB
     input=$(($input/1024))
     inputb=$(echo $input |awk '{print $1"KB"}')
      if [ $input -gt 1024 ] ; then
         #MB
         input=$(($input/1024))
         inputb=$(echo $input |awk '{print $1"MB"}')
         if [ $input -gt 1024 ] ; then
            #GB
            input=$(($input/1024))
            inputb=$(echo $input |awk '{print $1"GB"}')
         fi
      fi
  else
     inputb=$input
  fi
  if [ $output -gt 1024 ] ; then
      #KB
      output=$(($output/1024))
      outputb=$(echo $output |awk '{print $1"KB"}')
      if [ $output -gt 1024 ] ; then
         #MB
         output=$(($output/1024))
         outputb=$(echo $output |awk '{print $1"MB"}')
         if [ $output -gt 1024 ] ; then
           #GB
           output=$(($output/1024))
           outputb=$(echo $output |awk '{print $1"GB"}')
         fi
      fi
  else
    outputb=$output
  fi
  echo "   |------------------------------------"
  echo "   |$ip       $inputb       $outputb   "
done
  echo "   |------------------------------------"
rm -f flow.log  tmp.flow
 
 
效果截圖:
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章