ToiVietBlog - Ở bài viết này, mình sẽ hướng dẫn các bạn cài đặt Alertmanager và thiết lập cảnh báo Prometheus.
Mục lục nội dung
Alertmanager là gì?
Nếu bạn chưa biết về Alertmanager thì nó là một thành phần không thể thiếu khi sử dụng Prometheus. Alertmanager sẽ xử lý các cảnh báo gửi từ ứng dụng client như là Prometheus server.
Giải thích một cách dễ hiểu là:
- Bạn thu thập dữ liệu monitor từ các endpoint và lưu trong database Prometheus.
- Bạn tạo các rule alert trong Prometheus, ví dụ: ram server > 70%.
- Và bạn đẩy các alert này về Alertmanager, nó sẽ xử lý việc trùng lặp alert, group các alert và gửi alert tới kênh thông báo mà bạn muốn: mail, telegram,…
Cài đặt Alertmanager
Bạn kiểm tra phiên bản mới nhất của phần mềm tại link này.
Tải và giải nén phần mềm.
wget https://github.com/prometheus/alertmanager/releases/download/v0.21.0/alertmanager-0.21.0.linux-amd64.tar.gz tar -xvzf alertmanager-0.21.0.linux-amd64.tar.gz && rm -f alertmanager-0.21.0.linux-amd64.tar.gz cd alertmanager-0.21.0.linux-amd64/
Chuyển file thực thi binary vào thư mục local, tạo thư mục config cho alertmanager.
mv alertmanager /usr/local/bin/ mkdir -p /etc/alertmanager/data mv alertmanager.yml /etc/alertmanager/
Tạo user alertmanager.
useradd -rs /bin/false alertmanager chown alertmanager:alertmanager /usr/local/bin/alertmanager chown -R alertmanager:alertmanager /etc/alertmanager/*
Tạo file systemd cho dịch vụ alertmanager.
nano /etc/systemd/system/alertmanager.service
Copy nội dung sau đây vào file.
[Unit] Description=Alert Manager Service Wants=network-online.target After=network-online.target [Service] Type=simple User=alertmanager Group=alertmanager ExecReload=/bin/kill -HUP $MAINPID ExecStart=/usr/local/bin/alertmanager \ --config.file=/etc/alertmanager/alertmanager.yml \ --web.listen-address=":9093" \ --storage.path=/etc/alertmanager/data SyslogIdentifier=alertmanager Restart=always [Install] WantedBy=multi-user.target
Reload daemon và khởi động dịch vụ.
systemctl daemon-reload systemctl enable alertmanager.service systemctl start alertmanager.service
Thiết lập virtual host cho Alertmanager
Bây giờ bạn có thể mở trình duyệt và truy cập IP:9093
, ví dụ: http://192.168.1.10:9093
.
Nếu bạn sử dụng nginx proxy cho Prometheus như bài mình đã viết, bạn có thể truy cập alertmanager thông qua domain.
Bạn mở file virtual host của domain prometheus lên và thêm block sau vào file.
location /alertmanager/ { proxy_pass http://127.0.0.1:9093/; include /etc/nginx/conf.d/resource/proxypass.inc; auth_basic "Prometheus"; auth_basic_user_file /etc/nginx/htpasswd/prometheus; }
File virtual host sẽ giống như sau.
... server { listen 443 ssl http2; ... location / { ... } ... location /alertmanager/ { proxy_pass http://127.0.0.1:9093/; include /etc/nginx/conf.d/resource/proxypass.inc; auth_basic "Prometheus"; auth_basic_user_file /etc/nginx/htpasswd/prometheus; } }
Reload dịch vụ nginx.
service nginx reload
Bây giờ, bạn có thể mở trình duyệt và truy cập thông qua domain, ví dụ: https://prometheus.example.com/alertmanager
.
Tạo các file rule Prometheus
Trong bài viết cài đặt Prometheus, mình có tạo thư mục /etc/prometheus/rules
, mình sẽ tạo các file rule trong thư mục này.
Ví dụ, mình sẽ tạo file rule check nếu ping host down.
nano /etc/prometheus/rules/ping.yml
Copy nội dung dưới vào file và lưu lại.
groups: - name: Ping rules: - alert: Ping Host expr: probe_success{job="blackbox_ping"} == 0 for: 5s labels: severity: "Critical" annotations: summary: 'Host {{ $labels.instance }} down more than 5 second.'
Thiết lập file cấu hình Prometheus
Sau khi bạn tạo file rule xong, bây giờ bạn sẽ thiết lập Prometheus đẩy các alert về Alertmanager.
Bạn mở file prometheus.yml
.
nano /etc/prometheus/prometheus.yml
Bạn tìm trong file (đoạn trên đầu) và sửa lại giống như sau.
# Alertmanager configuration alerting: alertmanagers: - static_configs: - targets: - localhost:9093 # Load rules once and periodically evaluate them according to the global 'evaluation_interval'. rule_files: # - "first_rules.yml" # - "second_rules.yml" - "/etc/prometheus/rules/ping.yml"
Bây giờ, bạn reload lại dịch vụ prometheus và alertmanager.
service prometheus reload service alertmanager reload
Bạn có thể kiểm tra các rule trên giao diện Prometheus, bạn chọn menu Alerts.

Kết luận
Vậy là bài viết này đã hướng dẫn các bạn từng bước để cài đặt thành công Alertmanager. Tạo một rule trong Prometheus và thiết lập đẩy alert về Alertmanager. Trong bài viết tiếp theo, mình sẽ cài đặt Telegram Bot để có thể gửi các cảnh báo này về Telegram cho bạn.
If you appreciate what we share in this blog, you can support us by:
- Stay connected to: Facebook | Twitter | Google Plus | YouTube
- Subscribe email to recieve new posts from us: Sign up now.
- Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
- Become a Supporter - Make a contribution via PayPal.
- Support us by purchasing Ribbon Lite Child theme being using on this website.
We are thankful for your support.