1、使用 jmx_exporter 监控多个 tomcat 实例(shutdown 端口、Connector 端口、AJP 端口都不能相同)的状态,前提条件是同一台主机的每个 tomcat 实例要分配不同的 jmx 端口
2、下载 tomcat 安装包、 jmx_exporter 安装包和 jmx_exporter tomcat 配置文件
// 下载地址:https://tomcat.apache.org/download-70.cgi
# ls /usr/local/src/apache-tomcat-7.0.91.tar.gz
/usr/local/src/apache-tomcat-7.0.91.tar.gz
// 下载地址:https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.3.1/jmx_prometheus_javaagent-0.3.1.jar
# ls /usr/local/src/jmx_prometheus_javaagent-0.3.1.jar
/usr/local/src/jmx_prometheus_javaagent-0.3.1.jar
// jmx_exporter tomcat 配置文件下载地址:https://github.com/prometheus/jmx_exporter/tree/master/example_configs
# ls /usr/local/src/tomcat.yaml
/usr/local/src/tomcat.yaml
3、安装 tomcat
# cd /usr/local/src
# tar xf apache-tomcat-7.0.91.tar.gz -C /usr/local
# mv /usr/local/apache-tomcat-7.0.91 /usr/local/tomcat-1
4、tomcat server.xml 配置
<?xml version='1.0' encoding='utf-8'?>
<!-- 把此端口改成 -1 ,避免启动多个 tomcat 实例时端口冲突,但是停止实例只能通过 kill -9 "tomcat pid" 命令,无法使用 shutdown.sh -->
<Server port="-1" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<!-- 把此端口改成 25013,不同的实例监听在不同的端口,避免启动多个 tomcat 实例时端口冲突 -->
<Connector port="25013" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- 没有用到 AJP 连接方式,所以注释掉此行,不监听 8009 端口 -->
<!-- <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> -->
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="/oss" docBase="tx-web-oss" reloadable="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
5、配置 jmx exporter
# mkdir -p /usr/local/jmx/
# cp /usr/local/src/jmx_prometheus_javaagent-0.3.1.jar /usr/local/jmx/
// 下载地址:https://github.com/prometheus/jmx_exporter/blob/master/example_configs/tomcat.yml
# cp /usr/local/src/tomcat.yaml /usr/local/jmx/config.yaml
# cat /usr/local/jmx/config.yaml
---
lowercaseOutputLabelNames: true
lowercaseOutputName: true
rules:
- pattern: 'Catalina<type=GlobalRequestProcessor, name=\"(\w+-\w+)-(\d+)\"><>(\w+):'
name: tomcat_$3_total
labels:
port: "$2"
protocol: "$1"
help: Tomcat global $3
type: COUNTER
- pattern: 'Catalina<j2eeType=Servlet, WebModule=//([-a-zA-Z0-9+&@#/%?=~_|!:.,;]*[-a-zA-Z0-9+&@#/%=~_|]), name=([-a-zA-Z0-9+/$%~_-|!.]*), J2EEApplication=none, J2EEServer=none><>(requestCount|maxTime|processingTime|errorCount):'
name: tomcat_servlet_$3_total
labels:
module: "$1"
servlet: "$2"
help: Tomcat servlet $3 total
type: COUNTER
- pattern: 'Catalina<type=ThreadPool, name="(\w+-\w+)-(\d+)"><>(currentThreadCount|currentThreadsBusy|keepAliveCount|pollerThreadCount|connectionCount):'
name: tomcat_threadpool_$3
labels:
port: "$2"
protocol: "$1"
help: Tomcat threadpool $3
type: GAUGE
- pattern: 'Catalina<type=Manager, host=([-a-zA-Z0-9+&@#/%?=~_|!:.,;]*[-a-zA-Z0-9+&@#/%=~_|]), context=([-a-zA-Z0-9+/$%~_-|!.]*)><>(processingTime|sessionCounter|rejectedSessions|expiredSessions):'
name: tomcat_session_$3_total
labels:
context: "$2"
host: "$1"
help: Tomcat session $3 total
type: COUNTER
6、配置 tomcat setenv.sh,让 jmx_exporter 跟 tomcat 一起启动
// 指定 jmx_exporter 的运行端口为 30013,不同的实例要分配不同的端口
# cat /usr/local/tomcat-1/bin/setenv.sh
JAVA_OPTS="-javaagent:/usr/local/jmx/jmx_prometheus_javaagent-0.3.1.jar=30013:/usr/local/jmx/config.yaml"
7、prometheus 配置
// prometheus.yml 配置文件内容
- job_name: 'tomcat'
static_configs:
- targets: ['10.1.18.249:30013']
labels:
appname: 'tx-web-oss'
可以看到已经成功监控到 tomcat 实例
其他 tomcat 实例监控的配置都类似,如果多个 tomcat 实例在同一台机器上,注意要指定不同的 jmx 端口
8、grafana 配置
从 https://grafana.com/dashboards/8563 这里下载一个模板,导入 grafana 中,略作修改即可
添加应用名变量,方便查看
最后完成的 tomcat 实例监控图