socialnerds/hive
socialnerds
/
hive
Archived
1
0
Fork 0
This repository has been archived on 2020-12-06. You can view files and clone it, but cannot push or open issues or pull requests.
hive/disk_mon.py

64 lines
2.1 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 gather(self):
"""gather the actual information"""
self.partitions = [] #information about all partitions (list of lists)
self.part = [] #information about one partition
self.uuids = os.listdir("/dev/disk/by-uuid") #folder contains symlinks to the actual disk devices
for self.uuid in self.uuids: #write /dev/names as keys and uuids as values in the dictionary
self.part.append("/dev/" + os.readlink("/dev/disk/by-uuid/" + self.uuid)[6:])
self.part.append(self.uuid)
#getting mountpoint for partition from mtab
self.mtab = open("/etc/mtab", "r")
for self.line in self.mtab.readlines():
if self.part[0] in self.line:
self.part.append(self.line.split()[1])
self.part.append(self.line.split()[2])
#getting block infos
if os.path.ismount(self.part[2]):
self.fs = os.statvfs(self.part[2])
self.part.append(self.fs.f_bsize) #blocksize
self.part.append(self.fs.f_blocks) #total blocks
self.part.append(self.fs.f_bavail) #free blocks
try: self.part[2]
except IndexError:
for i in range(4):
self.part.append("not mounted")
self.mtab.close()
#adding partition to list
self.partitions.append(self.part)
#output
if __name__ == "__main__":
diskmoninstance = diskmon()
diskmoninstance.gather()
#print diskmoninstance.partitions
for partition in diskmoninstance.partitions:
print "device:\t\t" + str(partition[0]) + "\nuuid:\t\t" + str(partition[1]) + "\nmountpoint:\t" + str(partition[2]) + "\nfilesystem:\t" + str(partition[3]) + "\nblocksize:\t" + str(partition[4]) + "\ntotal blocks:\t" + str(partition[5]) + "\nfree blocks:\t" + str(partition[6]) + "\n"
#end of file