对于 Java 应用,可以借助于专门的jmx exporter方式来暴露相关的指标数据
1 准备java环境
#最新java8下载地址:
https://www.oracle.com/java/technologies/javase/javase8-archive-downloads.html
#安装java
tar xf jdk-8u121-linux-x64.tar.gz -C /usr/local
cd /usr/local/
ln -s jdk1.8.0_121 java
#配置java环境变量
echo 'export JAVA_HOME=/usr/local/java' >> /etc/profile.d/java.sh
echo 'export JRE_HOME=$JAVA_HOME/jre' >> /etc/profile.d/java.sh
echo 'export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar' >> /etc/profile.d/java.sh
echo 'export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH' >> /etc/profile.d/java.sh
source /etc/profile.d/java.sh
#检查效果
java -version
#获取软件tomcat
wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.56/bin/apache-tomcat-9.0.56.tar.gz
#解压软件
tar xf apache-tomcat-9.0.56.tar.gz -C /usr/local/
ln -s /usr/local/apache-tomcat-9.0.56 /usr/local/tomcat
#定制环境变量
echo 'PATH=$PATH:/usr/local/tomcat/bin' >> /etc/profile.d/tomcat.sh
source /etc/profile.d/tomcat.sh
#尝试启动tomcat
catalina.sh start
#检查效果
netstat -tnulp | grep java
tcp6 0 0 :::8080 :::* LISTEN 30079/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 30079/java
2 准备 jmx exporter
#获取软件
wget
https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.16.1/jmx_prometheus_javaagent-0.16.1.jar
wget
https://raw.githubusercontent.com/prometheus/jmx_exporter/master/example_configs/tomcat.yml
#转移文件
cp jmx_prometheus_javaagent-0.16.1.jar /usr/local/tomcat/bin/
cp tomcat.yml /usr/local/tomcat/bin/
#修改tomcat的启动脚本
vim catalina.sh
......
# OS specific support. $var _must_ be set to either true or false.
JAVA_OPTS="$JAVA_OPTS $JSSE_OPTS"
JAVA_OPTS="-javaagent:/usr/local/tomcat/bin/jmx_prometheus_javaagent-0.16.1.jar=2022:/usr/local/tomcat/bin/tomcat.yml"
#重启tomcat
catalina.sh stop
catalina.sh start
#查看效果
ss -tnulp | grep java
tcp LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
users:(("java",pid=59676,fd=66))
tcp LISTEN 0 50 *:2181 *:*
users:(("java",pid=928,fd=60))
tcp LISTEN 0 3 *:2022 *:*
users:(("java",pid=59676,fd=13))
tcp LISTEN 0 50 *:40651 *:*
users:(("java",pid=928,fd=45))
tcp LISTEN 0 50 *:8080 *:*
users:(("java",pid=928,fd=52))
tcp LISTEN 0 50 *:7000 *:*
users:(("java",pid=928,fd=47))
#浏览器访问:
http://192.168.255.120:2022/metrics
3 修改 Prometheus 配置
#修改prometheus的配置文件,让它自动过滤文件中的节点信息
vim /usr/local/prometheus/conf/prometheus.yml
...
- job_name: "mertics_from_tomcat"
static_configs:
- targets: ["192.168.255.120:2022"]
#重启服务
systemctl restart prometheus.service
z#稍等几秒钟,到浏览器中查看监控目标
4 Grafana 图形展示
导入grafana的镜像模板文件 https://grafana.com/grafana/dashboards/8704 ,查看效果
注意:默认job为java , 需要修改job名称为前面prometheus中配置的job名称 mertics_from_tomcat