LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-31-2015, 03:03 PM   #1
arunganga
LQ Newbie
 
Registered: Mar 2015
Posts: 5

Rep: Reputation: Disabled
log monitor shell script


Hi Guys,

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

please help me .

thanks,
Arun
 
Old 03-31-2015, 03:12 PM   #2
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Welcome to LQ!!!

As you are new to LQ go through LQ rules here

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.
 
1 members found this post helpful.
Old 03-31-2015, 03:22 PM   #3
arunganga
LQ Newbie
 
Registered: Mar 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
failed to open

Last edited by arunganga; 03-31-2015 at 11:57 PM.
 
Old 03-31-2015, 03:28 PM   #4
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
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
   }
 
Old 03-31-2015, 03:40 PM   #5
arunganga
LQ Newbie
 
Registered: Mar 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
failed to open

Last edited by arunganga; 03-31-2015 at 11:49 PM.
 
Old 03-31-2015, 03:59 PM   #6
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
I have put comment in your script:

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 #Doing grep this way will return non-numerical value. You should do as:
#E_count = grep -c "ERROR" /home/user001/Error_details.txt | wc -l # Like this
W_count = grep -c "WARNING" /home/user001/Error_details.txt #Same as above
#W_count = grep -c "WARNING" /home/user001/Error_details.txt | wc -l # This way it will return the number of ERROR it found.

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
   }
Give it a try and let us know how far it goes. Also run the script with -x to get the detailed output on what script is doing. Like sh -x <script.sh>
 
Old 03-31-2015, 04:50 PM   #7
arunganga
LQ Newbie
 
Registered: Mar 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
checking

Last edited by arunganga; 03-31-2015 at 11:52 PM.
 
Old 03-31-2015, 04:51 PM   #8
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
This here won't work:
Code:
E_count = grep -c "ERROR" /home/user001/Error_details.txt
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...
 
1 members found this post helpful.
Old 03-31-2015, 04:56 PM   #9
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
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...
 
Old 03-31-2015, 05:14 PM   #10
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@joe_2000: Good catch.

Yes it should be without spaces. Infact you could put it like this and it should work:

Code:
E_count=`grep "ERROR" /home/user001/Error_details.txt | wc -l`
Using back ticks.
 
Old 03-31-2015, 05:29 PM   #11
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Code:
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:
Code:
statusvar=`tail -n 1 /DBA/capsqa2/JavaCAPS62/appserver/domains/domain1/logs/server.log` >> /VZ/EAIworkspace/narender/Error_Temp_details.txt
I am not reading the whole code obviously this is something you have to debug. We guys can only give you pointers.
 
Old 03-31-2015, 11:54 PM   #12
arunganga
LQ Newbie
 
Registered: Mar 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
will check and provide the details
 
Old 04-01-2015, 10:32 AM   #13
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by T3RM1NVT0R View Post
Using back ticks.
Backticks work, too. I personally prefer the
Code:
var=$(grep whatever)
I already posted because I find it more readable. An additional bonus is that it can be nested
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to monitor the log file & kill the process if log is not updating. milu_k Programming 5 07-19-2012 08:23 AM
[SOLVED] Script question: Shell script in kde to log in on a server with ssh c4719929 Programming 18 01-31-2011 09:26 AM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 03:05 AM
Daemon shell or perl script to monitor a log file khriz Programming 4 01-07-2010 07:35 AM
shell script to monitor log file calipryss Linux - Newbie 14 08-05-2008 10:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration