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/df_check.py

99 lines
2.5 KiB
Python
Raw Normal View History

#!/usr/bin/python2
2012-05-14 00:13:50 +02:00
2012-05-15 22:53:08 +02:00
# import os for statvfs
2012-05-15 11:31:00 +02:00
import os
2012-05-15 23:25:27 +02:00
# 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")
2012-05-16 00:26:37 +02:00
mounts = fileparser("/proc/mounts") #bug.. what if uuids are in /proc/mounts
2012-05-15 11:12:02 +02:00
partitions = []
2012-05-15 17:22:05 +02:00
blocks = []
2012-05-15 19:03:27 +02:00
# getting all local partitions (regardless if mounted or not)
2012-05-15 22:53:08 +02:00
# and blocks from /proc/partitions and write it to lists
2012-05-15 17:22:05 +02:00
for line in parts.split("\n"):
2012-05-15 11:31:00 +02:00
if line and "name" not in line and "sr0" not in line:
2012-05-15 17:22:05 +02:00
if line.split()[2] != "1":
2012-05-15 19:03:27 +02:00
partitions.append("/dev/" + line.split()[3])
2012-05-15 17:22:05 +02:00
blocks.append(line.split()[2])
2012-05-15 11:12:02 +02:00
2012-05-15 19:03:27 +02:00
hold="nothing"
holdblocks = 0
blockcount = 0
counter = 0
2012-05-15 19:26:59 +02:00
partsnew = []
blocksnew = []
2012-05-15 22:53:08 +02:00
# detect disk like /dev/sda itself and
# filter it from partitions and blocks list
2012-05-15 19:03:27 +02:00
for part in partitions:
if hold in part:
blockcount = blockcount + int(blocks[counter])
2012-05-15 19:26:59 +02:00
partsnew.append(part)
blocksnew.append(blocks[counter])
2012-05-15 19:03:27 +02:00
else:
hold = part
holdblocks = blocks[counter]
blockcount = 0
counter += 1
2012-05-15 22:53:08 +02:00
# moving back to old var name
# too lazy to rename everything below
2012-05-15 19:26:59 +02:00
partitions = partsnew
blocks = blocksnew
2012-05-15 19:03:27 +02:00
2012-05-15 22:53:08 +02:00
# getting mounted partitions and their mountpoints
mountpoints = []
mountparts = []
2012-05-15 19:03:27 +02:00
for item in partitions:
for line in mounts.split("\n"):
if line and item in line:
mountpoints.append(line.split()[1])
mountparts.append(item)
break
2012-05-15 19:03:27 +02:00
2012-05-15 22:53:08 +02:00
# output for debugging
print "debug output:"
print "availible partitions: %s\nblocks: %s\nmounted partitions: %s\nmountpoints: %s" %(partitions, blocks, mountparts, mountpoints)
print "" #linebreak
2012-05-15 19:03:27 +02:00
2012-05-15 22:53:08 +02:00
2012-05-14 11:27:28 +02:00
# given paths(mountpoints)
#path=("/boot", "/", "/dev/sda6")
path = mountpoints
2012-05-14 00:13:50 +02:00
2012-05-15 11:31:00 +02:00
print "mountpoints\tfree megabytes"
print "------------------------------"
2012-05-14 11:27:28 +02:00
2012-05-15 22:53:08 +02:00
# getting usage information and print it to stdout
2012-05-15 11:31:00 +02:00
for mountpoint in path:
2012-05-14 11:27:28 +02:00
#check if given path is a mountpoint
2012-05-15 22:53:08 +02:00
if os.path.ismount(mountpoint):
2012-05-14 11:27:28 +02:00
#create statvfs object
2012-05-15 11:31:00 +02:00
fs = os.statvfs(mountpoint)
2012-05-14 11:27:28 +02:00
#calculate free megabytes
2012-05-15 11:31:00 +02:00
freembytes = fs.f_bavail * fs.f_bsize / 1024**2.0
2012-05-14 11:27:28 +02:00
#output
2012-05-15 11:31:00 +02:00
print "%s\t\t%.1f MB" %(mountpoint, freembytes)
2012-05-15 22:53:08 +02:00
else:
print "error: given path (%s) is not a mountpoint" %(mountpoint)
2012-05-14 00:13:50 +02:00
# end of file