From 4e3aee77385317991bc499f8ad556fe5fc42d4e7 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 20 May 2012 19:19:28 +0200 Subject: [PATCH] trying to get a nice disk display working --- monitor/disk_display.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/monitor/disk_display.py b/monitor/disk_display.py index 9563de5..5194163 100755 --- a/monitor/disk_display.py +++ b/monitor/disk_display.py @@ -4,17 +4,31 @@ import disk +def bytes2human(bytes): + units = ["B", "KB", "MB", "GB", "TB", "PB", "EB"] + counter = 0 + while bytes > 1024: + bytes = bytes / 1024 + counter += 1 + human = [bytes, units[counter]] + return human + +def blocks2bytes(blocks, blocksize): + bytes = float(blocks) * float(blocksize) + return bytes + + if __name__ == "__main__": diskmoninstance = disk.diskmon() for partition in diskmoninstance.partitions: - print "device\t\t:" + str(partition["device"]) + \ - "\nuuid\t\t:" + str(partition["uuid"]) + print "Device:\t\t" + str(partition["device"]) + \ + "\nUUID:\t\t" + str(partition["uuid"]) if partition.has_key("mountpoint"): - print "mountpoint:\t" + str(partition["mountpoint"]) + \ - "\nfilesystem:\t" + str(partition["filesystem"]) + \ - "\nblocksize:\t" + str(partition["blocksize"]) + \ - "\ntotal blocks:\t" + str(partition["total blocks"]) + \ - "\nfree blocks:\t" + str(partition["free blocks"]) + "\n" + totalbytes = blocks2bytes(partition["total blocks"], partition["blocksize"]) + totalhuman = bytes2human(totalbytes) + print "Mountpoint:\t" + str(partition["mountpoint"]) + \ + "\nFilesystem:\t" + str(partition["filesystem"]) + \ + "\nSize:\t\t%.2f %s\n" %(totalhuman[0], totalhuman[1]) else: print "\n"