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