Flipkart

Showing posts with label schedule. Show all posts
Showing posts with label schedule. Show all posts

Tuesday, 7 March 2017

How to Debug a Shell Script-(Linux&UNIX)

In this article, I will explain how to debug a shell script.
I am taking a script which was used in one of my previous articles that generates Alerts whenever /opt File System on your Red Hat/Centos Linux Server exceeds its usage 65%.

Below is the reference:
Shell SCRIPT to Monitor File System Usage/size in Linux

The script used to generate alerts is as shown below:
=====
#!/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 "Alert: /opt is Almost out of disk space on Server `uname -n`, Needs Immediate Attention"
serversupport@domain.com
fi
done
======


 Now, how can a system administrator debug this script to make sure if it works fine before he deploys this script on his production Linux Server?

SCRIPT DEBUGGING:

 1.To start debugging any script, take meaning full lines or parts of a line from the above script which works in a command line and try to get the output for each meaningful Linux command of the script.

Output from my system for the above script is as below:

[root@server2 ~]# df -kh | grep "/opt"
/dev/sda7             7.8G  1.4G  6.1G  68% /opt


 [root@server2 ~]# df -kh | grep "/opt" |  awk '{print $5" "$6}'
68% /opt


[root@server2 ~]# df -kh | grep "/opt" |  awk '{print $5" "$6}'|cut
 -d " " -f1 --output-delimiter='  '
68%


[root@server2 ~]# df -kh | grep "/opt" |  awk '{print $5" "$6}'|cut -d " " -f1 --output-delimiter='  '|tail -n 1
68%

  • Tail command helps us especially if there are more than one lines in the output to select the last line
[root@server2 ~]# echo 18%| awk '{ print $1}' | cut -d'%' -f1
68


Since 68 > 65, script is bound to generate an alert for us.

IMPORTANT NOTE:
  • It is always a better idea to redirect the output of this script to a log file which can be viewed later if you want to track what the script did when this job was actually run.
So a modified Crontab entry would look like below:

CRON ENTRY:

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

 You can open the log file /home/admin/FSalerts.log anytime you wish and see what was the activity done when the job was actually run.

This file is useful to troubleshoot the issues with the script or Cron when you did not receive any alert even if the FS usage exceeded your set threshold.

HAPPY LINUX LEARNING :)

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