一、概述
Pushgateway是Prometheus的一個(gè)組件,prometheus server默認(rèn)是通過(guò)Exporter主動(dòng)獲取數(shù)據(jù)(默認(rèn)采取pull拉取數(shù)據(jù)),Pushgateway則是通過(guò)exporter主動(dòng)方式推送數(shù)據(jù)到Pushgateway,再由Prometheus主動(dòng)去拉取 Pushgateway數(shù)據(jù),用戶(hù)可以寫(xiě)一些自定義的監(jiān)控腳本把需要監(jiān)控的數(shù)據(jù)發(fā)送給Pushgateway。從prometheus server角度看,都是由prometheus server主動(dòng)去拉取各個(gè)數(shù)據(jù)源(例:Exporter和Pushgateway)的數(shù)據(jù)。
1、Pushgateway優(yōu)點(diǎn):
- Prometheus 默認(rèn)采用定時(shí)pull 模式拉取targets數(shù)據(jù),但是如果不在一個(gè)子網(wǎng)或者防火墻,prometheus就拉取不到targets數(shù)據(jù),所以可以采用各個(gè)target往pushgateway上push數(shù)據(jù),然后prometheus去pushgateway上定時(shí)pull數(shù)據(jù)。
 - 在監(jiān)控業(yè)務(wù)數(shù)據(jù)的時(shí)候,需要將不同數(shù)據(jù)匯總, 匯總之后的數(shù)據(jù)可以由pushgateway統(tǒng)一收集,然后由 Prometheus 統(tǒng)一拉取,起到給Prometheus 減壓的作用。
 - 自定義采集指標(biāo)簡(jiǎn)單。
 
2、Pushgateway缺點(diǎn):
- Prometheus拉取狀態(tài)只針對(duì) pushgateway, 不能對(duì)每個(gè)節(jié)點(diǎn)都有效。
 - Pushgateway出現(xiàn)問(wèn)題,整個(gè)采集到的數(shù)據(jù)都會(huì)出現(xiàn)問(wèn)題。
 - Pushgateway 可以持久化推送給它的所有監(jiān)控?cái)?shù)據(jù)。因此,即使你的監(jiān)控已經(jīng)下線(xiàn),prometheus 還會(huì)拉取到舊的監(jiān)控?cái)?shù)據(jù),需要手動(dòng)清理 pushgateway 不要的數(shù)據(jù)。
 - 官方文檔:https://prometheus.io/docs/prometheus/
 - Prometheus GitHub地址:https://github.com/prometheus/prometheus/
 - Pushgetway GitHub地址:https://github.com/prometheus/pushgateway/
 
關(guān)于Prometheus整體介紹

二、Pushgateway 架構(gòu)

- Pushgateway就是個(gè)數(shù)據(jù)中轉(zhuǎn)站。提供API,支持?jǐn)?shù)據(jù)生產(chǎn)者隨時(shí)將數(shù)據(jù)推送過(guò)來(lái)。
 - Pushgateway提供exporter功能,在promethus server拉取數(shù)據(jù)時(shí),將自己保存的數(shù)據(jù)反饋給promethus server端。
 
三、Prometheus server 安裝
Prometheus基于Golang編寫(xiě),編譯后的軟件包,不依賴(lài)于任何的第三方依賴(lài)。用戶(hù)只需要下載對(duì)應(yīng)平臺(tái)的二進(jìn)制包,解壓并且添加基本的配置即可正常啟Prometheus Server。
1)下載
下載地址:https://prometheus.io/download/
wget https://github.com/prometheus/prometheus/releases/download/v2.40.6/prometheus-2.40.6.linux-amd64.tar.gz
tar -xf prometheus-2.40.6.linux-amd64.tar.gz
2)配置
解壓后當(dāng)前目錄會(huì)包含默認(rèn)的Prometheus配置文件promethes.yml,下面配置文件做下簡(jiǎn)略的解析:
# 全局配置
global:
  scrape_interval:     15s # 設(shè)置抓取間隔,默認(rèn)為1分鐘
  evaluation_interval: 15s #估算規(guī)則的默認(rèn)周期,每15秒計(jì)算一次規(guī)則。默認(rèn)1分鐘
  # scrape_timeout  #默認(rèn)抓取超時(shí),默認(rèn)為10s
