Prometheus+altermanager搭配釘釘報警

一、Prometheus介紹

Prometheus是一個開源系統監控和警報工具包,最初在 SoundCloud構建。自 2012 年成立以來,許多公司和組織都采用了 Prometheus,該項目擁有非常活躍的開發者和用戶社區。它現在是一個獨立的開源項目,獨立于任何公司維護。為了強調這一點,并明確項目的治理結構,Prometheus 于 2016 年加入 云原生計算基金會,成為繼Kubernetes之后的第二個托管項目。

Prometheus 將其指標收集并存儲為時間序列數據,即指標信息與記錄時的時間戳以及稱為標簽的可選鍵值對一起存儲。

有關 Prometheus 的更詳細概述,請參閱 媒體部分鏈接的資源。

特點

普羅米修斯的主要特點是:

  • 具有由度量名稱和鍵/值對標識的時間序列數據的多維數據模型
  • PromQL,一種 利用這種維度的靈活查詢語言
  • 不依賴分布式存儲;單個服務器節點是自治的
  • 時間序列收集通過 HTTP 上的拉模型進行
  • 通過中間網關支持推送時間序列
  • 通過服務發現或靜態配置發現目標
  • 多種圖形模式和儀表板支持
架構圖

二、搭建Prometheus監控體系

我這里提前準備了三臺虛擬機 (maste、node1、node2)

1、Prometheus 服務端 (master機)

2、expot 客戶端(node1、node2)

3、granfan 可視化展示Prometheus數據

1、搭建Prometheus服務端

1.1、下載并解壓縮

#下載
wget https://github.com/prometheus/prometheus/releases/download/v3.4.0-rc.0/prometheus-3.4.0-rc.0.linux-amd64.tar.gz
#解壓到 /usr/local
tar -xvf  prometheus-2.54.0-rc.1.linux-amd64.tar.gz -C /data/
#查看版本號
cd /data/prometheus-2.54.0-rc.1.linux-amd64/
./prometheus  --version
prometheus, version 2.54.0-rc.1 (branch: HEAD, revision: 6d7ed08850849c2adcedb63cddeb61fc4f6a425e)build user:       root@64a0056e2b2abuild date:       20240806-06:01:39go version:       go1.22.5platform:         linux/amd64tags:             netgo,builtinassets,stringlabels

1.2、Prometheus.yml配置解釋

這里我們可以看到有四個模塊
1、global 全局配置
2、
#1、global  
global:scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.# scrape_timeout is set to the global default (10s).
2、

1.3、設置systemctl啟動

vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=https://prometheus.io[Service]    
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml   # 啟動地址[Install]
WantedBy=multi-user.target

1.4、啟動Prometheus

#重載systemctl 配置
systemctl daemon-reload
#設置開機自啟
systemctl enable prometheus 
#啟動服務
systemctl start prometheus

1.5、查看是否啟動

systemctl status Prometheus

1.6、瀏覽器訪問

#開設9090端口號
iptables -I INPUT -p tcp -dport 9090 -j ACCEPT
#瀏覽器訪問
http://172.16.254.190:9090

2、搭建客戶端export (添加主句監控)

2.1、下載、解壓、并啟動

切換到node1上

#切換到node1主機上
ssh node1
#下載
wget https://github.com/prometheus/node_exporter/releases/download/v-1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
#解壓縮
tar -xfv node_exporter-1.8.2.linux-amd64.tar.gz  -C /data

2.2、設置systemctl 啟動

 vim  /usr/lib/systemd/system/node_exporter.service 
[Unit]
Description=Prometheus-Server
After=network.target[Service]
ExecStart=/data/node_exporter-1.8.2.linux-amd64/node_exporter  --web.listen-address=:9500
User=root[Install]
WantedBy=multi-user.target/data/node_exporter-1.8.2.linux-amd64/node_exporter --web.listen-address=:9500

2.3、啟動服務

#重載systemctl 配置
systemctl daemon-reload
#設置開機自啟
systemctl enable node_exporter
#啟動服務
systemctl start node_exporter

2.4、驗證是否啟動采集到數據

curl http://172.16.254.191:9100/metrics# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0

node2主機上也是按照此步驟操作

