一、pushgateway介绍

pushgateway是另一种数据采集的方式,采用被动推送来获取监控数据的prometheus插件,它可以单独运行在任何节点上,并不一定要运行在被监控的客户端。而后通过用户自定义编写的脚本把需要监控的数据发送给pushgateway,pushgateway再将数据推送给prometheus server。

二、pushgateway的安装运行和配置

2.1、pushgateway安装

官方下载地址:https://prometheus.io/download/#pushgateway

pushgateway和prometheus、node_exporter一样,直接下载,解压后直接运行即可。pushgateway启动后,默认监听9091端口,如下一顿操作:

# 下载
[root@prometheus ~]# wget https://github.com/prometheus/pushgateway/releases/download/v1.0.0/pushgateway-1.0.0.linux-amd64.tar.gz # 解压
[root@prometheus ~]# tar -zxf pushgateway-1.0.0.linux-amd64.tar.gz -C /usr/local/
[root@prometheus ~]# mv /usr/local/pushgateway-1.0.0.linux-amd64 /usr/local/pushgateway-1.0.0
[root@prometheus ~]# ln -sv /usr/local/pushgateway-1.0.0 /usr/local/pushgateway
‘/usr/local/pushgateway’ -> ‘/usr/local/pushgateway-1.0.0’ # 运行
[root@prometheus ~]# cd /usr/local/pushgateway
[root@prometheus pushgateway]# ll
total 16136
-rw-r--r-- 1 3434 3434 11357 Oct 16 04:10 LICENSE
-rw-r--r-- 1 3434 3434 487 Oct 16 04:10 NOTICE
-rwxr-xr-x 1 3434 3434 16505766 Oct 16 03:58 pushgateway
[root@prometheus pushgateway]# ./pushgateway &
[1] 28453
[root@prometheus pushgateway]# level=info ts=2019-12-11T05:44:40.631Z caller=main.go:81 msg="starting pushgateway" version="(version=1.0.0, branch=HEAD, revision=cc61f46971f5eb7a5be64e80c2ee03857ddbb41a)"
level=info ts=2019-12-11T05:44:40.631Z caller=main.go:82 build_context="(go=go1.13.1, user=root@58be538fc30e, date=20191015-19:58:18)"
level=info ts=2019-12-11T05:44:40.697Z caller=main.go:142 listen_address=:9091 [root@prometheus pushgateway]# netstat -tulnp |grep 9091
tcp6 0 0 :::9091 :::* LISTEN 28453/./pushgateway

再通过编写systemd的管理脚本对pushgateway进行管理:

[root@prometheus ~]# vim /usr/lib/systemd/system/pushgateway.service
[Unit]
Description=pushgateway
Documentation=https://prometheus.io/docs/introduction/overview
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target [Service]
Type=simple
PIDFile==/var/run/pushgateway.pid
ExecStart=/usr/local/pushgateway/pushgateway
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID [Install]
WantedBy=multi-user.target
[root@prometheus ~]# systemctl daemon-reload
[root@prometheus ~]# ps -ef |grep push
root 28453 27712 0 13:44 pts/1 00:00:12 ./pushgateway
root 30549 30468 0 17:56 pts/3 00:00:00 grep --color=auto push
[root@prometheus ~]# kill 28453
[root@prometheus ~]# ps -ef |grep push
root 30551 30468 0 17:56 pts/3 00:00:00 grep --color=auto push
[root@prometheus ~]# systemctl start pushgateway
[root@prometheus ~]# ps -ef |grep push
root 30558 1 5 17:56 ? 00:00:00 /usr/local/pushgateway/pushgateway
root 30565 30468 0 17:56 pts/3 00:00:00 grep --color=auto push
[root@prometheus ~]# netstat -tulnp |grep 9091
tcp6 0 0 :::9091 :::* LISTEN 30558/pushgateway

启动后,可以通过web界面进行访问pushgateway,192.168.0.143:9091,也可以通过命令行测试发送监控数据到pushgateway,如下:

[root@node02 ~]# echo "testjob 3.1415926" |curl --data-binary @- http://192.168.0.143:9091/metrics/job/testjob

