Search code examples
linuxbashshelldebiandisk

Bash monitor disk usage


I bought a NAS box which has a cut down version of debian on it.

It ran out of space the other day and I did not realise. I am basically wanting to write a bash script that will alert me whenever the disk gets over 90% full.

Is anyone aware of a script that will do this or give me some advice on writing one?


Solution

  • #!/bin/bash
    source /etc/profile
    
    # Device to check
    devname="/dev/sdb1"
    
    let p=`df -k $devname | grep -v ^File | awk '{printf ("%i",$3*100 / $2); }'`
    if [ $p -ge 90 ]
    then
      df -h $devname | mail -s "Low on space" my@email.com
    fi
    

    Crontab this to run however often you want an alert

    EDIT: For multiple disks

    #!/bin/bash
    source /etc/profile
    
    # Devices to check
    devnames="/dev/sdb1 /dev/sda1"
    
    for devname in $devnames
    do
      let p=`df -k $devname | grep -v ^File | awk '{printf ("%i",$3*100 / $2); }'`
      if [ $p -ge 90 ]
      then
        df -h $devname | mail -s "$devname is low on space" my@email.com
      fi
    done