1.设置外部邮箱
set from=xx@163.com
set smtp=smtp.163.com
set smtp-auth-user=xx@163.com
set smtp-auth-password=密码
set smtp-auth=login
2.编写监控脚本
#!/bin/bash
# 预警值
use_ratio=20
df_info="/home/monitor/df_info.txt"
task_status_file="/home/monitor/task_status.txt"
# 获取当前IP
ip=`ifconfig em1 | grep inet | grep netmask | awk '{print $2}'`
for value in `df -h | awk '{print $5}' | cut -f 1 -d %`
do
if [ $value -gt $use_ratio ]; then
df -h >> $df_info
if [ ! -f $task_status_file ];then
echo -e "服务器$ip磁盘使用超出预警值\n `cat $df_info` " | mail -s "服务器使用报警" 收件人邮箱地址;
echo "unfinish" > $task_status_file;
exit 0;
fi
fi
done
3.说明
查看使用率
df -h
获取具体使用率的值
方式一:
df -P | grep /home | awk '{print $5}' | cut -f 1 -d %
方式二:
df -P | grep /home | awk '{print $5}' | sed 's/%//g'
获取本机IP
方式一:
/sbin/ifconfig -a | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}' | tr -d "addr:"
方式二:
ifconfig em1 | grep "inet" | grep netmask | awk '{print $2}'