Monitoring Tomcat Instance using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Monitoring Tomcat Instance using shell script
# 1  
Old 11-17-2010
Monitoring Tomcat Instance using shell script

Hello Forum,

I have prepared script to monitor the tomcat status. Following is the script which will monitor tomcat instance.I need little modifcation in the script. My script will grep for java,the output of grep command will analyze by if condition under for loop and will
send following echo message on the email. The echo message which will get send is as follows echo "Tomcat Application Server is down on marsvcp1" | mailx -s "tomcatserver of marsvcp1 is down" $email. I need modifcation here ,it sends very generalised message i.e tomcat is down. I would like it to send message which tomcat is going down whether it is tomcat1,2,3,4,5,6. kindly assist.
Code:
tomcat1=`ps -eaf | grep -i jdk160_05_1 | grep -v grep | wc -l`

tomcat2=`ps -eaf | grep -i jdk160_05_2 | grep -v grep | wc -l`

tomcat3=`ps -eaf | grep -i jdk160_05_3 | grep -v grep | wc -l`

tomcat4=`ps -eaf | grep -i jdk160_05_4 | grep -v grep | wc -l`

tomcat5=`ps -eaf | grep -i jdk160_05_5 | grep -v grep | wc -l`

tomcat6=`ps -eaf | grep -i jdk160_05_6 | grep -v grep | wc -l`

for tomcat in $tomcat1 $tomcat2 $tomcat3 $tomcat4 $tomcat5 $tomcat6

do

if [ $tomcat -eq 0 ]

then

echo "Tomcat Application Server is down on marsvcp1" | mailx -s "tomcatserver of marsvcp1 is down" $email

else

echo "Tomcat Application Server is running fine on marsvcp1 @ `date` ">>/root/home/root/tomcatstatus.txt

fi

done


Last edited by Scott; 11-26-2010 at 06:53 AM.. Reason: Code tags
# 2  
Old 11-17-2010
A for loop counter may be used for that.

Code:
 
typeset -i cnt
cnt=1
for tomcat in $tomcat1 $tomcat2 $tomcat3 $tomcat4 $tomcat5 $tomcat6
do
if [ $tomcat -eq 0 ]
then
echo "Tomcat$cnt Application Server is down on marsvcp1" | mailx -s "tomcatserver of marsvcp1 is down" $email
else
echo "Tomcat$cnt Application Server is running fine on marsvcp1 @ `date` ">>/root/home/root/tomcatstatus.txt
fi
cnt=$cnt+1
done

# 3  
Old 11-17-2010
Hello Anurag,

Thanks for the solution. It's working fine. When script is running from command line it is working fine. But when running from crontab it is logging below message in the logs.
Code:
Tomcat1+1 Application Server is running fine on marsvcp1 @ Wed Nov 17 14:30:01 MET 2010
Tomcat1+1+1 Application Server is running fine on marsvcp1 @ Wed Nov 17 14:30:01 MET 2010
Tomcat1+1+1+1 Application Server is running fine on marsvcp1 @ Wed Nov 17 14:30:01 MET 2010
Tomcat1+1+1+1+1 Application Server is running fine on marsvcp1 @ Wed Nov 17 14:30:01 MET 2010
Tomcat1+1+1+1+1+1 Application Server is running fine on marsvcp1 @ Wed Nov 17 14:30:01 MET 2010


Last edited by Scott; 11-26-2010 at 06:53 AM.. Reason: Code tags
# 4  
Old 11-17-2010
Looks like you are missing something.Can we see the modified code?
# 5  
Old 11-17-2010
Code:
typeset -i cnt

cnt=1

tomcat1=`ps -eaf | grep -i jdk160_05_1 | grep -v grep | wc -l`

tomcat2=`ps -eaf | grep -i jdk160_05_2 | grep -v grep | wc -l`

tomcat3=`ps -eaf | grep -i jdk160_05_3 | grep -v grep | wc -l`

tomcat4=`ps -eaf | grep -i jdk160_05_4 | grep -v grep | wc -l`

tomcat5=`ps -eaf | grep -i jdk160_05_5 | grep -v grep | wc -l`

tomcat6=`ps -eaf | grep -i jdk160_05_6 | grep -v grep | wc -l`

for tomcat in $tomcat1 $tomcat2 $tomcat3 $tomcat4 $tomcat5 $tomcat6

do

if [ $tomcat -eq 0 ]

then

echo "Application Server Tomcat$cnt is down on marsvcp1" | mailx -s "tomcatserver of marsvcp1 is down" $email

else

echo "Application Server Tomcat$cnt is running fine on marsvcp1 @ `date` ">>/root/home/root/tomcatstatus.txt

fi

cnt=$cnt+1

done

but it's runnign fine from command line and not working from crontab.

Last edited by Scott; 11-26-2010 at 06:54 AM.. Reason: Code tags
# 6  
Old 11-17-2010
Instead of following in for loop
Code:
cnt=$cnt+1

Pls try
Code:
cnt=`expr $cnt+1`

OR
Code:
 
cnt=$(expr $cnt+1)

# 7  
Old 11-17-2010
or use the POSIX builtin:
Code:
i=$(( i+1 ))