这里的数据,仅仅只是发送到了pushgateway,还需要在prometheus server上进行配置收集。这里需要注意的是最后是将key & value推送给pushgateway,curl --data-binary是将HTTP POST请求中的数据发送给HTTP服务器(pushgateway),和用户提交THML表单时浏览器的行为是外安全一样的,HTTP POST请求中的数据为纯二进制数据。

2.2、pushgateway配置

在prometheus.yml配置文件中,单独定义一个job,然后将target指向pushgateway运行所在主机的主机名或ip和运行端口即可。如下:

...
- job_name: 'pushgateway'
scrape_interval: 5s
static_configs:
- targets: ['192.168.0.143:9091']
labels:
instance: pushgateway

配置完成后,重启prometheus,再在prometheus的web端查询数据,即可获得刚才的测试数据,如图:

三、自定义脚本发送pushgateway

pushgateway本身没有任何抓取监控数据的功能,它只是被动的等待数据推送过来,下面在来搞搞抓取TCP_ESTABLISHED的瞬时数量,如下脚本:

[root@node02 ~]# cd /usr/local/node_exporter
[root@node02 node_exporter]# cat tcp_establish.sh
#!/bin/bash
instance_name=$(hostname) #本机主机名变量,用于后面的标签
if [ $instance_name == "localhost" ];then
echo "Must change the hostname."
exit 1
fi
label="count_tcp_established" #定义一个新的key
count_tcp_established=$(netstat -an |grep -i ESTABLISHED |wc -l)
echo "$label $count_tcp_established" | curl --data-binary @- http://192.168.0.143:9091/metrics/job/pushgateway/instance/$instance_name # 增加可执行权限后,多执行几次
[root@node02 node_exporter]# chmod +x tcp_establish.sh
[root@node02 node_exporter]# sh tcp_establish.sh

执行完成后,在pushgateway 的web端以及prometheus server的web端查询数据图,如下:



这里需要了解的是最后收集数据pushgateway的组成,主要是通过linux的命令行去获取数据,并通过echo的方式建立key value数据推送到pushgateway。http://192.168.0.143:9091/metrics/job/pushgateway/instance/$instance_name,这条连接主要分为三个部分:

需要周期不断地收集该指标数据,当然免不了使用crontab,脚本配合crontab即可发挥强大功效。下面可以配置每5s收集一次该指标数据,又得写个脚本了,如下:

[root@node02 node_exporter]# vim tcp_establish.sh
#!/bin/bash
#每5秒钟刷新一次
step=5
for (( i = 0; i < 60; i=(i+step) )); do
/bin/sh /usr/local/node_exporter/tcp_establish.sh
sleep $step
done
[root@node02 node_exporter]# crontab -e
* * * * * /bin/sh /usr/local/node_exporter/crontab_tcp.sh

四、使用pushgateway的优缺点

这里也有官方的英文解释说明:https://prometheus.io/docs/practices/pushing/ ,总体来说主要下面2点:

  • (1)pushgateway是一个单点的瓶颈,如果有多个脚本同时发送给一个pushgateway的进程,如果该进程挂掉的话,那么监控数据也就木有了。

  • (2)pushgateway不能对发送过来的数据进行智能化判断,如果脚本中间采集有问题,那么pushgateway是会照单全收的发送给prometheus server

