如何通过pushgateway自定义监控

prometheus采用定时pull的模式,某些情况下(如防火墙或者子网不同),不能直接拉取各个Target的指标数据,此时可以采用各个Target往PushGateway上Push数据,然后Prometheus去PushGateway上定时pull

 

下载

pushgateway

https://github.com/prometheus/pushgateway/releases

解压

tar -zxvf pushgateway-1.7.0.linux-amd64.tar.gz

 

更改目录名

mv pushgateway-1.7.0.linux-amd64 pushgateway

 

systemctl方式运行

vim /usr/lib/systemd/system/pushgateway.service

粘贴下面内容

[Unit]
Description=pushgateway service
After=network-online.target

[Service]
Type=simple
ExecStart=/usr/local/pushgateway/pushgateway
Restart=on-failue
ExecStop=/bin/kill -9 $MAINPID

[Install]
WantedBy=multi-user.target

 

加载systemctl 服务

systemctl daemon-reload

启动

systemctl start pushgateway.service

查看状态

systemctl start pushgateway.service

 

整合到prometheus target上

- job_name: "pushgateway"
static_configs:
- targets: ["192.168.242.134:9091"]

 

使用和pushgateway同网段的机器来采集pushgateway上的数据

vim testdata

# HELP http_request__total total request.
# TYPE http_request_total counter
http_request_respone_time{code="200",label="nginx"} 276
# HELP http_request_respone_time http request respone time.
# TYPE http_request_respone_time gauge
http_request_respone_time{code="200",label="nginx"} 0.122

 

curl -XPOST --data-binary @testdata http://192.168.242.132:9091/metrics/job/nginx/instance/httpreq

pushgateway的metrics中可以看到数据已经接入2成功

http://192.168.242.132:9091/metrics

 

SDK

方式pull数据到pushgateway上

使用python3来编写SDK

安装prometheus-client

pip3 install prometheus-client

 

 

编写SDK文件

from prometheus_client import Counter
from prometheus_client import Gauge,push_to_gateway
from prometheus_client.core import CollectorRegistry

if __name__ == '__main__':
registry = CollectorRegistry()
labels = ['method','path','instance']
data1 = Gauge('pushgateway_python_gauge_metric','This is a pushgateway_python_gauge_metric',labels,registry=registry)
data1.labels(method='get',path='/mymetric',instance='nginx').inc(3)
push_to_gateway('192.168.242.132:9091', job='nginx-test-metric',registry=registry)

python3执行该文件

python3 ./test.py

 

posted @ 2024-03-06 18:23  ~技术小白  阅读(46)  评论(0)    收藏  举报