一、Pushgateway 是什么
Pushgateway 在 Prometheus 体系中是一个指标缓存中间件,可以用来接收那些生命周期太短、来不及被 Prometheus 拉取的批处理任务或临时任务的指标,也可以接收自定义监控指标。然后再由 Prometheus 从 Pushgateway 拉取。
生产环境最常用的推送方式有两种:
-
curl 直接推送:最轻量,适合 cron 定时任务、一次性脚本;
-
官方客户端库(Go、Python、Java 等)集成推送。
官网:
二、测试环境说明
| 主机 | 作用 | 描述 |
|---|---|---|
| 10.0.0.51 | prometheus-server51 | 部署了 prometheus-server |
| 10.0.0.61 | node61 / grafana / pushgateway | 部署了 node-exporter 作为客户端;部署了 grafana(机器充足时可单独部署);部署了 pushgateway |
三、部署pushgateway
1.下载
方式一、官网下载再上传到服务器
https://prometheus.io/download/#pushgateway
方式二、wget下载
root@node61 \~# wget https://github.com/prometheus/pushgateway/releases/download/v1.11.4/pushgateway-1.11.4.linux-amd64.tar.gz
2.解压安装
tar xf pushgateway-1.11.4.linux-amd64.tar.gz -C /usr/local/bin/ --strip-components=1
3.运行
root@node61 \~# pushgateway --web.telemetry-path="/metrics" --web.listen-address=:9091 --persistence.file=/data/pushgateway/pushgateway.data
--persistence.file=/data/pushgateway/pushgateway.data 的意思是:指定 Pushgateway 将指标数据持久化保存到磁盘上的哪个文件。这样即使Pushgateway 服务重启,之前推送过来的指标也不会丢失。
不指定该参数时 :Pushgateway 默认只把指标保存在内存里。一旦进程停止或服务器重启,所有已推送的指标数据都会消失。
4.访问WEBUI

推送上来的指标数据,访问路径

四、推送自定义数据到pushgateway
生产上常用的推送方式
1. 命令行 / 脚本(curl) 最轻量,适合 cron 定时任务、一次性脚本。直接通过 HTTP 把文本格式的指标 POST 上去。
案例1(自定义内存使用率指标)
自定义一个指标:node_memory_usage_percent(把服务器的内存使用率取出来作为这个指标的value)
把当前服务器的内存使用率指标推送到pushgateway,通过grafana自定义展示
一、定义node_memory_usage_percent指标
root@prometheus-server51 prometheus-2.53.2.linux-amd64# # 1. 从 /proc/meminfo 读取关键数值(单位:kB)
mem_total=(awk '/MemTotal/ {print 2}' /proc/meminfo)
mem_available=(awk '/MemAvailable/ {print 2}' /proc/meminfo)
2. 计算使用率百分比,保留两位小数
公式:(总内存 - 可用内存) / 总内存 * 100
mem_usage_percent=(awk -v total="mem_total" -v avail="$mem_available" \
'BEGIN {printf "%.2f", (total - avail) / total * 100}')
验证自定义指标
root@prometheus-server51 prometheus-2.53.2.linux-amd64# echo ${mem_usage_percent}
32.37
二、通过curl推送到pushgateway
echo "node_memory_usage_percent ${mem_usage_percent}" | \
curl --data-binary @- http://10.0.0.61:9091/metrics/job/pushgateway-job/instance/10.0.0.51

pushgateway WEBUI验证

Metrcis验证

三、prometheus配置pushgateway的job作业
1.prometheus.yml配置文件
scrape_configs:
- job_name: "pushgateway"
honor_labels: true
params:
static_configs:
targets:
10.0.0.61:9091

honor_labels: true 的作用是解决标签冲突。
当 Prometheus 抓取 Pushgateway 时,Pushgateway 上的指标自带了你在推送时指定的 job="pushgateway-job" 和 instance="10.0.0.51"。如果不开这个参数,Prometheus 会用抓取配置里的 job="pushgateway" 和 instance="10.0.0.61:9091" 覆盖它们,并把原来的名字改成 exported_job 和 exported_instance。
honor_labels: false时
源标签被覆盖

honor_labels: true时
源标签保留

2.热加载
3.prometheus WEBUI验证是否采集到自定义指标

四、自定义dashboard展示内存使用率
如何自定义dashboard推荐阅读:
告别硬编码 Query!Grafana 变量实现一套监控看板多主机复用(14)-CSDN博客
Grafana 自定义 Dashboard 实战案例 + 看板备份恢复完整指南(15)-CSDN博客

案例2.监控网络的丢包率
一、定义自定义指标:丢包率loss_packget+推送到pushgateway
我们可以写一个脚本,将丢包率指标取出来赋值给一个自定义变量:loss_packget
,然后用crontab定时调用这个脚本
1.取出监控指标:loss_packget
root@prometheus-server51 pushgateway# cat /root/pushgateway/loss_packet.sh
#!/bin/bash
host="www.baidu.com"
loss=`ping host -c 10 \| grep packet \| awk '{print 6}' | tr -d '%'`
echo loss_packet $loss | curl --data-binary @- http://10.0.0.61:9091/metrics/job/loss_packet/instance/10.0.0.51
root@prometheus-server51 pushgateway#

2.crontab定时执行(自己根据需要定时调用脚本即可)
二、pushgateway接入prometheus
1.prometheus.yml
A scrape configuration containing exactly one endpoint to scrape:
Here it's Prometheus itself.
scrape_configs:
- job_name: "pushgateway"
honor_labels: true
static_configs:
targets:
10.0.0.61:9091

2.热加载
3.prometheus WEBUI验证是否采集到loss_packget指标

三、自定义dashboard展示丢失率:loss_packget
如何自定义dashboard推荐阅读:
告别硬编码 Query!Grafana 变量实现一套监控看板多主机复用(14)-CSDN博客
Grafana 自定义 Dashboard 实战案例 + 看板备份恢复完整指南(15)-CSDN博客

本文来自csdn,作者JasonChen陈杰,当你迷茫的时候,想一想自己到底想要什么样的生活。