linux服務器性能(網卡流量、CPU、內存、磁盤使用率)監控


  轉載請注明:http://www.cnblogs.com/zishengY/p/6819606.html 

  廣義的網站的監控涵蓋所有的非業務行為的數據采集與管理,包括數據分析師和產品設計師使用的網站用戶行為日志、業務運行數據,以及供運維工程師和開發工程師使用的性能統計數據等。

     本文主要是通過shell腳本來收集服務器性能指標,如系統load、內存占用、磁盤IO、CPU占用,並將其寫入一個文件中,及時判斷應用情況,防患於未然 。

  實現步驟如下:

第一步:編寫shell腳本

vim check.sh

添加下面腳本之后保存 

#!/bin/bash
#這個腳本使用來統計CPU、磁盤、內存使用率、帶寬的
total=0
system=0
user=0
i=0

#帶寬使用情況
time=`date "+%Y-%m-%d %k:%M"`
day=`date "+%Y-%m-%d"`
minute=`date "+%k:%M"`
echo  "*************************************************************************" >> 123.txt
echo "統計開始時間:$day $minute" >> 123.txt

#循環五次,避免看到的是偶然的數據
echo "#帶寬的使用情況:#" >>123.txt
while (( $i<5 ))
do
#原先的`ifconfig eth0|sed -n "7p"|awk '{print $2}'|cut -c7-`方式獲取網卡的信息為空,已經注釋掉
#rx_before=`ifconfig eth0|sed -n "7p"|awk '{print $2}'|cut -c7-`
#tx_before=`ifconfig eth0|sed -n "7p"|awk '{print $6}'|cut -c7-`
rx_before=$(cat /proc/net/dev | grep 'eth' | tr : " " | awk '{print $2}')
tx_before=$(cat /proc/net/dev | grep 'eth' | tr : " " | awk '{print $10}')
sleep 2
#rx_after=`ifconfig eth0|sed -n "7p"|awk '{print $2}'|cut -c7-`
#tx_after=`ifconfig eth0|sed -n "7p"|awk '{print $6}'|cut -c7-`
rx_after=$(cat /proc/net/dev | grep 'eth' | tr : " " | awk '{print $2}')
tx_after=$(cat /proc/net/dev | grep 'eth' | tr : " " | awk '{print $10}')

rx_result=$[(rx_after-rx_before)/1024/1024/2*8]
tx_result=$[(tx_after-tx_before)/1024/1024/2*8]
echo  "$time Now_In_Speed: $rx_result Mbps Now_OUt_Speed: $tx_result Mbps" >>123.txt

let "i++"
done

rx_result=$(cat 123.txt|grep "$time"|awk '{In+=$4}END{print In}')
tx_result=$(cat 123.txt|grep "$time"|awk '{Out+=$7}END{print Out}')
In_Speed=$(echo "scale=2;$rx_result/5"|bc)
Out_Speed=$(echo "scale=2;$tx_result/5"|bc)
#echo "#帶寬的5次的平均值是:#" >>123.txt
echo  "$time In_Speed_average: $In_Speed Mbps Out_Speed_average: $Out_Speed Mbps" >>123.txt

#CPU使用情況
which sar > /dev/null 2>&1
if [ $? -ne 0 ]
then
  total=`vmstat 1 5|awk '{x+=$13;y+=$14}END{print x+y}'`
  average=$(echo "scale=2;$total/5"|bc)
fi
echo "#CPU使用率:#" >>123.txt
echo "Total CPU  is already use: $average%" >>123.txt
#磁盤使用情況(注意:需要用sed先進行格式化才能進行累加處理)
disk_used=$(df -m | sed '1d;/ /!N;s/\n//;s/ \+/ /;' | awk '{used+=$3} END{print used}')
disk_totalSpace=$(df -m | sed '1d;/ /!N;s/\n//;s/ \+/ /;' | awk '{totalSpace+=$2} END{print totalSpace}')
disk_all=$(echo "scale=4;$disk_used/$disk_totalSpace" | bc)
disk_percent1=$(echo $disk_all | cut -c 2-3)
disk_percent2=$(echo $disk_all | cut -c 4-5)
disk_warning=`df -m | sed '1d;/ /!N;s/\n//;s/ \+/ /;' | awk '{if ($5>85) print $5 $6;} '`
echo "#磁盤利用率#" >>123.txt
echo "hard disk has used: $disk_percent1.$disk_percent2%" >>123.txt
echo -e "\t\t#磁盤存在目錄使用率超過85%報警#" >>123.txt
echo -e "\t\tover used: $disk_warning" >>123.txt


#內存使用情況
memery_used=$(free -m | awk 'NR==2' | awk '{print $3}')
buffer_used=$(free -m | awk 'NR==2' | awk '{print $6}')
cache_used=$(free -m | awk 'NR==2' | awk '{print $7}')
free=$(free -m | awk 'NR==2' | awk '{printf $4}')
memery_all=$(free -m | awk 'NR==2' | awk '{print $2}')
used_all=$[memery_all-(free+buffer_used+cache_used)]
echo "$used_all $memery_all $free" >>123.txt
memery_percent=$(echo "scale=4;$memery_used / $memery_all" | bc)
memery_percent2=$(echo "scale=4; $used_all / $memery_all" | bc)
percent_part1=$(echo $memery_percent | cut -c 2-3)
percent_part2=$(echo $memery_percent | cut -c 4-5) 
percent_part11=$(echo $memery_percent2 | cut -c 2-3)
percent_part22=$(echo $memery_percent2 | cut -c 4-5)
echo "#內存使用率#" >> 123.txt
echo "system memery is already use: $percent_part1.$percent_part2%" >>123.txt
echo "actual memery is already use: $percent_part11.$percent_part22%" >>123.txt

echo  "結束本次統計:$day $minute" >> 123.txt
echo  "*************************************************************************" >> 123.txt
echo -e "\n\n\n\n" >> 123.txt

第二步:創建shell腳本中用到的文件123.txt    

touch 123.txt

第三步:給check.sh和123.txt授予所有權限  

chmod 777 check.sh
chmod 777 123.txt

第四步:執行check.sh腳本   

./check.sh

第五步:查看執行寫入文件的情況

出現這信息表名腳本成功運行實時統計情況。

 

參考文章

若是想使用定時任務,可使用crontab進行設置,請參見這篇文章

 

學習本就是一個不斷模仿、練習、再到最后面自己原創的過程。

雖然可能從來不能寫出超越網上通類型同主題博文,但為什么還是要寫?
於自己而言,博文主要是自己總結。假設自己有觀眾,畢竟講是最好的學(見下圖)。

於讀者而言,筆者能在這個過程get到知識點,那就是雙贏了。
當然由於筆者能力有限,或許文中存在描述不正確,歡迎指正、補充!
感謝您的閱讀。如果本文對您有用,那么請點贊鼓勵。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM