97 lines
2.4 KiB
Python
Executable File
97 lines
2.4 KiB
Python
Executable File
#!/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
|
|
mounts = fileparser("/etc/mtab") #maybe that fixes the bug
|
|
|
|
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 = {}
|
|
for item in partitions:
|
|
for line in mounts.split("\n"):
|
|
if line and item in line:
|
|
mountpoints[item]=line.split()[1]
|
|
break
|
|
|
|
# output for debugging
|
|
print "debug output:"
|
|
print "availible partitions: %s\nblocks: %s\nmounts: %s" %(partitions, blocks, mountpoints)
|
|
print "" #linebreak
|
|
|
|
# given paths(mountpoints)
|
|
#path=("/boot", "/", "/dev/sda6")
|
|
path = mountpoints.values()
|
|
|
|
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
|