or use:
Code:
$(( i+=1 ))

Code:
cnt=0
..
echo "Application Server Tomcat$((cnt+=1)) is running fine on marsvcp1

---------- Post updated at 16:35 ---------- Previous update was at 16:13 ----------

Alternatively you could do something like this:
shell code:
  1. tomcat_running() {
  2.   ps -eaf | grep -qi [j]dk160_05_$1
  3. }
  4.  
  5. for i in 1 2 3 4 5 6
  6. do
  7.   if tomcat_running $i; then
  8.     echo "Application Server Tomcat$i is running fine on marsvcp1 @ $(date)" >>/root/home/root/tomcatstatus.txt
  9.   else
  10.     echo "Application Server Tomcat$i is down on marsvcp1" | mailx -s "tomcatserver $i of marsvcp1 is down" $email
  11.   fi
  12. done
Note: be careful if the number of instance becomes larger than 9 then the i=1 matches jdk160_05_1 as well as jdk160_05_10

or, more efficient:
shell code:
  1. PS_STRING=$(ps -eaf | grep -i [j]dk160_05_)
  2.  
  3. for i in 1 2 3 4 5 6
  4. do
  5.   case $PS_STRING in
  6.     *"jdk160_05_$i"*)
  7.         echo "Application Server Tomcat$i is running fine on marsvcp1 @ $(date)" >>/root/home/root/tomcatstatus.txt ;;
  8.     *)  
  9.         echo "Application Server Tomcat$i is down on marsvcp1" | mailx -s "tomcatserver $i of marsvcp1 is down" $email ;;
  10.   esac
  11. done

Last edited by Scrutinizer; 11-17-2010 at 03:47 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Server monitoring using shell script

I want to write a shell script which is used in cron job and it runs every 4 hours to check whether tomcat servers are running or not . If servers are not running , one email should be triggered like alert notification. if servers are Running then no need to print anything. This is what i want... (5 Replies)
Discussion started by: kk123
5 Replies

2. Shell Programming and Scripting

Shell Monitoring Script

Hi guys, I didn't understand this monitoring script request - I don't ask for the script result. If you understand the request, I'm just asking an explanation to simplify it for me. THE Script Request: Our organization keeps various files in directories structured as... (2 Replies)
Discussion started by: moshesa
2 Replies

3. Shell Programming and Scripting

Monitoring Tomcat Service with shell script

Hello Forum, I have prepared script to monitor the tomcat status. Following is the script which will monitor tomcat instance. I need little modifcation in the script. My script will grep for process, the output of grep command will analyze by if condition under for loop and will send... (2 Replies)
Discussion started by: ooilinlove
2 Replies

4. Shell Programming and Scripting

Tomcat Monitoring log file

Hi, I ned to monitor the tomcat log file called "catalina.out" for "OutOfMemory" ,"java.sql.SQLException" and "Error" error. the script should monitor this file (catalina.out) and send us the mail as soon as it finds the string "Out of memory" or "java.sql.SQLException" or "Error" in the... (2 Replies)
Discussion started by: mnmonu
2 Replies

5. Shell Programming and Scripting

Multiple instance in tomcat

I need to install a tomcat6 with multiple instances like instance1,instance2 and instance3 in a server. I came to know that for that we need to install tomcat6,apache2.0,mod_jk1.2 and jre with tools.jar installed.And we need to create multiple instances with same web.xml and difference... (0 Replies)
Discussion started by: tuxslonik
0 Replies

6. Shell Programming and Scripting

Shell script for process monitoring

Im having a bit of troble coming up with a script that does this monitors processes to see if they die, if they do die, restart the process and write out to a log file that the process was restarted with the new PID and the date and time the new process was launched. Any suggestions? (1 Reply)
Discussion started by: jspinal
1 Replies

7. Shell Programming and Scripting

Need help in writing a shell Script to connect to oracle instance

Please help me in writing a shell script which connects to a Oracle instance and tables to get the required information...:wall: (1 Reply)
Discussion started by: Dpu
1 Replies

8. UNIX for Advanced & Expert Users

Multiple Instance of Unix Shell Script

Hi All, I have a problem mentioned below. I have a script which performs line by line operations on several files. I have a temp_file storing the list of names of the file to be validated. Right not in while loop i validate these files one by one. Is there anyway that i can modify... (1 Reply)
Discussion started by: amitaryans
1 Replies

9. Shell Programming and Scripting

fetch hostname and instance name using shell script

Hi All, Requirement is to fetch hostname and instance name using shell script from all configuration files on a server R12 on IBM AIX... could anyone please share such an experience encountered before.Is there such a script available in this forum or any other site.. Thanks for your time!... (0 Replies)
Discussion started by: a1_win
0 Replies

10. Shell Programming and Scripting

Single Instance of a Shell Script

Hi, I have a shell script. What should I do to allow only single instance of the script to be run by a user at a time. That is, Only one user can run that script at a given point of time. Please help.. Its very important for my project Thanks in advance (4 Replies)
Discussion started by: pathanjalireddy
4 Replies
Login or Register to Ask a Question