Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i have requirement for log monitor shell script as per below
a)grep WARNING or EXCEPTION or SEVERE from log file
b) count the grep results
c)send an email notification for grep results
We will not do your homework or the job / work which has been assigned to you. We as a community are here to help / assist you but you have to let us know what you have tried so far and where you are stuck.
You have pasted the script but you did not mention which section of the script you are facing issue with? Whenever you are pasting a script it is good idea to use code tags as I have used below to keep indentation:
Code:
#!/bin/bash
EMAIL_SUBJECT="Found several log file messages matching Exception"
EMAIL_FROM_ADDRESS=abc.com
EMAIL_TO_ADDRESS=test.com
COMMAND_DIR=log location
StatusFile_DIR=/home/user001/
for ((i = 0 ; i < 10000 ; i++ ));
do
statusvar=`tail -f log location` >> /home/user001/Error_Temp_details.txt
echo $i;
mv /home/user001/Error_Temp_details.txt /home/user001/Error_details.txt
done ;
E_count = grep -c "ERROR" /home/user001/Error_details.txt
W_count = grep -c "WARNING" /home/user001/Error_details.txt
if ($E_count>=0)
then
{
echo -e "Dear Team,"
echo -e "\n Found" $E_count "\n Found several log file messages matching ERROR"
mailx -s "$EMAIL_SUBJECT" -r $EMAIL_FROM_ADDRESS $EMAIL_TO_ADDRESS
}
if ($W_count>=0)
then
{
echo -e "Dear Team,"
echo -e "\n Found" $W_count "\n Found several log file messages matching WARNING"
mailx -s "$EMAIL_SUBJECT" -r $EMAIL_FROM_ADDRESS $EMAIL_TO_ADDRESS
}
First of all: for variable assignments in bash you cannot have the = sign enclosed with white spaces. It should always be
Code:
var=value
not
Code:
var = value
Secondly, you cannot assign the output of a command to a variable like that. It should be something like
Code:
var=$(grep whatever)
Next, this if condition looks wrong to me:
Code:
if($E_count>=0)
I would do something like
Code:
if [ $E_count -ge 0 ];then
Same goes for the second if condition below.
The initial loop looks very strange to me, too, I don't understand what exactly you are trying to accomplish there, but whatever it is I am sure it is very inefficiently coded...
Maybe one additional consideration. Put together a grep command that only gives you output if something is wrong.
Put that into a cronjob and set up cron such that it can email you.
Whenever your grep produces output it will be emailed to you automatically.
That should get you going and is probably an order of magnitude simpler and cleaner than what you are looking at right now...
for ((i = 0 ; i < 10000 ; i++ ));
do
statusvar=`tail -f /DBA/capsqa2/JavaCAPS62/appserver/domains/domain1/logs/server.log` >> /VZ/EAIworkspace/narender/Error_Temp_details.txt
echo $i;
mv /VZ/EAIworkspace/narender/Error_Temp_details.txt /VZ/EAIworkspace/narender/Error_details.txt
done ;
This is totally wrong. You are using for to run a loop for 10000 time but it will get stuck with the first run, the reason being you are using tail -f which will continuously keep on updating /VZ/EAIworkspace/narender/Error_Temp_details.txt. Basically you are getting into infinite condition. Even where there is no update on /DBA/capsqa2/JavaCAPS62/appserver/domains/domain1/logs/server.log it will just sit there. You will never reach 10000 as i=0 will run indefinitely. Fix that first.
Pointer: If you want to record 10000 lines anyways you can go with tail -n 1 instead. Example:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.