script to grep outofmemory message in logs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to grep outofmemory message in logs
# 1  
Old 12-06-2010
script to grep outofmemory message in logs

I have prepare script to grep for outofmemory messages in the logs. I need help in modifying script. I have implemented small logic. The outofmemory messages form six logs will store in variables.
Code:
 var1=`grep -i outofmemory $tomcat1logs  | sed -n '$p'| sed -n -e "s/.*\(outofmemory\).*/\1/p"`  
 var2=`grep -i outofmemory $tomcat2logs | sed -n '$p'| sed -n -e "s/.*\(outofmemory\).*/\1/p"`  
 var3=`grep -i outofmemory $tomcat3logs | sed -n '$p'| sed -n -e "s/.*\(outofmemory\).*/\1/p"`  
 var4=`grep -i outofmemory $tomcat4logs | sed -n '$p'| sed -n -e "s/.*\(outofmemory\).*/\1/p"`  
 var5=`grep -i outofmemory $tomcat5logs | sed -n '$p'| sed -n -e "s/.*\(outofmemory\).*/\1/p"`  
 var6=`grep -i outofmemory $tomcat6logs | sed -n '$p'| sed -n -e "s/.*\(outofmemory\).*/\1/p"`

My requirement is if there is an outofmmeory message in any of the varibale from above six. It should send the mail saying "there is an out outofmemory in tomcat1,tomcat2,tomcat3,tomcat4,tomcat5 or tomcat6 if it founds outofmemory invar1,var2,var3,var4,var5 or var6 respectively. Please help me to design the script for the above logic.Thanks in advance.

Last edited by jim mcnamara; 12-06-2010 at 08:26 AM.. Reason: code tags please
# 2  
Old 12-06-2010
Assuming the actual name of the log file is tomcatnlogs, where n is a number:
Code:
var=$(grep -l 'outofmemory' tomcat*logs)
if [ ! -z "$var" ] ; then
  echo "outofmemory error in file(s) $var" | mailx -s 'outofmemory error'  me@myaddress.com
fi

# 3  
Old 12-06-2010
Hello Jim,

Thanks for the response. My requirement is the script should check all variables i.e var1,var2,var3,var4,var5 and var6. If var1 contains "outofmemory" message then it should send message there is an outofmemory error on tomcat1 same for other variables like var2,var3,var4,var5,var6. If var2 contains outofmemory then it should send there is an outofmemory for tomcat2. Please guide me to implement this logic.

Also can you please explain me how can we use below command to grep for outofmemory message.Please let me know what should we keep in place of *(tomcat*logs).

var=$(grep -l 'outofmemory' tomcat*logs)
if [ ! -z "$var" ] ; then
echo "outofmemory error in file(s) $var" | mailx -s 'outofmemory error' me@myaddress.com
fi
# 4  
Old 12-06-2010
I understand what your code is trying to do.

What I am trying to do is give you the output you need, without the really complex code you wrote. So,

1. please give us some examples of the /path/to/tomcatlogs
2. please give us EXACTLY what you want in the mail message

If I undertstand correctly you do not need six very complex & hard to maintain lines of code that produce variables. Unless this is school work, which it does not appear to be. Plus several parts of the code will not work as posted, IMO. Example:
Code:
sed -n '$p'

If $p is a variable it has to be in double quotes:
Code:
sed -n "$p"

# 5  
Old 12-06-2010
Hello Jim,

I have six tomcat log files. The script should grep latest outofmemory message from the log files and send the mail alert with description "suppose if outofmmemory in tomcat1 server then it should send message 'there is outofmemory in tomcat1 server" and same for tomcat2,tomcat3,tomcat4,tomcat5 and tomcat6. kindly guide.

Path for tomcat log files.

tomcat1logs=/opt/local/tomcat1/logs/catalina.out

tomcat2ogs=/opt/local/tomcat2/logs/catalina.out

tomcat3logs=/opt/local/tomcat3/logs/catalina.out

tomcat4logs=/opt/local/tomcat4/logs/catalina.out

tomcat5logs=/opt/local/tomcat5/logs/catalina.out

tomcat6logs=/opt/local/tomcat6/logs/catalina.out
# 6  
Old 12-06-2010
Try this:
Code:
#!/bin/ksh
d=/opt/local/tomcat*
for fname in $d
do
   tom=$(basename $fname)
   f=${fname}/logs/catalina.out
   var=$(grep 'outofmemory' $f | tail -1)
   if [ ! -z "$var" ] ; then
      echo "$var" | mailx -s "error on $tom"  me@mycompany.com
   fi
done

This will only work if you log rotate the catalina.out files every day (or hour or whenever). Otherwise grep will keep finding the same errors over and over.
# 7  
Old 12-07-2010
Hello Jim,

Thanks for the response. The script is working fine for me after little modifcation.

h1=`hostname`

