詳解Prometheus 抓取 nginx 訪問日志的指標(biāo)
要通過 Prometheus 的 NGINX Exporter 來抓取 NGINX 中的日志信息,例如狀態(tài)碼為 4xx 或 5xx 的日志,需要結(jié)合以下幾種工具和方法:
1. NGINX Exporter 基礎(chǔ)功能
NGINX Exporter 是一個(gè) Prometheus Exporter,用于從 NGINX 的 /status
端點(diǎn)收集指標(biāo)數(shù)據(jù),但它本身并不直接處理日志文件。因此,若要監(jiān)控 NGINX 中狀態(tài)碼為 4xx 或 5xx 的日志信息,需要額外的步驟。
2. 使用 logstash 或 fluentd 收集 NGINX 日志
為了從 NGINX 的日志文件中提取特定信息,并將其轉(zhuǎn)換為 Prometheus 能夠抓取的指標(biāo),我們可以使用 logstash
或 fluentd
等日志收集工具。
使用 fluentd
示例 安裝 Fluentd 和 Prometheus 插件首先,需要安裝 Fluentd 和相關(guān)的插件,確保 Fluentd 可以從日志中提取數(shù)據(jù)并以 Prometheus 格式暴露出來:
gem install fluentd gem install fluent-plugin-prometheus
配置 Fluentd配置 Fluentd,解析 NGINX 日志并生成 Prometheus 格式的指標(biāo)。創(chuàng)建一個(gè) Fluentd 配置文件 fluent.conf
,內(nèi)容如下:
<source> @type tail path /var/log/nginx/access.log pos_file /var/log/td-agent/nginx-access.log.pos tag nginx.access <parse> @type nginx </parse> </source> <filter nginx.access> @type grep <regexp> key status pattern ^4|5[0-9][0-9]$ </regexp> </filter> <match nginx.access> @type prometheus <metric> name nginx_http_status_total type counter desc Nginx HTTP status code counter keys status <labels> status ${status} </labels> </metric> <metric> name nginx_http_request_bytes_total type counter desc Total bytes received by the Nginx server key bytes </metric> </match> <source> @type prometheus_monitor <labels> tag nginx.access </labels> </source> <source> @type prometheus_output_monitor <labels> tag nginx.access </labels> </source> <match **> @type stdout </match>
- **Source**:讀取 `/var/log/nginx/access.log` 文件,并解析日志。 - **Filter**:通過正則表達(dá)式過濾狀態(tài)碼為 4xx 或 5xx 的日志。 - **Match**:將解析后的日志信息轉(zhuǎn)化為 Prometheus 格式的指標(biāo)。 - **Prometheus 輸出**:啟動一個(gè) HTTP 端點(diǎn),供 Prometheus 抓取這些指標(biāo)。
啟動 Fluentd運(yùn)行 Fluentd,應(yīng)用配置文件:
fluentd -c /path/to/fluent.conf
Fluentd 會啟動一個(gè)監(jiān)聽端口,默認(rèn)是 http://localhost:24231/metrics
,以 Prometheus 格式暴露解析后的日志數(shù)據(jù)。
3. 配置 Prometheus 抓取 Fluentd 暴露的指標(biāo)
在 Prometheus 的配置文件 prometheus.yml
中,添加 Fluentd 作為新的抓取目標(biāo):
scrape_configs: - job_name: 'nginx_logs' static_configs: - targets: ['localhost:24231']
4. 使用 Grafana 可視化
現(xiàn)在 Prometheus 已經(jīng)可以抓取 NGINX 的 4xx 和 5xx 狀態(tài)碼日志信息,您可以在 Grafana 中創(chuàng)建儀表盤,展示這些信息的時(shí)間變化趨勢,分析 NGINX 服務(wù)的健康狀況。
總結(jié)
雖然 NGINX Exporter 本身無法直接抓取日志信息,但是結(jié)合 Fluentd 等日志收集工具,您可以輕松地將 NGINX 日志中的特定信息(如 4xx 和 5xx 狀態(tài)碼)轉(zhuǎn)化為 Prometheus 可抓取的指標(biāo),并最終在 Grafana 中進(jìn)行可視化。
到此這篇關(guān)于Prometheus 抓取 nginx 訪問日志的指標(biāo)的文章就介紹到這了,更多相關(guān)Prometheus nginx指標(biāo)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx之正向代理與反向代理進(jìn)階方式(支持https)
文章介紹了如何在Nginx中實(shí)現(xiàn)正向代理和反向代理對HTTP和HTTPS協(xié)議的支持,通過使用第三方模塊`ngx_http_proxy_connect_module`和Nginx內(nèi)置的`ngx_http_ssl_module`,可以實(shí)現(xiàn)Nginx對HTTPS的正向和反向代理2025-03-03nginx部署前端項(xiàng)目location時(shí)root和alias配置指南
nginx指定文件路徑有兩種方式root和alias,下面這篇文章主要給大家介紹了關(guān)于nginx部署前端項(xiàng)目location時(shí)root和alias配置的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01nginx配置訪問圖片路徑以及html靜態(tài)頁面的調(diào)取方法
這篇文章主要介紹了詳解nginx配置訪問圖片路徑以及html靜態(tài)頁面的調(diào)取方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。2016-12-12淺析nginx剛剛發(fā)布的JavaScript能力nginScript
Nginx [engine x]是全球最受歡迎,也是最優(yōu)秀的web服務(wù)器、反向代理服務(wù)器。nginScript是JavaScript/ECMAscript的子集,nginScript不是通過V8引擎實(shí)現(xiàn)的。本文給大家介紹nginx剛剛發(fā)布的JavaScript能力nginScript,感興趣的朋友跟著小編一起了解了解吧2015-09-09nginx中return和rewrite指令同時(shí)存在先執(zhí)行順序哪個(gè)
在Nginx配置中,當(dāng)return和rewrite指令同時(shí)存在,其執(zhí)行順序取決于配置的具體場景,這篇文章主要介紹了nginx中return和rewrite指令同時(shí)存在先執(zhí)行順序哪個(gè),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09