【基于 Pushgateway 的 Prometheus 自定义监控实践指南】 提示本文原创作品良心制作干货为主简洁清晰一看就会文章目录前言一、Pushgateway简介1.1 pushgateway是什么1.2 工作原理二、Pushgateway安装使用2.1 环境介绍2.2 Pushgateway安装配置2.3 向pushgateway推送数据使用curl向pushgateway推送监控数据使用python向pushgateway推送监控数据2.4 pushgateway实例用shell处理用python处理2.5 设置警告2.6 grafana添加dashboard前言Prometheus默认以拉取模式采集监控数据面对批量任务、临时脚本、异构业务等无法主动暴露指标的场景采集存在局限。PushGateway作为推送式网关可承接各类自定义指标上报补齐拉取模式短板。本文围绕PushGateway展开讲解自定义监控搭建流程实操指标推送、数据查看与告警配置助力灵活适配多样化业务监控需求一、Pushgateway简介1.1 pushgateway是什么PushGateway是Prometheus生态的推送网关接收业务主动推送指标再由Prometheus拉取存储适配无法暴露指标接口的监控场景使用它的主要原因是Prometheus采用pull模式可能由于不在一个子网或者防火墙原因导致Prometheus无法直接拉取各个target数据在监控业务数据的时候需要将不同数据汇总由Prometheus统一收集当exporter不能满足需要时也可以通过自定义pythonjavashell监控我们想要的数据使用它会产生的弊端存在单节点故障如果多个监控目标的数据都汇总到同一个pushgateway它就成了整个监控链路的单点故障源监控健康状态盲区Prometheus拉取状态up只针对pushgateway无法做到对每个节点有效数据残留引发的“僵尸指标”pushgateway会默认永久存储所有推送过的指标序列除非通过api主动删除当一个任务实例运行结束后它的指标数据依然残留在pushgateway中形成误导性的“僵尸指标”1.2 工作原理业务程序/脚本主动将指标POST推送到网关网关临时缓存上报数据Prometheus按配置周期主动拉取网关内指标入库二、Pushgateway安装使用2.1 环境介绍主机名ip地址服务备注prometheus192.168.13.141docker、docker-compose、prometheus、alertmanager、grafana监控端已安装关于监控端的服务我已经安装好了prometheus有两种安装方式二进制安装和docker安装本次实验使用的容器安装的后续被监控端我也统一使用容器部署大家可以自行选择关于监控端的服务如何安装这里不在赘述有不懂的同学可以查看此篇文章Prometheus二进制安装https://blog.csdn.net/m0_63756214/article/details/161196428?spm1001.2014.3001.5501Prometheus容器安装https://blog.csdn.net/m0_63756214/article/details/161225636?spm1001.2014.3001.55012.2 Pushgateway安装配置rootprometheus:~# mkdir /opt/prometheus/pushgatewayrootprometheus:~# cd /opt/prometheus/pushgatewayrootprometheus:/opt/prometheus/pushgateway# vim docker-compose.yamlversion:3.8services:pushgateway:image:prom/pushgatewaycontainer_name:pushgatewayrestart:alwaysexpose:-9091ports:-9091:9091rootprometheus:/opt/prometheus/pushgateway# docker-compose up -drootprometheus:~# vim /opt/prometheus/prometheus/prometheus.ymlscrape_configs:# 新增job-job_name:pushgatewayscrape_interval:15s# 保留推送过来的原始标签不被 Prometheus 自动覆盖honor_labels:truestatic_configs:-targets:[192.168.13.141:9091]labels:instance:pushgateway rootprometheus:~# curl -X POST http://localhost:9090/-/reload2.3 向pushgateway推送数据使用curl向pushgateway推送监控数据# 推送单条数据rootprometheus:~# echo test_data 2026 | curl --data-binary - http://192.168.13.141:9091/metrics/job/test浏览器访问192.168.13.141:9090# 删除单条数据rootprometheus:~# curl -X DELETE http://192.168.13.141:9091/metrics/job/test# 推送多条数据rootprometheus:~# cat eof | curl --data-binary - http://192.168.13.141:9091/metrics/job/test_job/instance/test_instancesome_metric{lableval}42 another_metrics 11.1 eofrootprometheus:~# curl -X DELETE http://192.168.13.141:9091/metrics/job/test_job使用python向pushgateway推送监控数据rootprometheus:~# apt -t install python3-piprootprometheus:~# pip3 install prometheus_clientrootprometheus:~# vim push.py# 导入依赖包用来创建指标注册指标推送到网关from prometheus_client import CollectorRegistry,Gauge,push_to_gateway# 创建一个“指标注册表”registry CollectorRegistry()# 创建一个Gauge指标g Gauge(job_last_success_unixtime,Last time a batch job successfully finished,registryregistry)# 把指标值设置为当前时间g.set_to_current_time()# 推送到 PushGatewaypush_to_gateway(192.168.13.141:9091,jobbatchA,registryregistry) rootprometheus:~# python3 push.py2.4 pushgateway实例目前需求监控/opt目录下的文件数量rootprometheus:~# ls /opt/containerd prometheus rootprometheus:~# ls -l /opt/ | sed 1d | wc -l2用shell处理rootprometheus:~# vim shell.sh#!/bin/bashfilenumls-l /opt|sed 1d|wc-l echo opt_file_num ${filenum}|curl--data-binary -http://192.168.13.141:9091/metrics/job/filenum/instance/opt_filename rootprometheus:~# crontab -e*/1* * * * bash /root/shell.sh/dev/null 21rootprometheus:~# touch /opt/1.txt用python处理rootprometheus:~# vim python.pyfrom prometheus_client import CollectorRegistry,Gauge,push_to_gateway import os path /opt# 输入文件夹地址files os.listdir(path)# 读入文件夹num_png len(files)# 统计文件夹中的文件个数registry CollectorRegistry() g Gauge(python_opt_file_num,opt file num,[instance],registryregistry) g.labels(test).set(num_png) push_to_gateway(192.168.13.141:9091,jobtest_job,registryregistry) rootprometheus:~# crontab -e*/1* * * * /usr/bin/python3 /root/python.py/dev/null 21rootprometheus:~#rootprometheus:~# touch /opt/2.txt2.5 设置警告rootprometheus:~# vim /opt/prometheus/prometheus/rules/pushgateway.ymlgroups:-name:pushgatewayrules:-alert:DataFileNumexpr:python_opt_file_num5for:0mlabels:severity:warningannotations:summary:opt数据目录文件数过多description:opt数据目录文件数5,当前数量:{{ $value }}rootprometheus:~# curl -X POST http://localhost:9090/-/reloadrootprometheus:~# touch /opt/3.txtrootprometheus:~# touch /opt/4.txt可以看到文件数量超过5已经触发了告警2.6 grafana添加dashboard登录grafana给刚才的指标添加一个dashboard注文中若有疏漏欢迎大家指正赐教。本文为100%原创转载请务必标注原创作者尊重劳动成果。求赞、求关注、求评论你的支持是我更新的最大动力评论区等你