我编写了一个简单的 bash 脚本来检查磁盘使用情况并为超过 85% 的所有分区发送电子邮件警报。我希望输出看起来像这样:
“/dev/mapper/rootvg-root 处于 87%。一封电子邮件已发送给您的管理员。”
但是,当前脚本打印每个分区,但使用的百分比与正确的分区不匹配。下面的代码与输出:
#!/bin/bash
admin="[email protected]"
threshold="85"
util=`df -h | tail -n +2 | awk '{print $5}' | sed 's/.$//'`
part=`df -h | tail -n +2 | head -$1 | awk '{print $1}'`
for i in $util;
do
if [ $i -ge $threshold ]; then
echo "$part is currently at ${i}%. An email has been sent to your administrator."
mail -s "Critical Alert: $part is currently at ${i}%. Clear space immeadiately!" $admin
fi
done
输出:
/dev/mapper/rootvg-root
devtmpfs
tmpfs
/dev/mapper/rootvg-var
/dev/mapper/rootvg-u01
/dev/mapper/rootvg-home
/dev/mapper/rootvg-apps
/dev/mapper/rootvg-tmp is currently at 92%. An email has been sent to your administrator.
/dev/mapper/rootvg-tmp 未达到 92%,/dev/mapper/rootvg-root 为 92%,低于 85% 的其他分区不应显示。
答案1
#!/bin/bash
admin="[email protected]"
threshold="85"
df -l --output=source,pcent | while IFS=" %" read -r part i; do
if [ $i -ge $threshold ] 2>/dev/null; then
echo "$part is currently at ${i}%. An email has been sent to your administrator."
mail -s "Critical Alert: $part is currently at ${i}%. Clear space immeadiately!" $admin
fi
done