Prometheus监控JMX-Exporter监控java应用
本文了解如何使用 Prometheus 与 JMX Exporter 来监控 Java 应用
tomcat应用准备
[root@node1 ~]# mkdir -p /apps
[root@node1 ~]# cd /apps
[root@node1 apps]# wget https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.98/bin/apache-tomcat-8.5.98.tar.gz
[root@node1 apps]# tar xf apache-tomcat-8.5.98.tar.gz
[root@node1 apps]# mv apache-tomcat-8.5.98 tomcat
[root@node1 apps]# cd tomcat/bin/
[root@node1 bin]# ./catalina.sh start
[root@node1 bin]# ss -antlp|grep 8080
[root@node1 bin]# ./shutdown.sh
了解下jmx_prometheus_httpserver
# httpserver监控下载
[root@node1 bin]# wget https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_httpserver/0.18.0/jmx_prometheus_httpserver-0.18.0.jar[root@node1 ~]# git clone https://github.com/prometheus/jmx_exporter.git
[root@node1 ~]# cd jmx_exporter/example_configs
[root@node1 example_configs]# cp tomcat.yml /apps/tomcat/bin/config.yaml
[root@node1 example_configs]# vim /apps/tomcat/bin/config.yaml
---
hostPort: 192.168.1.11:8081 #第一行下添加,远程监控的地址加端口。在启动此jar包后会通过该端口与tomcat进行通信[root@node1 example_configs]# nohup java -jar /apps/tomcat/bin/jmx_prometheus_httpserver-0.18.0.jar 18080 /apps/tomcat/bin/config.yaml &
http://192.168.1.11:18080/ 看到部分监控数据,此时tomcat还未启动
[root@node1 tomcat]# vim /apps/tomcat/bin/catalina.sh #添加监控相关参数
...
157 # Copy CATALINA_BASE from CATALINA_HOME if not already set
158 [ -z "$CATALINA_BASE" ] && CATALINA_BASE="$CATALINA_HOME"
##添加以下内容,注意tomcat8添加“CATALINA_OPTS”内容是必须写在同一行
159 CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -DJava.rmi.server.hostname=192.168.1.11 -Dcom.sun.management.jmxremote.port=8081 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmx remote.authenticate=false"-Dcom.sun.management.jmxremote:开启jmx远程监控。-DJava.rmi.server.hostname:监控所在的服务器ip地址。(被监控的ip,一般为本机ip地址)-Dcom.sun.management.jmxremote.port:开启监控的端口号。(被监控的端口,也就是jmx exporter通过哪个端口与tomcat进行通信,采集tomcat的数据。端口设置根据上面config.yaml的设置进行修改)-Dcom.sun.management.jmxremote.ssl:是否开启ssl连接。-Dcom.sun.management.jmxremote.authenticate:监控是否开启用户和密码。http://192.168.1.11:18080/ #查看到监控的数据。数据不是特别多,暂不做研究
部署prometheus
[root@node1 tomcat]# cd /apps/
[root@node1 apps]# wget https://github.com/prometheus/prometheus/releases/download/v2.28.1/prometheus-2.28.1.linux-amd64.tar.gz[root@node1 apps]# tar xf prometheus-2.28.1.linux-amd64.tar.gz
[root@node1 apps]# mv prometheus-2.28.1.linux-amd64/ prometheus[root@node1 apps]# cat prometheus/prometheus.yml
#添加- job_name: 'tomcat'static_configs:- targets: ['localhost:18080'][root@node1 apps]# cd prometheus/
[root@node1 prometheus]# ./prometheus --config.file=./prometheus.ymlhttp://192.168.1.11:9090/targets #看到tomcat
http://192.168.1.11:9090/graph
#简单测试: 搜jmx_exporter_build_info
jmx_exporter_build_info{instance="localhost:18080", job="tomcat", name="jmx_prometheus_httpserver", version="0.18.0"}
部署grafana
[root@node1 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/Packages/grafana-8.1.2-1.x86_64.rpm
[root@node1 ~]# yum -y install ./grafana-8.1.2-1.x86_64.rpm #直接装不知道为什么没有service文件,暂时不研究[root@node1 ~]# /usr/sbin/grafana-server --config=/etc/grafana/grafana.ini --homepath=/usr/share/grafana# 添加数据源,导入数据
192.168.1.11:3000 #配置prometheus数据源
部署jmx_prometheus_javaagent
[root@node1 apps]# cat prometheus/prometheus.yml
#添加- job_name: 'tomcat-jmx'static_configs:- targets: ['localhost:8088'][root@node1 apps]# cd /apps/tomcat/bin
[root@node1 bin]# wget https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.18.0/jmx_prometheus_javaagent-0.18.0.jar
[root@node1 bin]# cat prometheus-jmx-config.yaml
ssl: false
lowercaseOutputName: false
lowercaseOutputLabelNames: false[root@node1 bin]# vim /apps/tomcat/bin/catalina.sh #添加监控相关参数
CATALINA_OPTS="$JPDA_OPTS $CATALINA_OPTS -javaagent:/apps/tomcat/bin/jmx_prometheus_javaagent-0.18.0.jar=8088:/apps/tomcat/bin/prometheus-jmx-config.yaml"http://192.168.1.11:8088/metrics # 验证数据采集# JVM dashboard
grafana 导入仪表 ID: 8563 #调整变量 job --》tomcat-jmx 跟上面的job_name一致参考文档:
https://cloud.tencent.com/document/product/457/48724
https://www.xjx100.cn/news/133960.html