#!/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