2.5、修改Prometheus配置文件

  - job_name: "prometheus"# metrics_path defaults to '/metrics'# scheme defaults to 'http'.static_configs:- targets: ["localhost:9090"]- job_name: "node1"static_configs:- targets: ["172.16.254.191:9500"]labels:service: ops- job_name: "node2"static_configs:- targets: ["172.16.254.192:9500"]labels:service: ops#- targets 后服務器ip+端口號

2.6、重新啟動Prometheus

systemctl restart prometheus

2.7、瀏覽器訪問Prometheus

http:192.168.19.135/9090

我們這里可以看到node1和node2主機已經監控到了

2.8、配置Prometheus規則

在該目錄下添加rule規則

先打開Prometheus配置文件,并刪除rule注釋

 vim /data/prometheus-3.1.0.linux-amd64/prometheus.yml
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:- "first_rules.yml"
#   - "second_rules.yml"

vim /data/prometheus-3.1.0.linux-amd64/first_rules.yml 
groups:
- name: noderules:- alert: InstanceDownexpr: up{job="node1"} == 0for: 15slabels:severity: "critical"server: "ops"annotations:description: '{{ $labels.instance }} of job {{ $labels.job }} of service {{ $labels.service }} has been down for more than 30 seconds.'summary: Instance {{ $labels.instance }} down

2.9、重啟Prometheus服務

并點擊status>rule? ?下方表示rule設置成功

3、granfan搭建

3.1、下載granfan并啟動

yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-12.0.2+security~01-1.x86_64.rpmsystemctl start grafana

3.2、瀏覽器訪問

#開設3000端口
iptables -I INPUT -p tcp --dport 3000 -j ACCEPT
瀏覽器訪問
http://172.16.254.190:3000/

初始用戶和密碼都為admin? admin? 第一次會提示修改密碼

3.2.1、添加Add data source

代表成功

3.2.2、導入儀表盤

添加模版id 11074

4、添加alertmanager報警

4.1下載altermanager并解壓縮

在master主機上添加alertmanager

wget https://github.com/prometheus/alertmanager/releases/download/v0.28.0/alertmanager-0.28.1.linux-amd64.tar.gz
tar -xf alertmanager-0.28.0.linux-amd64.tar.gz -C /data

4.2、設置systemctl啟動服務

vim  /usr/lib/systemd/system/alertmanager.service
[unit]
Description=Alertmanager-Server
After=network.target[Service]
ExecStart=/data/alertmanager-0.28.0.linux-amd64/alertmanager --config.file=/data/alertmanager-0.28.0.linux-amd64/alertmanager.yml
User=root[Install]
WantedBy=multi-user.target

4.3、啟動服務

#重新加載配置文件
systemctl daemon-reload
#設置開機自啟
systemctl enable alertmanager.service
systemctl start alertmanager
#查看進程是否啟動
ps -ef|grep alertmanager
root      1122     1  0 00:40 ?        00:00:00 /data/alertmanager-0.28.0.linux-amd64/alertmanager --config.file=/data/alertmanager-0.28.0.linux-amd64/alertmanager.yml
root      1145 27020  0 00:40 pts/3    00:00:00 grep --color=auto alertmanager

4.4、可以用webhook.site測試一下是否能觸發報警

webhook.site是一個用于測試,轉換和自動化web請求和電子郵件平臺。他提供多種功能,能創建自定義的webhook URL,接受和查看HTTP請求的詳細信息,以及執行自定義的操作。

1、打開鏈接:(1) Webhook.site - Test, transform and automate Web requests and emails

2、打開alertmanager配置文件輸入上面復制的url
vim /data/alertmanager-0.28.0.linux-amd64/alertmanager.yml
route:group_by: ['alertname']group_wait: 30sgroup_interval: 5mrepeat_interval: 1hreceiver: 'web.hook'
receivers:- name: 'web.hook'webhook_configs:- url: 'https://webhook.site/a9cf0e68-6c0d-49ab-b553-cb9706a0ea70'
inhibit_rules:- source_match:severity: 'critical'target_match:severity: 'warning'equal: ['alertname', 'dev', 'instance']
3、這時我們登錄node1主機關閉node_export 就會自動觸發報警
#登錄node1
ssh node1
#關閉node節點
systemctl stop node_exporter

代表Prometheus+alertmanager成功搭建并建立連接。這時我們就可以使用釘釘告警通道觸發報警了。

5、釘釘實現報警

5.1、需要下載?Webhook-dingtalk安裝并啟動

