Tham khảo từ: https://github.com/messagebird/sachet/
MỤC LỤC
2. Cấu hình sachet cảnh báo qua telegram
4. Cấu hình sachet cảnh báo qua sms
yum install git golang -y
go get github.com/messagebird/sachet/cmd/sachet
export PATH=$PATH:/usr/local/go/bin:/root/go/bin
Chaỵ thử với commad sachet
, nếu không thấy báo lỗi gì thì ok, sau đó nhấn ctrl + c
để thoát
Xem một số các tùy chọn:
sachet -h
Testing: mặc định thì sẽ chạy trên cổng 9876, thực hiện câu lệnh sau
curl -H "Content-type: application/json" -X POST -d '{"receiver": "team-sms", "status": "firing", "alerts": [{"status": "firing", "labels": {"alertname": "test-123"} }], "commonLabels": {"key": "value"}}' http://localhost:9876/alert
Chú ý:
- Nếu gặp một số lỗi về biến môi trường thì thực hiện một số bước sau:
cat <<EOF >> ~/.bash_profile
export GOBIN="$HOME/go/bin"
export GOPATH="$HOME/go/src"
export GOROOT="/usr/lib/golang"
EOF
source /etc/profile && source ~/.bash_profile
- Thư mục home của go nằm ở
$HOME/go/
như sau:
[root@trang-20-51 ~]# tree -L 3 /root/go
/root/go
├── bin
│ └── sachet
├── pkg
│ └── linux_amd64
│ └── github.com
└── src
├── github.com
│ └── messagebird
mkdir /etc/sachet
cat <<EOF > /etc/sachet/sachet.yml
providers:
telegram:
# Get token from Bot Father for bot
token: "677443303:AAFE7CygQnVdjP2Y7_xxxxxx"
templates:
- /etc/sachet/notifications.tmpl
receivers:
- name: 'team-telegram'
provider: 'telegram'
to:
# Telegram ID user that want to send message
- 'xxxxxx'
text: '{{ template "telegram_message" . }}'
EOF
cat <<EOF > /etc/sachet/notifications.tmpl
{{ define "telegram_title" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .CommonLabels.alertname }} @ {{ .CommonLabels.identifier }} {{ end }}
{{ define "telegram_message" }}
{{ if gt (len .Alerts.Firing) 0 }}
*Alerts Firing:*
{{ range .Alerts.Firing }}• {{ .Labels.instance }}: {{ .Annotations.description }}
{{ end }}{{ end }}
{{ if gt (len .Alerts.Resolved) 0 }}
*Alerts Resolved:*
{{ range .Alerts.Resolved }}• {{ .Labels.instance }}: {{ .Annotations.description }}
{{ end }}{{ end }}{{ end }}
{{ define "telegram_text" }}{{ template "telegram_title" .}}
{{ template "telegram_message" . }}{{ end }}
EOF
sachet --config /etc/sachet/sachet.yml
Sau đó gửi thử một cảnh báo tới địa chỉ lắng nghe của sachet 192.168.20.51:9876
curl -H "Content-type: application/json" -X POST -d '{"receiver": "team-telegram", "status": "firing", "alerts": [{"staus": "firing", "labels": {"alertname": "test-123"} }], "commonLabels": {"key": "value"}}' http://localhost:9876/alert
Không báo lỗi gì thì ok, kết quả sẽ có một message gửi về telegram:
touch /etc/systemd/system/sachet.service
cat <<EOF > /etc/systemd/system/sachet.service
[Unit]
Description=Sachet
Wants=network-online.target
After=network-online.target
[Service]
User=root
Group=root
Type=simple
ExecStart=/root/go/bin/sachet \
--config /etc/sachet/sachet.yml \
-listen-address :9876
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl restart sachet
systemctl enable sachet
systemctl status sachet
Chỉnh sửa file cấu hình của alertmanager /etc/alertmanager/alertmanager.yml
tương tự như sau:
global:
slack_api_url: 'https://hooks.slack.com/services/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'
route:
group_by: [alertname, datacenter, app]
receiver: 'team-telegram'
group_wait: 30s
group_interval: 1m
repeat_interval: 1h
# routes:
# - receiver: 'team-1'
receivers:
- name: 'team-telegram' # name giong voi cau hinh cua sachet
slack_configs:
- channel: '#prometheus_alerts'
text: "<!channel> \nsummary: {{ .CommonAnnotations.summary }}\ndescription: {{ .CommonAnnotations.description }}"
webhook_configs:
- url: 'http://192.168.20.51:9876/alert'
Trong đó: http://192.168.20.51:9876/alert
là địa chỉ mà sachet đang lắng nghe trên đó
Khởi động lại dịch vụ:
systemctl restart alertmanager
Kết quả khi có cảnh báo gửi tới alertmanager, một message sẽ được gửi tới sachet, sachet sẽ xử lý và gửi về telegram của user:
curl -X POST https://rest.messagebird.com/messages \
-H 'Authorization: AccessKey kkqSeUsa7c1RBs2nyC8LqMfdj' \
-d "recipients=+84377328363" \
-d "originator=+84377328363" \
-d "body=Hello World, I am a text message."
Ví dụ sử dụng provider là messagebird thì cấu hình như sau:
providers:
telegram:
token: "677443303:AAFE7CygQnVdjP2Y7_xxxx"
messagebird:
access_key: 'test_3zInZPiDway4ibqxad1Nim9ul' # test or live
debug: true
templates:
- /etc/sachet/notifications.tmpl
receivers:
- name: 'team-telegram'
#provider: 'telegram'
#to:
# - 'xxxxxxx'
#text: '{{ template i"telegram_message" . }}'
#text: '{{ .GroupLabels.alertname }} @ {{ .Labels.instance }}: {{ .Status | toUpper }}'
provider: 'messagebird'
to:
- '+843xxxxx'
from: '+843xxxxx'
Phần này chưa làm được, gửi đi không lỗi nhưng, không nhận được, nên sẽ đổi sang dùng twilio
Truy cập trang https://www.twilio.com, tạo tài khoản dùng thử, và xin một TRIAL NUMBER để sử dụng, Twilio cung cấp một TRIAL BALANCE là $14.5. Sau đó Twilio sẽ cung cấp một số các thông tin như sau:
Copy lại ACCOUNT SID, AUTH TOKEN và TRIAL NUMBER để chỉnh sửa file cấu hình của sachet như sau:
[root@trang-20-51 ~]# cat /etc/sachet/sachet.yml
providers:
twilio:
account_sid: 'AC798ab97xx'
auth_token: 'a49aavbd75xx'
templates:
- /etc/sachet/notifications.tmpl
receivers:
- name: 'team-telegram'
provider: 'twilio'
to:
- '+84377xxx'
from: '+176xxx06097'
Khởi động lại dịch vụ:
systemctl restart sachet
Kết quả sẽ được gửi tới số điện thoại "+84377xxx":