Cấu hình Alert trong Prometheus và sử dụng telegram để làm công cụ gửi tin nhắn cảnh báo
Bước 01: Đầu tiên chúng ta sẽ download source cài đặt vào trong server
wget https://github.com/prometheus/alertmanager/releases/download/v0.20.0/alertmanager-0.20.0.linux-amd64.tar.gz
Bước 02: Giải nén và copy vào thư mục /usr/local để dễ dàng quản lý
tar -xvzf alertmanager-0.20.0.linux-amd64.tar.gz
mv alertmanager-0.20.0.linux-amd64 /usr/local/alertmanager
Bước 03: Tạo service trong systemd cho alertmanager
vi /etc/systemd/system/alertmanager.service
Nội dung file service như sau:
[Unit]
Description=AlertManager
Wants=network-online.target
After=network-online.target
[Service]
User=root
Group=root
Type=simple
ExecStart=/usr/local/alertmanager/alertmanager \
--config.file=/usr/local/alertmanager/alertmanager.yml
[Install]
WantedBy=multi-user.target
Enable và start service
systemctl daemon-reload
systemctl enable alertmanager
systemctl restart alertmanager
Bước 04: Cấu hình alert trong prometheus
vi /usr/local/prometheus/prometheus.yml
Tại targets: nhập thông tin ip, port , tại đây chính là ip của prometheus server và port 9093 (alertmanager dùng port này).
Bước
05: Tạo các rule alert trong prometheus. Để tạo alert trong prometheus,
bạn cần phải định nghĩa ra các rule. Tạo các file rule trong thư mục
promethues với format sau: rulename.yml
Nội dung file rule tùy các bạn định nghĩa.
Tại đây mình tạo 1 rule alert cho windows host: windows-rule.yml
vi /usr/local/prometheus/windows-rules.yml
Nội dung file rule như sau:
############# Define Rule Alert ###############
# my global config
############# Define Rule Alert ###############
groups:
- name: Windows-alert
rules:
################ Memory Usage High
- alert: Memory Usage High
expr: 100*(wmi_os_physical_memory_free_bytes) / wmi_cs_physical_memory_bytes > 90
for: 1m
labels:
severity: warning
annotations:
summary: "Memory Usage (instance {{ $labels.instance }})"
description: "Memory Usage is more than 90%\n VALUE = {{ $value }}\n LABELS: {{ $labels }}"
################ CPU Usage High
- alert: Cpu Usage High
expr: 100 - (avg by (instance) (irate(wmi_cpu_time_total{mode="idle"}[2m])) * 100) > 80
for: 1m
labels:
severity: warning
annotations:
summary: "CPU Usage (instance {{ $labels.instance }})"
description: "CPU Usage is more than 80%\n VALUE = {{ $value }}\n LABELS: {{ $labels }}"
################ Disk Usage
- alert: DiskSpaceUsage
expr:
100.0 - 100 * ((wmi_logical_disk_free_bytes{} / 1024 / 1024 ) /
(wmi_logical_disk_size_bytes{} / 1024 / 1024)) > 95
for: 1m
labels:
severity: error
annotations:
summary: "Disk Space Usage (instance {{ $labels.instance }})"
description: "Disk Space on Drive is used more than 95%\n VALUE = {{ $value }}\n LABELS: {{ $labels }}"
################ ServiceStatus
- alert: ServiceStatus
expr: wmi_service_status{status="ok"} != 1
for: 1m
labels:
severity: error
annotations:
summary: "Service Status (instance {{ $labels.instance }})"
description: "Windows Service state is not OK\n VALUE = {{ $value }}\n LABELS: {{ $labels }}"
################ CollectorError
- alert: CollectorError
expr: wmi_exporter_collector_success == 0
for: 1m
labels:
severity: error
annotations:
summary: "Collector Error (instance {{ $labels.instance }})"
description: "Collector {{ $labels.collector }} was not successful\n VALUE = {{ $value }}\n LABELS: {{ $labels }}"
Bước 06: Tiếp tục chúng ta sẽ khai báo tên của rule đó vào cấu hình của prometheus theo như hình bên dưới.
vi /usr/local/prometheus/prometheus.yml
Bước 07: Kiểm tra lại rule đã tạo bằng lệnh sau
./promtool check config prometheus.yml
Bước 08: Restart prometheus service và kiểm tra lại kết quả:
Bước 09: Ở bước này mình sẽ tạo 1 con telegram_bot sau đó gửi alert qua telegram. Download source code và build file binary
cd ${GOPATH-$HOME/go}/src/github.com
go get github.com/inCaller/prometheus_bot
cd ${GOPATH-$HOME/go}/src/github.com/inCaller/prometheus_bot
make clean
make
Copy bỏ vào thư mục /usr/local để dễ dàng quản lý.
mv /root/go/src/github.com/inCaller/prometheus_bot /usr/local
Đăng nhập vào telegram để tạo telegram bot và group nhận alert.
Cách tạo telegram bot tham khảo tại đây https://www.teleme.io/articles/create_your_own_telegram_bot?hl=vi
Khi tạo telegram bot mình sẽ ghi lại token của con bot, dùng để điền vào file config.yaml mà chúng ta tạo sau đó.
Bước 10: Tạo file cấu hình của prometheus_bot
vi /usr/local/prometheus_bot/config.yaml
Nội dung file config.yaml như sau:
telegram_token: "997872129:AAEPKYz3nPwmFsgq6ao-MdPsC5fy5z376GQ"
# ONLY IF YOU USING TEMPLATE required for test
template_path: "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
Bước 11: Tiếp tục cấu hình alert manager
vi /usr/local/alertmanager/alertmanager.yml
Thay đổi tham số url như sau: http://ipalertmanager:9087/alert/-chat_id
Bước 12: Thực hiện test gửi nhận tin nhắn trên telegram với telegram_bot với cú pháp sau:
export TELEGRAM_CHATID="-364942581"
make test
Comments
Post a Comment