✨✨ 欢迎大家来到景天科技苑✨✨
🎈🎈 养成好习惯,先赞后看哦~🎈🎈
🏆 作者简介:景天科技苑
🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。
🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi,flask等框架,云原生k8s,Prometheus监控,linux,shell脚本等实操经验,网站搭建,数据库等分享。所属的专栏:Prometheus监控系统零基础到进阶
景天的主页:景天科技苑
文章目录
Tomcat安装配置以及prometheus监控Tomcat
Tomcat本身无法对外提供Prometheus所兼容的Metrics,因此需要借助第三方exporter来提供:tomcat-exporter
https://github.com/nlighten/tomcat_exporter
一. 安装并配置tomcat
1、安装tomcat
yum install tomcat tomcat-webapps -y
2、然后下载依赖包
我们可以看到官方介绍,需要很多依赖包
wget https://search.maven.org/remotecontent?filepath=io/prometheus/simpleclient/0.12.0/simpleclient-0.12.0.jar
wget https://search.maven.org/remotecontent?filepath=io/prometheus/simpleclient_common/0.12.0/simpleclient_common-0.12.0.jar
wget https://search.maven.org/remotecontent?filepath=io/prometheus/simpleclient_hotspot/0.12.0/simpleclient_hotspot-0.12.0.jar
wget https://search.maven.org/remotecontent?filepath=io/prometheus/simpleclient_servlet/0.12.0/simpleclient_servlet-0.12.0.jar
wget https://search.maven.org/remotecontent?filepath=io/prometheus/simpleclient_servlet_common/0.12.0/simpleclient_servlet_common-0.12.0.jar
wget https://search.maven.org/remotecontent?filepath=nl/nlighten/tomcat_exporter_client/0.0.15/tomcat_exporter_client-0.0.15.jar
wget https://search.maven.org/remotecontent?filepath=nl/nlighten/tomcat_exporter_servlet/0.0.15/tomcat_exporter_servlet-0.0.15.war
当然也可以一次性全部下载
git clone https://github.com/littlefun91/tomcat-exporter.git
3、将jar包和war包分别拷贝至对应的目录下
[root@jingtian03 tomcat_exporter ]#cp *.jar /usr/share/tomcat/lib/
[root@jingtian03 tomcat_exporter ]#cp *.war /usr/share/tomcat/webapps/
启动后,metrics.war自动解压
4、启动Tomcat
systemctl start tomcat.service
查看运行状态
5、访问tomcat的metrics
http://10.10.0.32:8080/metrics/
6、配置prometheus
- 编辑prometheus配置文件,将Tomcat纳入监控
- job_name: "tomcat"
static_configs:
- targets: ["jingtian03:8080"]
- 重新加载prometheus配置文件
curl -X POST http://localhost:9090/-/reload
- 检查Prometheus的Status->Targets页面,验证Tomcat是否已经成功纳入监控中
可以看到Tomcat相关指标
二. Tomcat常用指标与示例
对于 Tomcat,我们通常会使用RED 方法,监控请求速率(Rate)、请求失败数(Errors)、请求延迟(Duration)来评估当前服务的质量。
1.Tomcat连接相关指标
最大连接数可以修改
在/usr/share/tomcat/conf/server.xml中修改
重启tomcat,再看下
案例:计算Tomcat的最大活动连接数的饱和度,计算公式:当前活跃连接数/ 最大活跃连接数 * 100
tomcat_connections_active_total / tomcat_connections_active_max * 100
2. Tomcat请求相关指标
tomcat_requestprocessor_time_seconds Tomcat服务器处理请求所花费的总时间(单位是秒)虽然显示是gauge类型指标,但它的值却是不断累加的
案例1:计算Tomcat最近5分钟,Http请求的错误率占比Http请求总数的比率。计算公式: 每5分钟的错误请求数 / 每5分钟的总请求数 * 100
rate(tomcat_requestprocessor_error_count_total[5m]) / rate(tomcat_requestprocessor_request_count_total[5m]) * 100
案例2:计算Tomcat最近5分钟,处理每个请求所需要花费的时间。
这个本来标注的是gauge类型的数据,但是其值是一直在增大的,因此可以使用irate()来求最近5分钟内,每个请求所花费的时间
irate(tomcat_requestprocessor_time_seconds[5m])
3.Tomcat会话相关指标
案例1:计算Tomcat创建会话的速率。
sum (rate(tomcat_session_created_total[5m])) by (instance,job,host)
案例2:计算被拒绝创建的会话占总创建会话的比率。计算公式:( 拒绝的会话数 / (创建的会话数 + 拒绝会话数) * 100 )
(tomcat_session_rejected_total / ( tomcat_session_created_total + tomcat_session_rejected_total )) * 100
4.Tomcat线程相关指标
允许的最大线程数,也是可以配置的
默认是200
案例1:计算Tomcat活跃的请求线程数占总请求的线程数比率。计算公式:当前活跃线程数 / 最大的线程数 * 100
(tomcat_threads_active_total / tomcat_threads_max) * 100
三. Tomcat告警规则文件
1、具体告警规则示例文件(可以根据公司实际情况进行调整)
cat /etc/prometheus/rules/tomcat_rules.yml
groups:
- name: tomcat告警规则
rules:
- alert: Tomcat活跃连接数过高
expr: tomcat_connections_active_total / tomcat_connections_active_max* 100 >=80
for: 1m
labels:
severity: warning
annotations:
summary: "Tomcat服务器活跃连接数过高, 实例:{{ $labels.instance }}"
description:
Tomcat最大连接数是 {{ printf `tomcat_connections_active_max{instance="%s",job="%s",name="%s"}` $labels.instance $labels.job $labels.name | query | first | value }} Tomcat目前连接数是 {{ printf `tomcat_connections_active_total{instance="%s",job="%s",name="%s"}` $labels.instance $labels.job $labels.name | query | first | value }} Tomcat活跃连接数已超过最大活跃连接数的80%, 当前值为 {{ $value }}%
- alert: Tomcat处理请求超过5秒
expr: rate(tomcat_requestprocessor_time_seconds[5m]) > 5
for: 5m
labels:
severity: warning
annotations:
summary: "Tomcat处理请求时间过长, 实例:{{ $labels.instance }}"
description: "Tomcat在过去5分钟的平均处理请求时间超过5秒,当前值 {{ $value}}。"
- alert: "Tomcat会话拒绝率超过20%"
expr: (tomcat_session_rejected_total / (tomcat_session_created_total +tomcat_session_rejected_total)) * 100 > 20
for: 5m
labels:
severity: critical
annotations:
summary: "Tomcat会话拒绝率过高, 实例:{{ $labels.instance }}"
description: "Tomcat在Host:{{ $labels.host }} 的 {{ $labels.context}} 的上下文中的会话拒绝率超过20%,当前值 {{ $value }}。"
- alert: "Tomcat线程使用率过高"
expr: (tomcat_threads_active_total / tomcat_threads_max) * 100 > 80
for: 5m
labels:
severity: warning
annotations:
summary: "Tomcat线程使⽤率过⾼, 实例:{{ $labels.instance }}"
description: Tmcat最大线程数是 {{ printf `tomcat_threads_max{instance="%s",job="%s",name="%s"}` $labels.instance $labels.job $labels.name | query | first| value }} Tomcat目前线程数是 {{ printf `tomcat_threads_active_total{instance="%s",job="%s",name="%s"}` $labels.instance $labels.job $labels.name | query | first | value }} Tomcat线程数已超过最大活跃连接数的80%, 当前值为 {{ $value }}%
2、验证告警规则
3、导入Tomcat图形
1、下载对应的dashboard
https://github.com/nlighten/tomcat_exporter/blob/master/dashboard/example.json
下载下来导入