Issues in monitoring apache tomcat server using Monit

monitmonitoringubuntu-12.04

I have installed Monit using the following command in Ubuntu Linux 12.04.1 server

sudo apt-get install monit

My main goal in using Monit is to restart my – MySQL and Apache tomcat services automatically when they are killed/stopped.

#Check and restart mysql service
check process mysqld with pidfile "/run/mysqld/mysqld.pid"
group database
start program = "/etc/init.d/mysql start"
stop program = "/etc/init.d/mysql stop"
if failed host 127.0.0.1 port 3306 protocol mysql then restart
if 5 restarts within 5 cycles then timeout

The above code works perfectly and Monit restarts MySQL service (when its killed/stopped).

I have actually configured tomcat as follows:
– Downloaded the tomcat zip and extracted to a particular location
– Configured this as my daemon service (and my tomcat server works properly).

As I don't have pid file for this tomcat setup, I am planning to check tomcat's status and start it using the following code:

#Check and restart tomcat service
check host tomcat with address 127.0.0.1
stop program = "/etc/init.d/tomcat stop"
start program = "/etc/init.d/tomcat start"
if failed port 8080 and protocol http
then start

However, I was unable to start tomcat successfully (when its killed). On investigating the Monit logs I found the following message:

[UTC Dec 24 12:08:23] error    : 'tomcat' failed, cannot open a connection to INET[127.0.0.1:8080] via TCP
[UTC Dec 24 12:08:23] info     : 'tomcat' start: /etc/init.d/tomcat

Through above logs, I found Monit is trying to start tomcat automatically for every particular time (when it finds tomcat service was killed). However, some inner conflicts stops this!

Other Information:

Tomcat server is properly running by using the port 8080:

netstat -an | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN     
tcp6       0      0 127.0.0.1:8080          127.0.0.1:53582         TIME_WAIT  


ps -ef|grep tomcat
root     16237     1  9 12:10 ?        00:00:37 /usr/lib/jvm/java-6-oracle/bin/java -Djava.util.logging.config.file=/usr/share/apache-tomcat-7.0.40/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1024m -Xmx1530m -XX:NewSize=512m -XX:MaxNewSize=1024m -XX:PermSize=512m -XX:MaxPermSize=1024m -Djava.endorsed.dirs=/usr/share/apache-tomcat-7.0.40/endorsed -classpath /usr/share/apache-tomcat-7.0.40/bin/bootstrap.jar:/usr/share/apache-tomcat-7.0.40/bin/tomcat-juli.jar -Dcatalina.base=/usr/share/apache-tomcat-7.0.40 -Dcatalina.home=/usr/share/apache-tomcat-7.0.40 -Djava.io.tmpdir=/usr/share/apache-tomcat-7.0.40/temp org.apache.catalina.startup.Bootstrap start
root     16851 16847  0 12:17 ?        00:00:00 sh -c su root -c ps\ \-ef\|grep\ tomcat 2>&1 2>/dev/null
root     16852 16851  0 12:17 ?        00:00:00 su root -c ps -ef|grep tomcat
root     16853 16852  0 12:17 ?        00:00:00 bash -c ps -ef|grep tomcat
root     16855 16853  0 12:17 ?        00:00:00 grep tomcat

Adding preferIPv4Addresses and preferIPv4Stack in catalina.sh file:

JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1024m -Xmx1530m
-XX:NewSize=512m -XX:MaxNewSize=1024m -XX:PermSize=512m -XX:MaxPermSize=1024m -Djava.net.preferIPv4Stack=true -Djava.net.preferIPv4Addresses=true"

Monit log:

[UTC Dec 24 12:48:39] error    : 'tomcat' failed protocol test [HTTP] at INET[127.0.0.1:8080] via TCP -- HTTP: Error receiving data -- Resource temporarily unavailable

Can anyone please guide me on this?

Thank You.

Best Answer

Seems that Tomcat binds itself to an ipv6 address for 127.0.0.1.

You can try to use preferIPv4Stack=true and preferIPv4Addresses=true directives in Java options to bind to an ipv4 address

Edit catalina.sh Tomcat startup script and add the following :

JAVA_OPTS="$JAVA_OPTS -Djava.net.preferIPv4Stack=true -Djava.net.preferIPv4Addresses=true"