跳到主要内容Prometheus 监控盲区:Pushgateway 实现临时任务可视化 | 极客日志Shell / Bash
Prometheus 监控盲区:Pushgateway 实现临时任务可视化
Prometheus 监控主要针对长期运行服务,临时短命任务难以采集。通过部署 Pushgateway,可实现临时任务的指标推送。演示了 Pushgateway 的二进制及 Docker 安装方法,配置 Prometheus 采集,并通过 Shell 和 Python 脚本将备份、数据处理等任务的耗时与状态推送到网关,最终在 Prometheus 大盘展示。解决了临时任务监控黑盒问题,无需修改核心拉取逻辑即可补全监控闭环。
小熊软糖2 浏览 前言
做运维和开发的朋友大概率都遇到过这种情况:Prometheus 监控大盘上,长期运行的服务数据一应俱全,但那些凌晨跑的备份脚本、几分钟就结束的 CI/CD 任务、临时调度的数据处理作业,却始终是'黑盒'——这些短命任务执行完就退出,Prometheus 的拉取机制根本抓不到它们的状态。
其实不用愁,Prometheus 官方早就给出了解决方案:Pushgateway。这篇实操文,就带大家把这些'看不见的任务'纳入监控体系,让每一次临时执行都有数据可查。

1.安装条件
本次演示环境,我是在虚拟机上安装 Linux 系统来执行操作,以下是安装的软件及版本:
Oracle VirtualBox: 5.1.20 r114628 (Qt5.6.2)
System: CentOS Linux release 7.9.2009 (Core)
Docker: 26.1.4
Prometheus: v3.5.0
PushGateway: 1.0.0
2.PushGateway 安装配置
2.1 二进制包安装
访问官网,下载二进制包:
下载 linux 版本的:

下载完成后,上传到/app目录下:

上传成功后,我们为它解压:
tar -zxvf pushgateway-1.11.2.linux-amd64.tar.gz

我这里为它重命名并删除压缩文件:
mv pushgateway-1.11.2.linux-amd64 pushgateway
rm -rf pushgateway-1.11.2.linux-amd64.tar.gz

sudo vim /etc/systemd/system/pushgateway.service
[Unit]
Description=Pushgateway for Prometheus
Documentation=https://github.com/prometheus/pushgateway
After=network-online.target
[Service]
Type=simple
User=prometheus
Group=prometheus
ExecStart=/app/pushgateway/pushgateway --web.listen-address=:9091 --web.enable-admin-api --log.level=info
WorkingDirectory=/app/pushgateway
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
SyslogIdentifier=pushgateway
[Install]
WantedBy=multi-user.target
设置文件权限(确保二进制文件可执行,且属主正确):
sudo chown -R prometheus:prometheus /app/pushgateway
sudo chmod +x /app/pushgateway/pushgateway
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl start pushgateway
sudo systemctl enable pushgateway
sudo systemctl status pushgateway
ps aux | grep pushgateway
curl http://localhost:9091/metrics
journalctl -u pushgateway -f
sudo firewall-cmd --permanent --add-port=9091/tcp
sudo firewall-cmd --reload
此时浏览器访问 http://<ip>:9091 即可访问 UI 页面,只不过默认 Metrics 上没有任何数据展示,那是因为我们还没有往 Pushgateway 上推送任何数据。
不过,PushGateway 服务本身是带了一些 Metrics 的,可以通过访问 http://<ip>:9091/metrics 地址来获取,可以看到里边包含了 go、process 等相关的一些监控指标。
2.2 docker 安装
使用 prom/pushgateway 的 Docker 镜像:
docker pull prom/pushgateway
docker run -d \
--name=pg \
-p 9091:9091 \
prom/pushgateway
3.prometheus 中配置 pushgateway
要使 Push Gateway 正常工作,必须要在 prometheus 中配置对应的 job 才行。
vi /app/prometheus/prometheus.yml
- targets: ['localhost:9091']
labels:
app: "pushgateway"
systemctl restart prometheus
systemctl status prometheus
通过浏览器访问'ip:9090'就可以看见 pushgateway 服务已经添加监控成功:
4.推送数据到 pushgateway
我们要 Push 数据到 PushGateway 中,可以通过其提供的 API 标准接口来添加。
默认 URL 地址为:http://<IP>:9091/metrics/job/{/<LABEL_NAME>/<LABEL_VALUE>}。
其中 <IP> 是必填项,为 job 标签值,后边可以跟任意数量的标签对,一般我们会添加一个 instance/<INSTANCE_NAME> 实例名称标签,来方便区分各个指标。
接下来,可以 Push 一个简单的指标数据到 PushGateway 中测试一下。
echo "test_metric 123456" | curl --data-binary @- http://192.168.42.140:9091/metrics/job/test_job
回到 pushgateway 页面就可以看见 test_job 啦!
除了 test_metric 外,同时还新增了 push_time_seconds 和 push_failure_time_seconds 两个指标,这两个是 PushGateway 系统自动生成的相关指标。
此时,我们在 Prometheus UI 页面上 Graph 页面可以查询的到该指标了。
添加更多更复杂数据,通常数据会带上 instance, 表示来源位置:
cat << EOF | curl --data-binary @- http://192.168.42.140:9091/metrics/job/some_job/instance/some_instance
# TYPE some_metric counter
some_metric{label="val1"} 42
# TYPE another_metric gauge
# HELP another_metric Just an example.
another_metric 2398.283
EOF
curl -X DELETE http://192.168.42.140:9091/metrics/job/some_job/instance/some_instance
curl -X DELETE http://192.168.42.140:9091/metrics/job/some_job
5.真实场景上演推送指标
5.1 shell 脚本
模拟一个备份任务的执行过程,并将任务的关键指标(耗时和成功状态)主动推送到 Prometheus 的 Pushgateway,以便被 Prometheus 采集和监控。
#!/bin/bash
JOB_NAME="daily_backup"
INSTANCE="server01"
PUSHGATEWAY_URL="http://localhost:9091"
start_time=$(date +%s)
echo "Starting backup..."
sleep 3
backup_success=1
end_time=$(date +%s)
duration=$((end_time - start_time))
cat << EOF | curl --data-binary @- http://localhost:9091/metrics/job/$JOB_NAME/instance/$INSTANCE
# HELP backup_duration_seconds Duration of the backup job in seconds
# TYPE backup_duration_seconds gauge
backup_duration_seconds $duration
# HELP backup_success Whether the backup succeeded (1) or failed (0)
# TYPE backup_success gauge
backup_success $backup_success
EOF
echo "Metrics pushed to Pushgateway."
backup_duration_seconds{job="daily_backup", instance="server01"} 3
backup_success{job="daily_backup", instance="server01"} 1
5.2 Python 脚本
数据处理任务完成后,将关键指标主动推送到 Prometheus 的 Pushgateway,从而实现对短生命周期批处理任务的监控。(让一次性的 Python 批处理任务,在结束后主动告诉监控系统:我干了多少活,成没成功。)
import requests
import time
def push_metrics(job, instance, records_processed, success):
metrics = f"""
# HELP data_records_processed Number of records processed
# TYPE data_records_processed gauge
data_records_processed {records_processed}
# HELP data_job_success Job success status (1 = success, 0 = failure)
# TYPE data_job_success gauge
data_job_success {int(success)}
"""
url = f"http://localhost:9091/metrics/job/{job}/instance/{instance}"
response = requests.post(url, data=metrics.encode('utf-8'))
if response.status_code == 202:
print("Metrics pushed successfully.")
else:
print(f"Failed to push metrics: {response.status_code}")
start = time.time()
try:
records = 1500
time.sleep(2)
success = True
except Exception as e:
records = 0
success = False
push_metrics(job="data_pipeline", instance="worker-node-01", records_processed=records, success=success)
推送后,Prometheus 采集 Pushgateway 数据,你会看到如下指标:
data_records_processed{job="data_pipeline", instance="worker-node-01"} 1500
data_job_success{job="data_pipeline", instance="worker-node-01"} 1
总结
说到底,Pushgateway 不是复杂的新工具,只是补全 Prometheus 监控闭环的关键一环。它没有改变 Prometheus 核心的拉取逻辑,却用'推送 + 中转'的思路,把监控的触角延伸到了系统里那些容易被忽略的临时任务上。实操下来你会发现,只要配置得当,不管是 Shell 脚本还是 Python 批处理,都能轻松把执行状态、耗时、处理量这些关键数据上报,稳稳纳入监控大盘。监控的本质是让系统状态可感知,而 Pushgateway,就是让这份感知不留死角的小而美的方案。
相关免费在线工具
- Base64 字符串编码/解码
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
- Base64 文件转换器
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
- Markdown转HTML
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online
- HTML转Markdown
将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online
- JSON 压缩
通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online
- JSON美化和格式化
将JSON字符串修饰为友好的可读格式。 在线工具,JSON美化和格式化在线工具,online