pushgateway的安装与使用

前言

环境:centos7.9 prometheus, version 2.5.0

pushgateway的使用

pushgateway 可以单独运行在任何节点上,并不一定要在被监控的客户端上,然后通过用户自定义开发脚本将需要监控的数据发送给pushgateway,然后pushgateway再把数据推送给prometheus server

下载安装pushgateway

GitHub下载解压 直接运行,pushgateway默认端口9091,如下所示:

#pushgateway下载地址
wget wget https://github.com/prometheus/pushgateway/releases/download/v1.4.3/pushgateway-1.4.3.linux-amd64.tar.gz
tar -zxvf pushgateway-1.4.3.linux-amd64.tar.gz -C /usr/local/
 cd /usr/local/pushgateway-1.4.3.linux-amd64.tar.gz/
[root@node2 pushgateway-1.4.3.linux-amd64.tar.gz]# ll		#解压之后就3个文件,一个可执行命令
total 16476
-rw-r--r-- 1 3434 3434    11357 May 31 03:07 LICENSE
-rw-r--r-- 1 3434 3434      487 May 31 03:07 NOTICE
-rwxr-xr-x 1 3434 3434 16853986 May 31 03:03 pushgateway	#可执行命令
[root@node2 pushgateway-1.4.3.darwin-arm64]# 

启动pushgateway,端口默认9091

#查看命令帮助,可知pushgateway的端口是9101
[root@node2 pushgateway-1.4.3.darwin-arm64]# ./pushgateway	-h

#启动pushgateway
[root@node2 pushgateway-1.4.3.linux-amd64]# nohup ./pushgateway &

#查看端口
[root@node2 pushgateway-1.4.3.linux-amd64]# lsof  -i:9091
COMMAND     PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
pushgatew 79808 root    3u  IPv6 16362297      0t0  TCP *:xmltec-xmlmail (LISTEN)
[root@node2 pushgateway-1.4.3.linux-amd64]# 

查看pushgateway的页面

打开pushgateway的web页面,http://192.168.118.133:9091,发现Metrics栏没有任何数据。因为此时还没有客户端推送数据给pushgateway

修改Prometheus server 配置文件,定义一个job

prometheus serverprometheus.yml文件中定义一个 job,然后targets指向pushgateway所在的ip和9091端口:

vim prometheus.yml
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['192.168.118.129:9101']

  - job_name: 'master'
    static_configs:
    - targets: ['192.168.118.131:9101']
  - job_name: 'node1'
    static_configs:
    - targets: ['192.168.118.132:9101']
  - job_name: 'node2'
    static_configs:
    - targets: ['192.168.118.133:9101']
  - job_name: 'pushgateway'					#定义一个job
    static_configs:
    - targets: ['192.168.118.133:9091']		#指定pushgateway所在的ip和端口     

#重启 Prometheus server
[root@nginx prometheus-2.5.0.linux-amd64]# kill -9 206697 && nohup ./prometheus & 
[1] 229403
[root@nginx prometheus-2.5.0.linux-amd64]# lsof  -i:9090
COMMAND      PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
prometheu 229404 root    3u  IPv6 1017085      0t0  TCP *:websm (LISTEN)
prometheu 229404 root    6u  IPv4 1012761      0t0  TCP localhost:50018->localhost:websm (ESTABLISHED)
prometheu 229404 root    7u  IPv6 1007575      0t0  TCP localhost:websm->localhost:50018 (ESTABLISHED)
[root@nginx prometheus-2.5.0.linux-amd64]# 

#这是打开http://192.168.118.129:9090/targets 就能看到pushgateway是up状态

编写脚本文件采集主机数据,然后推送给pushgateway

pushgateway启动之后本身没有任何抓取数据的功能,pushgateway只是被动的等待数据推送过来。
下面是一个简单抓取目标服务器的count_netstat_wait_connections的脚本,然后将数据推送给pushgateway

vim pushgateway.sh							#编写pushgateway脚本采集数据
#!/bin/bash
instance_name=`hostname -f | cut -d'.' -f1`	#截取主机名
if [ $instance_name == "localhost" ];then
   echo "Must FQDN hostname"				#要求主机名不能是localhost,不要主机名区别不了
   exit 1
fi
label="count_netstat_wait_connections"								#定义一个key
count_netstat_wait_connections=`netstat -an| grep -i wait| wc -l`	#定义values

#推送数据给pushgateway
echo "$label $count_netstat_wait_connections" | curl --data-binary @- http://192.168.118.133:9091/metrics/job/${instance_name}
#解析
 curl --data-binary是将http post 请求中的二进制数据发送给http服务服务器,这里的http服务器指的就是pushgateway服务器;
 指定pushgateway的ip地址和端口号,后面${instance_name}是指定job的名称,这里以主机名命名。
#指定脚本采集数据
[root@node1 opt]# bash pushgateway.sh
#写入定时任务采集
[root@node1 opt]# crontab -l
* * * * * bash /opt/pushgateway.sh
[root@node1 opt]# 


#查看pushgateway的页面,这时发现Metrics已经有数据了
http://192.168.118.133:9091/#

#查看Prometheus server的页面,输入我们的key为count_netstat_wait_connections,已经能查出来数据了
http://192.168.118.129:9090/graph
count_netstat_wait_connections
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值