一、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/ - 知乎