wegt https://github.com/timonwong/prometheus-webhook-dingtalk/releases/download/v2.1.0/prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz
#解壓
tar -xf prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz -C /datacd /data/prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz

5.2、添加systemctl管理

vim /usr/lib/systemd/system/webhook.service
[Unit]
Description=Prometheus-Server[Service]
ExecStart=/data/prometheus-webhook-dingtalk-2.1.0.linux-amd64/prometheus-webhook-dingtalk --config.file=/data/prometheus-webhook-dingtalk-2.1.0.linux-amd64/config.example.yml[Install]
WantedBy=multi-user.target

5.3、設置開機自啟并啟動服務

systemctl daemon-reload
systemctl enable webhook
systemctl start webhook

5.4、電腦下載釘釘軟件

步驟一:創建群聊添加機器人

步驟二、

步驟三、設置自定義

步驟四、

步驟五、設置加簽 并復制

步驟六、

這時我們就看到釘釘添加機器人成功了

5.5、在webhook配置文件當中添加webhook1

vim /data/prometheus-webhook-dingtalk-2.1.0.linux-amd64/config.example.yml
## Request timeout
# timeout: 5s## Uncomment following line in order to write template from scratch (be careful!)
#no_builtin_template: true## Customizable templates path
#templates:
#  - contrib/templates/legacy/template.tmpl## Targets, previously was known as "profiles"
targets:webhook1:
#步驟六的機器人地址url: https://oapi.dingtalk.com/robot/send?access_token=62638c23119337210244fa5c33b5d0dd5a5df105b3d802538df09e33c45333d5# secret for signature
步驟五的安全設置的加密secret: SEC35f4c12c1997a68be9230c1cdc07bb6100fc8f456a685ba29b0039adaf445734webhook2:url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxwebhook_legacy:url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx# Customize template contentmessage:# Use legacy templatetitle: '{{ template "legacy.title" . }}'text: '{{ template "legacy.content" . }}'webhook_mention_all:url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxmention:all: truewebhook_mention_users:url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxmention:mobiles: ['156xxxx8827', '189xxxx8325']

4.5、配置釘釘模版

vim /data/prometheus-webhook-dingtalk-2.1.0.linux-amd64/template.tmpl/template.tmpl{{ define "__subject" }}
[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}]
{{ end }}{{ define "__alert_list" }}{{ range . }}
---
{{ if .Labels.owner }}@{{ .Labels.owner }}{{ end }}**告警主題**: {{ .Annotations.summary }}**告警類型**: {{ .Labels.alertname }}**告警級別**: {{ .Labels.severity }} **告警主機**: {{ .Labels.instance }} **告警信息**: {{ index .Annotations "description" }}**告警時間**: {{ dateInZone "2006.01.02 15:04:05" (.StartsAt) "Asia/Shanghai" }}
{{ end }}{{ end }}{{ define "__resolved_list" }}{{ range . }}
---
{{ if .Labels.owner }}@{{ .Labels.owner }}{{ end }}**告警主題**: {{ .Annotations.summary }}**告警類型**: {{ .Labels.alertname }} **告警級別**: {{ .Labels.severity }}**告警主機**: {{ .Labels.instance }}**告警信息**: {{ index .Annotations "description" }}**告警時間**: {{ dateInZone "2006.01.02 15:04:05" (.StartsAt) "Asia/Shanghai" }}**恢復時間**: {{ dateInZone "2006.01.02 15:04:05" (.EndsAt) "Asia/Shanghai" }}
{{ end }}{{ end }}{{ define "default.title" }}
{{ template "__subject" . }}
{{ end }}{{ define "default.content" }}
{{ if gt (len .Alerts.Firing) 0 }}
**====偵測到{{ .Alerts.Firing | len  }}個故障====**
{{ template "__alert_list" .Alerts.Firing }}
---
{{ end }}{{ if gt (len .Alerts.Resolved) 0 }}
**====恢復{{ .Alerts.Resolved | len  }}個故障====**
{{ template "__resolved_list" .Alerts.Resolved }}
{{ end }}
{{ end }}{{ define "ding.link.title" }}{{ template "default.title" . }}{{ end }}
{{ define "ding.link.content" }}{{ template "default.content" . }}{{ end }}
{{ template "default.title" . }}
{{ template "default.content" . }}
vim /data/prometheus-webhook-dingtalk-2.1.0.linux-amd64/config.example.yml ## Request timeout
# timeout: 5s## Uncomment following line in order to write template from scratch (be careful!)
#no_builtin_template: true## Customizable templates path
#刪掉注釋模版文件
templates:- contrib/templates/legacy/template.tmpl

