检查内存使用情况,当内存可使用等于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"