要求:监控系统可用内存,小于100M就发送报警邮件 否则,提示内存充足,定时 任务每3分钟1次
1.取出当前内存
#centos 6.x buffers/cache 这一行 第3行 [root@localhost tmp]# free -h total used free shared buffers cached Mem: 980M 167M 812M 224K 8.0M 43M -/+ buffers/cache: 116M 864M Swap: 1.0G 0B 1.0G
#centos 7 free+buffer+cache 看available [root@m01 log]# free -h total used free shared buff/cache available Mem: 972M 90M 745M 7.6M 137M 728M Swap: 767M 0B 767M
2.书写脚本,发邮件
#centos7脚本 #!/bin/bash mem=$(free -m|awk 'NR==2{print $NF}') if [ "$mem" -lt "10000" ] then echo "mem is buzu" |tee /root/mem.log mail -s "memory is buzu" 841997568@qq.com </root/mem.log else echo "mem is zu" fi #centos6脚本 #!/bin/bash mem=$(free -m|awk 'NR==3{print $4}') if [ "$mem" -lt "10000" ] then echo "mem is buzu" |tee /root/mem.log mail -s "memory is buzu" 841997568@qq.com </root/mem.log else echo "mem is zu" fi
如果没mail服务会提示报错,直接yum装一个
yum install -y mailx
如何配置mailx发邮件:Linux系统使用mail发送邮件配置
3. 定时任务
[root@m01 /server/scripts]# crontab -e #check memory 3 * * * * sh /server/scripts/chk_mem.sh &>/dev/null
赞赏
微信赞赏
支付宝赞赏