一、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. ML学习笔记(1)

    2019/03/09 16:16 归一化方法: 简单放缩(线性归一化):这种归一化方法比较适用在数值比较集中的情况.这种方法有个缺陷,如果max和min不稳定,很容易使得归一化结果不稳定,使得后续使用 ...

  2. sparksql基础知识一

    目标 掌握sparksql底层原理 掌握sparksql中DataFrame和DataSet的数据结构和使用方式 掌握通过sparksql开发应用程序 要点 1.sparksql概述 1.1 spar ...

  3. haproxy 配置文件详解 之 backend

    配置示例: backend htmpool mode http option redispatch option abortonclose balance static-rr cookie SESSI ...

  4. Linux下rpm及yum安装jdk

    笔者使用的Linux操作系统为CentOS7,Linux中有三种安装jdk的方式,在这里笔者主要介绍两种方式,即rpm和yum安装及卸载jdk,还有一种是直接从官网下载 tar.gz 压缩文件上传到L ...

  5. 这样配置:让你的 IDEA 好用到飞起来

    阅读本文大概需要 7 分钟. 来源:blog.csdn.net/fly910905/article/details/77868300 1.设置maven 1.在File->settings-&g ...

  6. 基于32位Windows2003的数据库服务器优化,启用AWE,优化SQL Server

    最近几天,笔者所在的单位中的一台WEB服务器由于负载过大出现了问题,当同时在线的用户达到一定规模(2000-3000)时,频繁出现页面响应迟缓.超时等问题.服务器采用的操作系统是Windows Ser ...

  7. top命令输出解释以及load average 详解及排查思路

    原地址: https://blog.csdn.net/zhangchenglikecc/article/details/52103737 1.top输出以及load average 详解 昨天nagi ...

  8. 【Activiti学习之四】Activiti API(三)

    环境 JDK 1.8 MySQL 5.6 Tomcat 7 Eclipse-Luna activiti 6.0 一.启动流程 多种方式启动 package com.wjy.pro; import or ...

  9. Shell脚本之七 选择、循环结构

    一.if else if 语法格式 if condition then command1 command2 ... commandN fi 写成一行(适用于终端命令提示符): if [ $(ps -e ...

  10. Android 及 iOS 常用操作命令

    应用相关 1. 安装应用(真机) Android adb install xxx.apk iOS ideviceinstaller -i xxx.ipa 2. 卸载应用(真机) Android adb ...