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