shell script to check whether logs have errors or not

Today, I will discuss about “how to check logs files at regular interval and identify if any errors are there or not and if yes, then send email to the concerned team members. As mentioned in my previous blogs , I will be sharing pseudo code only.

STEP1 : Create a parameterized file which has all the details of log file names. For example, consider filename is (logs.config) and below is the content of the file
USECASE_NAME|SERVER_LOGNAME_PATTERN
RECHARGE_USSD|rechargeUssdMode

STEP2 : Create the shell script in such a way that when you trigger the script, it will run for infinity and check the logs for every 5 minutes.
* To run the code in infinite loop, use while [ 1==1 ]
* To run the code every 5 min inside that infinite loop, Use MOD function and divide the sysdate /5 , if this gives zero, executed the remaining statements.

STEP3 : Now, if you see the logs.config file, first line is a header. So , we need to remove the line and move to another temp file using below command.
sed '1d' logs.config > logs_temp.txt

STEP4 : Read the logs_temp.txt file using below code (while loop).
while read -r line
do
echo $line
---Block of statements---
done < logs_temp.txt


STEP5 : In “Block of statements” section , first get the logname_pattern using awk command , then get the latest log file . See the below commands.
Get the logfilePattern
logfilepattern=echo $line | awk -F"|" '{print $2}'

Get the logfileName
logfileName=ls -lrt *$logfilepattern* | tail -1 |awk '{print $9}'

STEP6 : Here is the main line of code . Read the logfile and search for error keywords .
Read last 1000 lines of log file and search for Error related keywords
tail -1000 $logfileName | grep -iE error|outofmemory|overheadexception|exception | wc -l
If the count > 0 , that means application is not working as expected.
Remaining code is as per project requirements. So, design it accordingly.



Related posts