PushGateway 介绍
Pushgateway是Prometheus的一个组件,Prometheus 默认是通过exporter主动获取数据(默认采取pull拉取数据),Pushgateway则是通过主动方式推送数据到Prometheus ,用户可以写一些自定义的监控脚本把需要监控的数据发送给Pushgateway, 然后Pushgateway再把数据推送给Prometheus 。
PushGateway 使用场景
PushGateway官网
https://github.com/Prometheus/pushgateway
PushGateway 部署
1,二进制包安装PushGateway
wget https://github.com/prometheus/pushgateway/releases/download/v1.5.0/pushgateway-1.5.0.linux-amd64.tar.gz
tar zvxf pushgateway-1.5.0.linux-amd64.tar.gz -C /usr/local/
cd /usr/local
mv pushgateway-1.5.0.linux-amd64/ pushgateway2,启动服务
./pushgateway2.1,systemctl 管理pushgateway
vim /usr/lib/systemd/system/pushgateway.service
[Unit]
Description=pushgateway
After=network.target
[Service]
User=root
Type=simple
ExecStart=/usr/local/prometheus_exporter/pushgateway/pushgateway
Restart=on-failure
[Install]
WantedBy=multi-user.target2.2,启动 pushgateway
systemctl daemon-reload
systemctl enable pushgateway && systemctl start pushgateway2.3, 查看日志
journalctl -u pushgateway -fn 2003,Pushgateway使用方法
usage: pushgateway [<flags>]
Flags:
--web.listen-address=":9091" 监听Web界面,API和遥测的地址。
--web.telemetry-path="/metrics" 公开metrics的路径。
--web.external-url= 可从外部访问Pushgateway的URL.
--web.route-prefix="" Web端点内部路由的前缀。默认为--web.external-url
--persistence.file="" 归档以保留metrics。如果为空,则metrics仅保留在内存中.
--persistence.interval=5m 写入持久性文件的最小间隔。
--log.level="info" 仅记录具有给定严重性或更高严重性的消息。
#有效级别:[debug, info, warn, error, fatal]
--log.format="logger:stderr" 设置日志目标和格式。
#示例:"logger:syslog?appname = bob&local = 7"或" logger: stdout?json = true"
--version 显示应用程序版本。4,访问 PushGateway UI
http://<ip>:9091

默认 Metrics 上没有数据展示,无节点向PushGateway 上推送数据。PushGateway 服务本身 Metrics 可以通过访问 http://<ip>:9091/metrics 地获取。Pushgateway每次只向Prometheus返回最后一次推送的数据,如果客户端一直没有推送新的指标到pushgateway,那么Prometheus将始终拉取最后push上来的数据。
Prometheus 对接 PushGateway
1,编辑prometheus.yml文件
vim prometheus.yml
- job_name: 'pushgateway'
scrape_interval: 30s
static_configs:
- targets: ['192.168.100.221:9091']
labels:
instance: pushgateway
honor_labels: true
honor_labels: true #避免收集数据本身的 job 和 instance被pushgateway实例信息覆盖
2,检查prometheus.yml文件格式
/usr/local/prometheus/promtool check config /usr/local/prometheus/prometheus.yml3,Prometheus的restful接口热加载配置
curl -X POST http://127.0.0.1:9090/-/reload
API 方式 Push 数据到 PushGateway
1,通过 API 接口Push 数据至PushGateway
命令默认格式
http://<ip>:9091/metrics/job/<JOBNAME>{/<LABEL_NAME>/<LABEL_VALUE>}<JOBNAME> 是必填项,为 job 标签值
2,Push 指标数据到 PushGateway 中测试
echo "pro_metric 80" | curl --data-binary @- http://192.168.100.221:9091/metrics/job/test_jobPushGateway Web UI 查看指标数据

除 pro_metric 指标外,同时新增 push_time_seconds 和 push_failure_time_seconds 两个指标,是 PushGateway 系统自动生成的相关指标。
3,Push 一次写入多个指标,而且每个指标添加 TYPE 及 HELP 说明
cat <<EOF | curl --data-binary @- http://192.168.100.221:9091/metrics/job/test_job/instance/test_instance
# TYPE test_metrics counter
test_metrics{label="app1",name="demo"} 100.00
# TYPE another_test_metrics gauge
# HELP another_test_metrics Just an example.
another_test_metrics 123.45
EOF4,Push 指标数据文件 pushgdata.txt 至 PushGateway
vim pushdata.txt
# TYPE promhttp_metric_handler_requests_in_flight gauge
promhttp_metric_handler_requests_in_flight 1
# HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code.
# TYPE promhttp_metric_handler_requests_total counter
promhttp_metric_handler_requests_total{code="200"} 67548
promhttp_metric_handler_requests_total{code="500"} 0
promhttp_metric_handler_requests_total{code="503"} 0Push curl pushdata.txt
curl -XPOST --data-binary @pushdata.txt http://192.168.100.221:9091/metrics/job/app/instance/192.168.100.100-app5,Pushgateway Web

6,Prometheus Web

PushGateway API 删除指标 (也可通过PushGateway 删除Group)
1,删除 job="test_job" 组下的所有指标值
curl -X DELETE http://192.168.100.221:9091/metrics/job/test_job2,删除 job="test_job" 组下test_instance指标
curl -X DELETE http://192.168.100.221:9091/metrics/job/test_job/instance/test_instancePushgateway 网络延迟指标案例
1,fping 编译安装
wget http://www.fping.org/dist/fping-5.0.tar.gz
tar -zxvf fping-5.0.tar.gz -C /usr/local/fping/
cd /usr/local/fping
./configure --prefix=/usr/local/fping --enable-ipv4 --enable-ipv6
make && make install
ln -s /usr/local/fping/sbin/fping /usr/sbin/fping
chmod u+s /usr/local/fping/sbin/fping2,编写shell脚本
vim pushfping.sh
#!/bin/bash
#set -x
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
job="fping"
instance="114.114.114.114"
icmppingloss=`fping -q -c3 "$instance" 2>&1 |awk -F '[/ %]+' '{print $9}'`
icmppingsec=`fping -q -c3 "$instance" 2>&1 |awk -F '[/ %]+' '{print $16}'`
icmpping=`fping -q -c3 "$instance" 2>&1 |awk -F '=' '{print $2}' |awk -F '/' '{print $2}'`
cat <<EOF | curl --data-binary @- http://192.168.100.221:9091/metrics/job/$job/instance/$instance
# TYPE node_fping_usages gauge
node_fpingloss_usages $icmppingloss
node_fpingsec_usages $icmppingsec
node_fping_usages $icmpping
EOF3,Pushgateway Web

4,Prometheus Web

5,设置定时上传指标的计划任务
crontab -e
*/1 * * * * /usr/bin/bash /mnt/pushfping.shPrometheus PushGateway 注意事项