5.6、配置alertmanager配置文件

route:
#分組聚合減少告警消息數量group_by: ['alertname']
#告警時,先收集報警然后在發送group_wait: 30s  #當收到告警時,先等待30秒在一起發送group_interval: 5m  #發送警告時間間隔repeat_interval: 1h #重復報警時間間隔receiver: 'dingding.webhook1'
receivers:- name: 'dingdingwebhook1'webhook_configs:- url: ''http://172.16.254.190:8060/dingtalk/webhook1/send'
inhibit_rules:- source_match:severity: 'critical'target_match:severity: 'warning'equal: ['alertname', 'dev', 'instance']

5.7、重啟webhook和alertmanager服務

systemctl restart webhook
systemctl restart alertmanager

6、查看是否能觸發報警

6.1、登錄node1服務器關閉node_exporte服務

systemctl restart alertmanager

三、生產環境場景

Nginx監控

nginx-module-vts:Nginx virtual host traffic status module,Nginx的監控模塊,能夠提供JSON格式的數據產出。

nginx-vts-exporter:Simple server that scrapes Nginx vts stats and exports them via HTTP for Prometheus consumption。主要用于收集Nginx的監控數據,并給Prometheus提供監控接口,默認端口號9913。

Prometheus:監控Nginx-vts-exporter提供的Nginx數據,并存儲在時序數據庫中,可以使用PromQL對時序數據進行查詢和聚合。

Nginx安裝

前期準備

1、Nginx源碼獲取

#創建源碼包文件
mkdir /data/sourcewget https://nginx.org/download/nginx-1.28.0.tar.gztar -xf nginx-1.28.0.tar.gz -C /data/source/

2、安裝編譯工具及依賴庫,腳本如下:

yum -y install gcc pcre-devel  zlib-devel openssl-devel libxml2-devel \libxslt-devel gd-devel GeoIP-devel jemalloc-devel libatomic_ops-devel \perl-devel  perl-ExtUtils-Embed

3、編譯配置參數配置