Prometheus学习笔记(4)什么是pushgateway???的更多相关文章

  1. Prometheus学习笔记(7)PromQL玩法入门

    目录 1.什么是PromQL??? 2.如何查询??? 1.什么是PromQL??? PromQL是Prometheus内置的数据查询语言,其提供对时间序列数据丰富的查询,聚合以及逻辑运算能力的支持. ...

  2. Prometheus学习笔记(1)Prometheus架构简介

    Prometheus简介和架构 Prometheus 是由 SoundCloud 开源监控告警解决方案.架构图如下: 如上图,Prometheus主要由以下部分组成: Prometheus Serve ...

  3. Prometheus学习笔记之教程推荐

    最近学习K8S和基于容器的监控,发现了如下的教程质量不错,记录下来以备参考 K8S最佳实战(包括了K8S的Prometheus监控和EFK日志搜集) https://jimmysong.io/kube ...

  4. Prometheus学习笔记(6)Alertmanager告警

    目录 一.Alertmanager简介 二.Alertmanager部署 三.Alertmanager配置 四.自定义告警规则和发送 五.自定义告警模板 一.Alertmanager简介 Promet ...

  5. Prometheus学习笔记(5)Grafana可视化展示

    目录 一.Grafana安装和启动 二.配置数据源 三.配置dashboard 四.配置grafana告警 一.Grafana安装和启动 Grafana支持查询Prometheus.从Grafana ...

  6. Prometheus学习笔记(3)什么是node_exporter???

    目录 Node_exporter安装配置启动 Node_exporter安装配置启动 node_exporter安装在被监控端,安装方式也比较简单,直接下载解压安装即可,默认启动后监听9100端口. ...

  7. Prometheus学习笔记(2)Prometheus部署

    目录 Prometheus的安装配置启动 Prometheus的安装配置启动 1.Prometheus二进制安装 Prometheus下载链接:https://prometheus.io/downlo ...

  8. 【Prometheus学习笔记】主机监控 -node_exporter

    Exporter for machine metrics prometheus/node_exporter 安装 Prometheus sudo tar -zxvf prometheus-*.tar. ...

  9. Contour 学习笔记(一):使用 Contour 接管 Kubernetes 的南北流量

    原文链接:Contour 学习笔记(一):使用 Contour 接管 Kubernetes 的南北流量 在 Kubernetes 中运行大规模以 Web 为中心的工作负载,最关键的需求之一就是在 L7 ...

随机推荐

  1. ESA2GJK1DH1K基础篇: 测试APP使用SmartConfig绑定Wi-Fi 设备并控制设备

    前言 实现功能概要 STM32控制WI-Fi模块以AT指令TCP透传方式连接MQTT服务器, 实现MQTT通信控制. 测试准备工作(详细下载步骤请参考 硬件使用说明 ) 一,下载单片机程序 二,安装A ...

  2. 请写出css中选择器(元素选择器、类选择器、id选择器)的优先级顺序,和当各种选择器组合时,优先级的计算规则是什么?

    id选择器>类选择器>元素选择器 规则:选择器的权重值表述为4个部分,用0,0,0,0表示. 通配符*的权重为0,0,0,0 标签选择器.伪元素选择器的权重为0,0,0,1 类选择器.属性 ...

  3. Java 读取和写入文本文件

    package test_java; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStre ...

  4. c语言之函数指针应用

    c语言之函数指针应用 1.函数指针与指针函数 在开始运用函数指针前,我们需要将两个概念即:函数指针与指针函数搞清楚. 函数指针,指明这个一个函数,但返回值为指针类型,语法格式为: 类型名* 函数名A( ...

  5. vscode配合less的编译

    1.安装Easy LESS插件 2.打开settings.json,添加以下代码: "less.compile": { "sourceMap": true, & ...

  6. VS2019 NetCore3.0找寻grpc模板

    今天研究Google的grpc框架的时候看到了https://www.cnblogs.com/yilezhu/p/10631420.html这哥们儿的博客 按照博客的内容找寻grpc模板,始终找不到A ...

  7. C#内存泄露与资源释放 经验总结

    本文链接:http://blog.csdn.net/yokeqi/article/details/41083939 C#相比其他语言,拥有强大的垃圾回收机制,但并不是这样,你就可以对内存管理放任不管, ...

  8. lhgDialog弹窗提示窗口组件

    原文地址:http://www.lhgdialog.com/api/ 在页面head引入lhgdialog(如果项目采用jQuery作为框架,则引用jQuery的库). <script type ...

  9. sqlcode与sqlerrm的区别

    sqlCode:是数据库操作的返回码,其中0--成功:-1--失败:100--没有检索到数据. sqlerrm:是数返回指定错误代码的错误信息. 在一个内在的异常中,SQLCODE返回Oracle错误 ...

  10. Python【每日一问】18

    问: [基础题]:请解释新式类跟经典类,并说明它们的区别[提高题]:请解释Python垃圾回收机制 答: [基础题]:请解释新式类跟经典类,并说明它们的区别 1.新式类都是继承内置 object 对象 ...