Promethues與Gateway模式對接

客戶端使用push的方式上報監控資料到pushgateway,prometheus會定期從pushgateway拉取資料。使用它的原因主要是:

1、Prometheus 採用 pull 模式,可能由於不在一個子網或者防火牆原因,導致Prometheus 無法直接拉取各個 target資料。

2、在監控業務資料的時候,需要將不同資料彙總, 由 Prometheus 統一收集。

Promethues與Gateway模式對接

一、部署 pushgateway

安裝pushgateway

wget https://github。com/prometheus/pushgateway/releases/download/v1。1。0/pushgateway-1。1。0。linux-amd64。tar。gztar zxvf pushgateway-1。1。0。linux-amd64。tar。gzmv pushgateway-1。1。0。linux-amd64 /usr/local/exporter/pushgateway

pushgateway啟動

nohup /usr/local/exporter/pushgateway/pushgateway \——web。enable-admin-api \——persistence。file=“pushfile。txt” \——persistence。interval=10m &

注意:為了防止 pushgateway 重啟或意外掛掉,導致資料丟失,可以透過 -persistence。file 和 -persistence。interval 引數將資料持久化下來。

訪問WEB curl http://127。0。0。1:9091/metrics

[root@Prometheus pushgateway]# curl http://127。0。0。1:9091/metrics# HELP go_gc_duration_seconds A summary of the GC invocation durations。# TYPE go_gc_duration_seconds summarygo_gc_duration_seconds{quantile=“0”} 0go_gc_duration_seconds{quantile=“0。25”} 0go_gc_duration_seconds{quantile=“0。5”} 0go_gc_duration_seconds{quantile=“0。75”} 0go_gc_duration_seconds{quantile=“1”} 0go_gc_duration_seconds_sum 0go_gc_duration_seconds_count 0# HELP go_goroutines Number of goroutines that currently exist。# TYPE go_goroutines gaugego_goroutines 10# HELP go_info Information about the Go environment。# TYPE go_info gaugego_info{version=“go1。13。6”} 1# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use。# TYPE go_memstats_alloc_bytes gaugego_memstats_alloc_bytes 2。859168e+06# HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed。# TYPE go_memstats_alloc_bytes_total countergo_memstats_alloc_bytes_total 2。859168e+06# HELP go_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table。# TYPE go_memstats_buck_hash_sys_bytes gaugego_memstats_buck_hash_sys_bytes 3037# HELP go_memstats_frees_total Total number of frees。# TYPE go_memstats_frees_total countergo_memstats_frees_total 402

Promethues與Gateway模式對接

Promethues與Gateway模式對接

二 prometheus 採集pushgateway資料

修改配置檔案 vim /usr/local/prometheus/prometheus。yml

- job_name: ‘pushgateway’ static_configs: - targets: [‘114。67。116。119:9091’] honor_labels: true

因為prometheus配置pushgateway 的時候,也會指定job和instance,但是它只表示pushgateway例項,不能真正表達收集資料的含義。所以配置pushgateway需要新增honor_labels:true,避免收集資料本身的job和instance被覆蓋。

重啟動配置服務 kill -hup pid

Promethues與Gateway模式對接

三、push資料到pushgateway

推送URL :pushgateway預設採用9091埠,路徑:/metrics/job/{//}是job標籤的值,後面跟任意數量的標籤對,instance標籤可以有也可以沒有。

1、push資料型別1

echo “test_metric 2333” | curl ——data-binary @- http://127。0。0。1:9091/metrics/job/test_job

2、push資料型別2

vim push_memory。sh#!/bin/bash # desc push memory info total_memory=$(free |awk ‘/Mem/{print $2}’)used_memory=$(free |awk ‘/Mem/{print $3}’)job_name=“custom_memory”instance_name=“10。0。0。11”cat <

3、push資料型別3, 用檔案匯入資料

curl -XPOST ——data-binary @data。txt http://127。0。0。1:9091/metrics/job/nginx/instance/114。67。116。119

vim data。txt

http_request_total{code=“200”,domain=“abc。cn”} 276683http_request_total{code=“400”,domain=“abc。cn”} 0http_request_total{code=“408”,domain=“abc。cn”} 7http_request_total{code=“401”,domain=“abc。cn”} 0http_request_total{schema=“http”,domain=“abc。cn”} 277725http_request_total{schema=“https”,domain=“abc。cn”} 0http_request_time{code=“total”,domain=“abc。cn”} 76335。809000http_request_uniqip{domain=“abc。cn”} 216944http_request_maxip{clientip=“114。67。116。119”,domain=“abc。cn”} 81

4、刪除指標:curl -X DELETE http://127。0。0。1:9091/metrics/job/test_job 也可以在web介面進行刪除

Promethues與Gateway模式對接

四、透過使用官方給的python library,使用push 方式把資料推送到pushgateway。

安裝prometheus_client

/usr/local/python3。6/bin/pip install ——upgrade pip/usr/local/python3。6/bin/pip install prometheus_client/usr/local/python3。6/bin/pip list

程式檔案client。py cat client。py

#!/usr/local/python3。6/bin/python3from prometheus_client import CollectorRegistry, Gauge, push_to_gatewayregistry = CollectorRegistry()g = Gauge(‘ping’, ‘檢測最大響應時間’,[‘dst_ip’,‘city’], registry=registry) #Guage(metric_name,HELP,labels_name,registry=registry)g。labels(‘10。0。0。5’,‘shenzhen’)。set(42。2) #set設定值g。labels(‘10。0。0。11’,‘shenzhen’)。dec(2) #dec遞減2g。labels(‘10。0。0。5’,‘shenzhen’)。inc() #inc遞增,預設增1push_to_gateway(‘localhost:9091’, job=‘ping_status’, registry=registry)

執行檔案 /usr/local/python3。6/bin/python3 client。py

介面

Promethues與Gateway模式對接

Promethues與Gateway模式對接