--prefix=PATH/usr/local編譯后代碼的安裝目錄
--with-select_module不編譯編譯 select I/O 事件機制模塊,在不支持 Nginx 默認 I/O 事件機制的操作系統下自動編譯該模塊
--without-select_module編譯不編譯 select I/O 事件機制模塊
--with-poll_module不編譯編譯 poll/O 事件機制模塊,在不支持 Nginx 默認 I/O 事件機制的操作系統下自動編譯該模塊
--without-poll_module編譯不編譯 poll I/O 事件機制模塊
--with-threads不編譯?啟用線程池支持
--with-file-aio不編譯啟用 AIO 支持
--with-http_ssl_module不編譯編譯 SSL 模塊
--with-http_v2_module不編譯編譯 HTTP/2 模塊
--with-http_realip_module不編譯編譯 HTTP 的真實 IP 模塊
--with-http_addition_module不編譯編譯響應內容追加模塊
--with-http_xslt_module不編譯編譯 XSLT 樣式表轉換模塊
--with-http_xslt_module=dynamic--動態編譯 XSLT 樣式表轉換 XML 響應模塊
--with-http_image_filter_module編譯編譯圖像轉換模塊
--with-http_image_filter_module=dynamic--動態編譯圖像轉換模塊
--with-http_geoip_module編譯編譯客戶端 IP 解析城市地址模塊
--with-http_geoip_module=dynamic--動態編譯客戶端 IP 解析城市地址模塊
--with-http_sub_module不編譯編譯字符串替換模塊
--with-http_dav_module不編譯編譯 WebDAV 協議支持模塊
--with-http_flv_module不編譯?編譯 FLV 文件偽流媒體服務器支持模塊
--with-http_mp4_module不編譯編譯 MP4 文件偽流媒體服務器支持模塊
--with-http_gunzip_module不編譯?編譯 gzip 壓縮兼容模塊
--with-http_gzip_static_module不編譯? ?編譯發送 gz 預壓縮文件數據模塊
--with-http_auth_request_module不編譯??編譯請求認證模塊
--with-http_random_index_module不編譯編譯隨機首頁模塊
--with-http_secure_link_module不編譯編譯請求連接安全檢查模塊
--with-http_degradation_module?不編譯?編譯內存不足響應模塊
--with-http_slice_module不編譯?編譯文件切片模塊
--with-http_stub_status_module不編譯?編譯運行狀態模塊
--without-http_charset_module編譯不編譯字符集轉換模塊
--without-http_gzip__module編譯不編譯 gzip 方式壓縮輸出模塊
--without-http_ssi_module編譯不編譯 SSI 支持模塊
--without-http_userid_module編譯不編譯 cookie 操作模塊
--without-http_access_module編譯不編譯基于 IP 的訪問控制模塊
--without-http_auth_basic_module編譯不編譯 HTTP 基本認證模塊
--without-http_mirror_module編譯??不編譯訪問鏡像模塊
--without-http_autoindex_module編譯不編譯自動目錄索引模塊
--without-http_geo_module編譯不編譯根據客戶 IP 創建變量模塊
--without-http_map_module編譯不編譯變量映射模塊
--without-http_split_clients_module編譯?不編譯自定義客戶請求分配模塊
--without-http_referer_module編譯不編譯 referer 操作模塊
--without-http_rewrite_module編譯不編譯 rewrite 規則模塊
--without-http_proxy_module編譯不編譯代理功能模塊
--without-http_fastcgi_module編譯不編譯 FastCGI 支持模塊
--without-http_uwsgi_module?編譯不編譯 uWSGI 支持模塊
--without-http_scgi_module編譯不編譯 SCGI 支持模塊
--without-http_grpc_module編譯不編譯 gRPC 支持模塊
--without-http_memcached_module編譯不編譯 Memcached 服務訪問模塊
--without-http_limit_conn_module編譯不編譯并發連接數控制模塊
--without-http_limit_req_module編譯不編譯單 IP 請求數限制模塊
--without-http_empty_gif_module編譯不編譯空 GIF 圖片模塊
--without-http_browser_module編譯不編譯客戶端瀏覽器識別模塊
--without-http_upstream_hash_module編譯不編譯 hash 負載均衡算法模塊
--without-http_upstream_ip_hash_module編譯不編譯 HTTP 協議 ip-hash 負載均衡模塊
--without-http_upstream_least_conn_module編譯不編譯最少連接數算法負載均衡模塊
--without-http_upstream_random_module編譯不編譯隨機選擇算法負載均衡模塊
--without-http_upstream_keepalive_module編譯不編譯負載均衡后端長連接支持模塊
--without-http_upstream_zone_module編譯不編譯負載均衡共享內存支持模塊
--with-http_perl_module不編譯編譯 Perl 腳本支持模塊
--with-http_perl_module=dynamic--動態編譯 Perl 腳本支持模塊
--with-stream不編譯編譯 TCP/UDP 代理模塊
--with-stream=dynamic--動態編譯 TCP/UDP 代理模塊
--with-stream_ssl_module不編譯編譯 TCP/UDP 代理 SSL 支持模塊
--with-stream_realip_module不編譯編譯 TCP/UDP 代理真實 IP 模塊
--with-stream_geoip_module不編譯編譯地域信息解析模塊
--with-stream_geoip_module=dynamic--動態編譯地域信息解析模塊
--with-stream_ssl_preread_module不編譯編譯 TCP/UDP 代理的 SSL 預處理模塊

4、編譯

cd /data/source/
./configure \--with-threads \--with-file-aio \--with-http_ssl_module \--with-http_v2_module \--with-http_realip_module \--with-http_addition_module \--with-http_xslt_module=dynamic \--with-http_image_filter_module=dynamic \--with-http_geoip_module=dynamic \--with-http_sub_module \--with-http_dav_module \--with-http_flv_module \--with-http_mp4_module \--with-http_gunzip_module \--with-http_gzip_static_module \--with-http_auth_request_module \--with-http_random_index_module \--with-http_secure_link_module \--with-http_degradation_module \--with-http_slice_module \--with-http_stub_status_module \--with-stream=dynamic \--with-stream_ssl_module \--with-stream_realip_module \--with-stream_geoip_module=dynamic \--with-stream_ssl_preread_module \--with-compat  \--with-pcre-jit
make && make install

編譯后,默認安裝目錄為 /usr/local/nginx。

5、添加第三方模塊

