DEV Community

Hisyam Johan
Hisyam Johan

Posted on • Edited on

Monitor Tomcat 9 with Prometheus

Bismillah..

We will configure tomcat server for integration with Prometheus.

Prerequisites:-

  • Tomcat 9
  • Docker
  • Centos 7

Setting up Tomcat to expose metrics
Firstly, we need to download jmx_prometheus_javaagent as jar and place it in /tmp. In this steps, we will be using version 0.16.1.

Second, we need to create file in $CATALINA_BASE/bin named setenv.sh and add this line into the file:-

CATALINA_OPTS="-javaagent:/tmp/jmx_prometheus_javaagent-0.16.1.jar=8088:/tmp/config.yml"
Enter fullscreen mode Exit fullscreen mode

Third, we need to create file name config.yml in /tmp and add this line to the file:-

startDelaySeconds: 0
ssl: false
lowercaseOutputName: false
lowercaseOutputLabelNames: false
Enter fullscreen mode Exit fullscreen mode

Then, we need to restart Tomcat server and the metrics should be available at http://HOST_IP:8088/metrics

you can verify it by running ps -ef | grep tomcat

Optional
If not available, this could be you are not open port 8088. To do that, you may follow this step to open it.

firewall-cmd --zone=public --add-port=8088/tcp --permanent
firewall-cmd --reload
iptables-save | grep 8088
Enter fullscreen mode Exit fullscreen mode

Configure Prometheus

  1. Create file promethues.yml in /home/$USER. And add this line
global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: 'codelab-monitor'

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'tomcat'
    static_configs:
      - targets: ['HOST_IP:8088']

Enter fullscreen mode Exit fullscreen mode
  1. Run Prometheus as Docker container
 docker run -d --name=prometheus -p 9090:9090 -v /home/$USER/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus --config.                                                                                                             file=/etc/prometheus/prometheus.yml
Enter fullscreen mode Exit fullscreen mode
  1. Go to http://PROMETHEUS_IP:9090 and go to tab Status->Target. You will see that Tomcat server status.

ref

If you're using Tomcat server as container you may execute this

docker run -d --name=myapp-p 8080:8080 -p 8088:8088 -v ~/<FILE_NAME>.war:/usr/local/tomcat/webapps/<FILE_NAME>.war -v ~/tomcat-metrics-config/setenv.sh:/usr/local/tomcat/bin/setenv.sh -v ~/tomcat-metrics-config:/tmp -v ~/logs/<FILE_NAME>.log:/usr/local/tomcat/bin/<FILE_NAME>.log tomcat:9-jdk8-corretto
# volume persistence for WAR & LOG file are optional
# make sure you placed the files in the correct location 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)