一,下載alertmanager
1,官網下載地址
https://prometheus.io/download/
2,原理圖:
二,下載和安裝
1,下載安裝包:
選擇linux+amd64的版本下載:
# wget https://github.com/prometheus/alertmanager/releases/download/v0.27.0/alertmanager-0.27.0.linux-amd64.tar.gz
2,安裝
解壓:
# tar -zxvf alertmanager-0.27.0.linux-amd64.tar.gz
移動到安裝目錄:
# mv alertmanager-0.27.0.linux-amd64 /usr/local/soft/
3,檢視版本:
# /usr/local/soft/alertmanager-0.27.0.linux-amd64/alertmanager --version
alertmanager, version 0.27.0 (branch: HEAD, revision: 0aa3c2aad14cff039931923ab16b26b7481783b5)
build user: root@22cd11f671e9
build date: 20240228-11:51:20
go version: go1.21.7
platform: linux/amd64
tags: netgo
三,配置、啟動
1,配置檔案:
# vi alertmanager.yml
內容:
global:
resolve_timeout: 5m
smtp_smarthost: 'smtp.163.com:465'
smtp_from: '您的郵箱'
smtp_auth_username: '您的郵箱使用者名稱'
smtp_auth_password: '此處替換為您郵箱的authcode'
smtp_require_tls: false
route:
group_by: ['alertname']
group_wait: 30s
group_interval: 5m
repeat_interval: 1h
receiver: 'web.hook'
receivers:
- name: 'web.hook'
email_configs:
- to: '此處替換為您的收件郵箱'
webhook_configs:
- url: 'http://127.0.0.1:5001/'
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
測試配置是否正確:
# ./amtool check-config alertmanager.yml
Checking 'alertmanager.yml' SUCCESS
Found:
- global config
- route
- 1 inhibit rules
- 1 receivers
- 0 templates
2,使用systemctl管理服務:
# vi /lib/systemd/system/alertmanager.service
內容:
[Unit]
Description=Alert Manager
wants=network-online.target
After=network-online.target
[Service]
Type=simple
Restart=always
ExecStart=/usr/local/soft/alertmanager-0.27.0.linux-amd64/alertmanager --config.file=/usr/local/soft/alertmanager-0.27.0.linux-amd64/alertmanager.yml --storage.path=/usr/local/so
ft/alertmanager-0.27.0.linux-amd64/data/
[Install]
WantedBy=multi-user.target
重新載入服務
# systemctl daemon-reload
3,啟動:
# systemctl start alertmanager.service
4,檢視埠:可以看到啟用了9093/9094兩個埠
# ss -lntp | grep alert
LISTEN 0 4096 *:9093 *:* users:(("alertmanager",pid=474456,fd=8))
LISTEN 0 4096 *:9094 *:* users:(("alertmanager",pid=474456,fd=3))
5,服務啟動後檢視配置:
訪問: 你的ip:9093 可以看到服務的狀態
如圖:
四,用postman測試傳送一封郵件
說明:
地址用:ip:9093/api/v2/alerts
方法用:POST
選擇Body,
然後選擇raw,
輸入郵件內容後,點 send
如圖: