我的脚本是:
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;
完毕