[root@node21 root]# vmstat
procs memory swap io system cpu r b w swpd free buff cache si so bi bo in cs us sy id 0 0 0 0 1066680 176680 415212 0 0 0 6 25 33 27 0 25 内存: free|grep Mem:|awk '{print $4 " " $6 " " $7 }' cpu: sar 1|grep Average:|awk '{print $3 " " $5 " " $4 }' swap: vmstat 1 2|awk '{print $8 " " $9 }' |grep -v ^swap |grep -v ^si|tail -n 1 context: vmstat 1 2|awk '{print $13 }' |grep -v ^system |grep -v ^cs|tail -n 1 netflow: eth0: sar -n DEV 1|grep eth0|grep -v ^Average:|awk '{print $3 " " $6 " " $7 }' eth1: sar -n DEV 1|grep eth1|grep -v ^Average:|awk '{print $3 " " $6 " " $7 }' $6为in的流量,$7为out流量,单位是bytes/s avgload: uptime|awk '{print $10 " " $11 " " $12}'|sed 's/,//g' this is righter: uptime|awk '{print $(NF-2) " " $(NF-1) " " $NF}'|sed 's/,//g' 其他命令 mpstat iostat ...
也可以通过从Proc文件获取,这种方式不依赖于linux命令,且性能较好.
CPU利用率的获取脚本
#!/bin/sh
interval=1
t=`date +%T`
#result=`cat /proc/stat |grep cpu -m 1`
result=`cat /proc/stat |grep "cpu "`
usr1=`echo $result|awk '{print $2}'`
nice1=`echo $result|awk '{print $3}'`
sys1=`echo $result|awk '{print $4}'`
idle1=`echo $result|awk '{print $5}'`
let t1=usr1+nice1+sys1+idle1
while [ 1 ]
do
sleep $interval
result=`cat /proc/stat |grep "cpu "`
usr2=`echo $result|awk '{print $2}'`
nice2=`echo $result|awk '{print $3}'`
sys2=`echo $result|awk '{print $4}'`
idle2=`echo $result|awk '{print $5}'`
let t2=usr2+nice2+sys2+idle2
let t3=t2-t1
if [ $t3 -eq 0 ];
then let t3=1
fi
let usr=/(usr2-usr1/)*10000/t3
let nice=/(nice2-nice1/)*10000/t3
let sys=/(sys2-sys1/)*10000/t3
let idle=/(idle2-idle1/)*10000/t3
let usr1=usr2
let nice1=nice2
let sys1=sys2
let idle1=idle2
let t1=usr1+nice1+sys1+idle1
echo $usr $nice $sys $idle>>CPUData$t.txt
done
Mem获取方法
#!/bin/sh
interval=1
t=`date +%T`
while [ 1 ];
do
sleep $interval
result=`cat /proc/meminfo`
free=`echo $result |grep "MemFree:" |awk '{print $5}'`
buffers=`echo $result |grep Buffers: |awk '{print $8}'`
cached=`echo $result |grep Cached: |awk '{print $11}'`
swap=`echo $result |awk '{print $14}'`
r=`echo $result |awk '{print $2}'`
let used=used/1024 let free=free/1024
let shared=shared/1024
let buffers=buffers/1024
let cached=cached/1024
# echo $free $buffers $cached $swap
echo $free $buffers $cached $swap >>MemData$t.txt
done
网络获取方法
#!/bin/sh
let interval=2
t=`date +%T`
result=`cat /proc/net/dev |grep eth0|sed 's/eth0://g'`
rb1=`echo $result|awk '{print $1}'`
rp1=`echo $result|awk '{print $2}'`
sb1=`echo $result|awk '{print $9}'`
sp1=`echo $result|awk '{print $10}'`
while [ 2 ]
do
sleep $interval
result=`cat /proc/net/dev |grep eth0|sed 's/eth0://g'`
rb2=`echo $result|awk '{print $1}'`
rp2=`echo $result|awk '{print $2}'`
sb2=`echo $result|awk '{print $9}'`
sp2=`echo $result|awk '{print $10}'`
let rb=/(rb2-rb1/)/2/1024
let rp=/(rp2-rp1/)/2
let sb=/(sb2-sb1/)/2/1024
let sp=/(sp2-sp1/)/2
let rb1=rb2
let rp1=rp2
let sb1=sb2
let sp1=sp2
echo $rb $rp $sb $sp>>NetData$t
done
killall
killall cpu.sh
killall net.sh
killall mem.sh
Linux System and Performance Monitoring
http://blog.chinaunix.net/u1/38871/showart_2158781.html