端口流量檢測,內存使用查看

查看內存mem

  1. #!/bin/sh 
  2.  
  3. mem=`free -m | awk 'NR==2{print $2}'` 
  4. ps -aux 2>&1 | sort -k 4 -r | awk '$4 ~ /^[0-9]/ && $4>0 {print $4,$11}' | awk '{print  
  5.  
  6. $1/100*mem"   "$2}' mem=$mem | sort -k 2 | awk ' 
  7.    a[$2] += $1; 
  8.    b[$2]++; 
  9.    total += $1; 
  10.    total++; 
  11. END{ 
  12.   for(i in a){ 
  13.     t=i
  14.     gsub(/:|.*\//, "", t); 
  15.     printf "%10s   %s\n" ,a[i]"MB", t"["b[i]"]"; 
  16.   } 
  17. print "Memory Total: "mem"MB, used: "total"MB, free: "mem-total"MB." 
  18. }' 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

  1. #!/bin/bash 
  2. # osdba 2008.10.22 monitor the interface's network traffic. 
  3. #   Zeuslion 2009.08.29. 
  4.  
  5. if [ $# -ne 3 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ];then 
  6.    echo Useage : $0 interface interval count 
  7.    echo Example: $0 eth0 2 10 
  8.    exit 
  9. fi 
  10. eth=$1 
  11. count=$3 
  12. interval=$2 
  13. inbytesfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $2}') 
  14. if [ -z "$inbytesfirst" ];then 
  15.     echo The network interface $eth is not exits! 
  16.     exit 1; 
  17. fi 
  18. outbytesfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $10}') 
  19. inpacketsfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $3}') 
  20. outpacketsfirst=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $11}') 
  21. sleep $interval"s" 
  22. i=0 
  23. while [ "$i" -lt "$count" ] 
  24. do 
  25.    inbytesend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $2}') 
  26.    outbytesend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $10}') 
  27.    inpacketsend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $3}') 
  28.    outpacketsend=$(cat /proc/net/dev |tr ':' ' '|awk '/'$eth'/{print $11}') 
  29.  
  30.    bytesin=$(($inbytesend-$inbytesfirst)) 
  31.    bytesout=$(($outbytesend-$outbytesfirst)) 
  32.    packetsin=$(($inpacketsend-$inpacketsfirst)) 
  33.    packetsout=$(($outpacketsend-$outpacketsfirst)) 
  34.  
  35.    if [ "$bytesin" -lt "0" ];then 
  36.       bytesin=$((4294967295-$inbytesfirst+$inbytesend)) 
  37.       #echo bytesin $bytesin $inbytesfirst $inbytesend 
  38.    fi 
  39.    if [ "$bytesout" -lt "0" ];then 
  40.       bytesout=$((4294967295-$outbytesfirst+$outbytesend)) 
  41.       #echo bytesout $bytesout $outbytesfirst $outbytesend 
  42.    fi 
  43.    if [ "$packetsin" -lt "0" ];then 
  44.       packetsin=$((4294967295-$inpacketsfirst+$inpacketsend)) 
  45.       #echo packetsin $packetsin $inpacketsfirst $inpacketsend 
  46.    fi 
  47.    if [ "$packetsout" -lt "0" ];then 
  48.       packetsout=$((4294967295-$outpacketsfirst+$outpacketsend)) 
  49.       #echo packetsout $packetsout $outpacketsfirst $outpacketsend 
  50.    fi 
  51.  
  52.    bytesin=$(($bytesin/$interval)) 
  53.    bytesout=$(($bytesout/$interval)) 
  54.    packetsin=$(($packetsin/$interval)) 
  55.    packetsout=$(($packetsout/$interval)) 
  56.  
  57.    sumbytesin=$(($sumbytesin+$bytesin)) 
  58.    sumbytesout=$(($sumbytesout+$bytesout)) 
  59.    sumpacketsin=$(($sumpacketsin+$packetsin)) 
  60.    sumpacketsout=$(($sumpacketsout+$packetsout)) 
  61.  
  62.    if [ $(($i%20)) -eq 0 ];then 
  63.       echo " ifname   | in_kbits/s out_kbits/s | in_kBytes/s out_kBytes/s | in_packets/s  
  64.  
  65. out_packets/s" 
  66.       echo "--------- | ---------- ----------- | ----------- ------------ | ------------ ----- 
  67.  
  68. --------" 
  69.    fi 
  70.    echo $eth $bytesin $bytesout $packetsin $packetsout |awk '{printf("%9s | %10d %11d | %11d % 
  71.  
  72. 12d | %12d %13d\n",$1,$2/128,$3/128,$2/1024,$3/1024,$4,$5)}' 
  73.    inbytesfirst=$inbytesend 
  74.    outbytesfirst=$outbytesend 
  75.    inpacketsfirst=$inpacketsend 
  76.    outpacketsfirst=$outpacketsend 
  77.     
  78.    i=$(($i+1)) 
  79.    sleep $interval"s" 
  80. done 
  81.  
  82. sumbytesin=$(($sumbytesin/$i)) 
  83. sumbytesout=$(($sumbytesout/$i)) 
  84. sumpacketsin=$(($sumpacketsin/$i)) 
  85. sumpacketsout=$(($sumpacketsout/$i)) 
  86.  
  87. echo "--------- | ---------- ----------- | ----------- ------------ | ------------ ----------- 
  88.  
  89. --" 
  90. echo Average $sumbytesin $sumbytesout $sumpacketsin $sumpacketsout |awk '{printf("%9s | %10d % 
  91.  
  92. 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章