Prometheus學習筆記(4)什么是pushgateway???


[toc]

一、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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM