PushGateway
由于Prometheus数据采集基于Pull模型进行设计,因此在网络环境的配置上必须要让Prometheus Server能够直接与Exporter进行通信。 当这种网络需求无法直接满足时,就可以利用PushGateway来进行中转。可以通过PushGateway将内部网络的监控数据主动Push到Gateway当中。而Prometheus Server则可以采用同样Pull的方式从PushGateway中获取到监控数据。
优点:可以像老牌监控一样运维人员可以通过 shell python 脚本自定义监控上报给PushGateway在上报给PrometheusServer,比编写Exporters简单
缺点:当监控项和被监控服务器数量增多,可能会有并发问题使上报数据相对便慢
1 PushGateway安装
wget https://github.com/prometheus/pushgateway/releases/download/v0.8.0/pushgateway-0.8.0.linux-amd64.tar.gz -P /usr/local/
tar xf pushgateway-0.8.0.linux-amd64.tar.gz
mv pushgateway-0.8.0.linux-amd64 pushgateway
注意:pushgateway 可以安装到任何节点,一般和prometheus放在一个服务器上面
2 将PushGateway写成系统服务
cat>/lib/systemd/system/pushgateway.service<<EOF
[Service]
Restart=on-failure
WorkingDirectory=/usr/local/pushgateway/
ExecStart=/bin/sh -c '/usr/local/pushgateway/pushgateway --web.listen-address=":9191"'
[Install]
WantedBy=multi-user.target
EOF
chown 644 /lib/systemd/system/pushgateway.service
systemctl daemon-reload
systemctl enable pushgateway
systemctl start pushgateway
systemctl status pushgateway
3 向prometheus注册
vim prometheus.yml
- job_name: 'pushgateway'
static_configs:
- targets: ['localhost:9191']
labels:
instance: pushgateway
systemctl restart prometheus
4 监控脚本示例
获取tcp连接数脚本
# cat tcpestab.sh
#!/bin/bash
#获取主机名,常传输到Prometheus标签以主机名
instance_name=`hostname -f | cut -d'.' -f1`
#判断主机名不能是localhost不然发送过的数据不知道是那个主机的
if [ $instance_name == "localhost" ];then
echo "Hostname must not localhost"
exit 1
fi
#自定义key,在Prometheus即可使用key查询
label="count_estab_connections"
#获取TCP estab 连接数
count_estab_connections=`netstat -an | grep -i 'established' | wc -l`
#将数据发送到pushgateway固定格式
echo "$label $count_estab_connections" | curl --data-binary @- http://192.168.184.130:9191/metrics/job/pushgateway/instance/$instance_name
5 设置数据采集频率
#15s 采集一次
* * * * * sleep 15,30,45,60; sh /root/tcpestab.sh >/dev/null 2>&1
6 测试
总结:使用PushGateway可以自定义监控项,通过脚本实现使用起来也是不错的,这种方式比自己去开发一个node_exporter要简单的多,不过需要注意的是如果随着监控项的增多可能会导致并发太高而延迟,但是可以多安装几个PushGateway来分散压力。
类似:
- job_name: 'pushgateway1'
static_configs:
- targets: ['localhost:9191']
labels:
instance: pushgateway1
- job_name: 'pushgateway2'
static_configs:
- targets: ['server2:9191']
labels:
instance: pushgateway2
......