On Nov 24, 11:46 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> I would like to write a script in Python to email me when disk space > gets below a certain value. OK, I'll give you the easy way using your example and popen, and then a more complex example that doesn't rely on df/grep/awk and uses only system calls: Easy way: import os # open a pipe to "df ...", read from its stdout, # strip the trailing \n, split it into a list on # every \n, and put the results in 'data' pipe = os.popen("df -x cifs -x iso9660 | " + "grep --color=never -E '^/dev' | " + "awk '{print $1, $4}'") data = pipe.read().strip().split('\n') pipe.close() # 'data' now looks like: # ['/dev/hda1 1405056', '/dev/hda3 152000'] quota = 2097152 # 2GB # iterate the list, splitting each element # on ' ', and assigning the first and second # elements to 'device' and 'free' for node in data: device, free = node.split(' ') if int(free) < quota: # your mail stuff here print "Device `%s' has less than %.2f GB free space" % \ (device, quota/1024.0/1024) More complex way (well, less complex actually, just requires more knowledge of python/fs): quota = 2097152 # 2 GB # map of mount point / device name device_map = {'/' : '/dev/hda1', '/mnt/shared' : '/dev/hda3'} for mount, device in device_map.items(): # statvfs returns tuple of device status, # see also "man statvfs" which this call wraps vstat = os.statvfs(mount) # (block size * free blocks) / 1024 = free bytes # NB: for large drives, there is a margin of error # in this naive computation, because it doesn't account # for things like inode packing by the fs. But for a # large quota like 2 GB, a +/- hundrend megs margin of # error should not cause any problem. free = (vstat[0] * vstat[4]) / 1024 if free < quota: # your mail stuff here print "Device `%s' has less than %.2f GB free space" % \ (device, quota/1024.0/1024) References: statvfs: http://www.python.org/doc/lib/os-file-dir.html http://www.python.org/doc/lib/module-statvfs.html Regards, Jordan -- http://mail.python.org/mailman/listinfo/python-list