檢查內存使用情況,當內存可使用等於100時,釋放緩存
[root@localhost thy]# cat checkMem.sh #!/bin/bash #防止內存溢出問題 used=`free -m | awk 'NR==2' | awk '{print $3}'` free=`free -m | awk 'NR==2' | awk '{print $4}'` echo "=======================================" >> /var/log/mem.log data >> /var/log/mem.log echo "Memory usage | [Use: ${used}MB][Free: ${free}MB]" >> /var/log/mem.log if [ $free -le 100 ];then sync && echo 1 > /proc/sys/vm/drop_caches sync && echo 2 > /proc/sys/vm/drop_caches sync && echo 3 > /proc/sys/vm/drop_caches echo "OK" >> /var/log/mem.log else echo "Not required" >> /var/log/mem.log
循環版本
[root@localhost thy]# cat ./clear_buff_cache.sh #!/bin/bash USED=`free -m | awk 'NR==2' | awk '{print $3}'` FREE=`free -m | awk 'NR==2' | awk '{print $4}'` while true; do echo "===========================" >> /var/log/check_buff_cache.log date >> /var/log/check_buff_cache.log echo "Memory usage | [Use:${USED}MB][Free:${FREE}MB]" >> /var/log/check_buff_cache.log if [ $FREE -le 1000 ] ; then sync && echo 1 > /proc/sys/vm/drop_caches sync && echo 2 > /proc/sys/vm/drop_caches sync && echo 3 > /proc/sys/vm/drop_caches echo "OK" >> /var/log/check_buff_cache.log else echo "Not required" >> /var/log/check_buff_cache.log fi sleep 300 done
監控內存和磁盤容量,小於給定值時報警
#!/bin/bash # 實時監控本機內存和硬盤剩余空間,剩余內存小於500M,根分區剩余空間小於1000M時,發送報警郵件給root管理員 # 得到根分區、內存剩余空間 disk_size=$(df / | awk '/\//{print $4}') mem_size=$(free | awk '/Mem/{print $4}') # PS:內存和磁盤提取的空間大小是以kb為單位 while : do # 判斷內存和磁盤容小於等於給定值時發送報警郵件 if [ $disk_size -le 1024000 -a $mem_size -le 512000 ]; then mail -s "Warning" root <<EOF Insufficient resources,資源不足 EOF fi done
注意EOF:
[root@VM_0_10_centos shellScript]# ./checkMem.sh ./checkMem.sh: line 18: warning: here-document at line 14 delimited by end-of-file (wanted `EOF')
./checkMem.sh: line 19: syntax error: unexpected end of file
PS:報如上錯誤是因為腳本中的EOF前面有空格。EOF前后都不應該有空格或其他符號
磁盤報警:
- 磁盤達到80%發送報警郵件
- 發送郵件命令格式
- 多個報警處理
- 把分區信息寫入文件
需要配置郵件服務,樂意參考我郵件服務配置博客:https://www.cnblogs.com/HeiDi-BoKe/p/11883323.html
[root@rhel8 shell]# cat auto_disk_warn.sh #!/bin/bash # auto monitor disk # by author tanbaobao 2020/06/12
D_NETWORK_NAME="ens160" WARN_DATE=`date +"%A %Y年%m月%d日 %H時:%M分:%S秒"` CURRENT_DIR=`pwd` echo -e "\033[31m \033[1m" rm -rf list.txt LIST=`df -h | grep "^/dev/" >>list.txt` cat <<EOF ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++ welcom to use auto monitor system +++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ EOF echo -e "\033[32m------------------------------------------------------------------\033[0m"
# echo # sleep 2
while read line do IP_ADDR=`ifconfig $D_NETWORK_NAME|grep -w "inet"|awk '{print $2}'` D_Name=`echo $line|awk '{print $1,$NF"分區"}'` D_Total=`echo $line|awk '{print $2}'` D_Avail=`echo $line|awk '{print $4}'` D_Percent=`echo $line|awk '{print $5}'|sed 's/%//g'` if [ "$D_Percent" -ge 50 ];then cat >email.txt <<EOF ***************** Email ***************** 通知類型:故障 服務:Disk Monitor 主機:$IP_ADDR 狀態:警告 日期/時間:$WARN_DATE 額外信息: CRITICAL - DISK Monitor: $D_Name Used more than ${D_Percent}% EOF echo -e "\033[32mThe $D_Name has been used for moren than ${D_Percent}%,Please Check.\033[0m" mail -s "$D_Name Warning" qq郵箱@qq.com < $CURRENT_DIR/email.txt # echo "The $D_Name has been userd for moren than ${D_Percent}%,Please Check."|mail -s "$D_Name Warning" 2360415871@qq.com
fi done <list.txt echo -e "\n\033[32m------------------------------------------------------------\033[1m" echo "Done"