1. 程式人生 > >Prometheus+AlertManager實現郵件報警

Prometheus+AlertManager實現郵件報警

AlertManager下載

https://prometheus.io/download/

解壓

新增配置檔案test.yml,配置收發郵件郵箱

 

Prometheus下載配置參考我的另一篇:

https://www.cnblogs.com/caizhenghui/p/9132414.html

 

參考配置:

複製程式碼
global:
  smtp_smarthost: 'smtp.163.com:25'  #163伺服器
  smtp_from: '[email protected]'        #發郵件的郵箱
  smtp_auth_username: '
[email protected]
'  #發郵件的郵箱使用者名稱,也就是你的郵箱 smtp_auth_password: 'XXX'        #發郵件的郵箱密碼 route: group_by: ['alertname'] repeat_interval: 1h receiver: live-monitoring receivers: - name: 'live-monitoring' email_configs: - to: '[email protected]'        #收郵件的郵箱
複製程式碼

更多配置參考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~