Another easy tip for the day: Want to receive a warning when one of your virtual machines’ filesystems is filling up? Use virt-df, awk, and cron like this.
Note: I’ve set this up so I can do it as non-root, but that meant I had to add myself to the ‘disk’ group first, so that virt-df can read out the contents of the disks of the virtual machines. There’s one extra thing needed which is to set the LIBVIRT_DEFAULT_URI environment variable to access the global libvirtd socket.
$ export LIBVIRT_DEFAULT_URI='qemu+unix:///system?socket=/var/run/libvirt/libvirt-sock-ro'
$ virt-df | awk 'strtonum(substr($5,0,length($5)-1))>=60 {print}'
Debian5x64:/dev/debian5x64/root 329233 200395 111840 61%
Windows7x32:/dev/vda2 10381308 7740852 2640456 75%
Windows7x64:/dev/vda2 10381308 9417676 963632 91%
You can set the threshold to whatever you want. In the above awk code it’s set to 60%, you’d probably want it higher.
Now put those two commands into a script file somewhere, and create a cronjob. The following one runs daily:
MAILTO=you@example.com 0 1 * * * /usr/local/etc/diskcheck.sh
What do you do when a VM is running out of space? Why, you run virt-resize of course.
Ob-geekiness: The full virt-df output from one of my test hosts. This one is used for a lot of interop testing:
$ virt-df
Filesystem 1K-blocks Used Available Use%
RHEL6200910210x32:/dev/vda1 198337 36582 151515 19%
RHEL6200910210x32:/dev/vg_rhel6200910210x32/lv_root
9147776 5305876 3377212 59%
RHEL6201002033x64:/dev/vda1 495844 32396 437848 7%
RHEL6201002033x64:/dev/vg_rhel6201002033x64/lv_root
18102140 4097800 13084788 23%
Debian5x64:/dev/debian5x64/home 3555936 1264800 2110504 36%
Debian5x64:/dev/debian5x64/root 329233 200395 111840 61%
Debian5x64:/dev/debian5x64/tmp 309401 10294 283133 4%
Debian5x64:/dev/debian5x64/usr 3539776 1434740 1925224 41%
Debian5x64:/dev/debian5x64/var 1741648 264632 1388544 16%
Debian5x64:/dev/vda1 233335 47272 173615 21%
Ubuntu910x64:/dev/vda1 9827520 4180524 5147780 43%
RHEL6Alpha3x64:/dev/vda1 198337 22879 165218 12%
RHEL6Alpha3x64:/dev/vg_rhel6alpha3x64/lv_root
8180152 4174904 3589712 52%
RHEL54Betax64:/dev/VolGroup00/LogVol00
15109112 8695548 5633688 58%
RHEL54Betax64:/dev/vda1 101086 12449 83418 13%
F10x32:/dev/VolGroup00/LogVol00 9191640 3083532 5642856 34%
F10x32:/dev/vda1 194442 20706 163697 11%
F12x64:/dev/vda1 198337 22782 165315 12%
F12x64:/dev/vg_f12x64/lv_root 9115576 4396520 4256004 49%
F13Rawhidex64:/dev/vda1 198337 60031 128066 31%
F13Rawhidex64:/dev/vg_f13rawhide/lv_root
9115576 6018664 2633860 67%
F12x64preview:/dev/vda1 198337 22782 165315 12%
F12x64preview:/dev/vg_f12x64/lv_root 9115576 4781384 3871140 53%
Win2003x32:/dev/vda1 20956760 3053092 17903668 15%
VSphere:/dev/vda1 31447204 8159028 23288176 26%
Windows7x32:/dev/vda1 102396 24712 77684 25%
Windows7x32:/dev/vda2 10381308 7740852 2640456 75%
Windows7x64:/dev/vda1 102396 24704 77692 25%
Windows7x64:/dev/vda2 10381308 9417676 963632 91%
CentOS5x32:/dev/VolGroup00/LogVol00 9014656 4069840 4479512 46%
CentOS5x32:/dev/vda1 101086 36210 59657 36%
Update There is a small but important bug in this code. Please see the fixed version.

Doesn’t that break for multi-line output like:
F13Rawhidex64:/dev/vg_f13rawhide/lv_root
9115576 6018664 2633860 67%
Hmm yes, that’s a bug. I’ll have to add an option to virt-df to print out everything on one line and/or switch to using the CSV format output.
Pingback: Tip: find out when filesystems get full with virt-df (working version!) « Richard WM Jones