farp332
farp332

Reputation: 746

How to add a health check on Tomcat?

I want to configure a simple health check on Tomcat for Linux, something like: http://localhost:8080/health

I have been checking Tomcat documentation, and I found this built-in server status site /manager/status/all, which indeed has a lot info, but requires that I login in there, so it's not useful if I do a "curl" as it won't retrieve any HTTP Response.

I expect to use something like this: http://localhost:8080/health, which returns a HTTP Response, i.e 200, so that I can add this url to my monitoring tool, in order to display the current status.

Upvotes: 4

Views: 20160

Answers (2)

Thomas Delrue
Thomas Delrue

Reputation: 151

Since Tomcat 9 there's a Valve you can use in your config exactly for this purpose: HealthCheckValve. See https://tomcat.apache.org/tomcat-9.0-doc/config/valve.html#Health_Check_Valve.

In the server.xml, something along the lines of...

...
<Host name="localhost" ...>
   ...
   <Valve className="org.apache.catalina.valves.HealthCheckValve" />
   ...
</Host>
...

... would create the /health endpoint that the OP was looking for.

Upvotes: 9

IMParasharG
IMParasharG

Reputation: 1895

In a simple way, you can create a directory like health inside webapps and create an index file named index.html inside webapps/health/ with the following contents.

<HTML>
  <HEAD>
    <TITLE>Tomcat status</TITLE>
  </HEAD>
  <BODY>
    <H1>Tomcat Running</H1>
   </BODY>
</HTML>

test with the following URL

http://localhost:8080/health

Upvotes: 5

Related Questions