1 pushgateway采集
1.1 自定義采集鍵值
如果自定義采集需求時,就可以通過寫腳本 + 定時任務定期發送數據到 pushgateway 達到自定義監控
1.部署 pushgateway,以 10.0.0.42 節點為例
1.下載組件
wget https://github.com/prometheus/pushgateway/releases/download/v1.9.0/pushgateway-1.9.0.linux-amd64.tar.gz2.解壓軟件包
tar xf pushgateway-1.9.0.linux-amd64.tar.gz -C /zhiyong18/softwares/3.啟動pushgateway組件,默認監聽9091端口
cd /zhiyong18/softwares/pushgateway-1.9.0.linux-amd64/
./pushgateway 4.訪問pushgateway的WebUI
http://10.0.0.41:9091/#
2.prometheus 增加新任務
[root@prometheus-server31 ~]# vim /zhiyong18/softwares/prometheus-2.53.2.linux-amd64/prometheus.yml
...- job_name: zhiyong18-zhiyong-pushgateway# 若不指定則默認值為false。# 當設置為true時,若采集的指標包含中和內置的標簽沖突時(比如job,instance)會覆蓋。# 當設置為false時,則不會覆蓋,而是在標簽前面加一個"exported_*"字段。honor_labels: truestatic_configs:- targets:- 10.0.0.41:9091
3.訪問測試:http://10.0.0.31:9090/targets
,可以看到新的 targets
4.發送測試數據到 pushgateway,注意:傳遞的數據是鍵值對,KEY一般是字符串類型,而value必須是一個數字類型。
echo "wzy_age 18" | curl --data-binary @- \
http://10.0.0.41:9091/metrics/job/zhiyong18_student/instance/10.0.0.31
5.訪問prometheus,查看接收到的數據
1.2 監控TCP狀態案例
1.編寫腳本,定期發送本機的TCP狀態指標到 pushgateway 10.0.0.41
cat /usr/local/bin/tcp_status.sh
#!/bin/bash
# 定義TCP的12種狀態
ESTABLISHED_COUNT=0
SYN_SENT_COUNT=0
SYN_RECV_COUNT=0
FIN_WAIT1_COUNT=0
FIN_WAIT2_COUNT=0
TIME_WAIT_COUNT=0
CLOSE_COUNT=0
CLOSE_WAIT_COUNT=0
LAST_ACK_COUNT=0
LISTEN_COUNT=0
CLOSING_COUNT=0
UNKNOWN_COUNT=0# 定義任務名稱
JOB_NAME=tcp_status
# 定義實例名稱
INSTANCE_NAME=harbor250
# 定義pushgateway主機
HOST=10.0.0.41
# 定義pushgateway端口
PORT=9091# TCP的12種狀態
ALL_STATUS=(ESTABLISHED SYN_SENT SYN_RECV FIN_WAIT1 FIN_WAIT2 TIME_WAIT CLOSE CLOSE_WAIT LAST_ACK LISTEN CLOSING UNKNOWN)# 聲明一個關聯數組,類似于py的dict,go的map
declare -A tcp_status# 統計TCP的11種狀態
for i in ${ALL_STATUS[@]}
dotemp=`netstat -untalp | grep $i | wc -l`tcp_status[${i}]=$temp
done# 將統計后的結果發送到pushgateway
for i in ${!tcp_status[@]}
do data="$i ${tcp_status[$i]}"# TODO: shell如果想要設計成相同key不同標簽的方式存在問題,只會有最后一種狀態被發送# 目前我懷疑是pushgateway組件不支持同一個metrics中key所對應的value不同的情況。#data="zhiyong18_tcp_all_status{status=\"$i\"} ${tcp_status[$i]}"#echo $dataecho $data | curl --data-binary @- http://${HOST}:${PORT}/metrics/job/${JOB_NAME}/instance/${INSTANCE_NAME}# sleep 1
done
運行:`bash /usr/local/bin/tcp_status.sh`
2.訪問
prometheus查看并搜索結果:
2 黑/白盒監控
2.1 黑白監控介紹
黑盒監控:黑盒監控是面向現象的,關注的是系統當前的狀態,而不是預測未來會發生的問題。比如,當系統出現故障時,黑盒監控會發出警報
**白盒監控:**白盒監控則更深入,依賴于對系統內部信息的檢測,如系統日志、HTTP節點等。它不僅能檢測到當前的問題,還能預測到即將發生的問題,甚至那些被重試掩蓋的問題
Prometheus基于blackbox進行黑盒監控
blackbox_exporter概述
-
blackbox exporter支持基于HTTP, HTTPS, DNS, TCP, ICMP, gRPC協議來對目標節點進行監控
-
比如基于http協議我們可以探測一個網站的返回狀態碼為200判讀服務是否正常
-
比如基于TCP協議我們可以探測一個主機端口是否監聽
-
比如基于ICMP協議來ping一個主機的連通性
-
比如基于gRPC協議來調用接口并驗證服務是否正常工作
-
比如基于DNS協議可以來檢測域名解析
2.2 blackbox監控網站狀態案例
01 安裝blackbox_exporter
在任意節點都能安裝,并不是agent效果
1 下載軟件
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.25.0/blackbox_exporter-0.25.0.linux-amd64.tar.gz2 解壓軟件包
tar xvf blackbox_exporter-0.25.0.linux-amd64.tar.gz -C /zhiyong18/softwares/3 啟動服務
cd /zhiyong18/softwares/blackbox_exporter-0.25.0.linux-amd64/
./blackbox_exporter 4 訪問blackbox的WebUI
http://10.0.0.32:9115/
02 添加blackbox數據
1.修改Prometheus配置文件,對blackbox創建采集 job
[root@prs31~]# cat /zhiyong18/softwares/prometheus-2.53.2.linux-amd64/prometheus.yml
...
- job_name: 'zhiyong18-blackbox-exporter-http'# 修改訪問路徑,若不修改,默認值為"/metrics"metrics_path: /probe# 配置URL的相關參數params:# 此處表示使用的是blackbox的http模塊,從而判斷相應的返回狀態碼是否為200module: [http_2xx] # 下面這兩個標簽是我自定義的,便于大家理解names: ["zhiyong18"]name: ["zhiyong"]# 靜態配置,需要手動指定監控目標static_configs:# 需要監控的目標- targets:# 支持https協議- https://www.jd.com/# 支持http協議,以grafna為例- http://10.0.0.31:3000# 支持http協議和自定義端口,以prometheus的web為例- http://10.0.0.31:9090# 對目標節點進行重新打標簽配置relabel_configs:# 指定源標簽,此處的"__address__"表示內置的標簽,存儲的是被監控目標的IP地址- source_labels: [__address__]# 指定目標標簽,其實就是在"Endpoint"中加了一個target字段(用于指定監控目標),target_label: __param_target# 指定需要執行的動作,默認值為"replace",常用的動作有: replace, keep, and drop。# 但官方支持十幾種動作: https://prometheus.io/docs/prometheus/2.53/configuration/configuration/#relabel_action# 將"__address__"傳遞給target字段。action: replace- source_labels: [__param_target]target_label: instance#target_label: instance2024# 上面的2個配置段也可以改寫成如下的配置喲~# - source_labels: [__address__]# target_label: instance# action: replace# - source_labels: [instance]# target_label: __param_target# action: replace- target_label: __address__# 指定要替換的值,此處我指定為blackbox exporter的主機地址replacement: 10.0.0.32:9115
無注釋版的配置:(以這個為最終測試)
- job_name: 'zhiyong18-blackbox-exporter-http'metrics_path: '/probe'params:module: [http_2xx] names: ["zhiyong18"]name: ["zhiyong"]static_configs:- targets:- https://www.jd.com/- http://10.0.0.31:3000# 重寫標簽relabel_configs:- source_labels: [__address__]target_label: __param_targetaction: replace- source_labels: [__param_target]target_label: instance# 向目標發起數據探測- target_label: __address__replacement: 10.0.0.32:9115
03 訪問測試
1.訪問prometheus的WebUI:http://10.0.0.31:9090/targets
2.訪問blackbox exporter的WebUI:http://10.0.0.32:9115/
3.grafana展示數據,這2個模版ID可以參考
7587
13659
監控到了網站的指標,ssl證書過期時間,流量,狀態碼…
2.3 基于ICMP監控主機存活
1.修改Prometheus配置文件增加ICMP采集任務
- job_name: 'zhiyong18-blackbox-exporter-icmp'metrics_path: /probeparams:# 如果不指定模塊,則默認類型為"http_2xx"module: [icmp]static_configs:- targets:- 10.0.0.41- 10.0.0.42- 10.0.0.66relabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]# 如果instance不修改,則instance和"__address__"的值相同target_label: instance- target_label: __address__replacement: 10.0.0.32:9115
2.查看 prometheus 的targets
3.訪問 blackbox 的頁面
4.dashboard查看
要使用13659模版,基于"zhiyong18-blackbox-exporter-icmp"標簽進行過濾。
2.4 監控TCP端口存活
1.修改配置
1 修改Prometheus配置文件
[root@prometheus-server31 ~]# vim /zhiyong18/softwares/prometheus-2.53.2.linux-amd64/prometheus.yml ...
scrape_configs:...- job_name: 'zhiyong18-blackox-exporter-tcp'metrics_path: /probeparams:module: [tcp_connect]static_configs:- targets:- 10.0.0.41:80- 10.0.0.42:22- 10.0.0.31:9090relabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]target_label: instance- target_label: __address__replacement: 10.0.0.32:9115
2.訪問prometheus
3.使用grafana查看數據
基于"zhiyong18-blackbox-exporter-tcp"標簽進行過濾。
3 prometheus擴展
3.1 遠端存儲VictoriaMetrics
VictoriaMetrics是一個快速、經濟高效且可擴展的監控解決方案和時間序列數據庫。如果數據全部存儲在prometheus一個節點上有單點的風險,有必要使用一個分布式的高可用的外置存儲。
官網:https://victoriametrics.com/
官方文檔:https://docs.victoriametrics.com/
GitHub地址:https://github.com/VictoriaMetrics/VictoriaMetrics
部署文檔:https://docs.victoriametrics.com/quick-start/
集群部署參考 (非官方)
01 安裝VictoriaMetrics
prometheus-server 10.0.0.32 進行單點安裝為例
1 下載軟件
wget https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/v1.93.16/victoria-metrics-linux-amd64-v1.93.16.tar.gz2 解壓軟件包
tar xf victoria-metrics-linux-amd64-v1.93.16.tar.gz -C /usr/local/bin/3 編寫啟動腳本
cat > /etc/systemd/system/victoria-metrics.service <<'EOF'
[Unit]
Description=zhiyong18 Linux VictoriaMetrics Server
Documentation=https://docs.victoriametrics.com/
After=network.target[Service]
ExecStart=/usr/local/bin/victoria-metrics-prod \-httpListenAddr=0.0.0.0:8428 \-storageDataPath=/zhiyong18/data/victoria-metrics \-retentionPeriod=6[Install]
WantedBy=multi-user.target
EOF4 重啟服務
systemctl daemon-reload
systemctl enable --now victoria-metrics.service
systemctl status victoria-metrics5 檢查端口是否存活ss -ntl | grep 8428
LISTEN 0 4096 0.0.0.0:8428 0.0.0.0:* 6 查看webUI
http://10.0.0.32:8428/
02 使用VictoriaMetrics
1.修改prometheus-server 31 的配置文件:vim /zhiyong18/softwares/prometheus-2.53.2.linux-amd64/prometheus.yml
...
# 在頂級字段中配置VictoriaMetrics地址,注意不要放到通用字段下
remote_write:
- url: http://10.0.0.32:8428/api/v1/write
2.重新加載prometheus的配置
systemctl stop prometheus-server/zhiyong18/softwares/prometheus-2.53.2.linux-amd64/prometheus \
--config.file=/zhiyong18/softwares/prometheus-2.53.2.linux-amd64/prometheus.yml
03 配置grafana新數據源
1.由于數據源發生了存儲到了 VictoriaMetrics ,所以grafana要添加新的數據源,否則查不到
2.導入儀表盤(1860)的時候要指定新的數據源
3.2 自定義exporter(python案例)
01 使用python自定義exporter
1.在prometheus-server31節點安裝 pip3
apt update
apt install -y python3-pip# 創建一個虛擬用戶,但是有家目錄
useradd -m -s /bin/bash python
2.切換用戶為python做如下操作:
3 修改配置文件(可選)
su - python
vim .bashrc
...
# 取消下面一行的注釋,添加顏色顯示
force_color_prompt=yes4 pip配置加速
mkdir ~/.pip; vim ~/.pip/pip.conf
# 注釋掉以前的,添加阿里源
# [global]
# index-url=https://pypi.tuna.tsinghua.edu.cn/simple
# [install]
# trusted-host=pypi.douban.com
[global]
index-url=https://mirrors.aliyun.com/pypi/simple
[install]
trusted-host=mirrors.aliyun.com5 安裝實際環境中相關模塊庫
pip3 install flask prometheus_client
pip3 list6 創建代碼目錄
mkdir code7 編寫python代碼
cd code
cat > flask_metric.py <<'EOF'
#!/usr/bin/python3from prometheus_client import start_http_server,Counter, Summary
from flask import Flask, jsonify
from wsgiref.simple_server import make_server
import timeapp = Flask(__name__)# Create a metric to track time spent and requests made
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')
COUNTER_TIME = Counter("request_count", "Total request count of the host")@app.route("/apps")
@REQUEST_TIME.time()
def requests_count():COUNTER_TIME.inc()return jsonify({"office": "wenzy18@qq.com"},{"auther":"Wen Zhiyong"})if __name__ == "__main__":start_http_server(8000)httpd = make_server( '0.0.0.0', 8001, app )httpd.serve_forever()
EOF8 啟動python程序
python3 flask_metric.py # 因為沒有客戶端訪問,所以沒有任何輸出
3.客戶端測試,使用任意一個節點均可
cat > zhiyong18_curl_metrics.sh <<'EOF'
#!/bin/bashURL=http://10.0.0.31:8001/appswhile true;docurl_num=$(( $RANDOM%50+1 ))sleep_num=$(( $RANDOM%5+1 ))for c_num in `seq $curl_num`;docurl -s $URL &> /dev/nulldonesleep $sleep_num
done
EOF# 回到之前的python執行窗口,可以看到以下輸出
10.0.0.31 - - [14/Dec/2024 22:33:28] "GET /apps HTTP/1.1" 200 55
10.0.0.31 - - [14/Dec/2024 22:33:28] "GET /apps HTTP/1.1" 200 55
10.0.0.31 - - [14/Dec/2024 22:33:28] "GET /apps HTTP/1.1" 200 55
10.0.0.31 - - [14/Dec/2024 22:33:28] "GET /apps HTTP/1.1" 200 55
10.0.0.31 - - [14/Dec/2024 22:33:28] "GET /apps HTTP/1.1" 200 55
10.0.0.31 - - [14/Dec/2024 22:33:28] "GET /apps HTTP/1.1" 200 55
10.0.0.31 - - [14/Dec/2024 22:33:28] "GET /apps HTTP/1.1" 200 55
10.0.0.31 - - [14/Dec/2024 22:33:28] "GET /apps HTTP/1.1" 200 55
4.python腳本 flask_metric.py
是一個簡單的web服務器,所以可以訪問,并且能打開
02 采集exporter數據
1.修改prometheus配置 vim /zhiyong18/softwares/prometheus-2.53.2.linux-amd64/prometheus.yml ,添加任務
- job_name: "wzy python 自定義exporter"static_configs:- targets:- 10.0.0.31:8000
2.在 http://10.0.0.31:9090/targets
可以看到這條指標
3.執行搜索,http://10.0.0.31:9090/
request_count_total
4.該指標可以自定義畫圖 grafana ,和 PQL 查詢一樣
# apps請求總數
request_count_total# 每分鐘請求數量曲線QPS
increase(request_count_total{job="wzy python 自定義exporter"}[1m])#每分鐘請求量變化率曲線
irate(request_count_total{job="wzy python 自定義exporter"}[1m])# 每分鐘請求處理平均耗時
request_processing_seconds_sum{job="wzy python 自定義exporter"} / request_processing_seconds_count{job=""}
me: "wzy python 自定義exporter"static_configs:- targets:- 10.0.0.31:8000
2.在 http://10.0.0.31:9090/targets
可以看到這條指標
[外鏈圖片轉存中…(img-Z4jtR4iO-1742134880458)]
3.執行搜索,http://10.0.0.31:9090/
request_count_total
4.該指標可以自定義畫圖 grafana ,和 PQL 查詢一樣
# apps請求總數
request_count_total# 每分鐘請求數量曲線QPS
increase(request_count_total{job="wzy python 自定義exporter"}[1m])#每分鐘請求量變化率曲線
irate(request_count_total{job="wzy python 自定義exporter"}[1m])# 每分鐘請求處理平均耗時
request_processing_seconds_sum{job="wzy python 自定義exporter"} / request_processing_seconds_count{job=""}