AlertManager下載
https://prometheus.io/download/
解壓
添加配置文件test.yml,配置收發郵件郵箱
參考配置:
global: smtp_smarthost: 'smtp.163.com:25' #163服務器 smtp_from: 'XXX@163.com' #發郵件的郵箱 smtp_auth_username: 'XXX@163.com' #發郵件的郵箱用戶名,也就是你的郵箱 smtp_auth_password: 'XXX' #發郵件的郵箱密碼 route: group_by: ['alertname'] repeat_interval: 1h receiver: live-monitoring receivers: - name: 'live-monitoring' email_configs: - to: 'czh1226@qq.com' #收郵件的郵箱
更多配置參考alertmanager包中的simple.yml
添加報警規則
prometheus targets 監控報警參考配置(node_down.yml):
按 Ctrl+C 復制代碼
按 Ctrl+C 復制代碼
節點內存使用率監控報警參考配置(memory_over.yml)
groups:
- name: example
rules:
- alert: NodeMemoryUsage
expr: (node_memory_MemTotal_bytes - (node_memory_MemFree_bytes+node_memory_Buffers_bytes+node_memory_Cached_bytes )) / node_memory_MemTotal_bytes * 100 > 80
for: 1m
labels:
user: caizh
annotations:
summary: "{{$labels.instance}}: High Memory usage detected"
description: "{{$labels.instance}}: Memory usage is above 80% (current value is:{{ $value }})"
當然,想要監控節點內存需要提前配置好node_exporter
修改prometheus配置文件prometheus.yml,開啟報警功能,添加報警規則配置文件
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets: ["localhost:9093"]
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "node_down.yml"
- "memory_over.yml"
配置完成!
啟動alertmanager
./alertmanager --config.file test.yml
啟動prometheus(默認會調用prometheus.yml)
./prometheus
http://localhost:9090/alerts
看配置與報警規則是否添加成功
成功則如下圖:

我的Prometheus Targets如下:

嘗試kill一個測試是否可以用郵件報警
例如在slave1節點上:
hadoop-daemon.sh stop datanode
InstanceDown會變成(1 active),並處在PENDING狀態

1min后變FIRING狀態

耐心等待幾分鍾,會收到報警郵件:

郵件可能會有延時,耐心等一會~
想測試內存使用率可以多開點占內存的服務,或者把報警規則中內存占用超過80%報警調小一些
Over~

