From e893cd76e2d53f2f544c98aaec26f26f1fa8ee1b Mon Sep 17 00:00:00 2001 From: David Date: Wed, 16 May 2012 01:05:02 +0200 Subject: [PATCH] added diskusage.py --- diskusage.py | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100755 diskusage.py diff --git a/diskusage.py b/diskusage.py new file mode 100755 index 0000000..77a8284 --- /dev/null +++ b/diskusage.py @@ -0,0 +1,98 @@ +#!/usr/bin/python2 + +# import os for statvfs +import os + + +# reading a file +def fileparser(path): + file = open(path, "r") + result = file.read() + file.close() + return result + +#file = open("/proc/partitions", "r") +#parts = file.read() +#file.close() +#file = open("/proc/mounts", "r") +#mounts = file.read() +#file.close() + +# reading needed files +parts = fileparser("/proc/partitions") +mounts = fileparser("/proc/mounts") #bug.. what if uuids are in /proc/mounts + +partitions = [] +blocks = [] +# getting all local partitions (regardless if mounted or not) +# and blocks from /proc/partitions and write it to lists +for line in parts.split("\n"): + if line and "name" not in line and "sr0" not in line: + if line.split()[2] != "1": + partitions.append("/dev/" + line.split()[3]) + blocks.append(line.split()[2]) + + +hold="nothing" +holdblocks = 0 +blockcount = 0 +counter = 0 +partsnew = [] +blocksnew = [] +# detect disk like /dev/sda itself and +# filter it from partitions and blocks list +for part in partitions: + if hold in part: + blockcount = blockcount + int(blocks[counter]) + partsnew.append(part) + blocksnew.append(blocks[counter]) + else: + hold = part + holdblocks = blocks[counter] + blockcount = 0 + counter += 1 + + +# moving back to old var name +# too lazy to rename everything below +partitions = partsnew +blocks = blocksnew + +# getting mounted partitions and their mountpoints +mountpoints = [] +mountparts = [] +for item in partitions: + for line in mounts.split("\n"): + if line and item in line: + mountpoints.append(line.split()[1]) + mountparts.append(item) + break + +# output for debugging +print "debug output:" +print "availible partitions: %s\nblocks: %s\nmounted partitions: %s\nmountpoints: %s" %(partitions, blocks, mountparts, mountpoints) +print "" #linebreak + + +# given paths(mountpoints) +#path=("/boot", "/", "/dev/sda6") +path = mountpoints + +print "mountpoints\tfree megabytes" +print "------------------------------" + +# getting usage information and print it to stdout +for mountpoint in path: + #check if given path is a mountpoint + if os.path.ismount(mountpoint): + #create statvfs object + fs = os.statvfs(mountpoint) + #calculate free megabytes + freembytes = fs.f_bavail * fs.f_bsize / 1024**2.0 + #output + print "%s\t\t%.1f MB" %(mountpoint, freembytes) + else: + print "error: given path (%s) is not a mountpoint" %(mountpoint) + + +# end of file