利用shell指令碼來監控linux系統的負載與CPU佔用情況

來源:互聯網
上載者:User

這幾天在學習研究shell指令碼,寫的一些系統負載與CPU監控指令碼程式。在沒有nagios監視軟體的情況下,只要伺服器能上互連網,就可通過發郵件的方式來提醒管理員系統負載與CPU佔用的使用方式。
一、安裝linux下面的一個郵件用戶端msmtp軟體(類似於一個foxmail的工具)
1、下載安裝:http://downloads.sourceforge.net/msmtp/msmtp-1.4.16.tar.bz2?modtime=1217206451&big_mirror=0
# tar jxvf msmtp-1.4.16.tar.bz2
# cd msmtp-1.4.16
# ./configure --prefix=/usr/local/msmtp
# make
# make install
2、建立msmtp設定檔和記錄檔(host為郵件網域名稱,郵件使用者名test,密碼123456)
# vim ~/.msmtprc
1.      account default 
2.      host 126.com 
3.      from test@126.com 
4.      auth login 
5.      user test 
6.      password 123456 
7.      logfile ~/.msmtp.log
# chmod 600 ~/.msmtprc
# touch ~/.msmtp.log
3、mutt安裝配置:(一般linux下有預設安裝mutt)
# vim ~/.muttrc
1.      set sendmail="/usr/local/msmtp/bin/msmtp"
2.      set use_from=yes
3.      set realname="moniter"  
4.      set from=test@126.com 
5.      set envelope_from=yes
6.      set rfc2047_parameters=yes
7.      set charset="utf-8"
 
4、郵件發送測試(-s郵件標題,-a表加附件)
# echo "郵件內容123456" | mutt -s "郵件標題測試郵件" -a /scripts/test.txt test@126.com
 
二、監控伺服器系統負載情況:
1、用uptime命令查看當前負載情況(1分鐘,5分鐘,15分鐘平均負載情況)
# uptime
15:43:59 up 186 days, 20:04, 1 user, load average: 0.01, 0.02, 0.00
 
