16.Pushgateway 实战:如何实现自定义监控数据的采集与展示?

本文详解Pushgateway在Prometheus生态中的作用,涵盖部署、持久化配置及通过curl推送自定义指标(如node_memory_usage_percent和loss_packet)的完整流程;介绍Prometheus如何通过honor_labels=true正确采集带标签的推送数据,并结合Grafana实现可视化展示,适用于短生命周期任务与临时脚本的监控场景。

 一、Pushgateway 是什么

Pushgateway 在 Prometheus 体系中是一个指标缓存中间件,可以用来接收那些生命周期太短、来不及被 Prometheus 拉取的批处理任务或临时任务的指标,也可以接收自定义监控指标。然后再由 Prometheus 从 Pushgateway 拉取。

生产环境最常用的推送方式有两种:

  • curl 直接推送:最轻量,适合 cron 定时任务、一次性脚本;

  • 官方客户端库(Go、Python、Java 等)集成推送。

官网:

Download | Prometheus

二、测试环境说明

主机作用描述
10.0.0.51prometheus-server51部署了 prometheus-server
10.0.0.61node61 / grafana / pushgateway部署了 node-exporter 作为客户端;部署了 grafana(机器充足时可单独部署);部署了 pushgateway

三、部署pushgateway

1.下载

方式一、官网下载再上传到服务器

Download | Prometheus

方式二、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

http://10.0.0.61:9091/#

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

http://10.0.0.61:9091/metrics

四、推送自定义数据到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验证

http://10.0.0.61:9091/metrics

三、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推荐阅读:

14.Grafana 监控看板实战:新建、调优、变量配置全流程-CSDN博客

15.Prometheus+Grafana:主机监控看板制作及备份恢复实操-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指标

http://10.0.0.51:9090/graph

三、自定义dashboard展示丢失率:loss_packget

 ​​​​​如何自定义dashboard推荐阅读:

14.Grafana 监控看板实战:新建、调优、变量配置全流程-CSDN博客

15.Prometheus+Grafana:主机监控看板制作及备份恢复实操-CSDN博客

本文作者吴玉桦,后续会结合企业生产场景,持续更新所有专题,敬请关注。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值