![仅当磁盘使用率超过 80% 时才发送电子邮件](https://linux22.com/image/83008/%E4%BB%85%E5%BD%93%E7%A3%81%E7%9B%98%E4%BD%BF%E7%94%A8%E7%8E%87%E8%B6%85%E8%BF%87%2080%25%20%E6%97%B6%E6%89%8D%E5%8F%91%E9%80%81%E7%94%B5%E5%AD%90%E9%82%AE%E4%BB%B6.png)
我的脚本是:
abc=$(df -h | sed s/%//g | awk '{ if($5 > 80) print "Alert "$0;}' | awk '{print $2,$6}') \
&& echo -e "Dear All, \n\n$abc\n\nABC" \
| mailx -s "Disk partition" [email protected]
当条件不成立时,我会在电子邮件中收到以下输出:
Filesystem Use
不过,我只在磁盘使用率超过 80% 时才需要邮件。
答案1
建议使用 a) multiliners 来处理此类事情,b) 检查“nagios”和 co。对于第一次尝试,请查看此脚本:
#!/bin/bash
function chk () {
# declare as integer to be used in arithmetic expressions
declare -i usage
echo "checking mount $1. has $2 percent"
usage=$2
if [[ ${usage} -gt 80 ]] ; then
echo "alert for partition $1"
#echo -e "Dear ..." | mailx -s "Disk partition ..."
fi
}
df -h | grep -v "none" | tail -n +2 | sed s/%//g | while read a b c d e f;
do chk $a $e;
完毕