系統負荷的經驗法則:(摘自http://www.ruanyifeng.com/blog/2011/07/linux_load_average_explained.html)
(1) 主要觀察"15分鐘系統負荷",將它作為電腦正常啟動並執行指標。
(2) 如果15分鐘內,(系統負荷除以CPU核心數目之後的)平均負荷大於1.0,表明問題持續存在,不是暫時現象。
(3) 當系統負荷持續大於0.7,你必須開始調查了,問題出在哪裡,防止情況惡化。
(4) 當系統負荷持續大於1.0,你必須動手尋找解決辦法,把這個值降下來。
(5) 當系統負荷達到5.0,就表明你的系統有很嚴重的問題,長時間沒有響應,或者接近死機了。
 
2、查看伺服器cpu的總核心數
# grep -c 'model name' /proc/cpuinfo
 
3、截取伺服器1分鐘、5分鐘、15分鐘的負載情況
# uptime | awk '{print $8,$9,$10,$11,$12}'
load average: 0.01, 0.02, 0.00
 
4、查看截取15分鐘的平均負載
# uptime | awk '{print $12}'
5、編寫系統負載監控的指令檔:
# vim /scripts/load-check.sh
1.      #!/bin/bash 
2.      #使用uptime命令監控linux系統負載變化 
3.     
4.      #取系統目前時間(以追加的方式寫入檔案>>) 
5.      date >> /scripts/datetime-load.txt    
6.     
7.      #提取伺服器1分鐘、5分鐘、15分鐘的負載情況 
8.      uptime | awk '{print $8,$9,$10,$11,$12}' >> /scripts/load.txt 
9.     
10.   #逐行串連上面的時間和負載相關行資料(每次重新寫入檔案>) 
11.   paste  /scripts/datetime-load.txt /scripts/load.txt   > /scripts/load_day.txt
# chmod a+x /scripts/load-check.sh
6、編寫系統負載結果檔案郵件發送指令碼:
# vim /scripts/sendmail-load.sh
1.      #!/bin/bash 
2.      #把系統負載監控產生的load_day.txt檔案通過郵件發送給使用者 
3.     
4.      #提取本伺服器的IP地址資訊 
5.      IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "` 
6.     
7.      #提取當前日期 
8.      today=`date -d "0 day" +%Y年%m月%d日` 
9.     
10.   #發送系統負載監控結果郵件 
11.   echo "這是$IP伺服器$today的系統負載監控報告,請下載附件。" | mutt -s "$IP伺服器$today的系統負載監控報告" -a /scripts/load_day.txt  test@126.com 
# chmod a+x /scripts/sendmail-load.sh
7、編寫系統負載監控的指令檔:
# vim /scripts/load-warning.sh
1.      #!/bin/bash 
2.      #使用uptime命令監控linux系統負載變化 
3.     
4.      #提取本伺服器的IP地址資訊 
5.      IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "` 
6.     
7.      #抓取cpu的總核心數 
8.      cpu_num=`grep -c 'model name' /proc/cpuinfo` 
9.     
10.   #抓取當前系統15分鐘的平均負載值 
11.   load_15=`uptime | awk '{print $12}'` 
12.  
13.   #計算當前系統單個核心15分鐘的平均負載值,結果小於1.0時前面個位元補0。 
14.   average_load=`echo "scale=2;a=$load_15/$cpu_num;if(length(a)==scale(a)) print 0;print a" | bc` 
15.  
#取上面平均負載值的個位整數 
16.   average_int=`echo $average_load | cut -f 1 -d "."` 
17.  
18.   #設定系統單個核心15分鐘的平均負載的警示值為0.70(即使用超過70%的時候警示)。 
19.   load_warn=0.70 
20.  
21.   #當單個核心15分鐘的平均負載值大於等於1.0(即個位整數大於0) ,直接發郵件警示;如果小於1.0則進行二次比較 
22.   if (($average_int > 0)); then 
23.         echo "$IP伺服器15分鐘的系統平均負載為$average_load,超過警戒值1.0,請立即處理!!!" | mutt -s "$IP 伺服器系統負載嚴重警示!!!"  test@126.com 
24.   else 
25.   #當前系統15分鐘平均負載值與警示值進行比較(當大於警示值0.70時會返回1,小於時會返回0 ) 
26.   load_now=`expr $average_load \> $load_warn` 
27.  
28.   #如果系統單個核心15分鐘的平均負載值大於警示值0.70(傳回值為1),則發郵件給管理員 
29.   if (($load_now == 1)); then 
30.       echo "$IP伺服器15分鐘的系統平均負載達到$average_load,超過警戒值0.70,請及時處理。" | mutt -s "$IP 伺服器系統負載警示"  test@126.com 
31.   fi 
32.   fi
# chmod a+x /scripts/load-warning.sh
三、監控伺服器系統cpu佔用情況:
1、使用top命令查看linux系統cpu使用方式:
# top -b -n 1 | grep Cpu (-b -n 1 表只需要1次的輸出結果)
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni, 99.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
(空閑值)
2、查看截取空閑cpu的百分比數值命令(只取整數部分):
# top -b -n 1 | grep Cpu | awk '{print $5}' | cut -f 1 -d "."
3、編寫cpu監控的指令檔:
# vim /scripts/cpu-check.sh
1.      #!/bin/bash 
2.      #使用top命令監控linux系統cpu變化 
3.     
4.      #取系統目前時間(以追加的方式寫入檔案>>) 
5.      date >> /scripts/datetime-cpu.txt    
6.     
7.      #抓取當前cpu的值(以追加的方式寫入檔案>>) 
8.      top -b -n 1 | grep Cpu  >> /scripts/cpu-now.txt  
9.     
10.   #逐行串連上面的時間和cpu相關行資料(每次重新寫入檔案>) 
11.   paste  /scripts/datetime-cpu.txt   /scripts/cpu-now.txt  >> /scripts/cpu.txt
# chmod a+x /scripts/cpu-check.sh
 
4、查看CPU監控的結果檔案:
# cat /scripts/cpu.txt
 
5、編寫cpu結果檔案郵件發送指令碼:
# vim /scripts/sendmail-cpu.sh
1.      #!/bin/bash 
2.      #把產生的cpu.txt檔案通過郵件發送給使用者 
3.     
4.      #提取本伺服器的IP地址資訊 
5.      IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "` 
6.     
7.      #提取當前日期
8.      today=`date -d "0 day" +%Y年%m月%d日` 
9.     
10.   #發送cpu監控結果郵件 
11.   echo "這是$IP伺服器$today的cpu監控報告,請下載附件。" | mutt -s "$IP伺服器$today的CPU監控報告" -a /scripts/cpu.txt  test@126.com
# chmod a+x /scripts/sendmail-cpu.sh
四、監控系統cpu的情況,當使用超過80%的時候發警示郵件:
# vim /scripts/cpu-warning.sh
1.      #!/bin/bash 
2.      #監控系統cpu的情況指令碼程式 
3.     
4.      #提取本伺服器的IP地址資訊 
5.      IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "` 
6.     
7.      #取當前空閑cpu百份比值(只取整數部分) 
8.      cpu_idle=`top -b -n 1 | grep Cpu | awk '{print $5}' | cut -f 1 -d "."` 
9.     
10.   #設定空閑cpu的警示值為20%,如果當前cpu使用超過80%(即剩餘小於20%),立即發郵件警示 
11.   if (($cpu_idle < 20)); then 
12.         echo "$IP伺服器cpu剩餘$cpu_idle%,使用率已經超過80%,請及時處理。" | mutt -s "$IP 伺服器CPU警示"  test@126.com 
13.   fi
# chmod a+x /scripts/cpu-warning.sh
五、加入任務計劃:系統負載與CPU佔用率每十分鐘檢測一次,有警示則立即發郵件(十分鐘發一次),負載與CPU檢測結果郵件每天早上8點發一次
 
# crontab -e
1.      */10 * * * *  /scripts/load-check.sh 
2.      */10 * * * *  /scripts/load-warning.sh 
3.      0 8 * * *  /scripts/sendmail-load.sh 
4.     
5.      */10 * * * *  /scripts/cpu-check.sh 
6.      */10 * * * *  /scripts/cpu-warning.sh 
7.      0 8 * * *  /scripts/sendmail-cpu.sh
# service crond restart

作者 我的營運之路
  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.