Find error/down/critical (specific) keyword from "specific log file", without searching whole file for keyword.

Problem this snippet solves:

Sometimes, we are searching for a single keyword in log file like, /var/log/ltm or gtm which is very difficult when the number of lines are large and reading logs line by line is not feasible. In such situation, this code will help a lot and the result will be fast and 100% accurate.

How to use this snippet:

At line number 11, give the path of the file from which you want a keyword to be filtered. It is starting from "cat". In the same line, after "egrep -i" add the desired keyword with | symbol. That's it. Your code is ready. If really some critical or positive result will be there it will be shown in RED color. You can change (reduce or increase) the number in last third line.

Code :

#The file name is "check_down_error.sh" which is located in the /opt
#!/bin/bash
clear
_mmddyytt=$(date '+%d_%b_%Y_%a_%R')
echo
echo
echo $_mmddyytt
echo
latest="down_error_events_$_mmddyytt.txt"
#Add the filters you want to search in the log file.
cat /var/log/ltm | egrep -i 'down|DOWN|err|crit|error' > /opt/$latest
if [ ! -s $latest ]
then
echo -e "\033[7mNo error or Down Message has been found!!\033[0m"
echo
echo
else
echo -e "\033[7m\033[31mPlease check some ERROR or DOWN Message is there!!\033[0m\033[0m"
echo "Please press any key to See Error logs"
echo
read
tail -n 30 /opt/$latest
fi
rm -f /opt/$latest
Published Oct 24, 2017
Version 1.0

Was this article helpful?

No CommentsBe the first to comment