09 . Prometheus監控tomcat+jvm

men發表於2020-07-01

List

  CentOS7.3
  prometheus-2.2.1.linux-amd64.tar.gz
  redis_exporter-v0.30.0.linux-amd64.tar.gz
節點名 IP 軟體版本 硬體 網路 說明
Prometheus 192.168.43.144 list 裡面都有 2C4G Nat,內網 測試環境
Tomcat 192.168.43.134 list 裡面都有 2C4G Nat,內網 測試環境

部署Prometheus

部署Promehteus請看我前面寫的文章

https://www.cnblogs.com/you-men/p/12839535.html

部署Tomcat

下載安裝tomcat
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.55/bin/apache-tomcat-8.5.55.tar.gz
tar xvf apache-tomcat-8.5.47.tar.gz -C /usr/local/
ln -s /usr/local/apache-tomcat-8.5.47/ /usr/local/tomcat
tail -1 /etc/profile
export CATALINA_HOME=/usr/local/tomcat

source /etc/profile
env |grep -i home
CATALINA_HOME=/usr/local/tomcat
HOME=/root

# 檢視熵池的大小: 
cat /proc/sys/kernel/random/entropy_avail 180
增加熵池大小,解決Tomcat在CentOS 7巨慢的問題

# 安裝rng服務,增大熵池
yum -y install rng-tools
systemctl start rngd && systemctl enable rngd
# 啟動服務訪問Tomcat頁面快一百倍..
啟動tomcat
/usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
ss -antp |grep java
LISTEN     0      100          *:8009                     *:*                   users:(("java",pid=857,fd=58))
LISTEN     0      100          *:8080                     *:*                   users:(("java",pid=857,fd=53))
LISTEN     0      1      127.0.0.1:8005                     *:*                   users:(("java",pid=857,fd=73))
tomcat三個埠
# 8005:    是tomcat本身的埠,本地管理埠號  
# 8080:    主埠號,是tomcat負責建立http連線的埠,通過瀏覽器訪問tomcat伺服器的web 應用時,使用此埠  
# 8009:    是tomcat負責和其他http伺服器建立連線的埠,ajp的編輯器 
tomcat啟動指令碼
vim /etc/init.d/tomcat
#!/bin/bash
#chkconfig: 2345 96 14
export JAVA_HOME=/usr/java/jdk1.8.0_121
export CATALINA_HOME=/usr/local/tomcat
exec $CATALINA_HOME/bin/catalina.sh $*

# $*將指令碼本身自帶的引數全部傳遞過來,就是catalina.sh --help看到的所有的引數start stop等

chmod +x /etc/init.d/tomcat
chkconfig --add tomcat
chkconfig tomcat on
service tomcat start
systemctl status tomcat
tail -f catalina.out 服務啟動不起來檢視這個日誌檔案

配置JMX Exporter

專案地址:

https://github.com/prometheus/jmx_exporter

# jar包地址:
https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.3.1/jmx_prometheus_javaagent-0.3.1.jar

mkdir -p /usr/local/jmx/
cp 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

收集tomcat資料

Jar包應用
java -javaagent:./jmx_prometheus_javaagent-0.3.0.jar=9151:config.yaml -jar yourJar.jar
War包應用

進入bin目錄$TOMCAT_HOME/bin,將jmx_exporter.jar包檔案和config.yaml檔案複製到這裡。然後修改裡面的一個catalina.sh的指令碼,找到JAVA_OPTS,加上以下配置(代理):

如果有多tomcat,建議將jmx_prometheus_javaagent和config.yaml檔案放到固定的目錄,$TOMCAT_HOME/bin/catalina.sh檔案中寫絕對路徑.

# 修改bin/catalina.sh 檔案
# 新增:
JAVA_OPTS="-javaagent:/usr/local/jmx/jmx_prometheus_javaagent-0.3.1.jar=39081:/usr/local/jmx/config.yaml"

# service tomcat restart

ss -atnlp |grep java
LISTEN     0      100          *:8080                     *:*                   users:(("java",pid=2110,fd=57))
LISTEN     0      1      127.0.0.1:8005                     *:*                   users:(("java",pid=2110,fd=76))
LISTEN     0      100          *:8009                     *:*                   users:(("java",pid=2110,fd=61))
LISTEN     0      3            *:39081                    *:*                   users:(("java",pid=2110,fd=14))

配置Prometheus

新增tomcat到prometheus
tail -3 /usr/local/prometheus/prometheus.yml 
  - job_name: 'tomcat'
    static_configs:
      - targets: ['192.168.43.226:39081']
重啟服務
systemctl restart prometheus

配置Grafana

尋找tomcat模板

grafana匯入模板
檢視tomcat儀表盤

相關文章