#d1=`date +%D::::%T`

d1=`date`

tomcat1logs=/opt/local/tomcat1

tomcat2logs=/opt/local/tomcat2

tomcat3logs=/opt/local/tomcat3

tomcat4logs=/opt/local/tomcat4

tomcat5logs=/opt/local/tomcat5

tomcat6logs=/opt/local/tomcat6


for fname in $tomcat1logs $tomcat2logs $tomcat3logs $tomcat4logs $tomcat5logs $tomcat6logs

do

tom=$(basename $fname)

f=${fname}/logs/catalina.out

var=`tail -10 $f | grep -i outofmemory | sed -n '$p' | sed -n -e "s/.*\(OutOfMemoryError\).*/\1/p"`

if [ ! -z "$var" ] ;

then

echo "$var error on $tom on server $h1 @ $d1" | mailx -s "$var error in $tom" $sms_list

else

echo "No OutofMemory on $tom on server $h1 @ $d1" >>/opt/app/mxora/home/mxora/outofmemory.txt

fi

done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If I ran perl script again,old logs should move with today date and new logs should generate.

Appreciate help for the below issue. Im using below code.....I dont want to attach the logs when I ran the perl twice...I just want to take backup with today date and generate new logs...What I need to do for the below scirpt.............. 1)if logs exist it should move the logs with extention... (1 Reply)
Discussion started by: Sanjeev G
1 Replies

2. Shell Programming and Scripting

Script to grep logs for Errors

Hi Guys, I want to write a script which can grep the logs (server.log) from a file for Error String and output to a other file. Problems: How to know about the errors only between the current restart and not in previous as server.log has earlier restarts also? thanks for the help! Much... (5 Replies)
Discussion started by: ankur328
5 Replies

3. Shell Programming and Scripting

Unable to get full message text from Windows Event Logs

Hi all, . I am developing a log monitoring solution in perl for Windows I am using the CPAN module Win32 ::EventLog (0.076) version for getting the events from windows. The problem which I am facing now is all the Windows 2008 machines are upgraded with Service pack2 from then I couldn’t able... (2 Replies)
Discussion started by: kar_333
2 Replies

4. Shell Programming and Scripting

shell script to grep 500 error messages from access logs

Hello Team, I need help to improve my script which is used to grep 500 error messages in the logs. I am using following logic in the script to grep 500 error messages in the logs. var1=`awk '$9 == "500"' access_log | tail -1` The above logic is not useful if logs are not getting... (1 Reply)
Discussion started by: coolguyamy
1 Replies

5. Shell Programming and Scripting

script to grep latest outofmemory string from the logs

I have requirement to prepare script which will grep for latest outofmemory message from the logs. I have used following command to grep the string from the logs,this script is not effective when logs are not getting updated as it will grep for old message. f=catalina.out var=`tail -10 $f |... (17 Replies)
Discussion started by: coolguyamy
17 Replies

6. Shell Programming and Scripting

Rotating logs in Perl without message loss

(I'm aware log rotation is a common subject, but I tried searching and couldn't find an answer) For some time now, I've been using the Logfile::Rotate module to rotate logs in a log-monitoring script. So far, I haven't experienced any problems, and it works great because I can use it in Linux... (1 Reply)
Discussion started by: w1r3d
1 Replies

7. Shell Programming and Scripting

Grep yesterday logs from weblogic logs

Hi, I am trying to write a script which would go search and get the info from the logs based on yesterday timestamp and write yesterday logs in new file. The log file format is as follows: """"""""""""""""""""""""""... (3 Replies)
Discussion started by: harish.parker
3 Replies

8. Shell Programming and Scripting

Grep logs on the fly

Hi, We use an application that is dumping logs to a file on disk. However, this is dumping very verbosely and there is no method of turning down the logging level. We need to remove certain contents from these before they are commited to disk. Has anybody got any ideas how I can do this... (3 Replies)
Discussion started by: harperonline
3 Replies

9. UNIX for Advanced & Expert Users

ppp errror message in logs

I am getting this message in the log file. Apr 29 15:32:02 router ppp: Warning: Label COPYRIGHT rejected -direct connection: Configuration label not found This repeats every so often, the link is up however...Any ideas why i am getting this. Its freebsd 6.1 and pppoE. Frank (1 Reply)
Discussion started by: frankkahle
1 Replies

10. UNIX for Dummies Questions & Answers

Message Logs Error

I'm about 5 months new on an 5 year old unix system. If anyone can help me identify what causing the below errors i'd really appreciate it! unix: WARNING: /pci@1f,0/pci@1,1/ide@3/dad@1,0 (dad1): Uncorrectable data Error: Block 57e10 Unix: WARNING: /pci@1f,0/pci@1,1/ide@3/dad@1,0 (dad1):... (1 Reply)
Discussion started by: ByasB
1 Replies
Login or Register to Ask a Question