Hướng dẫn cấu hình Prometheus tích hợp cảnh báo qua Telegram – Phần 6

Ở những bài trước của loạt bài này, tôi đã hướng dẫn các bạn cấu hình Alert Manager cảnh báo với Email và Slack, bài này tôi sẽ hướng dẫn cấu hình cảnh báo qua Telegram.

Xem lại bài trước ở đây Phần 5.

1. Tạo bot để gửi cảnh báo trên telegram

Bước 1: Chat với BotFather để tạo ra một con bot trên telegram, lưu lại token của bot để sử dụng cho bước sau

Token có dạng: 6732414303:AAHefSxJWdt……DnU

2. Lấy telegram id để gửi cảnh báo

Bước 2: Nếu cảnh báo đến 1 user, thực hiện chat với bot userinfobot để lấy telegram id của mình.

Nếu cảnh báo đến một group:

+ Thực hiện add bot đã tạo ở bước 1 vào group,

+ Tiếp tục chat một tin nhắn bất kỳ vào group, sau đó truy cập vào “https://api.telegram.org/bot[TOKEN]/getUpdates” (thay [TOKEN] thành token của bot), lấy group id. Thường group id có dang -423123876

3. Cấu hình trên Alertmanager node

Bước 3: Cài đặt Prometheus Bot để gửi cảnh báo trên Alertmanager node

yum install git golang -y
go get github.com/inCaller/prometheus_bot
cd go/src/github.com/inCaller/prometheus_bot/
make clean
make
cp prometheus_bot /usr/bin
  • Kiểm tra cài đặt thành:
[root@alertmanager prometheus_bot]# prometheus_bot --help
Usage of prometheus_bot:
  -c string
        Path to a config file (default "config.yaml")
  -d    Debug template
  -l string
        Listen address (default ":9087")
  -t string
        Path to a template file
  • Tạo một file /etc/systemd/system/prometheus-bot.service với nội dung như sau:
[Unit]
Description=Prometheus-bot
Wants=network-online.target
After=network-online.target

[Service]
User=alertmanager
Group=alertmanager
Type=simple
WorkingDirectory=/etc/alertmanager/
ExecStart=/usr/bin/prometheus_bot -c /etc/alertmanager/config.yaml -l "0.0.0.0:9087"

[Install]
WantedBy=multi-user.target
  • Khởi động dịch vụ
systemctl daemon-reload
systemctl restart prometheus-bot
systemctl enable prometheus-bot

Bước 4: Tạo một file config cho Prometheus Bot, tạo file /etc/alertmanager/config.yaml có nội dung như sau:

telegram_token: "67732343:AAHeajs...dkU"

# ONLY IF YOU USING TEMPLATE required for test
template_path: "/etc/alertmanager/template.tmpl"
time_zone: "Asia/Ho_Chi_Minh"
split_token: "|"

# ONLY IF YOU USING DATA FORMATTING FUNCTION, NOTE for developer: important or test fail
time_outdata: "02/01/2006 15:04:05"
split_msg_byte: 4000

Sửa lại telegram_tokentemplate_path phù hợp

Bước 5: Tạo một template file /etc/alertmanager/template.tmpl

<b>Scada UUID:</b>{{ index .CommonLabels "scada_uuid"}}
{{ $length := len .GroupLabels -}} {{ if ne $length 0 }}
<b>Grouped for:</b>
{{ range $key,$val := .GroupLabels -}}
    {{$key}} = <code>{{$val}}</code>
{{ end -}}
{{ end -}}

{{if eq .Status "firing"}}
Status: <b>{{.Status | str_UpperCase}} 🔥</b>
{{end -}}

{{if eq .Status "resolved"}}
Status: <b>{{.Status | str_UpperCase}} ✅</b>
{{end }}
<b>Active Alert List:</b>
{{- range $val := .Alerts }}
  Alert: <a href="{{ $val.GeneratorURL }}">{{ index $val.Annotations "name" }}</a>
  Current value:{{if HasKey $val.Annotations "measureUnit" -}}
  {{str_Format_MeasureUnit (index $val.Annotations "measureUnit") (index $val.Annotations "value")}}
  {{else -}}
  {{if HasKey $val.Annotations "value" -}}
  {{ index $val.Annotations "value" | str_FormatFloat}}
  {{end -}}
  {{end -}}
  {{$severity := index $val.Labels "severity" -}}
  {{if eq $severity "Warning" -}}
  Severity: {{$severity}} ⚠️
  {{else if eq $severity "Critical" -}}
  Severity: {{$severity}} 🚨
  {{else -}}
  Severity: {{$severity}}
  {{ end -}}
  Active from: {{ $val.StartsAt | str_FormatDate }}
  {{ range $key, $value := $val.Annotations -}}
  {{ if and (and (ne $key "name") (ne $key "value")) (ne $key "measureUnit") }}
  {{$key}}: {{$value}}
  {{- end -}}
{{- end -}}
{{- end -}}

Bước 6: Khởi động lại Prometheus Bot

systemctl restart prometheus-bot

Bước 7: Thêm cấu hình cho Alert manager, thêm vào file /etc/alertmanager/alertmanager.yml phần cấu hình webhook vào receivers muốn nhận cảnh báo:

webhook_configs:
    - url: 'http://localhost:9087/alert/000000000'

Thay thế “000000000” là telegram_id của người nhận hoặc group nhận cảnh báo. Ví dụ một file cấu hình hoàn chỉnh như sau:

global:
  smtp_smarthost: 'smtp.bizflycloud.vn:587'
  smtp_from: '[email protected]'
  smtp_auth_username: '[email protected]'
  smtp_auth_password: 'xxxxxxx'
  slack_api_url: 'https://hooks.slack.com/services/Tasjdjhgjdkkaxxxxx'



route:
  group_by: [alertname, datacenter, app]
  receiver: 'group-1'

receivers:
  - name: 'group-1'
    email_configs:
    - to: '[email protected]'
    slack_configs:
    - channel: '#prometheus_alerts'
      text: "<!channel> \nsummary: {{ .CommonAnnotations.summary }}\ndescription: {{ .CommonAnnotations.description }}"
    webhook_configs:
    - url: 'http://localhost:9087/alert/0000000'

4. Kiểm tra cấu hình thành công

Các rules để test được cấu hình trên prometheus server tương tự với phần trước (cảnh báo gmail, slack). Thực hiện tắt 1 node, đợt khoảng 10s nếu thấy có cảnh bảo gửi về telegram được cấu hình ở bước 7 tương tự như dưới đây là bạn đã cấu hình thành công.

Sau khi node bị tắt hoạt động lại bình thường sẽ có thông báo như sau:

Chúc các bạn thành công! 😀

Leave a Comment