2

I use df to check if any of my mounted file systems are close to getting full. I have a cron job that sends me email if any of them are getting to be above 90% full.

The problem is that some applications (Ubuntu distribution) are now installed using snap which creates a read only file system that is mounted specifically for that application. They are always 100% full as reported by df. I guess that is appropriate since they are read only and can't get fuller. I'd like to exclude them from the df output.

df has an -x, --exclude-type=TYPE command line flag that looks like it could be helpful, but the man page doesn't document possible values for TYPE so I'm not sure if I can use that to exclude read only file systems or not.

Here is an example of df output on one of my systems:

$ df --output=pcent,target 
Use% Mounted on
  0% /dev
  1% /run
  7% /
  1% /dev/shm
  1% /run/lock
  0% /sys/fs/cgroup
100% /snap/gnome-3-26-1604/74
100% /snap/gnome-characters/139
100% /snap/core/5662
100% /snap/gnome-system-monitor/57
100% /snap/gnome-calculator/238
100% /snap/gnome-logs/45
100% /snap/gtk-common-themes/701
100% /snap/core/5742

Here is my script that is run by cron:

#!/bin/bash    
set -e 
THRESHOLD_PERCENT=90    
host=`hostname -f`
df --output=pcent,target | while read line 
do
    if [[ "$line" != Use* ]]
    then
        percent=${line/\%*/};
        if (( percent >= THRESHOLD_PERCENT ))
        then
            file=${line/* /}
            echo "$percent% disk usage on $host:$file"
        fi
    fi
done

I'd like to remove all those full snap entries and any other file systems that might be mounted read-only in the future.

  • 3
    For the specific case of snaps, you might find df --exclude-type=squashfs helpful – steeldriver Nov 1 '18 at 11:29
2

The file system type used in df refers to the format of the file system, like ext4.

You can find the read-write file systems by filtering /proc/mounts (or the output of mount) for rw and then use these to filter the output of df.

You can't use all rw entries from /proc/mounts because that also contains entries like /proc that you don't want in your list.

Edit

#!/bin/bash
df --output=pcent,target | grep -f <(awk '$4 ~ /^rw.*/ { print $2"$" }' /proc/mounts)
| improve this answer | |
  • Do you have a command line that will apply /proc/mounts to df? I realize the info I need is in there, but I don't know how I would filter df by it because it is so buried. – Stephen Ostermiller Nov 1 '18 at 11:19
  • I added the command for bash. With another shell, you may need a temporary file. – RalfFriedl Nov 1 '18 at 11:57
  • That seems to work, my only concern is that I can't find documentation that rw has to be first in the list of mount options. It seems to be first in all cases, on my system, but I don't know if that has to be the case. I'm thinking that $4 ~ /.*(^|,)rw($|,).*/ would be a safer regex to find the rw anywhere in that comma delimited list of mount options. Or maybe it should be $4 !~ /.*(^|,)ro($|,).*/ (not read only). – Stephen Ostermiller Nov 1 '18 at 12:38
  • I've never seen roor rw other that at the start, but it may be possible. – RalfFriedl Nov 1 '18 at 12:58
2

Yes, snap's intrusion into the filesystems list can be annoying...

You're on the right lines with df -x.... All snaps use the read-only Squashfs filesystem1, so you can filter them all out with:

df -xsquashfs
| improve this answer | |
0

To my knowledge df doesn't know about mount options, as there is the mount command for that. Which you could use to get a list of devices and pass those as an argument to df. Like this:

df --output=pcent,target $(mount -t ext4 | grep rw | cut -d" " -f1)

This filters by filesystem ext4 and then greps for those with mount options rw, cleans up the output to be used for df and then passes on the list of devices to df. You can play with the mount command inside the $(...) to get your filtering right for your purposes.

| improve this answer | |
  • Why just ext4 mounted file systems? I'd like to monitor anything that could fill up including nfs, reiserfs, and ext3. – Stephen Ostermiller Nov 1 '18 at 11:45
  • As I pointed out in my answer the options on mount are just examples and can of course be modified. However not filtering on filesystem type makes the output of mount potentially more messy. – Stefan Horning Nov 1 '18 at 16:14
  • See this answer unix.stackexchange.com/a/159984/318807 for examples on how to filter mount ouput. – Stefan Horning Nov 1 '18 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.