Linux中使用shell+python實現監控系統負載+郵件警示

來源:互聯網
上載者:User

1. 擷取ip、cpu、記憶體資訊、loadAverage

ip使用ifconfig獲得

localip=$(ifconfig eth0 | grep 'inet addr' | awk '{print $2}' | cut -f2 -d:)

空閑cpu由mpstat獲得

cpuidle=$(mpstat | grep all | awk '{print $11}')

記憶體使用量情況由free獲得

freemem=$(free | grep Mem | awk '{print $4}')

totalmem=$(free | grep Mem | awk '{print $2}')

pcent=$(free | grep Mem | awk '{print $4/$2}')

loadAverage由uptime獲得

loadavg1=$(uptime | awk '{print $10}' | cut -f1 -d,)
loadavg5=$(uptime | awk '{print $11}' | cut -f1 -d,)
loadavg15=$(uptime | awk '{print $12}')

. /etc/profileAlertNginxCpu=50AlertNginxMem=50AlertPcent=0.5AlertCpu=50AlertLoad=1msg=Subject='伺服器超載警報'mailto(){    /application/search/sendmail.py "$Subject" "$msg"    msg=    Subject='伺服器超載警報'}date=$(date)localip=$(/sbin/ifconfig eth0 | grep 'inet addr' | awk '{print $2}' | cut -f2 -d:)nginxcpu=$(ps axu|grep nginx |grep search | grep worker | awk '{print $3}')nginxmempcent=$(ps axu|grep nginx |grep search | grep worker | awk '{print $4}')nginxmem=$(ps axu|grep nginx |grep search | grep worker | awk '{print $6}')cpuidle=$(mpstat | grep all | awk '{print $11}')freemem=$(free | grep Mem | awk '{print $4}')totalmem=$(free | grep Mem | awk '{print $2}')freemempcent=$(free | grep Mem | awk '{print $4/$2}')freeswap=$(free | grep Swap | awk '{print $4}')totalswap=$(free | grep Swap | awk '{print $2}')freeswappcent=$(free | grep Swap | awk '{print $4/$2}')loadavg1=$(uptime | awk '{print $10}' | cut -f1 -d,)loadavg5=$(uptime | awk '{print $11}' | cut -f1 -d,)loadavg15=$(uptime | awk '{print $12}')echo $dateecho $localipecho '空閑cpu:'$cpuidleecho '空閑記憶體:'$freememecho '總記憶體:'$totalmemecho '空閑記憶體比:'$freemempcentecho '空閑交換記憶體:'$freeswapecho '總交換記憶體:'$totalswapecho '空閑交換記憶體比:'$freeswappcentecho $loadavg1echo $loadavg5echo $loadavg15alldata=''$date'| '$localip'| nginx:'$nginxcpu','$nginxmempcent'('$nginxmem')| cpu_idle:'$cpuidle'| Mem:'$freemem'/'$totalmem'='$freemempcent'| Swap:'$freeswap'/'$totalswap'='$freeswappcent'| loadavg:'$loadavg1','$loadavg5','$loadavg15''echo $alldata >> monitor.logif [ $(echo "$nginxcpu >= $AlertNginxCpu"|bc) = 1 ]; then    msg=''$alldata'| nginx cpu'$nginxcpu':高於'$AlertNginxCpu''    Subject=''$Subject':'$localip': nginx cpu'$nginxcpu'高於'$AlertNginxCpu''    echo $msg >> monitor.log    mailtofiif [ $(echo "$nginxmempcent >= $AlertNginxMem"|bc) = 1 ]; then    msg=''$alldata'| nginx mem'$nginxmempcent':高於'$AlertNginxMem''    Subject=''$Subject':'$localip': nginx mem'$nginxmempcent'高於'$AlertNginxMem''    echo $msg >> monitor.log    mailtofiif [ $(echo "$freeswappcent <= $AlertPcent"|bc) = 1 ]; then    msg=''$alldata'| 剩餘虛擬記憶體佔比'$freeswappcent':低於'$AlertPcent''    Subject=''$Subject':'$localip':剩餘虛擬記憶體佔比'$freeswappcent'低於'$AlertPcent''    echo $msg >> monitor.log    mailtofiif [ $(echo "$cpuidle <= $AlertCpu"|bc) = 1 ]; then    msg=''$alldata'| cpu空閑時間'$cpuidle':低於'$AlertCpu''    Subject=''$Subject':'$localip':cpu空閑時間'$cpuidle'低於'$AlertCpu''    echo $msg >> monitor.log    mailtofiif [ $(echo "$loadavg1 >= $AlertLoad"|bc) = 1 ]; then    msg=''$alldata'| load'$loadavg1':高於'$AlertLoad''    Subject=''$Subject':'$localip':load'$loadavg1'高於'$AlertLoad''    echo $msg >> monitor.log    mailtofiif [ -n $msg ];then    echo '系統運行正常'fi

2. 空閑cpu小於cpu警示閾值或空閑記憶體比例低於記憶體警示閾值或loadAverage超過閾值時發送郵件警示

#! /usr/bin/env pythonimport smtplib import sysfrom email.mime.text import MIMEText  mailto_list=[""]  mail_host = "smtp.126.com" mail_user = "monitor_algo" mail_pass = "" mail_postfix="126.com"  def send_mail(to_list, sub, context):     me = mail_user + "<"+mail_user+"@"+mail_postfix+">"     msg = MIMEText(context)     msg['Subject'] = sub     msg['From'] = me     msg['To'] = ";".join(to_list)     try:         send_smtp = smtplib.SMTP()         send_smtp.connect(mail_host)         send_smtp.login(mail_user, mail_pass)         send_smtp.sendmail(me, to_list, msg.as_string())         send_smtp.close()         return True     except (Exception, e):         print(str(e))         return False if __name__=="__main__":#    print ("start") #    for a in range(1, len(sys.argv)):#        print sys.argv[a]    if (True == send_mail(mailto_list,sys.argv[1],sys.argv[2])): pass#        print ("sucess")    else: pass#        print ("failed") 

3. 加入crontab定時任務

注意一點,crontan中的環境變數與使用者環境的環境變數不一樣,因其不會從預設的使用者profile檔案中讀入環境變數參數,最簡單的方法是用source命令(.)

*/1 * * * * . /etc/profile; sh /application/search/monitor.sh

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.