🌟Grafana的Dashboard的權限管理
創建團隊
創建用戶
設置團隊權限
🌟Prometheus啟用https及認證功能
自建ca的證書
準備證書目錄
mkdir /app/tools/prometheus-2.53.4.linux-amd64/certs
cd /app/tools/prometheus-2.53.4.linux-amd64/certs
生成ca的私鑰
openssl genrsa -out ca.key 4096
生成ca的自簽名證書
openssl req -x509 -new -nodes -sha512 -days 3650 \-subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=zhubl.xyz" \-key ca.key \-out ca.crt
基于自建CA證書生成Prometheus服務端證書
準備證書存放目錄
mkdir server
生成prometheus主機的私鑰
openssl genrsa -out server/prometheus.zhubl.xyz.key 4096
生成prometheus主機的證書申請
openssl req -sha512 -new \-subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=prometheus.zhubl.xyz" \-key server/prometheus.zhubl.xyz.key \-out server/prometheus.zhubl.xyz.csr
生成x509 v3擴展文件
cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names[alt_names]
DNS.1=zhubl.xyz
DNS.2=zhubl
DNS.3=prometheus.zhubl.xyz
EOF
使用"v3.ext"給prometheus主機簽發證書
openssl x509 -req -sha512 -days 3650 \-extfile v3.ext \-CA ca.crt -CAkey ca.key -CAcreateserial \-in server/prometheus.zhubl.xyz.csr \-out server/prometheus.zhubl.xyz.crt
將crt文件轉換為cert客戶端證書文件
openssl x509 -inform PEM -in server/prometheus.zhubl.xyz.crt -out server/prometheus.zhubl.xyz.cert
生成認證初始用戶(可跳過)
方式一
python3 -c 'import bcrypt; print("k8s: " + bcrypt.hashpw("zhubl".encode(), bcrypt.gensalt(rounds=10)).decode())'
方式二
[root@prometheus-server31 ~]# apt-get update
[root@prometheus-server31 ~]# apt-get -y install apache2-utils
[root@prometheus-server31 ~]# htpasswd -Bbn k8s zhubl
k8s:$2y$05$cwtXieUbCkQSFK0EiCPPr.49/JO8MwD6zbTS0IEArl2K9EyyWFbtq[root@prometheus-server31 ~]#
準備Prometheus的認證文件
vim /app/tools/prometheus-2.53.4.linux-amd64/auth.yml
tls_server_config:cert_file: /app/tools/prometheus-2.53.4.linux-amd64/certs/server/prometheus.zhubl.xyz.crtkey_file: /app/tools/prometheus-2.53.4.linux-amd64/certs/server/prometheus.zhubl.xyz.key
# 下面的認證信息可以注釋,若啟用,則訪問https頁面時需要指定認證信息喲~
basic_auth_users:k8s: $2b$10$LRfmdrmUBOfL3GrAvCp69ep36rDgpqfKNbO7/u/jjOTJraBgWS3f2
修改Prometheus的啟動腳本添加’–web.config.file’參數指定認證文件
vim /etc/systemd/system/prometheus-server.service
...
[Service]
...
ExecStart=/bin/bash -c "/app/tools/prometheus-2.53.4.linux-amd64/prometheus --web.config.file=/app/tools/prometheus-2.53.4.linux-amd64/auth.yml ....
重啟Prometheus服務
1.加載服務配置文件
systemctl daemon-reload2.重啟服務
systemctl restart prometheus-server.service3.檢查
ss -ntl | grep 9090
訪問測試
?使用認證文件的用戶配置即可。
?溫馨提示: 生產環境中,如果使用權威機構證書,可跳過前2個步驟。
🌟Prometheus基于文件的服務發現案例
靜態配置:(static_configs)
- 修改Prometheus的配置文件時需要熱加載配置文件或者重啟服務生效。
動態配置:(*_sd_config)
- 無需重啟或熱加載服務,可以監聽本地的文件,或者通過注冊中心,服務發現中心發現要監控的目標。
參考鏈接:
https://prometheus.io/docs/prometheus/latest/configuration/configuration/#file_sd_config
修改配置文件
[root@prometheus-server31 ~]# vim /app/tools/prometheus-2.53.4.linux-amd64/prometheus.yml
...- job_name: "file-sd"file_sd_configs:- files:- /tmp/xixi.json- /tmp/haha.yaml
熱加載配置文件
[root@promethues-server31 prometheus-2.53.4.linux-amd64]# curl -X POST https://10.0.0.31:9090/-/reload -k -u k8s:zhubl
[root@promethues-server31 prometheus-2.53.4.linux-amd64]#
[root@promethues-server31 prometheus-2.53.4.linux-amd64]# ./promtool check config prometheus.yml
Checking prometheus.ymlWARNING: file "/tmp/xixi.json" for file_sd in scrape job "file-sd" does not existWARNING: file "/tmp/haha.yaml" for file_sd in scrape job "file-sd" does not existSUCCESS: prometheus.yml is valid prometheus config file syntax
修改json格式文件
[root@prometheus-server31 /app/tools/prometheus-2.53.4.linux-amd64]# cat > /tmp/xixi.json <<EOF
[{"targets": [ "10.0.0.41:9100" ],"labels": {"haha": "xixi", "xixi": "xixi-json"}}
]
EOF
驗證是否自動監控目標
https://10.0.0.31:9090/targets?search=
再次編寫yaml文件
[root@prometheus-server31 /app/tools/prometheus-2.53.4.linux-amd64]# cat > /tmp/haha.yaml <<EOF
- targets:- '10.0.0.42:9100' - '10.0.0.43:9100' labels:address: ShaHeClassRoom: haha
EOF
[root@promethues-server31 prometheus-2.53.4.linux-amd64]# ./promtool check config prometheus.yml
Checking prometheus.ymlSUCCESS: prometheus.yml is valid prometheus config file syntax
驗證是否自動監控目標
https://10.0.0.31:9090/targets?search=
Grafana導入模板ID
1860
🌟基于consul的服務發現案例
官方文檔:
https://www.consul.io/
https://developer.hashicorp.com/consul/install#linux
部署consul集群
下載consul(41-43節點)
wget https://releases.hashicorp.com/consul/1.21.4/consul_1.21.4_linux_amd64.zip
解壓consul
[root@node-exporter41 ~]# unzip consul_1.21.4_linux_amd64.zip -d /usr/local/bin/[root@node-exporter41 ~]# scp /usr/local/bin/consul 10.0.0.42:/usr/local/bin/
[root@node-exporter41 ~]# scp /usr/local/bin/consul 10.0.0.43:/usr/local/bin/
運行consul 集群
服務端43:
[root@node-exporter43 ~]# consul agent -server -bootstrap -bind=10.0.0.43 -data-dir=/app/tools/consul -client=10.0.0.43 -ui客戶端42:
[root@node-exporter42 ~]# consul agent -server -bind=10.0.0.42 -data-dir=/app/tools/consul -client=10.0.0.42 -ui -retry-join=10.0.0.43客戶端41:
[root@node-exporter41 ~]# consul agent -server -bind=10.0.0.41 -data-dir=/app/tools/consul -client=10.0.0.41 -ui -retry-join=10.0.0.43
查看各節點的監聽端口
ss -ntl | egrep "8300|8500"
訪問console服務的WebUI
http://10.0.0.43:8500/ui/dc1/nodes
使用consul實現自動發現
修改prometheus的配置文件
[root@prometheus-server31 ~]# vim /app/tools/prometheus-2.53.4.linux-amd64/prometheus.yml- job_name: "consul-seriver-discovery"# 配置基于consul的服務發現consul_sd_configs:# 指定consul的服務器地址,若不指定,則默認值為"localhost:8500".- server: 10.0.0.43:8500- server: 10.0.0.42:8500- server: 10.0.0.41:8500relabel_configs:# 匹配consul的源標簽字段,表示服務名稱- source_labels: [__meta_consul_service]# 指定源標簽的正則表達式,若不定義,默認值為"(.*)"regex: consul# 執行動作為刪除,默認值為"replace",有效值有多種# https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_actionaction: drop
檢查配置文件是否正確
[root@prometheus-server31 ~]# cd /app/tools/prometheus-2.53.4.linux-amd64/
[root@prometheus-server31 prometheus-2.53.4.linux-amd64]#
[root@prometheus-server31 prometheus-2.53.4.linux-amd64]# ./promtool check config ./prometheus.yml
Checking ./prometheus.ymlSUCCESS: ./prometheus.yml is valid prometheus config file syntax
重新加載配置
[root@prometheus-server31 ~]# curl -X POST https://10.0.0.31:9090/-/reload -k -u k8s:zhubl
被監控節點注冊到console集群
1 注冊節點
curl -X PUT -d '{"id":"prometheus-node42","name":"prometheus-node42","address":"10.0.0.42","port":9100,"tags":["node-exporter"],"checks": [{"http":"http://10.0.0.42:9100","interval":"5m"}]}' http://10.0.0.43:8500/v1/agent/service/registercurl -X PUT -d '{"id":"prometheus-node43","name":"prometheus-node43","address":"10.0.0.43","port":9100,"tags":["node-exporter"],"checks": [{"http":"http://10.0.0.43:9100","interval":"5m"}]}' http://10.0.0.43:8500/v1/agent/service/register
被監控節點注銷
curl -X PUT http://10.0.0.43:8500/v1/agent/service/deregister/prometheus-node42
TODO—> 目前有個坑
你注冊時找得哪個節點,那么注銷時也要找這個節點注銷,待解決…
🌟Prometheus監控https實戰案例
參考鏈接:
https://prometheus.io/docs/prometheus/2.54/configuration/configuration/#scrape_config
https://prometheus.io/docs/prometheus/2.54/configuration/configuration/#tls_config
修改Prometheus的配置文件
[root@prometheus-server31 ~]# vim /app/tools/prometheus-2.53.4.linux-amd64/prometheus.yml - job_name: "prometheus"# 指定要使用的協議scheme: 'https'# 配置https證書相關信息tls_config:ca_file: certs/ca.crtcert_file: certs/server/prometheus.zhubl.xyz.crtkey_file: certs/server/prometheus.zhubl.xyz.keyinsecure_skip_verify: true# 認證相關的信息basic_auth:username: k8spassword: zhubl
熱加載配置
[root@prometheus-server31 ~]# curl -X POST https://10.0.0.31:9090/-/reload -k -u k8s:zhubl
Prometheus的WebUI驗證
https://10.0.0.31:9090/targets?search=
🌟node-exporter的黑白名單
參考鏈接:
https://github.com/prometheus/node_exporter
停止服務
systemctl stop node-exporter.service
配置黑名單
./node_exporter --no-collector.cpu
配置白名單
./node_exporter --collector.disable-defaults --collector.cpu --collector.uname
相關指標測試
node_cpu_seconds_total----》 cpunode_uname_info----》 uname
🌟Prometheus server實現黑白名單
黑名單
修改配置文件
vim /app/tools/prometheus-2.53.4.linux-amd64/prometheus.yml- job_name: "k8s_exporter"params:exclude[]:- cpu static_configs:- targets: ["10.0.0.42:9100"]
過濾方式
node_cpu_seconds_total{job="k8s_exporter"}
白名單
修改配置文件
vim /app/tools/prometheus-2.53.4.linux-amd64/prometheus.yml- job_name: "dba_exporter"params:collect[]:- unamestatic_configs:- targets: ["10.0.0.41:9100"]
過濾方式
node_uname_info{job="dba_exporter"}
🌟Prometheus的標簽管理
什么是標簽
標簽用于對數據分組和分類,利用標簽可以將數據進行過濾篩選
標簽管理的常見場景
- 刪除不必要的指標
- 從指標中刪除敏感或不需要的標簽
- 添加,編輯或修改指標的標簽值或標簽格式
標簽的分類
默認標簽
Prometheus自身內置的標簽,格式為"__LABLE__"。如上圖所示,典型點如下所示:- "__metrics_path__"- "__address__"- "__scheme__"- "__scrape_interval__"- "__scrape_timeout__"- "instance"- "job"
應用標簽
應用本身內置,尤其是監控特定的服務,會有對應的應用標簽,格式一般為"__LABLE"如下圖所示,以consul服務為例,典型點如下所示:- "__meta_consul_address"- "__meta_consul_dc"- ...
自定義標簽
指的是用戶自定義的標簽,我們在定義targets可以自定義。
標簽主要有兩種表現形式
私有標簽
以"__*“樣式存在,用于獲取監控目標的默認元數據屬性,比如"scheme”,“address”,"metrics_path"等。
普通標簽
對監控指標進行各種靈活管理操作,常見的操作有刪除不必要敏感數據,添加,編輯或修改指標標簽紙或者標簽格式等。
Prometheus對數據處理的流程
- 1.服務發現: 支持靜態發現和動態發現,主要是找打到對應的target。
- 2.配置: 加載"scheme",“address”,"metrics_path"等信息。
- 3.重新標記: relabel_configs,主要針對要監控的target的標簽。
- 4.抓取: 開始抓取數據。
- 5.重新標記: metric_relabel_configs,主要針對已經抓取回來的metrics的標簽的操作。
為targets自定義打標簽案例
vim /app/tools/prometheus-2.53.4.linux-amd64/prometheus.yml- job_name: "node-exporter-lable"static_configs:- targets: ["10.0.0.41:9100","10.0.0.42:9100","10.0.0.43:9100"]labels:auther: zhuhaha: hahahaxixi: xixixi
查看webUI
https://10.0.0.31:9090/targets?search=
🌟relabel_configs替換標簽replace案例
修改prometheus的配置文件
vim /app/tools/prometheus-2.53.4.linux-amd64/prometheus.yml
...- job_name: "node-exporter-relabel_configs"static_configs:- targets: ["10.0.0.41:9100","10.0.0.42:9100","10.0.0.43:9100"]labels:auther: zhublog: zhubaolin.blog.csdn.netrelabel_configs:# 指定正則表達式匹配成功的label進行標簽管理的列表- source_labels:- __scheme__- __address__- __metrics_path__# 表示source_labels對應Label的名稱或值進行匹配此處指定的正則表達式。# 此處我們對數據進行了分組,后面replacement會使用"${1}"和"$2"進行引用。regex: "(http|https)(.*)" # 指定用于連接多個source_labels為一個字符串的分隔符,若不指定,默認為分號";"。# 假設源數據如下:# __address__="10.0.0.31:9100"# __metrics_path__="/metrics"# __scheme__="http"# 拼接后操作的結果為: "http10.0.0.31:9100/metrics"separator: ""# 在進行Label替換的時候,可以將原來的source_labels替換為指定修改后的label。# 將來會新加一個標簽,標簽的名稱為"prometheus_ep",值為replacement的數據。target_label: "prometheus_ep"# 替換標簽時,將target_label對應的值進行修改成此處的值replacement: "${1}://${2}"# 對Label或指標進行管理,場景的動作有replace|keep|drop|lablemap|labeldrop等,默認為replace。action: replace
熱加載配置
curl -X POST https://10.0.0.31:9090/-/reload -k -u k8s:zhubl
webUI驗證
https://10.0.0.31:9090/targets?search=
總結
相對來說,relabel_configs和labels的作用類似,也是為實例打標簽,只不過relabel_configs的功能性更強。我們可以基于標簽來對監控指標進行過濾。
🌟relabel_configs新增標簽映射labelmap案例
修改prometheus的配置文件
vim /app/tools/prometheus-2.53.4.linux-amd64/prometheus.yml- job_name: "node-exporter-relabel_configs-labeldrop"static_configs:- targets: ["10.0.0.31:9100","10.0.0.41:9100","10.0.0.42:9100","10.0.0.43:9100"]relabel_configs:- regex: "(job|app)"replacement: "${1}_labelmap_kubernetes"action: labelmap- regex: "(job|app)"# 刪除regex匹配到的標簽action: labeldrop
熱加載配置
curl -X POST https://10.0.0.31:9090/-/reload -k -u k8s:zhubl
webUI驗證
https://10.0.0.31:9090/targets?search=
🌟metric_relabel_configs修改metric標簽案例
修改prometheus的配置文件
vim /app/tools/prometheus-2.53.4.linux-amd64/prometheus.yml- job_name: "node-exporter-metric_relabel_configs"static_configs:- targets: ["10.0.0.31:9100","10.0.0.41:9100","10.0.0.42:9100","10.0.0.43:9100"]metric_relabel_configs:- source_labels:- __name__regex: "node_cpu_.*"action: drop
熱加載配置
curl -X POST https://10.0.0.31:9090/-/reload -k -u k8s:zhubl
webUI驗證
https://10.0.0.31:9090/targets?search=
🌟部署blackbox-exporter黑盒監控
blackbox-exporter概述
一般用于監控網站是否監控,端口是否存活,證書有效期等。
blackbox exporter支持基于HTTP, HTTPS, DNS, TCP, ICMP, gRPC協議來對目標節點進行監控。
比如基于http協議我們可以探測一個網站的返回狀態碼為200判讀服務是否正常。
比如基于TCP協議我們可以探測一個主機端口是否監聽。
比如基于ICMP協議來ping一個主機的連通性。
比如基于gRPC協議來調用接口并驗證服務是否正常工作。
比如基于DNS協議可以來檢測域名解析。
下載blackbox-exporter
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.27.0/blackbox_exporter-0.27.0.linux-amd64.tar.gz
解壓軟件包
[root@node-exporter43 ~]# tar xf blackbox_exporter-0.27.0.linux-amd64.tar.gz -C /usr/local/
[root@node-exporter43 ~]# cd /usr/local/blackbox_exporter-0.27.0.linux-amd64/
啟動blackbox服務
[root@node-exporter43 blackbox_exporter-0.27.0.linux-amd64]# ./blackbox_exporter
訪問blackbox的WebUI
http://10.0.0.43:9115/
http://10.0.0.43:9115/probe?target=prometheus.io&module=http_2xx
🌟Prometheus server整合blackbox實現網站監控
修改Prometheus的配置文件
vim /app/tools/prometheus-2.53.4.linux-amd64/prometheus.yml# 指定作業的名稱,生成環境中,通常是指一類業務的分組配置。- job_name: 'blackbox-exporter-http'# 修改訪問路徑,若不修改,默認值為"/metrics"metrics_path: /probe# 配置URL的相關參數params:# 此處表示使用的是blackbox的http模塊,從而判斷相應的返回狀態碼是否為200module: [http_2xx]# 靜態配置,需要手動指定監控目標static_configs:# 需要監控的目標- targets:# 支持https協議- https://www.zhubl.xyz/# 支持http協議- http://10.0.0.41# 支持http協議和自定義端口- 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.45/configuration/configuration/# 將"__address__"傳遞給target字段。action: replace- source_labels: [__param_target]target_label: instance- target_label: __address__# 指定要替換的值,此處我指定為blackbox exporter的主機地址replacement: 10.0.0.43:9115
熱加載配置
curl -X POST https://10.0.0.31:9090/-/reload -k -u k8s:zhubl
驗證webUI
https://10.0.0.31:9090/targets?search=
導入grafana的模板ID
7587
13659
🌟prometheus基于blackbox的ICMP監控目標主機是否存活
修改Prometheus配置文件
vim /app/tools/prometheus-2.53.4.linux-amd64/prometheus.yml- job_name: 'blackbox-exporter-icmp'metrics_path: /probeparams:# 如果不指定模塊,則默認類型為"http_2xx",不能亂寫!亂寫監控不到服務啦!module: [icmp]static_configs:- targets:- 10.0.0.41- 10.0.0.42relabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]# 指定注意的是,如果instance不修改,則instance和"__address__"的值相同# target_label: iptarget_label: instance- target_label: __address__replacement: 10.0.0.43:9115
檢查配置文件是否正確
cd /app/tools/prometheus-2.53.4.linux-amd64/
./promtool check config prometheus.yml
熱加載配置
curl -X POST https://10.0.0.31:9090/-/reload -k -u k8s:zhubl
訪問prometheus的WebUI
http://10.0.0.31:9090/targets
訪問blackbox的WebUI
http://10.0.0.41:9115/
grafana過濾jobs數據
基于"blackbox-exporter-icmp"標簽進行過濾。
🌟prometheus基于blackbox的TCP案例監控端口是否存活
修改Prometheus配置文件
vim /app/tools/prometheus-2.53.4.linux-amd64/prometheus.yml- job_name: 'blackbox-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.43:9115
檢查配置文件是否正確
./promtool check config prometheus.yml
熱加載配置
curl -X POST https://10.0.0.31:9090/-/reload -k -u k8s:zhubl
訪問prometheus的WebUI
http://10.0.0.31:9090/targets
訪問blackbox exporter的WebUI
http://10.0.0.41:9115/
使用grafana查看數據
基于"blackbox-exporter-tcp"標簽進行過濾。
🌟pushgateway組件實現自定義監控直播人數案例
什么是pushgateway
說白了,就是Prometheus官方用來短期自定義監控指標。
如果長期監控的組件建議運維開發人員編寫相應的exporters。
部署pushgateway
wget https://github.com/prometheus/pushgateway/releases/download/v1.11.1/pushgateway-1.11.1.linux-amd64.tar.gz
解壓軟件包
[root@node-exporter42 ~]# tar xf pushgateway-1.11.1.linux-amd64.tar.gz -C /usr/local/bin/ pushgateway-1.11.1.linux-amd64/pushgateway --strip-components=1
運行pushgateway
[root@node-exporter42 ~]# pushgateway --web.telemetry-path="/metrics" --web.listen-address=:9091 --persistence.file=/data/pushgateway.data
訪問pushgateway的WebUI
http://10.0.0.42:9091/#
模擬直播在線人數統計
使用curl工具推送測試數據pushgateway
[root@node-exporter43 ~]# echo "zhibo_online 35" | curl --data-binary @- http://10.0.0.42:9091/metrics/job/zhibo/instance/10.0.0.43
pushgateway查詢數據是否上傳成功
[root@node-exporter43 ~]# curl -s http://10.0.0.42:9091/metrics | grep zhibo_online
Prometheus server監控pushgateway
修改prometheus的配置文件
vim /app/tools/prometheus-2.53.4.linux-amd64/prometheus.yml- job_name: "pushgateway"# 采集數據標簽沖突是,遠程的標簽會覆蓋本地Prometheus server的標簽。# 默認值為false,有沖突時,則會使用源標簽前加一個"exported_*"的前綴標簽。honor_labels: truestatic_configs:- targets: - 10.0.0.42:9091
熱加載配置
[root@prometheus-server31 ~]# curl -X POST https://10.0.0.31:9090/-/reload -k -u k8s:zhubl
驗證配置是否生效
http://10.0.0.31:9090/targets?search=
查詢特定指標
zhibo_online
Grafana出圖展示
模擬直播間人數的變化
[root@node-exporter41 ~]# echo "zhibo_online $RANDOM" | curl --data-binary @- http://10.0.0.42:9091/metrics/job/zhibo/instance/10.0.0.43[root@node-exporter41 ~]# echo "zhibo_online $RANDOM" | curl --data-binary @- http://10.0.0.42:9091/metrics/job/zhibo/instance/10.0.0.43
🌟使用pushgateway監控TCP的十二種狀態
編寫監控腳本
#!/bin/bash
pushgateway_url="http://10.0.0.42:9091/metrics/job/tcp_status"state="SYN-SENT SYN-RECV FIN-WAIT-1 FIN-WAIT-2 TIME-WAIT CLOSE CLOSE-WAIT LAST-ACK LISTEN CLOSING ESTAB UNKNOWN"for i in $statedocount=`ss -tan |grep $i |wc -l`echo tcp_connections{state=\""$i"\"} $count >> /tmp/tcp.txt
done;cat /tmp/tcp.txt | curl --data-binary @- $pushgateway_url
rm -rf /tmp/tcp.txt
調用腳本
bash /usr/local/bin/tcp_status.sh
Prometheus查詢數據
tcp_connections
Grafana出圖展示
自定義變量
添加儀表盤
tcp_connections{state="$state"}
🌟網絡的丟包率監控
編寫監控腳本
#!/bin/bash
host="www.zhubl.xyz"loss=`ping $host -c 10 | grep packet | awk '{print $6}' | tr -d '%'`echo loss_packet $loss | curl --data-binary @- http://10.0.0.42:9091/metrics/job/loss_packet/instance/10.0.0.41
發送測試數據
bash /usr/local/bin/loss_packet.sh
Grafana出圖展示
🌟監控ES集群索引數量
編寫監控腳本
#!/bin/bashpushgateway_url="http://10.0.0.42:9091/metrics/job/indices"indices=`curl -s https://10.0.0.91:9200/_cat/indices?v -u elastic:123456 -k | wc -l`echo indices_num $indices | curl --data-binary @- $pushgateway_url
發送數據
bash /usr/local/bin/es_index_num.sh