# Alertmanager相關(guān)配置
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093
# 規(guī)則文件列表,使用'evaluation_interval' 參數(shù)去抓取
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"
#  抓取配置列表
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']
3)啟動(dòng)服務(wù)
# 查看幫助
./prometheus -h
# 直接啟動(dòng)服務(wù),但是不提倡這種,因?yàn)橥顺隹刂婆_(tái)服務(wù)也就退出了,雖然可以加nohup啟動(dòng),但是也不是特別友好,下面將配置prometheus.server啟動(dòng)
# 默認(rèn)端口是:9090,如需要修改默認(rèn)端口,可以使用--web.listen-address=:9099,還可以指定配置文件--config.file=prometheus.yml
./prometheus
配置prometheus.service 啟動(dòng)腳本
cat >/usr/lib/systemd/system/prometheus.service<<EOF
[Unit]
Descriptinotallow=Prometheus
After=network.target
[Service]
Type=simple
ExecStart=/opt/prometheus/prometheus_server/prometheus-2.40.6.linux-amd64/prometheus --config.file=/opt/prometheus/prometheus_server/prometheus-2.40.6.linux-amd64/prometheus.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
啟動(dòng)服務(wù)
# 執(zhí)行 systemctl daemon-reload 命令重新加載systemd
systemctl daemon-reload
# 啟動(dòng)
systemctl start prometheus
# 檢查
systemctl status prometheus
netstat -tnlp|grep :9090
ps -ef|grep prometheus

web訪(fǎng)問(wèn):http://ip:9090

四、Pushgateway 安裝
1)下載
下載地址:https://prometheus.io/download/#pushgateway
wget https://github.com/prometheus/pushgateway/releases/download/v1.5.1/pushgateway-1.5.1.linux-amd64.tar.gz
2)啟動(dòng)服務(wù)
# 查看幫助
./pushgateway  -h
# 啟動(dòng)服務(wù),這里也不使用直接啟動(dòng)的方式,配置pushgateway.service啟動(dòng)
./pushgateway
默認(rèn)監(jiān)聽(tīng)的是9091端口??梢酝ㄟ^(guò)以下配置進(jìn)行更改:
usage: pushgateway [<flags>]
Flags:
      --web.listen-address=":9091"      監(jiān)聽(tīng)Web界面,API和遙測(cè)的地址。
      --web.telemetry-path="/metrics"     公開(kāi)metrics的路徑。
      --web.external-url=             可從外部訪(fǎng)問(wèn)Pushgateway的URL.
      --web.route-prefix=""           Web端點(diǎn)內(nèi)部路由的前綴。 默認(rèn)為--web.external-url的路徑.
      --persistence.file=""           歸檔以保留metrics。 如果為空,則metrics僅保留在內(nèi)存中.
      --persistence.interval=5m       寫(xiě)入持久性文件的最小間隔。
      --log.level="info"              僅記錄具有給定嚴(yán)重性或更高嚴(yán)重性的消息。 有效級(jí)別:[debug, info, warn, error, fatal]
      --log.format="logger:stderr"      設(shè)置日志目標(biāo)和格式。 示例:“ logger:syslog?appname = bob&local = 7”或“ logger:stdout?json = true”
      --version                       顯示應(yīng)用程序版本。
配置pushgateway.service 啟動(dòng)腳本
cat >/usr/lib/systemd/system/pushgateway.service<<EOF
[Unit]
Descriptinotallow=Pushgetway
After=network.target
[Service]
Type=simple
ExecStart=/opt/prometheus/pushgateway/pushgateway-1.5.1.linux-amd64/pushgateway
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
啟動(dòng)服務(wù)
# 執(zhí)行 systemctl daemon-reload 命令重新加載systemd
systemctl daemon-reload
# 啟動(dòng)
systemctl start pushgateway
# 檢查
systemctl status pushgateway
netstat -tnlp|grep :9091
ps -ef|grep pushgateway

web訪(fǎng)問(wèn):ip:9091/metrics

3)接入Prometheus
更改prometheus配置文件,增加如下內(nèi)容:
- job_name: 'pushgateway_name' 
    scrape_interval: 30s 
    honor_labels: true  #加上此配置,exporter節(jié)點(diǎn)上傳數(shù)據(jù)中的一些標(biāo)簽將不會(huì)被pushgateway節(jié)點(diǎn)的相同標(biāo)簽覆蓋 
    static_configs: 
        - targets: ["192.168.182.110:9091"] 
          labels: 
              instance: pushgateway_instance               
# pushgateway 中的數(shù)據(jù)我們通常按照 job 和 instance 分組分類(lèi),所以這兩個(gè)參數(shù)不可缺少。
重啟Prometheus服務(wù),或進(jìn)行熱加載
# curl -X POST http://192.168.182.110:9090/-/reload
systemctl restatus prometheus
再查看prometheus web界面:http://ip:9090/targets

