diff --git a/df_check.py b/df_check.py index 3df01c5..0001b09 100755 --- a/df_check.py +++ b/df_check.py @@ -1,9 +1,10 @@ #!/usr/bin/python2 +# import os for statvfs import os -#fileoperations +#reading needed files file = open("/proc/partitions", "r") parts = file.read() file.close() @@ -11,11 +12,10 @@ file = open("/proc/mounts", "r") mounts = file.read() file.close() -#definitions 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": @@ -29,6 +29,8 @@ 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]) @@ -40,11 +42,14 @@ for part in partitions: blockcount = 0 counter += 1 + +# moving back to old var name +# too lazy to rename everything below partitions = partsnew blocks = blocksnew -#getting mountpoints to devices +# getting mounted partitions and their mountpoints mountpoints = [] for item in partitions: for line in mounts.split("\n"): @@ -52,31 +57,29 @@ for item in partitions: mountpoints.append(line.split()[1]) break - - +# output for debugging print "partitions: %s\nblocks: %s\nmountpoints: %s" %(partitions, blocks, mountpoints) -print "\n" + # given paths(mountpoints) #path=("/boot", "/", "/dev/sda6") path = mountpoints -#output head print "mountpoints\tfree megabytes" print "------------------------------" -#work path tuple +# getting usage information and print it to stdout for mountpoint in path: #check if given path is a mountpoint -# if os.path.ismount(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 is not a mountpoint" + else: + print "error: given path (%s) is not a mountpoint" %(mountpoint) # end of file