邮箱配置参考Linux mail 命令(smtp.163.com)_Jerry00713的博客-CSDN博客_linux mail命令
1.安装mailx
yum install mailx -y
2.修改配置文件
vi /etc/mail.rc 最后添加 set from=**********@qq.com set smtp=smtp.qq.com set smtp-auth-user=**********@qq.com set smtp-auth-password=********** set smtp-auth=login
3.写shell脚本
vi /tmp/test.sh #!/bin/bash #当前时间 time=`date '+%F %H:%M:%S'` #当前根分区剩余空间 diskfree=$(df -h | awk '{if (NR==2) print $4}' | cut -d "G" -f 1) if [[ $diskfree -lt 15 ]] then echo -e "磁盘剩余空间是:$diskfree"G"小于15G\n当前时间:$time" | mail -s 'performance test' ********@163.com fi #当前内存剩余 memory=`free -m | awk '{if (NR==2) print $4}'` if [[ $memory -lt 200 ]] then echo -e "内存剩余:$memory 小于200\n当前时间:$time" | mail -s 'memory test' *********@163.com fi #脚本功能描述:依据/proc/stat文件获取并计算CPU使用率 #CPU时间计算公式:CPU_TIME=user+system+nice+idle+iowait+irq+softirq #CPU使用率计算公式:cpu_usage=(idle2-idle1)/(cpu2-cpu1)*100 #默认时间间隔 TIME_INTERVAL=5 LAST_CPU_INFO=$(cat /proc/stat | grep -w cpu | awk '{print $2,$3,$4,$5,$6,$7,$8}') LAST_SYS_IDLE=$(echo $LAST_CPU_INFO | awk '{print $4}') LAST_TOTAL_CPU_T=$(echo $LAST_CPU_INFO | awk '{print $1+$2+$3+$4+$5+$6+$7}') sleep ${TIME_INTERVAL} NEXT_CPU_INFO=$(cat /proc/stat | grep -w cpu | awk '{print $2,$3,$4,$5,$6,$7,$8}') NEXT_SYS_IDLE=$(echo $NEXT_CPU_INFO | awk '{print $4}') NEXT_TOTAL_CPU_T=$(echo $NEXT_CPU_INFO | awk '{print $1+$2+$3+$4+$5+$6+$7}') #系统空闲时间 SYSTEM_IDLE=`echo ${NEXT_SYS_IDLE} ${LAST_SYS_IDLE} | awk '{print $1-$2}'` #CPU总时间 TOTAL_TIME=`echo ${NEXT_TOTAL_CPU_T} ${LAST_TOTAL_CPU_T} | awk '{print $1-$2}'` CPU_USAGE=`echo ${SYSTEM_IDLE} ${TOTAL_TIME} | awk '{printf "%.2f", 100-$1/$2*100}'` cpu_usage=$(echo $CPU_USAGE | cut -d "." -f 1) if [[ $cpu_usage > 80 ]] then echo -e "CPU Usage:${cpu_usage}% \n当前时间:$time" | mail -s 'cpu test' **********@163.com fi
4.定时任务
crontab -e */5 * * * * /usr/bin/sh /tmp/test.sh