From f06c3ccc5698926882dc989e4af0d24e196769da Mon Sep 17 00:00:00 2001 From: David Date: Sun, 20 May 2012 17:51:55 +0200 Subject: [PATCH] cleaned up repository(master) and moved display code for disk monitor to monitor/disk_display.py --- archive/diskusage.py | 96 ---------------------------------- archive/diskusage_diezweite.py | 62 ---------------------- disk_mon.py => monitor/disk.py | 18 ------- monitor/disk_display.py | 20 +++++++ 4 files changed, 20 insertions(+), 176 deletions(-) delete mode 100755 archive/diskusage.py delete mode 100755 archive/diskusage_diezweite.py rename disk_mon.py => monitor/disk.py (65%) mode change 100755 => 100644 create mode 100755 monitor/disk_display.py diff --git a/archive/diskusage.py b/archive/diskusage.py deleted file mode 100755 index 70a773f..0000000 --- a/archive/diskusage.py +++ /dev/null @@ -1,96 +0,0 @@ -#!/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 diff --git a/archive/diskusage_diezweite.py b/archive/diskusage_diezweite.py deleted file mode 100755 index 41a3372..0000000 --- a/archive/diskusage_diezweite.py +++ /dev/null @@ -1,62 +0,0 @@ -#!/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"]] - - - - - - - - - - diff --git a/disk_mon.py b/monitor/disk.py old mode 100755 new mode 100644 similarity index 65% rename from disk_mon.py rename to monitor/disk.py index 83e2af2..e9de2a7 --- a/disk_mon.py +++ b/monitor/disk.py @@ -1,5 +1,3 @@ -#!/usr/bin/python2 - """ disk monitor """ class diskmon(object): @@ -40,21 +38,5 @@ class diskmon(object): #adding partition to list self.partitions.append(part) -#output -if __name__ == "__main__": - diskmoninstance = diskmon() - for partition in diskmoninstance.partitions: - 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" - else: - print "\n" - - #end of file diff --git a/monitor/disk_display.py b/monitor/disk_display.py new file mode 100755 index 0000000..ba067d3 --- /dev/null +++ b/monitor/disk_display.py @@ -0,0 +1,20 @@ +#!/usr/bin/python2 + +""" simple display to show results of disk.py """ + +import disk + +#output +if __name__ == "__main__": + diskmoninstance = disk.diskmon() + for partition in diskmoninstance.partitions: + 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" + else: + print "\n"