Add diskuage script made in python

This commit is contained in:
Stefan Weijers 2021-07-25 12:50:42 +02:00
parent 78c8523511
commit fdc7764050
1 changed files with 47 additions and 0 deletions

47
diskusage.py Normal file
View File

@ -0,0 +1,47 @@
import os
barWidth = 50
class bcolors:
EMPTY = '\033[34m' #BLUE
MIDWAY = '\033[33m' #YELLOW
FULL = '\033[31m' #RED
RESET = '\033[0m' #RESET COLOR
UNUSED = '\033[90m' #UNUSED
# Function to generate usage bar based on percentage of storage in use
def generateBar(percentage):
no_used = int((percentage/100)*barWidth)
bar = '['
if no_used != 0 and no_used != barWidth:
bar = bar + bcolors.EMPTY
elif no_used == barWidth:
bar = bar + bcolors.FULL
bar = bar + '='*no_used
bar = bar + bcolors.UNUSED + '='*(barWidth - no_used)
return bar + bcolors.RESET + ']'
mountpoints = ['/', '/dev']
cmd = 'df -h '
# Generate command to be executed
for mount in mountpoints:
cmd = cmd + mount + ' '
du = os.popen(cmd).read() # Execute command on system
# Get percentages from each mounting point in order
percinfo = du.split()[11::6]
percs = []
for perc in percinfo:
percs.append(int(perc.split('%')[0]))
# Get full information from df and split it for fancy output
full = du.split('\n')
full = full[:-1] # Get rid of trailing empty string
# Output disk usage along with bar to indicate usage
print(full[0])
for i in range(1, len(full)):
print(full[i])
print(generateBar(percs[i-1]))