Prometheus-PushGateway

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

905042d16d73c8018b47ec4fe832bddb55e.jpg

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 测试

a4f148a30399c39c56813468040ca3163c2.jpg

adb9e01594a484e84a1ba07a45dcc1c112c.jpg

总结:使用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
......

 

转载于:https://my.oschina.net/54188zz/blog/3071312

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值