You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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/hgfs mount point. The -h flag ensures sizes are displayed in GB/MB instead of raw blocks, making it easier to read.
  • tail -n +2: Skips the first header line of df's output (since we only care about the actual data row for /mnt/hgfs).
  • tr -s ' ': Compresses multiple consecutive spaces into a single space. df uses variable spacing between columns, which can throw off awk'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.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:29:39