Flipkart

Wednesday, 1 March 2017

SCRIPT: To Monitor File System Size in Linux

I thougt of introducing a Mid-Week Post going forward along with my Saturday weekly article.

Here comes a shell script which would help you to monitor size of critical file systems for the threshold you wish on Linux(Centos/Fedora/RHEL) servers.

Say the name of this script I am going to discuss is: /home/admin/FSalerts.sh

========SCRIPT STARTS=======

#!/bin/sh
df -kh | grep "/opt" |  awk '{print $5" "$6}'|cut -d " " -f1 --output-delimiter='  '|tail -n 1 | while read Value;
do
  echo $Value
  fssize=$(echo $Value | awk '{ print $1}' | cut -d'%' -f1)

 if [ $fssize -ge 65 ]; then
mail -s "CRITICAL FS ALERT: /opt is Almost out of disk space on Server `uname -n`" serversupport@domain.com
fi
done

=========SCRIPT ENDS========


  • In the script, I used #uname -n command to print the server name. It is much safer than hostname command :)
  • This script actually introduced/used CUT command along with AWK command.
  • This script basically sends you an email for /opt file system when it reaches 65% of its used space to the specified recipient email.


However, how do you make this script to run every 5 Minutes on Linux server? Just place the script in cron and schedule to run every 5 Minutes or the frequency you wish.

CRON ENTRY:

##This job runs every 5 minutes to check file system usage
*/5 * * * * /home/admin/FSalerts.sh

So, with the above entry, I have schedule a cron job which runs the FSalerts.sh script every 5 minutes and sends us the details.

Follow up points to experience more hands on with the above logic:
---------------------------------------------------------------------------------

1. Try to use SED instead of cut command and make the script work. I believe using SED, script looks much simpler
2. Try to implement the same script for all the File Systems in a single script so that a single report is received as and when a file system is breached its threshold
3. Try with different thresholds, I used 65% as the threshold here.

HAPPY LINUX LEARNING :)

Related posts from my blog:
Learning AWK and SED Tools for LINUX/UNIX

No comments:

Post a Comment