通過(guò)prometheus監(jiān)控Tomcat運(yùn)行狀態(tài)的操作流程
Tomcat安裝配置以及prometheus監(jiān)控Tomcat
Tomcat本身無(wú)法對(duì)外提供Prometheus所兼容的Metrics,因此需要借助第三方exporter來(lái)提供:tomcat-exporter
https://github.com/nlighten/tomcat_exporter

一. 安裝并配置tomcat
1、安裝tomcat
yum install tomcat tomcat-webapps -y

2、然后下載依賴(lài)包
我們可以看到官方介紹,需要很多依賴(lài)包

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
當(dāng)然也可以一次性全部下載
git clone https://github.com/littlefun91/tomcat-exporter.git


3、將jar包和war包分別拷貝至對(duì)應(yīng)的目錄下
[root@jingtian03 tomcat_exporter ]#cp *.jar /usr/share/tomcat/lib/ [root@jingtian03 tomcat_exporter ]#cp *.war /usr/share/tomcat/webapps/
啟動(dòng)后,metrics.war自動(dòng)解壓

4、啟動(dòng)Tomcat
systemctl start tomcat.service
查看運(yùn)行狀態(tài)

5、訪問(wèn)tomcat的metrics
http://10.10.0.32:8080/metrics/

6、配置prometheus
編輯prometheus配置文件,將Tomcat納入監(jiān)控
- job_name: "tomcat"
static_configs:
- targets: ["jingtian03:8080"]
重新加載prometheus配置文件
curl -X POST http://localhost:9090/-/reload
檢查Prometheus的Status->Targets頁(yè)面,驗(yàn)證Tomcat是否已經(jīng)成功納入監(jiān)控中

可以看到Tomcat相關(guān)指標(biāo)

二. Tomcat常用指標(biāo)與示例
對(duì)于 Tomcat,我們通常會(huì)使用RED 方法,監(jiān)控請(qǐng)求速率(Rate)、請(qǐng)求失敗數(shù)(Errors)、請(qǐng)求延遲(Duration)來(lái)評(píng)估當(dāng)前服務(wù)的質(zhì)量。
1.Tomcat連接相關(guān)指標(biāo)

最大連接數(shù)可以修改

在/usr/share/tomcat/conf/server.xml中修改


重啟tomcat,再看下

案例:計(jì)算Tomcat的最大活動(dòng)連接數(shù)的飽和度,計(jì)算公式:當(dāng)前活躍連接數(shù)/ 最大活躍連接數(shù) * 100
tomcat_connections_active_total / tomcat_connections_active_max * 100

2. Tomcat請(qǐng)求相關(guān)指標(biāo)


tomcat_requestprocessor_time_seconds Tomcat服務(wù)器處理請(qǐng)求所花費(fèi)的總時(shí)間(單位是秒)雖然顯示是gauge類(lèi)型指標(biāo),但它的值卻是不斷累加的

案例1:計(jì)算Tomcat最近5分鐘,Http請(qǐng)求的錯(cuò)誤率占比Http請(qǐng)求總數(shù)的比率。計(jì)算公式: 每5分鐘的錯(cuò)誤請(qǐng)求數(shù) / 每5分鐘的總請(qǐng)求數(shù) * 100
rate(tomcat_requestprocessor_error_count_total[5m]) / rate(tomcat_requestprocessor_request_count_total[5m]) * 100
案例2:計(jì)算Tomcat最近5分鐘,處理每個(gè)請(qǐng)求所需要花費(fèi)的時(shí)間。
這個(gè)本來(lái)標(biāo)注的是gauge類(lèi)型的數(shù)據(jù),但是其值是一直在增大的,因此可以使用irate()來(lái)求最近5分鐘內(nèi),每個(gè)請(qǐng)求所花費(fèi)的時(shí)間
irate(tomcat_requestprocessor_time_seconds[5m])

3.Tomcat會(huì)話相關(guān)指標(biāo)


案例1:計(jì)算Tomcat創(chuàng)建會(huì)話的速率。
sum (rate(tomcat_session_created_total[5m])) by (instance,job,host)
案例2:計(jì)算被拒絕創(chuàng)建的會(huì)話占總創(chuàng)建會(huì)話的比率。計(jì)算公式:( 拒絕的會(huì)話數(shù) / (創(chuàng)建的會(huì)話數(shù) + 拒絕會(huì)話數(shù)) * 100 )
(tomcat_session_rejected_total / ( tomcat_session_created_total + tomcat_session_rejected_total )) * 100
4.Tomcat線程相關(guān)指標(biāo)

允許的最大線程數(shù),也是可以配置的

默認(rèn)是200

