【转】prometheus数据上报方式pushgateway
原文:https://www.cnblogs.com/xiaobaozi-95/p/10684524.html
-------------------------
pushgateway
客戶(hù)端使用push的方式上報(bào)監(jiān)控?cái)?shù)據(jù)到pushgateway,prometheus會(huì)定期從pushgateway拉取數(shù)據(jù)。使用它的原因主要是:
Prometheus 采用 pull 模式,可能由于不在一個(gè)子網(wǎng)或者防火墻原因,導(dǎo)致Prometheus 無(wú)法直接拉取各個(gè) target數(shù)據(jù)。
在監(jiān)控業(yè)務(wù)數(shù)據(jù)的時(shí)候,需要將不同數(shù)據(jù)匯總, 由 Prometheus 統(tǒng)一收集。
拓?fù)鋱D
pushgateway安裝
# wget https://github.com/prometheus/pushgateway/releases/download/v0.7.0/pushgateway-0.7.0.linux-amd64.tar.gz
# tar xzvf pushgateway-0.7.0.linux-amd64.tar.gz
# mv pushgateway-0.7.0.linux-amd64 /usr/local/pushgateway
運(yùn)行
# ./pushgateway
#curl localhost:9091/metrics #訪(fǎng)問(wèn) 127.0.0.1:9091/metrics可以看到指標(biāo)
# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 1.8299e-05
go_gc_duration_seconds{quantile="0.25"} 1.8299e-05
go_gc_duration_seconds{quantile="0.5"} 2.8353e-05
go_gc_duration_seconds{quantile="0.75"} 2.8353e-05
...
prometheus.yml添加target
- job_name: 'push-metrics'
static_configs:
- targets: ['localhost:9091']
honor_labels: true
# 因?yàn)閜rometheus配置pushgateway 的時(shí)候,也會(huì)指定job和instance,但是它只表示pushgateway實(shí)例,不能真正表達(dá)收集數(shù)據(jù)的含義。所以配置pushgateway需要添加honor_labels:true,避免收集數(shù)據(jù)本身的job和instance被覆蓋。
注意:為了防止 pushgateway 重啟或意外掛掉,導(dǎo)致數(shù)據(jù)丟失,可以通過(guò) -persistence.file 和 -persistence.interval 參數(shù)將數(shù)據(jù)持久化下來(lái)。
push數(shù)據(jù)到pushgateway
推送URL :pushgateway默認(rèn)采用9091端口,路徑:/metrics/job/<JOBNAME>{/<LABEL_NAME>/<LABEL_VALUE>}
<JOBNAME>是job標(biāo)簽的值,后面跟任意數(shù)量的標(biāo)簽對(duì),instance標(biāo)簽可以有也可以沒(méi)有。
示例:
# echo "test_metric 2333" | curl --data-binary @- http://127.0.0.1:9091/metrics/job/test_job
推送之后可以看到
# TYPE test_metric untyped
test_metric{instance="",job="test_job"} 2333
推送一個(gè)更復(fù)雜的
# cat <<EOF | curl --data-binary @- http://127.0.0.1:9091/metrics/job/test_job/instance/test_instance
# TYPE test_metric counter
test_metric{label="val1"} 100
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 123.45
EOF
或者是先把指標(biāo)數(shù)據(jù)寫(xiě)入文件
# curl -XPOST --data-binary @data.txt http://127.0.0.1:9091/metrics/job/nginx/instance/172.27.0.3
http_request_total{code="200",domain="abc.cn"} 276683
http_request_total{code="400",domain="abc.cn"} 0
http_request_total{code="408",domain="abc.cn"} 7
http_request_total{code="401",domain="abc.cn"} 0
http_request_total{schema="http",domain="abc.cn"} 277725
http_request_total{schema="https",domain="abc.cn"} 0
http_request_time{code="total",domain="abc.cn"} 76335.809000
http_request_uniqip{domain="abc.cn"} 216944
http_request_maxip{clientip="172.27.0.12",domain="abc.cn"} 81
刪除指標(biāo):
# curl -X DELETE http://127.0.0.1:9091/metrics/job/test_job
#說(shuō)明: 刪除標(biāo)識(shí)的組中的所有指標(biāo){job="some_job"}(請(qǐng)注意,這不包括{job="some_job",instance="some_instance"}中的指標(biāo),即使這些指標(biāo)具有相同的 job 標(biāo)簽
# curl -X DELETE http://127.0.0.1:9091/metrics/job/test_job/instance/test_instance
也可以在界面上刪除
也可以通過(guò)使用官方給的python library,使用push 方式把數(shù)據(jù)推送到pushgateway。
# cat client.py
#!/usr/bin/python3
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
registry = CollectorRegistry()
g = Gauge('ping', '檢測(cè)最大響應(yīng)時(shí)間',['dst_ip','city'], registry=registry) #Guage(metric_name,HELP,labels_name,registry=registry)
g.labels('192.168.1.10','shenzhen').set(42.2) #set設(shè)定值
g.labels('192.168.1.11','shenzhen').dec(2) #dec遞減2
g.labels('192.168.1.12','shenzhen').inc() #inc遞增,默認(rèn)增1
push_to_gateway('localhost:9091', job='ping_status', registry=registry)
查看
需要注意:使用這種方法,如果使用相同的job名 ,后面插入的數(shù)據(jù)會(huì)覆蓋掉之前的。
例如,job 都叫 ping_status
#!/usr/bin/env python3
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
registry = CollectorRegistry()
g = Gauge('test_metrics', '描述信息',['label_name'], registry=registry)
g.labels('label1').set(2)
push_to_gateway('localhost:9091', job='ping_status', registry=registry)
執(zhí)行腳本,查看結(jié)果,可以看到之前的數(shù)據(jù)已經(jīng)不存在了:
要?jiǎng)h除這條數(shù)據(jù) ,可以直接使用:curl -XDELETE 'http://localhost:9091/metrics/job/ping_status'
總結(jié)
以上是生活随笔為你收集整理的【转】prometheus数据上报方式pushgateway的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 人生感悟 --是人才就不要等着老板来安
- 下一篇: 常见网络命令之Ping命令