Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question_1: Checking Server File System Using bash shell, grep, sed, and awk, wr

ID: 3594174 • Letter: Q

Question

Question_1: Checking Server File System

Using bash shell, grep, sed, and awk, write a script to check the root file system usage. If the usage is greater than 95%, then your script must automatically send an email to you (assuming you are the system administrator) with the following message;

“The root file system is => 95%. Please take immediate action to reduce the disk space usage. This is an automatic message sent by the system”

At present, the server root file system is about 9% usage. You can modify your script for testing purposes, say; if it is =>8% then send email to yourself.

You can use the server mailx utility to send email to yourself. The following is an example of how to email to login id sam with subject header:

mailx –s “Sending you a passwd file” sam@.edu < /etc/shells

Note: The mailx utility in server sycamore.rice.iit.edu may take about 1 to 4 minutes for your email to be processed. So when you test your script, you may have to wait for a while to receive your email.

Explanation / Answer

EMAIL = "sam@.edu" #mention emails id here comma separated here if more than one

LIMIT=8 # for testing taking 8% please use 95 here after successfully tested

#The below command df -h is used to print the space consumed and available output.

#this we are filtering out using grep in this we discard all tmpfs output so we only left with our overlay and drive output of space.

#then again filtering this using awk to print 5th row of the output which contains all used % and 1st our drive names after that.

#then reading the above output using while  

df -h | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;

do

#here we are cutting out the integer part for comparison using the cut command

used=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )

#now comparing our used with the limit which is 8 for testing you can change it to 95. if it is greater than LIMIT we are sending mail usinng the message.

if [ $used -ge $LIMIT ]; then

echo "The root file system is => 95%. Please take immediate action to reduce the disk space usage. This is an automatic message sent by the system" | mailx -s "Alert: Almost out of disk space $used" $EMAIL < /etc/shells

fi

done

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote