Prometheus配合Alertmanager報警系統

Promethous+Alertmanager+Grafana

監控技術棧如下:
Prometheus(最新版):基於TSDB的微服務指標採集&報警;
Alertmanager:報警服務;
Grafana(>=5.x):監控報表展示。

一、軟件部署

1.1 Prometheus安裝
# wget https://github.com/prometheus/prometheus/releases/download/v2.5.0/prometheus-2.5.0.linux-amd64.tar.gz
# tar zxvf prometheus-2.5.0.linux-amd64.tar.gz -C /usr/local/
# cd /usr/local/
# ln -s prometheus-2.5.0.linux-amd64 prometheus
# chown work:work prometheus* -R
# cd prometheus
# ls
# console_libraries consoles LICENSE NOTICE prometheus prometheus.yml promtool
1.2 Alertmanager安裝
# wget https://github.com/prometheus/alertmanager/releases/download/v0.15.3/alertmanager-0.15.3.linux-amd64.tar.gz
# tar zxvf alertmanager-0.15.3.linux-amd64.tar.gz 
# ln -s alertmanager-0.15.3.linux-amd64 alertmanager
# chown work:work alertmanager* -R
# cd alertmanager
# ls
# alertmanager alertmanager.yml amtool LICENSE NOTICE
1.3 Grafana安裝
# wget wget https://dl.grafana.com/oss/release/grafana-5.3.4-1.x86_64.rpm 
# rpm -Uvh grafana-5.3.4-1.x86_64.rpm 
# systemctl restart grafana.service

二、服務配置

2.1 Prometheus配置

指定服務監聽alertmanager端口及報警規則目錄

vim /usr/local/prometheus/prometheus.yml

#配置alertmanager信息
alerting:
  alertmanagers:
  - static_configs:
    - targets: ['localhost:9093']
#配置告警規則目錄
rule_files:
  - /usr/local/prometheus/rules/*.rules
2.2 Rules策略配置

創建一個服務down的報警規則

vim /usr/local/prometheus/rules/service_down.rules

groups:
- name: ServiceStatus #規則組名稱   
  rules:
  - alert: ServiceStatusAlert  #單個規則的名稱
    expr: up == 0   #匹配規則, up==0
    for: 10s        #持續時間
    labels:         #標籤
      project: APP    #自定義lables
    annotations:            #告警正文
      summary: "Instance {{ $labels.instance }} down"
      description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minutes."
2.3 Alertmanager配置
vim /usr/local/alertmanager/alertmanager.yml
 
 
#全局配置,比如配置發件人
global:
  resolve_timeout: 5m    #處理超時時間,默認爲5min
  smtp_smarthost: 'smtp.163.com:25'  # 郵箱smtp服務器代理
  smtp_from: '[email protected]' # 發送郵箱名稱
  smtp_auth_username: '[email protected]' # 郵箱名稱
  smtp_auth_password: '12345678xxOO'              # 郵箱密碼或授權碼
 
 
# 定義模板信息,可以自定義html模板,發郵件的時候用自己定義的模板內容發
templates:
  - 'template/*.tmpl'
 
# 定義路由樹信息,這個路由可以接收到所有的告警,還可以繼續配置路由,比如project: APP(prometheus 告警規則中自定義的lable)發給誰
route:
  group_by: ['alertname'] # 報警分組依據
  group_wait: 10s         # 最初即第一次等待多久時間發送一組警報的通知
  group_interval: 60s     # 在發送新警報前的等待時間
  repeat_interval: 1h     # 發送重複警報的週期 對於email配置中,此項不可以設置過低,否則將會由於郵件發送太多頻繁,被smtp服務器拒絕
  receiver: 'email'       # 發送警報的接收者的名稱,以下receivers name的名稱
 

# 定義警報接收者信息
receivers:
  - name: 'email'  # 路由中對應的receiver名稱
    email_configs: # 郵箱配置
    - to: '[email protected]'   # 接收警報的email配置
      #html: '{{ template "test.html" . }}'  # 設定郵箱的內容模板

三、服務啓動

3.1 Prometheus啓動

/usr/local/prometheus/prometheus --config.file=prometheus.yml --web.enable-lifecycle --web.external-url=http://127.0.0.1:9090 --storage.tsdb.path=/data1/prometheus/data &

3.2 Alertmanager啓動

/usr/local/alermanager/alertmanager &

四、報警驗證

4.1 Prometheus
4.2 Rules
4.3 Alerts
4.4 Mails

五、參考

https://yunlzheng.gitbook.io/prometheus-book/parti-prometheus-ji-chu/alert
https://prometheus.io/docs/prometheus/latest/getting_started/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章