Shell script to monitor /boot disk usage

terminalLinux tends to allocate a relatively small amount of storage space to the /boot volume during the install process and, as a result, it can quickly fill up with kernel updates, particularly if you have your server set up to automatically apply any security updates. In my experience, when this happens then the server can begin to act twitchy.

So I needed to find a way for my servers to automatically notify me so I can get in there and clean up /boot before it becomes a problem. Some quick Google searching turned up this great shell script, which I’ve repurposed for my own use:

#!/bin/sh
 ADMIN="recipientEmailAddress"
 ALERT=80
 df -h /dev/sda1 | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
 do
    #echo $output
    usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
    partition=$(echo $output | awk '{ print $2 }' )
    if [ $usep -ge $ALERT ]; then
       echo "" | mail -s "ALERT: /boot is $usep% full" $ADMIN -aFrom:'senderName<senderEmailAddress>' -aImportance:'high'
    fi
 done

Note that there are a few caveats here:

  1. Line 2: You can specify multiple recipients by separating each e-mail addresses with a comma.
  2. Line 3: You can change the ALERT value to whatever percentage full you want to be alerted at.
  3. Line 4: I added the /dev/sda1 value so that the df command will only check the /boot volume (without it, this script will generate an alert for each volume on the machine that is getting full).
  4. Line 10: I made a few tweaks to the mail command – I added the -aFrom variable so I could create a descriptive sender e-mail address (otherwise, this shows as having been sent by whatever *nix account was used to execute the shell script) and I used -aImportance to set the message’s priority as “high” in order to make it stand out when sent as an email.

Add this script as a cron job and you’ll never get caught flat-footed by your /boot volume again!

P.S. – Don’t forget to check out my earlier post, Clearing space on Linux /boot partition, to learn how to properly remove those space-wasting, old kernels.


Update
Based on comments, here’s a simplified version (16.03.31: now including code to automatically run autoremove in order to clean up /boot without user intervention; I’ve kept the notification code in in case something goes wrong with autoremove):

#!/bin/sh
ADMIN="recipientEmailAddress"
ALERT=80

usep=$(df -h /boot | grep -vE '^Filesystem' | awk '{ print $5 }' | cut -d'%' -f1);
if [ $usep -ge $ALERT ]; then
   apt-get autoremove -y
fi
usep=$(df -h /boot | grep -vE '^Filesystem' | awk '{ print $5 }' | cut -d'%' -f1);
if
   [ $usep -ge $ALERT ]; then  echo "" | mail -s "ALERT: /boot is $usep% full" $ADMIN -aFrom:'senderName<senderEmailAddress>' -aImportance:'high'
fi

2 thoughts on “Shell script to monitor /boot disk usage

  1. Couple of things. Just use df -P /boot. No need to use a device file. And if you are doing that you only need to either grep out the first line, or use tail, or something. You would’nt need to grep out tempfs and cdrom since you already chose the mount point. But I can see that you made changes to a script that loops through all mounted file systems. It could be a lot simpler. There is really no need for the loop.

  2. Feeble:

    Great points – it was kind of bothering me that I was specifying /dev/sda1 rather than the volume name, as that ties to closely to the assumption that /boot is always located on /dev/sda1. And you’re right about the loop; no need need if I’m not monitoring the entire system (as this script was originally designed to do).

    Clearly, this script kiddie has some more critical thinking to do.

Leave a Reply to feeble Cancel reply

Your email address will not be published. Required fields are marked *