socialnerds/hive
socialnerds
/
hive
Archived
1
0
Fork 0

cleaned up repository(master) and moved display code for disk monitor to monitor/disk_display.py

This commit is contained in:
David 2012-05-20 17:51:55 +02:00
parent 326b4d70d5
commit f06c3ccc56
4 changed files with 20 additions and 176 deletions

View File

@ -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

View File

@ -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"]]

18
disk_mon.py → monitor/disk.py Executable file → Normal file
View File

@ -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

20
monitor/disk_display.py Executable file
View File

@ -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"