Prometheus-PushGateway自定义监控项

文章目录

一、前言

pushgateway相比较exporter是主动向服务器发送请求,pushgateway本身也是一个程序,可以运行在任意节点上(不是必须在被监控端),运行本身没有抓取数据的功能,它只是被动的等待推送过来,然后发现服务端。

二、PushGateway安装

1、GitHub下载解压
PushGateway下载地址:

bash 复制代码
wget https://github.com/prometheus/pushgateway/releases/download/v1.5.0/pushgateway-1.5.0.linux-amd64.tar.gz

2、运行pushgateway 默认端口是9091,可以使用--web.listen-address更改默认端口

bash 复制代码
tar zxf pushgateway-1.5.0.linux-amd64.tar.gz 
mv pushgateway-1.5.0.linux-amd64 /usr/local/pushgateway
cd /usr/local/pushgateway
./pushgateway --web.listen-address=:19091

PS:前台启动没有报错就可以 ^C 停止掉了,我们配置systemd管理!

3、配置system管理

bash 复制代码
cat > /usr/lib/systemd/system/pushgateway.service << EOF
[Unit]
Description=prometheus pushgateway
After=network.target

[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/pushgateway/pushgateway --web.listen-address=:19091
ExecStop=/usr/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

启动并加入开机自启动

bash 复制代码
systemctl enable pushgateway.service --now
systemctl status pushgateway.service

如上图,查看状态是active (running)表示已经起来了!

4、Prometheus配置文件中,添加PushGateway地址

ymal 复制代码
  - job_name: "prometheus gateway"
    static_configs:
      - targets: ["localhost:19091"]
bash 复制代码
systemctl restart prometheus.service
systemctl status prometheus.service

5、验证

浏览器访问 IP:19091 验证pushgateway页面

验证Prometheus页面

三、PushGateway的使用

1、推送单个样本

bash 复制代码
echo "test_metric 1314521" | curl --data-binary @- http://localhost:19091/metrics/job/test_job

pushgateway页面可以看到推送的数据 如下图所示:

  • test_metric 1314521: 推送的键 值
  • job/test_job:标签job=test_job,多个标签直接往后添加即可
  • data-binary:发送post请求 以二进制数据

四、PushGateway脚本思路

使用shell脚本推送 netstat wait数量

bash 复制代码
#!/bin/bash
pushgateway="localhost:19091"
lable="count_netstat_wait_connections"
count_netstat_wait_connections=$(netstat -an|grep -i wait|wc -l)

if [ ${HOSTNAME} == "localhost" ];then
	echo "主机名不可使用 'localhost'" && exit 5
fi

# 这里定义了两个标签 job=pushgateway-1 hostname=${HOSTNAME}
echo "${lable} $count_netstat_wait_connections"|curl --data-binary @- http://${pushgateway}/metrics/job/pushgateway-1/hostname/${HOSTNAME}

执行脚本 检查是否能推送到pushgateway页面

使用定时任务执执行脚本,每3秒推送一次数据

bash 复制代码
crontab -e
* * * * * /usr/bin/sleep 3; /bin/bash /root/qinzt/pushgateway.sh

prometheus中查询数据

相关推荐
XMYX-013 小时前
Spring Boot + Prometheus 实现应用监控(基于 Actuator 和 Micrometer)
spring boot·后端·prometheus
时间裂缝里的猫-O-3 天前
@Prometheus 监控-MySQL (Mysqld Exporter)
数据库·mysql·prometheus
qq_312920114 天前
K8S上使用helm部署 Prometheus + Grafana
kubernetes·grafana·prometheus
MyikJ6 天前
Java 面试实录:从Spring到微服务的技术探讨
java·spring boot·微服务·kafka·spring security·grafana·prometheus
怡人蝶梦6 天前
Java大厂后端技术栈故障排查实战:Spring Boot、Redis、Kafka、JVM典型问题与解决方案
java·jvm·redis·elk·kafka·springboot·prometheus
tatasix6 天前
从监控到告警:Prometheus+Grafana+Alertmanager+告警通知服务全链路落地实践
grafana·prometheus
时间裂缝里的猫-O-6 天前
@Docker Compose部署Alertmanager
docker·容器·eureka·prometheus
怡人蝶梦7 天前
Java后端技术栈问题排查实战:Spring Boot启动慢、Redis缓存击穿与Kafka消费堆积
java·jvm·redis·kafka·springboot·prometheus
怡人蝶梦7 天前
Spring Boot启动慢?Redis缓存击穿?Kafka消费堆积?——Java后端常见问题排查实战
java·jvm·redis·kafka·springboot·prometheus·microservices