查看內存mem
- #!/bin/sh
- mem=`free -m | awk 'NR==2{print $2}'`
- ps -aux 2>&1 | sort -k 4 -r | awk '$4 ~ /^[0-9]/ && $4>0 {print $4,$11}' | awk '{print
- $1/100*mem" "$2}' mem=$mem | sort -k 2 | awk '
- {
- a[$2] += $1;
- b[$2]++;
- total += $1;
- total++;
- }
- END{
- for(i in a){
- t=i;
- gsub(/:|.*\//, "", t);
- printf "%10s %s\n" ,a[i]"MB", t"["b[i]"]";
- }
- print "Memory Total: "mem"MB, used: "total"MB, free: "mem-total"MB."
- }' mem=$mem | sort -n -r
#chmod u+x mem
#./mem
1980.16MB httpd[165]
324.027MB python[2]
96.008MB php[7]
84.007MB mysqld[1]
Memory Total: 12001MB, used: 2659.21MB, free: 9341.79MB.
查看端口流量net
- #!/bin/bash
- # osdba 2008.10.22 monitor the interface's network traffic.
- # Zeuslion 2009.08.29.
- if [ $# -ne 3 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ];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)}'
#chmod u+x net
#./net eth0 2 10
ifname | in_kbits/s out_kbits/s | in_kBytes/s out_kBytes/s | in_packets/s out_packets/s
--------- | ---------- ----------- | ----------- ------------ | ------------ -------------
eth0 | 200 2210 | 25 276 | 224 273
eth0 | 170 1251 | 21 156 | 175 187
eth0 | 803 850 | 100 106 | 211 181
eth0 | 235 2669 | 29 333 | 235 305
eth0 | 228 2183 | 28 272 | 187 274
eth0 | 72 333 | 9 41 | 80 82
eth0 | 229 2823 | 28 352 | 269 330
eth0 | 366 3215 | 45 401 | 339 388
eth0 | 1022 5156 | 127 644 | 435 580
eth0 | 272 2970 | 34 371 | 281 333
--------- | ---------- ----------- | ----------- ------------ | ------------ -------------
Average | 360 2366 | 45 295 | 243 293