該指令碼檢測cpu和記憶體的使用情況,只需要調整memorysetting、cpusetting、useremail要發郵件報警的email位址即可
如果沒有配置發郵件引數的哥們,已配置了的,直接飛到**區:
1.vim /etc/mail.rc
2.找到以下內容
set [email protected] #來自什麼
set smtp=smtp.exmail.qq.com #根據您的郵箱發件伺服器填寫,我這位是tx的企業郵箱
set [email protected] #郵箱使用者名稱
set smtp-auth-password=您的密碼 #注意是發郵件密碼,有的郵箱服務商登陸密碼和發件密碼不一樣的哦
set smtp-auth=login
**配置完成後可以直接echo 『test content』 |mail -s 『test title』 [email protected]
如果收到郵件,說明您郵件配置是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="
[email protected] [email protected]
"webip="
www101.200.***.***
"memorysetting="80"
cpusetting="80"
#check memory used rate
totalmemory=$(free -m|awk
''|sed -n '2p'
)usedmemory=$(free -m|awk
''|sed -n '3p'
)freememory=$(free -m|awk
''|sed -n '3p'
)usedpermemory=$(awk
'begin')
freepermemory=$(awk
'begin')
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 !
" | mail -s "
used memory was high!
"$useremail
fi#check cpu used rate
cpuused=$(top -n 1 | awk -f '
[ %]+''
nr==3
'|awk -f '.'
'')if [ $cpuused -gt $cpusetting ]
then
echo
"$(date) cpu used $cpuused%
"echo
"$(date) cpu used $cpuused%
"|mail -s "
$webip cpu used $cpuused%
"$useremail
fi
原文:
Shell指令碼監控Linux系統CPU使用率
通過top或者htop命令。可以看到每乙個cpu核心的使用情況,但是伺服器的整體cpu使用情況,就無法直觀的看到。需要通過shell指令碼才能實現。cpu ck.sh bin bash environment variable source etc profile cpu cpu us vmstat...
監控cpu 記憶體
pid 1 獲取程序pid echo pid interval 1 設定採集間隔 while true do echo date y m d h m s proc memlog.txt cat proc pid status grep e vmrss proc memlog.txt 獲取記憶體占用 ...
shell 監控CPU 記憶體和硬碟利用率
1 cpu 借助vmstat工具來分析cpu統計資訊。bin bash date date f h m ip ifconfig eth0 awk f inet addr 只支援centos6 mail example mail.com if which vmstat dev null then ec...