I need to write a code that will loop through the output of command "df -h" and check if it's 100% utilized and print that specific line in red and if it's 80% utilized, the line to be printed in Yellow.
Output should be similar to :
Filesystem Size Used Avail Use% Mounted on
***/dev/sda1 44G 44G 44G 100% /*** -> Should be in red.
udev 8.9G 112K 8.9G 1% /dev
tmpfs 8.9G 84K 8.9G 1% /dev/shm
/dev/sdb1 6.9G 4.5G 4.5G 80% /storage/log -> Should be in yellow.
/dev/sdb2 7.9G 147M 7.4G 2% /storage/ext
/dev/sdc1 25G 173M 24G 1% /storage/artifactory
/dev/sdd1 50G 5.8G 42G 13% /storage/db
The code I have written so far is simple, but I am not quite sure how to loop in the subprocess output and check the used % field.
def DiskSize():
import subprocess
dfh = subprocess.Popen(["df","-h"],stdout=subprocess.PIPE)
for line in iter(dfh.stdout.readline,''):
print line.rstrip()
Output I get, is the expected :
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 44G 44G 44G 100% /
tmpfs 8.9G 84K 8.9G 1% /dev/shm
/dev/sdb1 6.9G 4.5G 4.5G 80% /storage/log
/dev/sdb2 7.9G 147M 7.4G 2% /storage/ext
/dev/sdc1 25G 173M 24G 1% /storage/artifactory
/dev/sdd1 50G 5.8G 42G 13% /storage/db
Thanks for your help.