Prometheus监控系统(8)PushGateway的使用

TangLu 系统运维 2025-02-06 17812 0

一、PushGateway 介绍

在 Prometheus 的体系结构中,需要依赖 Pushgateway 对一些生命周期较短(生命周期较短是指还未达到 Prometheus 发起探测时任务就结束了)或者无法被 Prometheus 直接拉取到指标的对象(无法暴露 HTTP 端点让 Prometheus 拉取数据的服务,如一些嵌入式设备、防火墙等)进行监控。通过把 Pushgateway 作为中转站的方式,接受并缓存这些监控对象的数据,然后让 Prometheus 再从中获取到监控数据


二、PushGateway 安装与配置

1、下载PushGateway

访问官网下载页面https://prometheus.io/download/可进行下载

wget https://github.com/prometheus/pushgateway/releases/download/v1.11.0/pushgateway-1.11.0.linux-amd64.tar.gz


2、PushGateway解压后就可以直接运行,默认监听在9091端口

./pushgateway


3、将PushGateway进行服务注册

· --persistence.file:指定 PushGateway 持久化数据存储的文件路径,重启时从该文件恢复数据

· --persistence.interval:定义 PushGateway 进行持久化操作的间隔

vi /etc/systemd/system/pushgateway.service

[Unit]
Description=Prometheus Pushgateway
After=network.target

[Service]
ExecStart=/usr/local/bin/pushgateway --web.listen-address=:9091 --persistence.file=/var/lib/pushgateway/pushgateway.db --persistence.interval=5m
ExecStop=/bin/kill -s SIGTERM $MAINPID
Restart=always
RestartSec=10s
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target


#启动服务
systemctl daemon-reload
systemctl enable pushgateway
systemctl start pushgateway


4、修改Prometheus的配置文件,定义PushGateway的job

- job_name: 'pushgateway'
    static_configs:
      - targets: ['192.168.159.101:9091']  #PushGateway的地址


5、重新加载Prometheus服务

curl -X POST http://192.168.159.101:9090/-/reload


6、手动推送数据到PushGateway测试

推送一条名为test_metric的指标,值为1,并将它推送到 Pushgateway  的test_job 中

echo "test 1" | curl --data-binary @- http://localhost:9091/metrics/job/test_job


7、查看PushGateway数据


8、在Prometheus界面使用推送的指标查看数据


7、自定义脚本采集数据,这里以监控timewait的数量为例

vi count_netstat_wait_connections.sh
#!/bin/bash
instance_name=`hostname -f | cut -d'.' -f1`  #获取本机名,用于后面的的标签
label="count_netstat_wait_connections"  #定义key名
count_netstat_wait_connections=`netstat -an | grep -i wait | wc -l`  #获取数据的命令
echo "$label: $count_netstat_wait_connections"
echo "$label  $count_netstat_wait_connections" | curl --data-binary @- http://server.com:9091/metrics/job/pushgateway_test/instance/$instance_name  #这里pushgateway_test就是prometheus主配置文件里job的名字,需要保持一致,这样数据就会推送给这个job。后面的instance则是指定机器名,使用的就是脚本里获取的那个变量值


pushgateway.png

评论