shell腳本監控cpu/內存使用率 轉


該腳本檢測cpu和內存的使用情況,只需要調整memorySetting、cpuSetting、userEmail要發郵件報警的email地址即可
如果沒有配置發郵件參數的哥們,已配置了的,直接飛到代碼區:
1.vim /etc/mail.rc
2.找到以下內容
set from=yangxingyi@duoduofenqi.com #來自什么
set smtp=smtp.exmail.qq.com #根據您的郵箱發件服務器填寫,我這位是TX的企業郵箱
set smtp-auth-user=yangxingyi@duoduofenqi.com #郵箱用戶名
set smtp-auth-password=您的密碼 #注意是發郵件密碼,有的郵箱服務商登陸密碼和發件密碼不一樣的哦
set smtp-auth=login
**配置完成后可以直接echo ‘test content’ |mail -s ‘test title’ yangxingyi@duoduofenqi.com
如果收到郵件,說明您郵件配置是ok的,否則就是有見沒配置好哦,重新檢查用戶名密碼,smtp有沒有填錯!!!**
如果您覺得有用的話直接搬走就行了,不用聯系我!!!

#/bin/sh
#auth yangxingyi 2017-12-12 17:50
#email openweixin666@126.com
#this script check cpu used rate and memory used rate
userEmail="269754243@qq.com openweixin666@126.com"
webIp="www101.200.***.***"
memorySetting="80"
cpuSetting="80"
#check memory used rate 
totalMemory=$(free -m|awk '{print $2}'|sed -n '2p')
usedMemory=$(free -m|awk '{print $3}'|sed -n '3p')
freeMemory=$(free -m|awk '{print $4}'|sed -n '3p')
usedPerMemory=$(awk 'BEGIN{printf "%.0f",('$usedMemory'/'$totalMemory')*100}')
freePerMemory=$(awk 'BEGIN{printf "%.0f",('$freeMemory'/'$totalMemory')*100}')
if [ $usedPerMemory -ge $memorySetting ]
then
minfo="totalMemory:$totalMemory MB,used:$usedMemory MB,free:$freeMemory MB,usedPercent:$usedPerMemory%,freePrecent:$freePerMemory%"
echo "$(date) $minfo used memory was gt $memorySetting% !" >> /sh/log_hard_disk_check
echo " $minfo {$webIp}!" | mail -s "{$webIp} used memory was high!" $userEmail
fi

#check cpu used rate
cpuUsed=$(top -n 1 | awk -F '[ %]+' 'NR==3 {print $2}'|awk -F '.' '{print $1}')
if [ $cpuUsed -gt $cpuSetting ]
then
echo "$(date) cpu used $cpuUsed% "
echo "$(date) cpu used $cpuUsed%"|mail -s "$webIp cpu used $cpuUsed%" $userEmail
fi

 


原文:https://blog.csdn.net/qq_27517377/article/details/78784473


免責聲明!

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



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