案例1:計(jì)算Tomcat活躍的請(qǐng)求線程數(shù)占總請(qǐng)求的線程數(shù)比率。計(jì)算公式:當(dāng)前活躍線程數(shù) / 最大的線程數(shù) * 100
(tomcat_threads_active_total / tomcat_threads_max) * 100
三. Tomcat告警規(guī)則文件
1、具體告警規(guī)則示例文件(可以根據(jù)公司實(shí)際情況進(jìn)行調(diào)整)
cat /etc/prometheus/rules/tomcat_rules.yml
groups:
- name: tomcat告警規(guī)則
rules:
- alert: Tomcat活躍連接數(shù)過(guò)高
expr: tomcat_connections_active_total / tomcat_connections_active_max* 100 >=80
for: 1m
labels:
severity: warning
annotations:
summary: "Tomcat服務(wù)器活躍連接數(shù)過(guò)高, 實(shí)例:{{ $labels.instance }}"
description:
Tomcat最大連接數(shù)是 {{ printf `tomcat_connections_active_max{instance="%s",job="%s",name="%s"}` $labels.instance $labels.job $labels.name | query | first | value }} Tomcat目前連接數(shù)是 {{ printf `tomcat_connections_active_total{instance="%s",job="%s",name="%s"}` $labels.instance $labels.job $labels.name | query | first | value }} Tomcat活躍連接數(shù)已超過(guò)最大活躍連接數(shù)的80%, 當(dāng)前值為 {{ $value }}%
- alert: Tomcat處理請(qǐng)求超過(guò)5秒
expr: rate(tomcat_requestprocessor_time_seconds[5m]) > 5
for: 5m
labels:
severity: warning
annotations:
summary: "Tomcat處理請(qǐng)求時(shí)間過(guò)長(zhǎng), 實(shí)例:{{ $labels.instance }}"
description: "Tomcat在過(guò)去5分鐘的平均處理請(qǐng)求時(shí)間超過(guò)5秒,當(dāng)前值 {{ $value}}。"
- alert: "Tomcat會(huì)話拒絕率超過(guò)20%"
expr: (tomcat_session_rejected_total / (tomcat_session_created_total +tomcat_session_rejected_total)) * 100 > 20
for: 5m
labels:
severity: critical
annotations:
summary: "Tomcat會(huì)話拒絕率過(guò)高, 實(shí)例:{{ $labels.instance }}"
description: "Tomcat在Host:{{ $labels.host }} 的 {{ $labels.context}} 的上下文中的會(huì)話拒絕率超過(guò)20%,當(dāng)前值 {{ $value }}。"
- alert: "Tomcat線程使用率過(guò)高"
expr: (tomcat_threads_active_total / tomcat_threads_max) * 100 > 80
for: 5m
labels:
severity: warning
annotations:
summary: "Tomcat線程使?率過(guò)?, 實(shí)例:{{ $labels.instance }}"
description: Tmcat最大線程數(shù)是 {{ printf `tomcat_threads_max{instance="%s",job="%s",name="%s"}` $labels.instance $labels.job $labels.name | query | first| value }} Tomcat目前線程數(shù)是 {{ printf `tomcat_threads_active_total{instance="%s",job="%s",name="%s"}` $labels.instance $labels.job $labels.name | query | first | value }} Tomcat線程數(shù)已超過(guò)最大活躍連接數(shù)的80%, 當(dāng)前值為 {{ $value }}%
2、驗(yàn)證告警規(guī)則

3、導(dǎo)入Tomcat圖形
1、下載對(duì)應(yīng)的dashboard
https://github.com/nlighten/tomcat_exporter/blob/master/dashboard/example.json
下載下來(lái)導(dǎo)入



以上就是通過(guò)prometheus監(jiān)控Tomcat運(yùn)行狀態(tài)的操作流程的詳細(xì)內(nèi)容,更多關(guān)于prometheus監(jiān)控Tomcat運(yùn)行狀態(tài)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Tomcat配置虛擬主機(jī)的實(shí)現(xiàn)示例
在Tomcat中配置虛擬主機(jī)允許你通過(guò)不同的域名或IP地址訪問(wèn)同一臺(tái)服務(wù)器上的不同Web應(yīng)用,這通常通過(guò)編輯Tomcat的配置文件server.xml來(lái)實(shí)現(xiàn),本文就來(lái)介紹一下,感興趣的可以了解一下2024-11-11
tomcat服務(wù)安裝步驟及詳細(xì)配置實(shí)戰(zhàn)教程
Tomcat是由Apache開(kāi)發(fā)的一個(gè)開(kāi)源Java WEB應(yīng)用服務(wù)器,下面這篇文章主要給大家介紹了關(guān)于tomcat服務(wù)安裝步驟及詳細(xì)配置實(shí)戰(zhàn)教程,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
Tomcat 請(qǐng)求資源[/XXX/]不可用問(wèn)題的解決方法
Tomcat 請(qǐng)求的資源[/XXX/]不可用問(wèn)題,通過(guò)正確路徑訪問(wèn)(運(yùn)行代碼時(shí)候自動(dòng)打開(kāi)瀏覽器訪問(wèn)的路徑),和錯(cuò)誤路徑訪問(wèn),它的表現(xiàn)是不一樣的,本文就來(lái)介紹一下解決方法2023-11-11
Tomcat中實(shí)現(xiàn)Session小結(jié)
本篇文章主要介紹了Tomcat中實(shí)現(xiàn)Session小結(jié) ,Session的主要目的就是為了彌補(bǔ)Http的無(wú)狀態(tài)特性。簡(jiǎn)單的說(shuō),就是服務(wù)器可以利用session存儲(chǔ)客戶(hù)端在同一個(gè)會(huì)話期間的一些操作記錄。2017-01-01
解析Tomcat的啟動(dòng)腳本--catalina.bat
本文主要對(duì)Tomcat的三個(gè)最重要的啟動(dòng)腳本之一--catalina.bat腳本做了詳細(xì)分析,具有很好的參考價(jià)值,需要的朋友可以看下2016-12-12

