如何通过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 @   ~技术小白  阅读(24)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示