Prometheus监控tomcat

节点配置说明

node1节点安装Prometheus
node2节点安装tomcat


node1/192.168.1.111/2G/2U/80G
node2/192.168.1.112/2G/2U/80G

node1节点上配置

# 下载prometheus安装包
[root@node1 ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.30.0/prometheus-2.30.0.linux-amd64.tar.gz
[root@node1 ~]# tar -zxvf prometheus-2.30.0.linux-amd64.tar.gz -C /usr/local/
[root@node1 ~]# cp /usr/local/prometheus-2.30.0.linux-amd64/prometheus /usr/local/bin/


# 创建prometheus数据存放目录
[root@node1 ~]# mkdir -p /home/data/prometheus/


# 配置启动文件
[root@node1 ~]# vim /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus Server
After=network.target
Documentation=https://prometheus.io/docs/introduction/overview/

[Service]
Type=simple
WorkingDirectory=/home/data/prometheus/
ExecStart=/usr/local/bin/prometheus \
  --config.file=/usr/local/prometheus-2.30.0.linux-amd64/prometheus.yml \
  --web.read-timeout=5m \
  --web.max-connections=512 \
  --storage.tsdb.retention=15d \
  --storage.tsdb.path=/home/data/prometheus \
  --query.timeout=2m

Restart=on-failure

[Install]
WantedBy=multi-user.target




启动prometheus
[root@node1 ~]# systemctl daemon-reload
[root@node1 ~]# systemctl start prometheus && systemctl enable prometheus
[root@node1 ~]# systemctl status prometheus

node2节点配置

# 安装java环境
# https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html

[root@node2 ~]# tar -zxvf jdk-8u301-linux-x64.tar.gz -C /usr/local/
# 设置环境变量
[root@node2 ~]# vim /etc/profile
JAVA_HOME=/usr/local/jdk1.8.0_301       
JRE_HOME=/usr/local/jdk1.8.0_301/jre     
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME CLASS_PATH PATH

[root@node2 ~]# source /etc/profile
[root@node2 ~]# java -version

# 安装tomcat
[root@node2 ~]# wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.53/bin/apache-tomcat-9.0.53.tar.gz
[root@node2 ~]# tar -zxvf apache-tomcat-9.0.53.tar.gz -C /usr/local/
[root@node2 ~]# ln -s /usr/local/apache-tomcat-9.0.53/ /usr/local/tomcat9
# [root@node2 ~]# echo "export CATALINA_HOME=/usr/local/tomcat9">>/etc/profile
[root@node2 ~]# source /etc/profile
[root@node2 ~]# env|grep "_HOME"

=========== 待验证 =============
# 查看熵池的大小: 
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脚本
[root@node2 ~]# vim /etc/init.d/tomcat
#!/bin/bash
#chkconfig: 2345 96 14
export JAVA_HOME=/usr/local/jdk1.8.0_301
export CATALINA_HOME=/usr/local/tomcat9
exec $CATALINA_HOME/bin/catalina.sh $*


[root@node2 ~]# chmod +x /etc/init.d/tomcat
[root@node2 ~]# chkconfig --level 2345 tomcat on


[root@node2 ~]# ss -anpt|grep java
tomcat三个端口
# 8005:是tomcat本身的端口,本地管理端口号  
# 8080:主端口号,是tomcat负责建立http连接的端口,通过浏览器访问tomcat服务器的web 应用时,使用此端口  
# 8009:是tomcat负责和其他http服务器建立连接的端口,ajp的编辑器 


# 下载prometheus_javaagent的jar包
[root@node1 ~]# wget https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.16.1/jmx_prometheus_javaagent-0.16.1.jar -O /usr/local/tomcat9/bin/jmx_prometheus_javaagent-0.16.1.jar

# 下载tomcat.yml
# https://github.com/prometheus/jmx_exporter/blob/master/example_configs/tomcat.yml
[root@node1 ~]# cp tomcat.yaml  /usr/local/tomcat9/bin/config.yaml 
[root@node1 ~]# cat /usr/local/tomcat9/bin/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


[root@node2 ~]# cd /usr/local/tomcat9/bin/
[root@node2 bin]# vim catalina.sh 
JAVA_OPTS="$JAVA_OPTS $JSSE_OPTS"     # 在该行下面添加如下行
JAVA_OPTS="-javaagent:/usr/local/tomcat9/bin/jmx_prometheus_javaagent-0.16.1.jar=39081:/usr/local/tomcat9/bin/config.yaml"


[root@node2 ~]# service tomcat start         {start|stop}
[root@node2 ~]# systemctl status tomcat

[root@node2 ~]# ss -anpt|grep java
LISTEN     0   1     [::ffff:127.0.0.1]:8005  [::]:*    users:(("java",pid=12163,fd=71))
LISTEN     0   3     [::]:39081               [::]:*    users:(("java",pid=12163,fd=13))
LISTEN     0   100   [::]:8080                [::]:*    users:(("java",pid=12163,fd=65))


# 浏览器访问node节点
http://192.168.1.112:39081/

添加tomcat到prometheus

# 在node1节点上添加node2上tomcat到prometheus
[root@node1 ~]# vim /usr/local/prometheus-2.30.0.linux-amd64/prometheus.yml
<在最后追加上去以下内容>
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]          #往上是自带的
====================================================
  - job_name: 'node2-tomcat'                 #往下是新添加的
    static_configs:
      - targets: ['192.168.1.112:39081']

      
注:targets可为多个,如:
    static_configs:
      - targets:
        - localhost:9100
        - web1:9100
        - web2:9100



        
# node1节点上重启Prometheus
[root@node1 ~]# systemctl restart prometheus

浏览器访问node1节点的9090端口
http://192.168.1.111:9090/
up{instance="192.168.1.112:39081"}

Prometheus监控tomcat

node1节点安装grafana

[root@node1 ~]# wget https://dl.grafana.com/enterprise/release/grafana-enterprise-8.1.4-1.x86_64.rpm
[root@node1 ~]# yum -y install grafana-enterprise-8.1.4-1.x86_64.rpm
# 启动
[root@node1 ~]# systemctl start grafana-server && systemctl enable grafana-server
[root@node1 ~]# systemctl status grafana-server
浏览器访问node1节点IP+3000(默认端口),账密都是admin,第一次登录后需修改密码
上一篇:prometheus-黑盒探针blackbox_exporter


下一篇:Prometheus自定义远端存储实现