socialnerds/hive
socialnerds
/
hive
Archived
1
0
Fork 0
This repository has been archived on 2020-12-06. You can view files and clone it, but cannot push or open issues or pull requests.
hive/diskusage_diezweite.py

63 lines
1.6 KiB
Python
Executable File

#!/usr/bin/python2
import os
def getmountpoint(device):
mtab = open("/etc/mtab", "r")
for line in mtab.readlines():
if device in line:
return line.split()[1]
break
mtab.close()
# getting all local partitions plus their uuids
def getpartitions():
uuids = os.listdir("/dev/disk/by-uuid") #folder contains symlinks to the actual disk devices
parts = {} #initializing empty dictionary for partitions and uuids
for uuid in uuids: #write /dev/names as keys and uuids as values in the dictionary
parts["/dev/" + os.readlink("/dev/disk/by-uuid/" + uuid)[6:]] = uuid
return parts #return the dictionary for further useage
test = getpartitions()
print test
print ""
for device in test.keys():
ismounted = getmountpoint(device)
if ismounted != None:
print str(device) + " is mounted at " + str(ismounted)
else:
print str(device) + " is not mounted"
# getting usage information and print it to stdout
for mountpoint in mountpoints:
#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)
partitions = [["/dev/sda1", "uuid", "/home", "filesystem", "total blocks", "free blocks"], ["/dev/sda2", "uuid", "/", "filesystem", "total blocks", "free blocks"]]