did some code commenting in df_check
This commit is contained in:
parent
1fc755d8a3
commit
178b04fbf2
27
df_check.py
27
df_check.py
|
@ -1,9 +1,10 @@
|
||||||
#!/usr/bin/python2
|
#!/usr/bin/python2
|
||||||
|
|
||||||
|
# import os for statvfs
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
#fileoperations
|
#reading needed files
|
||||||
file = open("/proc/partitions", "r")
|
file = open("/proc/partitions", "r")
|
||||||
parts = file.read()
|
parts = file.read()
|
||||||
file.close()
|
file.close()
|
||||||
|
@ -11,11 +12,10 @@ file = open("/proc/mounts", "r")
|
||||||
mounts = file.read()
|
mounts = file.read()
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
#definitions
|
|
||||||
partitions = []
|
partitions = []
|
||||||
blocks = []
|
blocks = []
|
||||||
|
|
||||||
# getting all local partitions (regardless if mounted or not)
|
# 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"):
|
for line in parts.split("\n"):
|
||||||
if line and "name" not in line and "sr0" not in line:
|
if line and "name" not in line and "sr0" not in line:
|
||||||
if line.split()[2] != "1":
|
if line.split()[2] != "1":
|
||||||
|
@ -29,6 +29,8 @@ blockcount = 0
|
||||||
counter = 0
|
counter = 0
|
||||||
partsnew = []
|
partsnew = []
|
||||||
blocksnew = []
|
blocksnew = []
|
||||||
|
# detect disk like /dev/sda itself and
|
||||||
|
# filter it from partitions and blocks list
|
||||||
for part in partitions:
|
for part in partitions:
|
||||||
if hold in part:
|
if hold in part:
|
||||||
blockcount = blockcount + int(blocks[counter])
|
blockcount = blockcount + int(blocks[counter])
|
||||||
|
@ -40,11 +42,14 @@ for part in partitions:
|
||||||
blockcount = 0
|
blockcount = 0
|
||||||
counter += 1
|
counter += 1
|
||||||
|
|
||||||
|
|
||||||
|
# moving back to old var name
|
||||||
|
# too lazy to rename everything below
|
||||||
partitions = partsnew
|
partitions = partsnew
|
||||||
blocks = blocksnew
|
blocks = blocksnew
|
||||||
|
|
||||||
|
|
||||||
#getting mountpoints to devices
|
# getting mounted partitions and their mountpoints
|
||||||
mountpoints = []
|
mountpoints = []
|
||||||
for item in partitions:
|
for item in partitions:
|
||||||
for line in mounts.split("\n"):
|
for line in mounts.split("\n"):
|
||||||
|
@ -52,31 +57,29 @@ for item in partitions:
|
||||||
mountpoints.append(line.split()[1])
|
mountpoints.append(line.split()[1])
|
||||||
break
|
break
|
||||||
|
|
||||||
|
# output for debugging
|
||||||
|
|
||||||
print "partitions: %s\nblocks: %s\nmountpoints: %s" %(partitions, blocks, mountpoints)
|
print "partitions: %s\nblocks: %s\nmountpoints: %s" %(partitions, blocks, mountpoints)
|
||||||
|
|
||||||
print "\n"
|
|
||||||
# given paths(mountpoints)
|
# given paths(mountpoints)
|
||||||
#path=("/boot", "/", "/dev/sda6")
|
#path=("/boot", "/", "/dev/sda6")
|
||||||
path = mountpoints
|
path = mountpoints
|
||||||
|
|
||||||
#output head
|
|
||||||
print "mountpoints\tfree megabytes"
|
print "mountpoints\tfree megabytes"
|
||||||
print "------------------------------"
|
print "------------------------------"
|
||||||
|
|
||||||
#work path tuple
|
# getting usage information and print it to stdout
|
||||||
for mountpoint in path:
|
for mountpoint in path:
|
||||||
#check if given path is a mountpoint
|
#check if given path is a mountpoint
|
||||||
# if os.path.ismount(mountpoint):
|
if os.path.ismount(mountpoint):
|
||||||
#create statvfs object
|
#create statvfs object
|
||||||
fs = os.statvfs(mountpoint)
|
fs = os.statvfs(mountpoint)
|
||||||
#calculate free megabytes
|
#calculate free megabytes
|
||||||
freembytes = fs.f_bavail * fs.f_bsize / 1024**2.0
|
freembytes = fs.f_bavail * fs.f_bsize / 1024**2.0
|
||||||
#output
|
#output
|
||||||
print "%s\t\t%.1f MB" %(mountpoint, freembytes)
|
print "%s\t\t%.1f MB" %(mountpoint, freembytes)
|
||||||
# else:
|
else:
|
||||||
# print "error: given path is not a mountpoint"
|
print "error: given path (%s) is not a mountpoint" %(mountpoint)
|
||||||
|
|
||||||
|
|
||||||
# end of file
|
# end of file
|
||||||
|
|
Reference in New Issue