Linux: How to monitor the disk usage

In time the disk usage of servers increase and if you do not monitor regularly due to insufficiant disk space your server may not function properly. In order to not to face such trouble it is necessary to follow the disk usage all the time by just creating the shell script file below and adding it to crontab to run the script periodically. According to your needs you may change the alert value. Do not forget to set an email account in the script.

  • disk_usage.sh

    1. ADMIN="admin@yourdomain.com" # Set admin email so that you can get email.
      ALERT=75 # set alert level 75%
      # Exclude list of unwanted monitoring, if several partions then use "|" to separate the partitions.
      # An example: EXCLUDE_LIST="/dev/hdd1|/dev/hdc5"
      EXCLUDE_LIST="/auto/ripper"
      #========================================
      function main_prog() {
      while read output;
      do
      usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1)
      partition=$(echo $output | awk '{print $2}')
      if [ $usep -ge $ALERT ] ; then
      echo "Server disk usage \"$partition ($usep%)\" on server $(hostname), $(date)" | \
      mail -s "Warning: Server disk usage is in critical level $usep%" $ADMIN
      fi
      done
      }
      # ======================================
      if [ "$EXCLUDE_LIST" != "" ] ; then
      df -H | grep -vE "^Filesystem|tmpfs|cdrom|${EXCLUDE_LIST}" | awk '{print $5 " " $6}' | main_prog
      else
      df -H | grep -vE "^Filesystem|tmpfs|cdrom" | awk '{print $5 " " $6}' | main_prog
      fi
  • Add the following line to crontab to run the script every hour:

    1. 0 * * * * /root/scripts/disk_usage.sh>/dev/null

    Bir Cevap Yazın

    E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir


    − 2 = four

    Şu HTML etiketlerini ve özelliklerini kullanabilirsiniz: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

    Yandex.Metrica