前言
大部分情況下,Prometheus周期性調用HTTP接口獲取監控數據。某些情況下,主機無法暴露接口,或者不需要頻繁推送數據,可以使用pushgateway進行主動數據上報。
此外,對于主動上報的數據,本文還將討論Prometheus的告警設置。
什么是Pushagateway
PushGateway 作為 Prometheus 生態中的一個重要一員,它允許任何客戶端向其 Push 符合規范的自定義監控指標,在結合 Prometheus 統一收集監控。

配置Pushgateway
使用docker的方式配置后運行,9091端口便是push入口
docker pull prom/pushgateway
docker run -d --name=pg -p 9091:9091 prom/pushgateway
接入Prometheus
打開prometheus.yml文件,在scrape_configs內添加下面配置:
- job_name: pushgateway
static_configs:
- targets: ['127.0.0.1:9091']
labels:
instance: pushgateway
重啟Promtheus后,Promtheus Server便會定期從Pushgateway拉取數據。
如何推送數據至Pushgateway
假設某服務器上存在一個后臺進程,它會周期性檢測機器的狀態,當發生異常時,則推送數據至Pushgateway。
在Bash腳本中,我們可以使用curl命令完成推送。
#!/bin/bash
# 定義 Pushgateway 地址和端口
pushgateway="127.0.0.1:9091"
# 定義指標名稱和值
metric_name="memory_used"
metric_value=70
# 定義錯誤變量
error=1
# 判斷是否推送指標
if [ "$error" -eq 1 ]; then
# 拼接推送指標的字符串
data="$metric_name $metric_value"
# 發送指標數據到 Pushgateway
echo -n "$data" | curl --data-binary @- "$pushgateway/metrics/job/my_job"
fi
bash中,推送的URL為127.0.0.1:9091/metrics/job/my_job。其中job為tag_name,my_job為tag_value。我們可以以此類推添加多個tag。
比如127.0.0.1:9091/metrics/job/test_job/instance/local/就是添加了{job:test_job, instance:local}兩個tag。
如何刪除Pushgateway上的數據
類似地,使用curl就能完成刪除的指令。
curl -X DELETE 127.0.0.1:9091/metrics/job/test_job/instance/local
被動上報與主動上報
上面討論了Pushgateway的上報方式,也就是主動上報的形式。那么,普通HTTP接口和Pushgateway的區別是什么呢?
| 上報方式 | 簡述 | 優點 | 缺點 |
| 被動上報/pull | 客戶端使用library,變成exporter,然后prometheus server定時從exporter pull數據。 | 某一個節點掛了,時序缺失,Prometheus能感知到 | 無 |
| 主動上報/push | 使用pushgateway,所有客戶端push數據到pushgateway,然后prometheus server定時從pushgateway pull數據。 | 解決客戶端無法暴露接口等安全問題 | 如果某一個上報方掛了,pushgateway無法感知上報方的狀態 |
總的說來,被動上報(pull)更好,也是官方推薦的數據上報方式,但push方式能解決上報難問題,具體問題要具體分析。
設置告警規則
在yml中配置告警規則
groups:
- name: Prometheus.rules
rules:
- alert: Memory Usage Too High
expr: memory_used{job="test_job", instance="local"} > 70
for: 2m
labels:
severity: critical
annotations:
title: 'Memory Usage Too High'
description: "memory used too high"
- alert:告警規則的名稱;
- expr:基于PromQL表達式告警觸發條件,用于計算是否有時間序列滿足該條件;
- for:評估等待時間,可選參數。用于表示只有當觸發條件持續一段時間后才發送告警。在等待期間新產生告警的狀態為pending;
- labels:自定義標簽,允許用戶指定要附加到告警上的一組附加標簽;
- annotations:用于指定一組附加信息,比如用于描述告警詳細信息的文字等,annotations的內容在告警產生時會一同作為參數發送到Alertmanager;
因此,上面的告警規則定義了,當內存使用高于70%且持續2分鐘時,便會發出警告。
另外,prometheus.yml中需要配置好rule文件的路徑
rule_files:
[ - <filepath_glob> ... ]
告警規則例子
groups:
- name: hostStatsAlert
rules:
- alert: hostCpuUsageAlert
expr: |
sum(avg without (cpu)(irate(node_cpu{mode!='idle'}[5m]))) by (instance) > 0.85
for: 1m
labels:
severity: page
annotations:
summary: "Instance {{ $labels.instance }} CPU usage high"
description: "{{ $labels.instance }} CPU usage above 85% (current value: {{ $value }})"
- alert: hostMemUsageAlert
expr: |
(node_memory_MemTotal - node_memory_MemAvailable) / node_memory_MemTotal > 0.85
for: 1m
labels:
severity: page
annotations:
summary: "Instance {{ $labels.instance }} MEM usage high"
description: "{{ $labels.instance }} MEM usage above 85% (current value: {{ $value }})"
上面展示的告警規則為
- 當CPU 最近5分鐘內的平均使用率高于85%且保持了1分鐘后,發出告警
- 當內存使用率高于85%后且保持了1分鐘后,發出告警
告警方式
prometheus.yml只是定義了規則,“告警”這一動作還需由AlertManager進行。AlertManager可以通過郵件、即時通訊軟件等方式發送告警。另外,AlertManager還擁有豐富的自定義功能,管理告警間隔、告警分組、receiver分組、告警路由等等。
下面是一個最簡單的郵件發送告警,通過SMTP配置發送者,receivers中配置接收者。
global:
smtp_smarthost: 'smtp.163.com:25'
smtp_from: 'xxx@163.com'
smtp_auth_username: 'xxx@163.com'
smtp_auth_password: 'xxxxxx' # 郵箱的授權密碼
smtp_require_tls: false
templates:
- '/alertmanager/template/*.tmpl'
route:
group_by: ['alertname', 'cluster', 'service']
group_wait: 30s
group_interval: 5m
repeat_interval: 10m
receiver: default-receiver
receivers:
- name: 'default-receiver'
email_configs:
- to: 'xxx@qq.com'
html: '{ alert.html }'
headers: { Subject: "Prometheus[WARN] 內存使用過高!!!" }