#赵云
#2011年12月27日
#定义mem使用率的函数
#按百分比显示使用率
mem() {
memrate=`free -m |grep Mem |awk '{print $2,$4+$6+$7}'|awk '{print $1,$1-$2}'| awk '{OFMT="%.1f";print $2/$1*100}'`
echo "MEM $memrate"
}
#按百分比显示使用率
cpu() {
cpuf1=/tmp/.cpu1
cpuf2=/tmp/.cpu2
cat /proc/stat |grep cpu > $cpuf1
sleep 1
cat /proc/stat |grep cpu > $cpuf2
cpunumber=`cat /proc/stat |grep cpu |wc -l |awk '{print $1-1}'`
echo "CPUn $cpunumber"
cpunumber=`cat /proc/stat |grep cpu |wc -l |awk '{print $1}'`
for ((i=1;i<=$cpunumber;i++))
do
totala=`cat $cpuf1 | sed -n "$i p" | awk '{print $2+$3+$4+$5+$6+$7+$8}'`
idlea=`cat $cpuf1 | sed -n "$i p" | awk '{print $5}'`
totalb=`cat $cpuf2 | sed -n "$i p" | awk '{print $2+$3+$4+$5+$6+$7+$8}'`
idleb=`cat $cpuf2 | sed -n "$i p" | awk '{print $5}'`
total=`echo "$totalb-$totala" |bc`
idle=`echo "$idleb-$idlea" |bc`
usage=`echo "$idle/$total*100"|bc -l`
rate=`echo "100-$usage "|bc -l`
userate=`echo "scale=1;$rate/1"|bc`
number=`echo "$i-1"|bc`
if [ $number -eq 0 ] ; then
echo "CPU $userate"
else
number=`echo "$number-1"|bc`
echo "CPU$number $userate"
fi
done
}
#按百分比显示使用率
free -m |grep Swap |awk '{print $3}' |egrep [1-9] &>/dev/null
if [ $? -eq 0 ] ; then
swaprate=`free -m |grep Swap | awk '{OFMT="%.1f";print $3/$2*100}' 2>/dev/null`
echo "SWAP $swaprate"
else
echo "SWAP 0"
fi
}
#定义java进程使用率的函数
#按百分比显示使用率
javarate=`ps aux |grep java |awk '{print $4}'|awk '{print total+=$1}'|tail -n 1`
echo "JAVA $javarate"
}
#定义网卡使用率的函数
#以每秒KB显示数据
#只监控插上网线的网卡数据
networkcard() {
netfile2=/tmp/.net2
OK=`mii-tool 2>/dev/null |grep ok &>/dev/null;echo $?`
if [ $OK -eq 0 ] ; then
for interface in `mii-tool 2>/dev/null |grep ok |awk -F: '{print $1}' `
do
sleep 1
cat /proc/net/dev |egrep -v 'Inter|face' |grep $interface | awk -F: '{print $1" "$2}' | awk '{print $1" "$2" "$10}' |column -t > $netfile2
number=`wc -l $netfile1 |awk '{print $1}'`
echo "FEn $number"
for ((i=1;i<=$number;i++))
do
A=`cat $netfile1 |head -n $i |tail -n 1 |awk '{print $2}'`
B=`cat $netfile2 |head -n $i |tail -n 1 |awk '{print $2}'`
D=`cat $netfile2 |head -n $i |tail -n 1 |awk '{print $1}'`
H1=`echo "($B-$A)*8/1000" |bc`
E=`cat $netfile1 |head -n $i |tail -n 1 |awk '{print $3}'`
F=`cat $netfile2 |head -n $i |tail -n 1 |awk '{print $3}'`
H=`echo "($F-$E)*8/1000" |bc`
nu=`echo $interface |awk -Fh '{print $2}' `
echo "FE$nu-rx $H1"
echo "FE$nu-tx $H"
done
done
else
for interface in `ifconfig |grep eth |awk '{print $1}'|awk -F: '{print $1}' |sort -u`
do
cat /proc/net/dev |egrep -v 'Inter|face' |grep $interface | awk -F: '{print $1" "$2}' | awk '{print $1" "$2" "$10}' |column -t > $netfile1
sleep 1
cat /proc/net/dev |egrep -v 'Inter|face' |grep $interface | awk -F: '{print $1" "$2}' | awk '{print $1" "$2" "$10}' |column -t > $netfile2
number=`wc -l $netfile1 |awk '{print $1}'`
echo "FEn $number"
for ((i=1;i<=$number;i++))
do
A=`cat $netfile1 |head -n $i |tail -n 1 |awk '{print $2}'`
B=`cat $netfile2 |head -n $i |tail -n 1 |awk '{print $2}'`
D=`cat $netfile2 |head -n $i |tail -n 1 |awk '{print $1}'`
H1=`echo "($B-$A)*8/1000" |bc`
E=`cat $netfile1 |head -n $i |tail -n 1 |awk '{print $3}'`
F=`cat $netfile2 |head -n $i |tail -n 1 |awk '{print $3}'`
H=`echo "($F-$E)*8/1000" |bc`
nu=`echo $interface |awk -Fh '{print $2}' `
echo "FE$nu-rx $H1"
echo "FE$nu-tx $H"
done
done
fi
}
#单位以KB显示
if [ ! -e /var/log/disklock ] ; then
vmstat 1 >> $diskfile &
touch /var/log/disklock
fi
diskr=`cat $diskfile |awk '{print $9}' |grep -v bi |grep -v '-' |tail -n 1 `
diskw=`cat $diskfile |awk '{print $10}' |grep -v bi |grep -v '-' |tail -n 1 `
echo "DISK-r $diskr"
echo "DISK-w $diskw"
}
#调用定义好的函数,提取数据
for i in $* ;
do
case $i in
--cpu )
cpu
;;
--mem )
mem
;;
--swap)
swap
;;
--java)
java
;;
--disk)
diskrw
;;
--if)
networkcard
;;
esac
done
![调取系统资源的脚本_职场](https://s2.51cto.com/attachment/201112/151822372.jpg?x-oss-process=image/resize,m_fixed,w_1184)
LISTEN 13
ESTABLISHED 38
[root@localhost ~]# netstat -ant |awk '/^tcp/{print $6}'|sort|uniq -c
13 LISTEN