五、實(shí)戰(zhàn)操作演示
1)推送數(shù)據(jù)定義
/metrics/job/<JOB_NAME>{/<LABEL_NAME>/<LABEL_VALUE>}其中job是必須參數(shù),label_name部分是可選的,URL中的job和label組合唯一標(biāo)識(shí)pushgateway中的Group。
- 在推送的數(shù)據(jù)部分,格式定義如下:
 
## TYPE metric_name type
metric_name{lable_name="label_value",...}  value
1)推送數(shù)據(jù)
推送一個(gè)group定義為{job=“some_job”}的數(shù)據(jù)
echo "some_metric 3.14" | curl --data-binary @- http://192.168.182.110:9091/metrics/job/some_job
推送一個(gè)group定義為{job=“some_job”,instance=“some_instance”}的數(shù)據(jù)
#  --data-binary 表示發(fā)送二進(jìn)制數(shù)據(jù),注意:它是使用POST方式發(fā)送的!
cat <<EOF | curl --data-binary @- http://192.168.182.110:9091/metrics/job/some_job/instance/some_instance
  # TYPE some_metric counter
  some_metric2{label="val1"} 42
  # TYPE another_metric gauge
  # HELP another_metric Just an example.
  another_metric 2398.283
EOF
2)刪除數(shù)據(jù)
刪除group定義為{job=“some_job”}下的所有數(shù)據(jù)
curl -X DELETE http://192.168.182.110:9091/metrics/job/some_job/instance/some_instance
刪除所有g(shù)roup下的所有metrics(啟動(dòng)pushgateway時(shí)需加上命令行參數(shù)--web.enable-admin-api)
curl -X PUT http://192.168.182.110:9091/api/v1/admin/wipe
說(shuō)明:
- 刪除數(shù)據(jù)是以Group為單位的,Group由job name和URL中的label唯一標(biāo)識(shí)。
 - 舉例中刪除{job=“some_job”}數(shù)據(jù)的語(yǔ)句并不會(huì)刪除{job=“some_job”,instance=“some_instance”}的數(shù)據(jù)。因?yàn)閷儆诓煌腉roup。如需要?jiǎng)h除{job=“some_job”,instance=“some_instance”}下的數(shù)據(jù),需要使用。
 - 這里刪除數(shù)據(jù)是指刪除pushgateway中的數(shù)據(jù),跟promethues沒(méi)有關(guān)系。
 
上面的演示示例是官方提供:https://github.com/prometheus/pushgateway/
3)?定義編寫(xiě)腳本的?法 發(fā)送pushgateway 采集
模板
cat <<EOF | curl --data-binary @- http://192.168.182.110:9091/metrics/job/some_job/instance/some_instance
# A histogram, which has a pretty complex representation in the text format:
# HELP http_request_duration_seconds A histogram of the request duration.
# TYPE http_request_duration_seconds histogram
disk_usage{instance="local-168-182-110",job="disk",disk_name="/run/user/0"} 11
disk_usage{instance="local-168-182-110",job="disk",disk_name="/run/user/1"} 22
disk_usage{instance="local-168-182-110",job="disk",disk_name="/run/user/2"} 33
disk_usage{instance="local-168-182-110",job="disk",disk_name="/run/user/3"} 44
disk_usage{instance="local-168-182-110",job="disk",disk_name="/run/user/4"} 55
EOF
編寫(xiě)采集腳本推送數(shù)據(jù)到Pushgateway
cat >disk_usage_metris.sh<<EOF
#!/bin/bash
hostname=`hostname -f | cut -d '.' -f1`
metrics=""
for line in `df |awk 'NR>1{print $NF "=" int($(NF-1))}'`
do
  disk_name=`echo $line|awk -F'=' '{print $1}'`
  disk_usage=`echo $line|awk -F'=' '{print $2}'`
  metrics="$metrics\ndisk_usage{instance=\"$hostname\",job=\"disk\",disk_name=\"$disk_name\"} $disk_usage"
done
echo -e "# A histogram, which has a pretty complex representation in the text format:\n# HELP http_request_duration_seconds A histogram of the request duration.\n# TYPE http_request_duration_seconds histogram\n$metrics" | curl --data-binary @- http://192.168.182.110:9091/metrics/job/pushgateway/instance/disk_usage
EOF
查看Pushgetway web

查看Prometheus web
