前言?
1. Alertmanager 發(fā)送告警的介紹
Prometheus 對(duì)指標(biāo)的收集、存儲(chǔ)與告警能力分屬于 Prometheus Server 和 AlertManager 兩個(gè)獨(dú)立的組件,前者僅負(fù)責(zé)定義告警規(guī)則生成告警通知, 具體的告警操作則由后者完成。
Alertmanager 負(fù)責(zé)處理由 Prometheus Server 發(fā)來的告警通知,Alertmanager對(duì)告警通知進(jìn)行分組、去重后,根據(jù)路由規(guī)則將其路由到不同的receiver,如Email、釘釘或企業(yè)微信等。
除了基本的告警通知能力外,Altermanager還支持對(duì)告警進(jìn)行去重、分組、抑制、靜默和路由等功能:
●分組(Grouping):將相似告警合并為單個(gè)告警通知的機(jī)制,在系統(tǒng)因大面積故障而觸發(fā)告警潮時(shí),分組機(jī)制能避免用戶被大量的告警噪聲淹沒,進(jìn)而導(dǎo)致關(guān)鍵信息的隱沒
●抑制(Inhibition):系統(tǒng)中某個(gè)組件或服務(wù)故障而觸發(fā)告警通知后,那些依賴于該組件或服務(wù)的其它組件或服務(wù)可能也會(huì)因此而觸發(fā)告警,抑制便是避免類似的級(jí)聯(lián)告警的一種特性,從而讓用戶能將精力集中于真正的故障所在
●靜默(Silent):是指在一個(gè)特定的時(shí)間窗口內(nèi),即便接收到告警通知,Alertmanager也不會(huì)真正向用戶發(fā)送告警信息的行為;通常,在系統(tǒng)例行維護(hù)期間,需要激活告警系統(tǒng)的靜默特性
●路由(route):用于配置Alertmanager如何處理傳入的特定類型的告警通知,其基本邏輯是根據(jù)路由匹配規(guī)則的匹配結(jié)果來確定處理當(dāng)前告警通知的路徑和行為
?
2.Alertmanager郵箱報(bào)警設(shè)置
(1)上傳 alertmanager-0.24.0.linux-amd64.tar.gz 到 /opt 目錄中,并解壓
cd /opt/
tar xf alertmanager-0.24.0.linux-amd64.tar.gz
mv alertmanager-0.24.0.linux-amd64 /usr/local/alertmanager
(2)修改 alertmanager 配置文件,添加郵件告警路由信息
vim /usr/local/alertmanager/alertmanager.yml
#global 配置段用于定義全局配置
#templates 配置段負(fù)責(zé)自定義告警內(nèi)容模板文件
#route 配置段用于指定如何處理傳入的告警
#receiver 配置段則定義了告警信息的接收器,每個(gè)接收器都應(yīng)該有其具體的定義
?
global: ? ? ? ? ? ? ? ? ? ? ? #在全局配置段設(shè)置發(fā)件人郵箱信息
? resolve_timeout: 5m ? ? ? ? ? ? #定義持續(xù)多長(zhǎng)時(shí)間未接收到告警通知后,就將告警狀態(tài)標(biāo)記為resolved
? smtp_smarthost: 'smtp.qq.com:25'
? smtp_from: '這里使用的是個(gè)人郵箱'
? smtp_auth_username: '這里使用的是個(gè)人郵箱'
? smtp_auth_password: 'xxxxxx' ? ?#此處為授權(quán)碼,登錄QQ郵箱【設(shè)置】->【賬戶】中的【生成授權(quán)碼】獲取
? smtp_require_tls: false ? ? ? ? #禁用TLS的傳輸方式
route: ? ? ? ? ? ? ? ? ? ? ? ?#設(shè)置告警的分發(fā)策略
? group_by: ['alertname'] ? ? ? ? #采用哪個(gè)標(biāo)簽來作為分組依據(jù),這里使用告警名稱做為規(guī)則,滿足規(guī)則的告警將會(huì)被合并到一個(gè)通知中
? group_wait: 20s ? ? ? ? ? ? ? ? #一組告警第一次發(fā)送之前等待的時(shí)延,即產(chǎn)生告警20s將組內(nèi)新產(chǎn)生的消息合并發(fā)送,通常是0s~幾分鐘(默認(rèn)是30s)
? group_interval: 5m ? ? ? ? ? ? ?#一組已發(fā)送過初始告警通知的告警,接收到新告警后,下次發(fā)送通知前等待時(shí)延,通常是5m或更久(默認(rèn)是5m)
? repeat_interval: 20m ? ? ? ? ? ?#一組已經(jīng)發(fā)送過通知的告警,重復(fù)發(fā)送告警的間隔,通常設(shè)置為3h或者更久(默認(rèn)是4h)
? receiver: 'my-email' ? ?#定義告警接收人
receivers: ? ? ? ? ? ? ? ? ? ?#設(shè)置收件人郵箱信息
- name: 'my-email'
? email_configs:
? - to: '收件人的郵箱' ? ?#設(shè)置收件人郵箱地址
? ? send_resolved: true
?
?
?
(3)配置啟動(dòng)文件
cat > /usr/lib/systemd/system/alertmanager.service <<'EOF'
[Unit]
Description=alertmanager
Documentation=https://prometheus.io/
After=network.target
?
[Service]
Type=simple
ExecStart=/usr/local/alertmanager/alertmanager \
--config.file=/usr/local/alertmanager/alertmanager.yml \
--log.level=debug
?
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
?
[Install]
WantedBy=multi-user.target
EOF
?
#啟動(dòng) Alertmanager
systemctl start alertmanager
systemctl enable alertmanager
?
netstat -natp | grep :9093
(4)添加告警規(guī)則?
mkdir /usr/local/prometheus/alter_rules
?
vim /usr/local/prometheus/alter_rules/instance_down.yaml
groups:
#若某個(gè) Instance 的 up 指標(biāo)的值轉(zhuǎn)為 0 持續(xù)超過 1 分鐘后,將觸發(fā)告警
- name: AllInstances
? rules:
? - alert: InstanceDown ? ? ? ? ? ? ? ? ?#告警規(guī)則的名稱,一個(gè)組內(nèi)的告警規(guī)則名稱必須惟一
? ? # Condition for alerting
? ? expr: up == 0 ? ? ? ? ? ? ? ? ? ? ? ?#基于PromQL表達(dá)式的告警觸發(fā)條件(布爾表達(dá)式)
? ? for: 1m ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#控制在觸發(fā)告警之前,測(cè)試表達(dá)式的值必須為true的時(shí)長(zhǎng)
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #表達(dá)式值為true,但其持續(xù)時(shí)間未能滿足for定義的時(shí)長(zhǎng)時(shí),相關(guān)的告警狀態(tài)為pending
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #滿足該時(shí)長(zhǎng)之后,相關(guān)的告警將被觸發(fā),并轉(zhuǎn)為firing狀態(tài)
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #表達(dá)式的值為false時(shí),告警將處于inactive狀態(tài)
? ? # Annotation - additional informational labels to store more information
? ? annotations: ? ? ? ? ? ? ? ? ? ? ? ? #附加在告警之上的注解信息
? ? ? title: 'Instance down'
? ? ? description: Instance has been down for more than 1 minute.'
? ? # Labels - additional labels to be attached to the alert
? ? labels:
? ? ? severity: 'critical' ? ? ? ? ? ? ? #在告警上附加的自定義的標(biāo)簽
#CPU 使用率大于 80% 觸發(fā)告警
- name: node_alert
? rules:
? - alert: cpu_alert
? ? expr: 100 -avg(irate(node_cpu_seconds_total{mode="idle"}[1m])) by (instance)* 100 > 80
? ? for: 5m
? ? labels:
? ? ? level: warning
? ? annotations:
? ? ? description: "instance: {{ $labels.instance }} ,cpu usage is too high ! value: {{$value}}"
? ? ? summary: ?"cpu usage is too high"
?(5)修改?prometheus?配置文件,添加 Alertmanager 實(shí)例的配置
vim /usr/local/prometheus/prometheus.yml
......
alerting:
? alertmanagers:
? ? - static_configs:
? ? ? ? - targets:
? ? ? ? ? ? - 192.168.80.30:9093 #任意一臺(tái)k8s主機(jī)IP及exporter服務(wù)監(jiān)聽端口
?
rule_files:
? - "/usr/local/prometheus/alter_rules/*.yaml"
??
?
systemctl reload prometheus
?
?
?(6)進(jìn)行報(bào)警測(cè)試
systemctl stop node_exporter.service
?
?3.Alertmanager釘釘報(bào)警設(shè)置
(1)上傳 prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz 到 /opt 目錄中,并解壓
cd /opt/
tar xf prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz
mv prometheus-webhook-dingtalk-2.1.0.linux-amd64 /usr/local/dingtalk
?(2)登錄阿里釘釘,并且進(jìn)行設(shè)置
?創(chuàng)建群 -> 群設(shè)置 -> 智能群助手 -> 添加機(jī)器人 -> 添加機(jī)器人 -> 自定義
消息推送 ? ?開啟
Webhook ? ? 復(fù)制
安全設(shè)置 -> 勾選 加簽 -> 復(fù)制
點(diǎn)擊完成
(3)修改 dingtalk 告警插件配置文件 ?
cd /usr/local/dingtalk
cp -p config.example.yml config.yml
?
vim config.yml
timeout: 5s
?
## Uncomment following line in order to write template from scratch (be careful!)
#no_builtin_template: true
?
## Customizable templates path
templates:
? - contrib/templates/legacy/template.tmpl
?
## You can also override default template using `default_message`
## The following example to use the 'legacy' template from v0.3.0
default_message:
? title: '{{ template "legacy.title" . }}'
? text: '{{ template "legacy.content" . }}'
?
## Targets, previously was known as "profiles"
targets:
? webhook1:
? ? url: <粘貼Webhook的內(nèi)容>
? ? # secret for signature
? ? secret: <粘貼加簽的內(nèi)容>
?
#啟動(dòng)服務(wù)?
./prometheus-webhook-dingtalk文章來源:http://www.zghlxwxcb.cn/news/detail-600305.html
(4)修改 alertmanager 配置文件
vim /usr/local/alertmanager/alertmanager.yml
global:
? resolve_timeout: 5m
?
route:
? group_by: [alertname]
? group_wait: 10s
? group_interval: 15s
? repeat_interval: 20m
? receiver: 'dingding.webhook1'
?
receivers:
- name: 'dingding.webhook1'
? webhook_configs:
? - url: 'http://192.168.73.108:8060/dingtalk/webhook1/send'
? ? send_resolved: true
?
??
systemctl reload alertmanager文章來源地址http://www.zghlxwxcb.cn/news/detail-600305.html
(5)測(cè)試告警
systemctl stop node_exporter
到了這里,關(guān)于【云原生】Prometheus之部署 Alertmanager 發(fā)送告警的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!