1 #!/usr/bin/env python 2 3 """This collects filesystem capacity info using the 'df' command. Tuples of 4 filesystem name and percentage are stored in a list. A simple report is 5 printed. Filesystems over 95% capacity are highlighted. Note that this does not 6 parse filesystem names after the first space, so names with spaces in them will 7 be truncated. This will produce ambiguous results for automount filesystems on 8 Apple OSX. """ 9 10 import pexpect 11 12 child = pexpect.spawn ('df') 13 14 # parse 'df' output into a list. 15 pattern = "\n(\S+).*?([0-9]+)%" 16 filesystem_list = [] 17 for dummy in range (0, 1000): 18 i = child.expect ([pattern, pexpect.EOF]) 19 if i == 0: 20 filesystem_list.append (child.match.groups()) 21 else: 22 break 23 24 # Print report 25 print 26 for m in filesystem_list: 27 s = "Filesystem %s is at %s%%" % (m[0], m[1]) 28 # highlight filesystems over 95% capacity 29 if int(m[1]) > 95: 30 s = '! ' + s 31 else: 32 s = ' ' + s 33 print s 34 35