fz_all.sh

#!/bin/bash
#mwg@2012-2-5
function usage
{
 echo -e "Usage:  "
 echo -e "\te.g: $0 eth0 16"
 exit 65
}
if [ $# -ne 2 ];then
usage
fi
log=`dirname $0`/fz_all.log
eth=$1
timer=$2
in_old=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $1 }' )
out_old=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $9 }' )
###
####old_cpu
declare -a  oneUse_cpu_old=($(cat /proc/stat |grep "^cpu" |awk '{printf"%d ", $2+$3+$4+$6+$7+$8}'))
declare -a  oneAll_cpu_old=($(cat /proc/stat |grep "^cpu" |awk '{printf"%d " ,$2+$3+$4+$5+$6+$7+$8}'))
##############
while true
do
sleep ${timer}
tim=`date +%Y%m%d_%T`
##############
##mem
Mem_total=`cat /proc/meminfo |grep MemTotal |awk -F '[: ]+' '{print $2}'`
Mem_free=`cat /proc/meminfo |grep MemFree |awk -F '[: ]+' '{print $2}'`
Mem_buffer=`cat /proc/meminfo |grep Buffers |awk -F '[: ]+' '{print $2}'`
Mem_cache=`cat /proc/meminfo |grep '\<Cached\>' |awk -F '[: ]+' '{print $2}'`
Mem_rea_free=`expr $Mem_free + $Mem_buffer + $Mem_cache`
Mem_use=`expr $Mem_total - $Mem_rea_free`
mem=`awk 'BEGIN{printf "%.0f\n",('$Mem_use'/'$Mem_total')*100}'`
##swap
swap_total=`cat /proc/meminfo |grep SwapTotal |awk -F '[: ]+' '{print $2}'`
swap_free=`cat /proc/meminfo |grep SwapFree |awk -F '[: ]+' '{print $2}'`
swap_use=`expr $swap_total - $swap_free`
swap=`awk 'BEGIN{printf "%.0f\n",('$swap_use'/'$swap_total')*100}'`
##load
Load=`cat /proc/loadavg  |awk '{print $1}'`
###################################################
##count
connect=$(netstat -antp |grep -v 0.0.0.0:|grep -c :66[6-7][0-9])
connect1=$(netstat -antp |grep -v 0.0.0.0:|grep -c :6661)
connect2=$(netstat -antp |grep -v 0.0.0.0:|grep -c :6662)
connect3=$(netstat -antp |grep -v 0.0.0.0:|grep -c :6663)
connect4=$(netstat -antp |grep -v 0.0.0.0:|grep -c :6664)
##liuliang
in=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $1 }' )
out=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $9 }' )
dif_in=`echo "($in-$in_old)*8/$timer"|bc`
dif_out=`echo "($out-$out_old)*8/$timer"|bc`
##
dif_in_m=`echo "scale=3;($in-$in_old)*8/$timer/1024/1024"|bc`
dif_out_m=`echo "scale=3;($out-$out_old)*8/$timer/1024/1024"|bc`
##
#one_in=`echo "scale=2;$dif_in/$connect/1024"|bc`
#one_out=`echo "scale=2;$dif_out/$connect/1024"|bc`
####
in_old=${in}
out_old=${out}
##
if [ $dif_in -le 0 ];then
continue
fi
if [ $dif_out -le 0 ];then
continue
fi
#############################################
####cpu
declare -a  oneUse_cpu=($(cat /proc/stat |grep "^cpu" |awk '{printf"%d ", $2+$3+$4+$6+$7+$8}'))
declare -a  oneAll_cpu=($(cat /proc/stat |grep "^cpu" |awk '{printf"%d " ,$2+$3+$4+$5+$6+$7+$8}'))
####函数
cpu_math()
{
Use_cpu=${oneUse_cpu[$1]}
All_cpu=${oneAll_cpu[$1]}
Use_cpu_old=${oneUse_cpu_old[$1]}
All_cpu_old=${oneAll_cpu_old[$1]}
dif_all_cpu=`expr $All_cpu - $All_cpu_old|bc`
dif_use_cpu=`expr $Use_cpu - $Use_cpu_old|bc`
cpu=`awk 'BEGIN{printf "%.0f\n",('$dif_use_cpu'/'$dif_all_cpu')*100}'`
echo $cpu
return 0
}
####
printcpu()
{
LIMIT=`expr ${#oneUse_cpu[@]} - 1`
for ((i=0; i<=LIMIT; i++))
do
        eval cpu$i=`cpu_math $i`
        eval echo CPU$i:\${cpu${i}}%
done
}
declare -a mcpu=(`printcpu`)
####
declare -a  oneUse_cpu_old=(${oneUse_cpu[@]})
declare -a  oneAll_cpu_old=(${oneAll_cpu[@]})
#############################################
echo -e "$tim ${mcpu[@]}|MEM:$mem%|SWAP:$swap%|LOAD:$Load|IN:${dif_in_m}Mbit/s|OUT:${dif_out_m}Mbit/s|COUNT:${connect}|COUNT1:${connect1}|COUNT2:${connect2}|COUNT3:${connect3}|COUNT4:${connect4}|ONE_IN:${one_in}Kbit/s|ONE_OUT:${one_out}Kbit/s " >> $log
done
exit 0