如何使用df -h、awk等工具为/mnt/hgfs创建自定义告警列
Custom Disk Alert Column for /mnt/hgfs Using df, awk, tail, tr
Perfect, let's build this custom disk alert for /mnt/hgfs using the tools you mentioned. Here's a clean, functional one-liner that does exactly what you need, plus I'll break down each part so you understand how it works:
df -h /mnt/hgfs | tail -n +2 | tr -s ' ' | awk '{ usage = substr($5, 1, length($5)-1) if (usage >= 75 && usage <= 80) alert = "Warning" else if (usage >= 81 && usage <= 95) alert = "Critical" else if (usage > 95) alert = "Alarm" else alert = "OK" print $0, "\t", alert }'
Breakdown of Each Component
Let's walk through what each tool is doing here:
df -h /mnt/hgfs: Fetches human-readable disk usage stats specifically for the/mnt/hgfsmount point. The-hflag ensures sizes are displayed in GB/MB instead of raw blocks, making it easier to read.tail -n +2: Skips the first header line ofdf's output (since we only care about the actual data row for/mnt/hgfs).tr -s ' ': Compresses multiple consecutive spaces into a single space.dfuses variable spacing between columns, which can throw offawk's field detection—this small fix ensures each column is recognized correctly.awk(the core logic):- Extracts the numeric usage value:
substr($5, 1, length($5)-1)strips off the%symbol from the 5th column (the usage percentage), turning it into a number we can compare directly. - Runs conditional checks to set the alert status based on your thresholds:
- 75-80% → Warning
- 81-95% → Critical
95% → Alarm
- All other values → OK
- Prints the original disk stats followed by a tab-separated alert status for clean, readable output.
- Extracts the numeric usage value:
Bonus: Include a Custom Header
If you want to keep the original df header and add an "Alert" column to make the output more structured, use this modified version:
df -h /mnt/hgfs | tr -s ' ' | awk ' NR == 1 {print $0, "\t", "Alert"; next} { usage = substr($5, 1, length($5)-1) if (usage >=75 && usage <=80) alert="Warning" else if (usage >=81 && usage <=95) alert="Critical" else if (usage >95) alert="Alarm" else alert="OK" print $0, "\t", alert } '
This will output a proper table with headers like Filesystem Size Used Avail Use% Mounted on Alert, making it even easier to parse at a glance.
内容的提问来源于stack exchange,提问作者John Rudolph
相关产品推荐
相关产品推荐

