首先确保服务器安装有 Mailx
,对应命令是 mail
:
1 2
| [root@localhost ~]# which mail /usr/bin/mail
|
然后编辑 Mailx
的配置文件:
在结尾添加以下内容:
1 2 3 4 5
| set from=xxxx@qq.com set smtp=smtp.qq.com set smtp-auth-user=xxxx@qq.com set smtp-auth-password=yyyyyyyyyyyyyy set smtp-auth=login
|
其中xxxx是邮箱账户(我这里用的QQ邮箱),yyyyyyyyyyyy是 安全码
,需要自行获取.
然后是执行的脚本,自行保存为 *.sh
,并赋予执行权限:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| #!/bin/bash
# CPU使用率阈值 CPU=40 # 内存使用率阈值 RAM=60 # 磁盘使用率阈值 DISK=60 # 温度阈值 TEMPERATURE=60
ALARM=0;
function check { # 检测CPU cpu=$(mpstat | grep all | awk '{print int($12)}') cpu=$((100-$cpu)) if [ $cpu -gt $CPU ] then echo "检测CPU:$cpu% (异常)";ALARM=1 else echo "检测CPU:$cpu% (正常)"; fi # 检测内存 ram_free=$(vmstat -s | sed -n '5p' | egrep -o "[0-9]*") ram_inactive=$(vmstat -s | sed -n '4p' | egrep -o "[0-9]*") ram_total=$(vmstat -s | sed -n '1p' | egrep -o "[0-9]*") ram=$((($ram_free + $ram_inactive) * 100 / $ram_total)) ram=$((100-$ram)) if [ $ram -gt $RAM ] then echo "检测内存:$ram% (异常)";ALARM=1 else echo "检测内存:$ram% (正常)" fi # 检测磁盘 disk=$(df -P | grep "^/dev" | sort -rnk 5 | awk '{print $5}' | sed 's/%//g' | head -n 1) if [ $disk -gt $DISK ] then echo "检测磁盘:$disk% (异常)";ALARM=1 else echo "检测磁盘:$disk% (正常)" fi # 检测温度 temperature=$(sensors | egrep -o 'id 0: +(\S*)°C' | egrep -o '[0-9]{2,3}') if [ $temperature -gt $TEMPERATURE ] then echo "检测温度:$temperature°C (异常)";ALARM=1 else echo "检测温度:$temperature°C (正常)" fi }
function print {
echo 【CPU占用前十明细】 ps aux | head -1; ps aux | grep -v PID | sort -rn -k +3 | head echo
echo 【内存占用前十明细】 ps aux | head -1; ps aux | grep -v PID | sort -rn -k +4 | head echo
echo 【磁盘使用情况明细】 df -P | grep "^/dev/*" echo
echo 【机器温度情况明细】 sensors echo }
function send { TIME=$(date +%s) check >> /tmp/mail_$TIME.log echo >> /tmp/mail_$TIME.log print >> /tmp/mail_$TIME.log mailx -v -s 服务器监控报警-$TIME -a /tmp/mail_$TIME.log xxxx@qq.com < /tmp/mail_$TIME.log }
check if [ $ALARM != 0 ]; then send fi
|
最后是定时任务,先检测下服务有没有:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| [root@localhost ~]# systemctl status crond ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since 一 2018-08-13 08:46:22 CST; 7h ago Main PID: 1160 (crond) Tasks: 1 Memory: 700.0K CGroup: /system.slice/crond.service └─1160 /usr/sbin/crond -n
8月 13 08:46:22 my-6xyun-cn systemd[1]: Started Command Scheduler. 8月 13 08:46:22 my-6xyun-cn systemd[1]: Starting Command Scheduler... 8月 13 08:46:22 my-6xyun-cn crond[1160]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 51% if used.) 8月 13 08:46:22 my-6xyun-cn crond[1160]: (CRON) INFO (running with inotify support)
|
然后添加一个定时任务(我这每分钟执行一次):
添加以下行:
1
| */1 * * * * /root/monitor.sh > /dev/null 2>&1
|
最后确认一下:
1 2
| [root@localhost ~]# crontab -l */1 * * * * /root/monitor.sh > /dev/null 2>&1
|
注意定时任务中不要随意输出,否则 Cron
会把这些输出通过邮件发送给用户(root
).
具体表现就是终端自动提示收到邮件:
1
| 您在 /var/spool/mail/root 中有新邮件
|
详情请看:http://blog.chinaunix.net/uid-434226-id-3337369.html