5.1、查看都安裝了哪些模塊
/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.28.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-stream=dynamic --with-stream_ssl_module --with-stream_realip_module --with-stream_geoip_module=dynamic --with-stream_ssl_preread_module --with-compat --with-pcre-jit
5.2、下載第三方模塊
5.3、添加到nginx
#復制原先模塊后添加第三方模塊
cd /data/source/nginx-1.28.0/
./configure     --with-threads     --with-file-aio     --with-http_ssl_module     --with-http_v2_module     --with-http_realip_module     --with-http_addition_module     --with-http_xslt_module=dynamic     --with-http_image_filter_module=dynamic     --with-http_geoip_module=dynamic     --with-http_sub_module     --with-http_dav_module     --with-http_flv_module     --with-http_mp4_module     --with-http_gunzip_module     --with-http_gzip_static_module     --with-http_auth_request_module     --with-http_random_index_module     --with-http_secure_link_module     --with-http_degradation_module     --with-http_slice_module     --with-http_stub_status_module     --with-stream=dynamic     --with-stream_ssl_module     --with-stream_realip_module     --with-stream_geoip_module=dynamic     --with-stream_ssl_preread_module     --with-compat      --with-pcre-jit --with-http_ssl_module  --add-module=/usr/local/nginx-module-vts-0.2.4make

切記不要make install 會覆蓋之前的模塊。

5.4、替換二進制文件
#這里復制一下原來的文件,出了問題還可以還原
mv /usr/local/nginx/sbin/nginx  /usr/local/nginx/sbin/nginx.bak
復制這個文件復制過去cp /data/source/nginx-1.28.0/objs/nginx /usr/local/nginx/sbin/
pkill -9 nginx ## 關閉nginx
#啟動nginx
/usr/local/nginx/sbin/nginx 
.5.5、修改nginx.conf配置,實驗安裝是否成功:

