#!/bin/bash
threshold="1"
{
>/tmp/output
for fs in $(df -hk | awk '{print $6}' | sed '1 d'); do
chk=$(df -hk ${fs} | sed '1 d' | awk '{print $5}' | awk -F\% '{print $1}')
if [ "${chk}" -gt "${threshold}" ]; then
echo "$(hostname): Alert Fileystem ${fs} is above ${threshold}%." >>/tmp/output
fi
done
cat /tmp/output| mailx -s "sub" [email protected]
}
现在,如果 fs 超过阈值限制,我们应该收到邮件
答案1
只是建议您如何做到这一点。请参阅脚本中的注释。
#!/bin/bash
# define constants on top so you can find and edit them easily
declare -ri threshold=1
declare -r mailsubject="[$(hostname)] FS Alert Exceed ${threshold}%"
declare -r mailbodystart="$(hostname) Filesystem Alert, threshold=${threshold}%\n"
declare -r mailto="[email protected]"
# use variable instead of output file
declare mailbody=""
for fs in $(df -hk | awk '{print $6}' | sed '1d'); do
chk=$(df -hk ${fs} | sed '1d' | awk '{print $5}' | awk -F\% '{print $1}')
if [ "$chk" -gt "$threshold" ]; then
# append to mailbody
mailbody="${mailbody}${chk}% ${fs}\n"
fi
done
# send mail if mailbody is non-empty
if [ -n "$mailbody" ]; then
# sort by filesystem size, largest first
mailbody=$(echo -e "$mailbody" | sort -nr)
echo -e "${mailbodystart}${mailbody}" | mailx -s "$mailsubject" "$mailto"
fi