通過prometheus監(jiān)控Tomcat運(yùn)行狀態(tài)的操作流程
Tomcat安裝配置以及prometheus監(jiān)控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
當(dāng)然也可以一次性全部下載
git clone https://github.com/littlefun91/tomcat-exporter.git
3、將jar包和war包分別拷貝至對應(yīng)的目錄下
[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
查看運(yùn)行狀態(tài)
5、訪問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àn)證Tomcat是否已經(jīng)成功納入監(jiān)控中
可以看到Tomcat相關(guān)指標(biāo)
二. Tomcat常用指標(biāo)與示例
對于 Tomcat,我們通常會使用RED 方法,監(jiān)控請求速率(Rate)、請求失敗數(shù)(Errors)、請求延遲(Duration)來評估當(dāng)前服務(wù)的質(zhì)量。
1.Tomcat連接相關(guān)指標(biāo)
最大連接數(shù)可以修改
在/usr/share/tomcat/conf/server.xml中修改
重啟tomcat,再看下
案例:計(jì)算Tomcat的最大活動連接數(shù)的飽和度,計(jì)算公式:當(dāng)前活躍連接數(shù)/ 最大活躍連接數(shù) * 100
tomcat_connections_active_total / tomcat_connections_active_max * 100
2. Tomcat請求相關(guān)指標(biāo)
tomcat_requestprocessor_time_seconds Tomcat服務(wù)器處理請求所花費(fèi)的總時(shí)間(單位是秒)雖然顯示是gauge類型指標(biāo),但它的值卻是不斷累加的
案例1:計(jì)算Tomcat最近5分鐘,Http請求的錯(cuò)誤率占比Http請求總數(shù)的比率。計(jì)算公式: 每5分鐘的錯(cuò)誤請求數(shù) / 每5分鐘的總請求數(shù) * 100
rate(tomcat_requestprocessor_error_count_total[5m]) / rate(tomcat_requestprocessor_request_count_total[5m]) * 100
案例2:計(jì)算Tomcat最近5分鐘,處理每個(gè)請求所需要花費(fèi)的時(shí)間。
這個(gè)本來標(biāo)注的是gauge類型的數(shù)據(jù),但是其值是一直在增大的,因此可以使用irate()來求最近5分鐘內(nèi),每個(gè)請求所花費(fèi)的時(shí)間
irate(tomcat_requestprocessor_time_seconds[5m])
3.Tomcat會話相關(guān)指標(biāo)
案例1:計(jì)算Tomcat創(chuàng)建會話的速率。
sum (rate(tomcat_session_created_total[5m])) by (instance,job,host)
案例2:計(jì)算被拒絕創(chuàng)建的會話占總創(chuàng)建會話的比率。計(jì)算公式:( 拒絕的會話數(shù) / (創(chuàng)建的會話數(shù) + 拒絕會話數(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活躍的請求線程數(shù)占總請求的線程數(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ù)過高 expr: tomcat_connections_active_total / tomcat_connections_active_max* 100 >=80 for: 1m labels: severity: warning annotations: summary: "Tomcat服務(wù)器活躍連接數(shù)過高, 實(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ù)已超過最大活躍連接數(shù)的80%, 當(dāng)前值為 {{ $value }}% - alert: Tomcat處理請求超過5秒 expr: rate(tomcat_requestprocessor_time_seconds[5m]) > 5 for: 5m labels: severity: warning annotations: summary: "Tomcat處理請求時(shí)間過長, 實(shí)例:{{ $labels.instance }}" description: "Tomcat在過去5分鐘的平均處理請求時(shí)間超過5秒,當(dāng)前值 {{ $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會話拒絕率過高, 實(shí)例:{{ $labels.instance }}" description: "Tomcat在Host:{{ $labels.host }} 的 {{ $labels.context}} 的上下文中的會話拒絕率超過20%,當(dāng)前值 {{ $value }}。" - alert: "Tomcat線程使用率過高" expr: (tomcat_threads_active_total / tomcat_threads_max) * 100 > 80 for: 5m labels: severity: warning annotations: summary: "Tomcat線程使?率過?, 實(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ù)已超過最大活躍連接數(shù)的80%, 當(dāng)前值為 {{ $value }}%
2、驗(yàn)證告警規(guī)則
3、導(dǎo)入Tomcat圖形
1、下載對應(yīng)的dashboard
https://github.com/nlighten/tomcat_exporter/blob/master/dashboard/example.json
下載下來導(dǎo)入
以上就是通過prometheus監(jiān)控Tomcat運(yùn)行狀態(tài)的操作流程的詳細(xì)內(nèi)容,更多關(guān)于prometheus監(jiān)控Tomcat運(yùn)行狀態(tài)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Tomcat部署SpringBoot項(xiàng)目的war包的方法及詳細(xì)步驟
這篇文章主要介紹了Tomcat部署SpringBoot項(xiàng)目的war包的方法,本文分步驟結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07liunx下centos7中tomcat報(bào)錯(cuò)訪問域名超時(shí)的問題解決
本文主要介紹了liunx下centos7中tomcat報(bào)錯(cuò)訪問域名超時(shí),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02解決Tomcat啟動報(bào)錯(cuò):嚴(yán)重:Unable?to?process?Jar?entry?[META-INF/v
這篇文章主要介紹了解決Tomcat啟動報(bào)錯(cuò):嚴(yán)重:Unable?to?process?Jar?entry?[META-INF/versions/9/module-info.class]問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Tomcat啟動springboot項(xiàng)目war包報(bào)錯(cuò):啟動子級時(shí)出錯(cuò)的問題
這篇文章主要介紹了Tomcat啟動springboot項(xiàng)目war包報(bào)錯(cuò):啟動子級時(shí)出錯(cuò)的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08簡單記錄Cent OS服務(wù)器配置JDK+Tomcat+MySQL
這篇文章主要介紹了簡單記錄Cent OS服務(wù)器配置JDK+Tomcat+MySQL,需要的朋友可以參考下2014-12-12Linux部署Tomcat發(fā)布項(xiàng)目過程中各種問題及解決方法
這篇文章主要介紹了解決Linux部署Tomcat發(fā)布項(xiàng)目過程中各種問題,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04