81 lines
2.8 KiB
Python
Executable File
81 lines
2.8 KiB
Python
Executable File
#!/usr/bin/python2
|
|
|
|
#
|
|
# disk monitor
|
|
#
|
|
|
|
#imports
|
|
import os
|
|
|
|
|
|
class diskmon(object):
|
|
"""collect information about local partitions"""
|
|
|
|
#constructor
|
|
def __init__(self):
|
|
pass
|
|
|
|
# def get_part(self):
|
|
# uuids = os.listdir("/dev/disk/by-uuid")
|
|
# for uuid in uuids:
|
|
# part = []
|
|
# part.append("/dev/" + os.readlink("/dev/disk/by-uuid/" + uuid)[6:])
|
|
# part.append(uuid)
|
|
# yield part
|
|
|
|
def gather(self):
|
|
"""gather the actual information"""
|
|
|
|
self.partitions = [] #information about all partitions (list of lists)
|
|
uuids = os.listdir("/dev/disk/by-uuid") #folder contains symlinks to the actual disk devices
|
|
for uuid in uuids: #write /dev/names as keys and uuids as values in the dictionary
|
|
#part = []
|
|
part = {}
|
|
#part.append("/dev/" + os.readlink("/dev/disk/by-uuid/" + uuid)[6:])
|
|
#part.append(uuid)
|
|
part["device"] = str("/dev/" + os.readlink("/dev/disk/by-uuid/" + uuid)[6:])
|
|
part["uuid"] = uuid
|
|
|
|
#for part in self.get_part():
|
|
|
|
#getting mountpoint for partition from mtab
|
|
mtab = open("/etc/mtab", "r")
|
|
#for line in
|
|
for line in mtab:
|
|
if part["device"] in line:
|
|
#part.append(line.split()[1])
|
|
#part.append(line.split()[2])
|
|
part["mountpoint"] = line.split()[1]
|
|
part["filesystem"] = line.split()[2]
|
|
#getting block infos
|
|
if os.path.ismount(part["mountpoint"]):
|
|
fs = os.statvfs(part["mountpoint"])
|
|
#part.append(fs.f_bsize) #blocksize
|
|
#part.append(fs.f_blocks) #total blocks
|
|
#part.append(fs.f_bavail) #free blocks
|
|
part["blocksize"] = fs.f_bsize #blocksize
|
|
part["total blocks"] = fs.f_blocks #total blocks
|
|
part["free blocks"] = fs.f_bavail #free blocks
|
|
|
|
break
|
|
|
|
mtab.close()
|
|
|
|
#adding partition to list
|
|
self.partitions.append(part)
|
|
|
|
#output
|
|
if __name__ == "__main__":
|
|
diskmoninstance = diskmon()
|
|
diskmoninstance.gather()
|
|
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
|