http {...vhost_traffic_status_zone;vhost_traffic_status_filter_by_host on;...server {...   location /status {vhost_traffic_status_display;vhost_traffic_status_display_format html;}
}

配置解析:

打開vhost過濾:

vhost_traffic_status_filter_by_host on;

開啟此功能,在Nginx配置有多個server_name的情況下,會根據不同的server_name進行流量的統計,否則默認會把流量全部計算到第一個server_name上。

在不想統計流量的server區域禁用vhost_traffic_status,配置示例

server {
...
vhost_traffic_status off;
...
}
5.6、啟動服務
/usr/local/nginx/sbin/nginx

打開瀏覽器訪問ip/status:

5.7、安裝nginx-vts-exporter

wget https://github.com/hnlq715/nginx-vts-exporter/bin/nginx-vts-exporter -nginx.scrape_uri=http://172.16.254.191/status/format/json

grafana

導入2949模板

四、node_export 自定義監控指標

Prometheus 的 node_exporter 是一個非常流行的導出器,用于收集系統級別的指標。然而,有時你可能需要監控一些自定義的指標。以下是如何配置 node_exporter 來監控自定義指標的步驟。

1、自定義監控nginx服務存活性

例如,創建一個名為 custom_metrics.sh 的 Bash 腳本

#!/bin/bash
echo "# HELP custom_metric_example This is an example of a custom metric"
echo "# TYPE custom_metric_example gauge"
echo "nginx up "

服務監控腳本

#!/bin/bashps -ef|grep nginx|grep -v grep > /dev/null
if [ $? -eq 0 ];then
echo "# HELP custom_metric_example This is an example of a custom metric"
echo "# TYPE custom_metric_example gauge"
echo "nginx_up 1"
else
echo "# HELP custom_metric_example This is an example of a custom metric"
echo "# TYPE custom_metric_example gauge"
echo "nginx_up 0"

2. 配置 node_exporter 以執行自定義腳本

node_exporter 支持通過 textfile 模式來導入自定義指標。你需要將自定義腳本的輸出寫入到一個特定的目錄中,node_exporter 會定期讀取這個目錄中的文件并將其內容作為指標導入。
創建一個目錄來存放自定義指標文件:


mkdir -p/data/node_exporter-1.8.2.linux-amd64/textfile_collectorsh custom_metrics.sh > //data/node_exporter-1.8.2.linux/amd64/textfile_collector/custom_metrics.prom

3、添加到systemctl服務

 cat/usr/lib/systemd/system/node_exporter.service 
[Unit]
Description=Prometheus-Server
After=network.target[Service]
ExecStart=/data/node_exporter-1.8.2.linux-amd64/node_exporter --collector.textfile.directory=/data/node_exporter-1.8.2.linux-amd64/textfile_collector  --web.listen-address=:9500
User=root[Install]
WantedBy=multi-user.target

4、啟動服務

systemctl restart node_exporter #curl 請求一下看是否有數據
[root@node1 node_exporter-1.8.2.linux-amd64]# curl 172.16.254.191:9500/metrics |grep nginx% Total    % Received % Xferd  Average Speed   Time    Time     Time  CurrentDload  Upload   Total   Spent    Left  Speed0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0# HELP nginx_up Metric read from /data/node_exporter-1.8.2.linux-amd64/textfile_collector/custom_metrics.prom
# TYPE nginx_up untyped
nginx_up 1
100 76559    0 76559    0     0  4662k      0 --:--:-- --:--:-- --:--:-- 4984k

5、瀏覽器訪問Prometheus是否有數據

ip:端口號

6、配置自定義grafana

借鑒大佬文章:

Prometheus+Alertmanager+webhook-dingtalk實現釘釘告警 - 不會游泳的魚丶 - 博客園

(6 封私信) https://webhook.site/ - 知乎

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/915870.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/915870.shtml
英文地址,請注明出處:http://en.pswp.cn/news/915870.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【小白量化智能體】應用6:根據通達信指標等生成機器學習Python程序

【小白量化智能體】應用6:根據通達信指標等生成機器學習Python程序 【小白量化智能體】是指能夠自主或半自主地通過與環境的交互來實現目標或任務的計算實體。智能體技術是一個百科全書,又融合了人工智能、計算機科學、心理學和經濟學等多個領域的知識&a…

k8s的calico無法啟動報錯解決

報錯信息[INFO][1] main.go 138: Failed to initialize datastore errorGet "https://10.245.0.1:443/apis/crd.projectcalico.org/v1/clusterinformations/default": dial tcp 10.245.0.1:443: connect: no route to host 2025-07-21 06:15:42.055 [FATAL][1] main.…

MySQL多表查詢中的笛卡爾積問題

精選專欄鏈接 🔗 MySQL技術筆記專欄Redis技術筆記專欄大模型搭建專欄Python學習筆記專欄深度學習算法專欄 歡迎訂閱,點贊+關注,每日精進1%,與百萬開發者共攀技術珠峰 更多內容持續更新中!希望能給大家帶來…

深度解析 HTML `loading` 屬性:優化網頁性能的秘密武器

在開發網頁時,我常常被頁面加載速度慢的問題困擾,尤其是在圖片和嵌入內容較多的頁面上。用戶還沒看到內容就可能因為等待時間過長而離開,這對用戶體驗和 SEO 都是致命打擊。后來,我發現了 HTML 的 loading 屬性——一個簡單卻強大…

[C/C++內存安全]_[中級]_[安全處理字符串]

場景 在現代C開發指南出來后,并不建議使用C的某些內存不安全的字符串處理函數。那么有哪些函數不安全? 說明 內存安全方面,肯定是要向Rust看齊的。使用標準std::string字符串類,很大情況能避免緩沖區溢出問題。 如果舊項目里有…

【CNN】卷積神經網絡- part1

1.卷積1.局部連接定義:只是于輸入數據的一部分區域相連,每個神經元只關注一小部分作用:模仿人類的視野機制,極大的減少了模型參數的數量,降低了計算成本2.權重共享定義:所有神經元使用相同的權重向量來檢測…

漏洞生命周期管理:從發現到防護的全流程方案

漏洞并非孤立存在,而是遵循 “發現→評估→修復→驗證→閉環” 的生命周期。多數企業安全事件的根源并非缺乏漏洞發現能力,而是對漏洞生命周期的管理缺失 —— 大量漏洞被發現后長期未修復,或修復后未驗證效果。構建全流程漏洞生命周期管理體…

opencv圖像基本操作解析與實操

圖片操作cv2.namedWindow() 創建命名窗口cv2.imshow()顯示窗口cv2.destroyAllwindws()摧毀窗口cv2.resizeWindow()改變窗口大小cv2.waitKey()等待用戶輸入cv2.imread()讀取圖像img.shape 圖片h、w、c(高、寬、通道數import cv2 # opencv讀取的格式是BGR import m…

kafka--基礎知識點--6.1--LEO、HW、LW

在 Apache Kafka 中,LEO(Log End Offset)、HW(High Watermark)、和 LW(Low Watermark) 是副本機制和日志管理中的核心概念,共同確保數據一致性、可見性和存儲效率。以下是它們的詳細…

在線深凹槽深檢測方法都有哪些 —— 激光頻率梳 3D 輪廓檢測

引言在制造業中,深凹槽深度的精確檢測是保證零部件質量的關鍵環節。隨著智能制造的推進,在線檢測需求日益迫切,傳統檢測方法在效率和精度上的不足逐漸顯現。本文將梳理在線深凹槽深的傳統檢測方法,并重點探討激光頻率梳 3D 輪廓檢…

NumPy 數組拼接的高級技巧與實踐

在數據處理和機器學習領域,NumPy 是 Python 中最核心的科學計算庫之一。NumPy 數組(ndarray)的拼接操作是數據預處理中極為常見的需求。本文將深入探討如何將不同形狀的 NumPy 數組進行拼接,特別是如何將多個一維數組與二維數組進…

原創-基于 PHP 和 MySQL 的證書管理系統 第三版

第一版屬于開源版本,所以后臺功能沒有開發許多出來,今天分享證書查詢第三版; 通過幾天的緊急寫代碼及測試;第三版基本可以上線上,不過后面有一些BUG只能一邊修復。 演示地址:物星科云證書管理系統 第三版…

爬蟲虛擬環境

conda create --name myrepenv python3.12創建一個名為 myrepenv、Python 版本為 3.12 的全新 Conda 虛擬環境,適合需要隔離依賴或測試不同 Python 版本的項目。我的方式創建(需要指定盤符)conda create --prefixD:\Anaconda3\envs\myrepenv python3.12展示所下的co…

Netty集群方案詳解與實戰(Zookeeper + Redis + RabbitMQ)

一、背景 二、Netty 單體架構的優缺點 優點 缺點 三、Netty 集群架構的優缺點 優點 缺點 四、適用場景對比 五、Netty單體架構代碼實現 六、Netty集群架構方案實現 方案一、Nginx負載均衡實現集群(較為簡單) Nginx配置 前端連接方式 方案二、NacosGateway(結合…

Oracle遷移到高斯,查詢字段默認小寫,解決辦法

一、問題說明 Oracle中,查詢結果字段默認大寫。高斯中,查詢結果字段默認小寫。在Mybatis的xml中,如果查詢語句使用Map接收查詢結果,使用resultType"java.util.HashMap"或resultType"Map"等寫法,返…

Android Jetpack Compose + MVVM 開發流程深度分析

核心組件關系圖 [View] -- 觀察 --> [ViewModel] -- 操作 --> [Repository]| | Compose UI StateFlow/LiveData| | 用戶交互事件 Room/Retrofit| …

Tailwind CSS快速上手 Tailwind CSS的安裝、配置、使用

📚前言 在Web前端開發的歷史長河中,CSS的編寫方式經歷了多次演進,從早期的原生CSS 到 CSS預處理(Less/Sass/Stylus) 到 CSS-in-JS(Styled-Components/Emotion) 再到 Utility-First 原子化CSS。每一種演進方案其本質都是圍繞“開發效率”、“…

單例模式的智慧:從UVM看控制的藝術

有時候,生活中的很多東西其實只需要一個就夠了,就像一個公司只需要一個CEO,一個王朝只需要一個皇帝。在UVM驗證環境中,也有很多這樣的需求——有些對象,我們希望它在整個仿真過程中只存在一個實例。這就是我們今天要聊…

Hexo - 免費搭建個人博客01 - 安裝軟件工具

導言我的博客:https://q164129345.github.io/ Hexo 作為一個 Node.js 框架,它依賴于 Node.js 運行時環境來執行。 一、安裝Node.js官方網址:https://nodejs.org/zh-cn追求系統穩定性、可靠性以及希望減少維護頻率的用戶來說,LTS版…

【Kubernetes】集群啟動nginx,觀察端口映射,work節點使用kubectl配置

參考b站叩丁狼總結:完整版Kubernetes(K8S)全套入門微服務實戰項目,帶你一站式深入掌握K8S核心能力 在master節點執行 kubectl create deployment nginx --imagenginxkubectl expose deployment nginx --port80 --typeNodePort1. …