Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
快速完成Prometheus搭建中,我們部署了alertmanager,但是并不能跑起來(lái)。接下來(lái)我們需要考慮是用slack通知,還是郵件通知。
# vi /etc/alertmanager/alertmanager.yml
# 全局配置項(xiàng)
global:
resolve_timeout: '5m' #處理超時(shí)時(shí)間,默認(rèn)為5min
smtp_smarthost: 'smtp.qq.com:587' # 郵箱smtp服務(wù)器代理
smtp_from: 'xxx@qq.com' # 發(fā)送郵箱名稱(chēng)
smtp_auth_username: 'xxx@qq.com' # 郵箱名稱(chēng)
smtp_auth_password: 'xxxxxxx' # 郵箱密碼或授權(quán)碼
smtp_require_tls: false
# 定義模板郵件
templates:
- '/etc/alertmanager/template/*.tmpl'
# 定義路由樹(shù)信息
route:
group_by: ['alertname'] # 報(bào)警分組依據(jù)
group_wait: 10s # 最初即第一次等待多久時(shí)間發(fā)送一組警報(bào)的通知
group_interval: 10s # 在發(fā)送新警報(bào)前的等待時(shí)間
repeat_interval: 5m # 發(fā)送重復(fù)警報(bào)的周期 對(duì)于email配置中,此項(xiàng)不可以設(shè)置過(guò)低,否則將會(huì)由于郵件發(fā)送太多頻繁,被smtp服務(wù)器拒絕
receiver: operations-team
# 定義警報(bào)接收者信息
receivers:
- name: 'operations-team'
email_configs: # 郵箱配置
- to: 'xxx@foxmail.com' # 接收警報(bào)的email配置
html: '{{ template "test.html" . }}' # 設(shè)定郵箱的內(nèi)容模板
headers: { Subject: "[WARN] 報(bào)警郵件"} # 接收郵件的標(biāo)題
slack_configs:
- api_url: "https://hooks.slack.com/services/TAX2D1UFK/B01GJE6SGV7/xxxxxxxxxxx"
channel: "#prometheus"
text: "{{ range .Alerts }} {{ .Annotations.description}}\n {{end}} {{ .CommonAnnotations.username}} "
title: "{{.CommonAnnotations.summary}}"
title_link: "{{.CommonAnnotations.link}}"
color: "{{.CommonAnnotations.color}}"
send_resolved: true
# 一個(gè)inhibition規(guī)則是在與另一組匹配器匹配的警報(bào)存在的條件下,使匹配一組匹配器的警報(bào)失效的規(guī)則。兩個(gè)警報(bào)必須具有一組相同的標(biāo)簽。
# 這里的critical 會(huì)抑制warning 規(guī)則, 比如磁盤(pán)使用率超過(guò)90%, 那80%的告警就不會(huì)發(fā)送通知。
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname','instance']
- source_match:
alertname: 'NodeFilesystemSpaceUsage'
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname','instance']
說(shuō)明:
以上配置,會(huì)發(fā)送slack,同時(shí)發(fā)送郵件通知。如果只需要一個(gè),去掉不需要的即可。
inhibit_rules是我們配置的抑制規(guī)則。
郵件通知,我們使用了test.html模板。
重啟alertmanager
systemctl restart alertmanager
檢查下alertmanager頁(yè)面 http://192.168.0.107:9093/#/alerts ,如下界面說(shuō)明啟動(dòng)成功。
alertmanager
郵件通知模板/etc/alertmanager/template/test.html
{{ define "test.html" }}
<table border="1">
<tr>
<td>報(bào)警項(xiàng)</td>
<td>實(shí)例</td>
<td>概要</td>
<td>描述</td>
<td>開(kāi)始時(shí)間</td>
<td>link</td>
</tr>
{{ range $i, $alert :=.Alerts }}
<tr>
<td>{{ .Labels.alertname }} </td>
<td>{{ .Labels.instance }} </td>
<td>{{ .Annotations.summary }}</td>
<td>{{ .Annotations.description }}</td>
<td>{{ .StartsAt.Format "2021-01-23 16:01:01" }}</td>
<td><a href="{{ .GeneratorURL }}">鏈接到Prometheus</a></td>
</tr>
{{ end }}
</table>
{{ end }}
獲取slack webhook地址
1.打開(kāi)https://slack.com/ ,注冊(cè)賬號(hào)
2.創(chuàng)建一個(gè)channel #prometheus
創(chuàng)建channel
3.添加APP
選中channel #prometheus
add an app
搜索"incoming-webhook",選中直接安裝即可。
incoming-webhook
在彈出的link里面找到 webhook URL
這個(gè)url 就是 alertmanager配置文件里面的api_url
api_url: "https://hooks.slack.com/services/TAX2D1UFK/B01GJE6SGV7/xxxxxxxxxxx"
slack 配置完成。
1.登陸QQ郵箱網(wǎng)頁(yè)版,點(diǎn)擊設(shè)置
獲取授權(quán)碼,填入alertmanager里smtp_auth_password
smtp_auth_password: 'xxxxxxx'
SLACK通知結(jié)果展示:
郵件通知結(jié)果展示:
rometheus發(fā)出告警時(shí)分為兩部分。首先,Prometheus按告警規(guī)則(rule_files配置塊)向Alertmanager發(fā)送告警(即告警規(guī)則是在Prometheus上定義的)。然后,由Alertmanager來(lái)管理這些告警,包括去重(Deduplicating)、分組(Grouping)、靜音(silencing)、抑制(inhibition)、聚合(aggregation ),最終將面要發(fā)出的告警通過(guò)電子郵件、webhook等方式將告警通知路由(route)給對(duì)應(yīng)的聯(lián)系人。
分組:就是將具有相同性質(zhì)的告警先分類(lèi),然后當(dāng)作單個(gè)通知發(fā)送出來(lái)。比如A和B兩臺(tái)主機(jī)的磁盤(pán)(CPU/內(nèi)存)使用率都在告警,則磁盤(pán)的告警就可以合并在一個(gè)通知中發(fā)送出來(lái)。可以想像某個(gè)服務(wù)部署了100個(gè)節(jié)點(diǎn),在一次升版后因?yàn)閎ug,日志中均報(bào)同樣一類(lèi)錯(cuò)誤,如果不合并這類(lèi)通知,那就是告警風(fēng)暴。
抑制:就是某些告警觸發(fā)后,則抑制(禁止)另一些告警。比如收到一條告警提示集群故障無(wú)法訪問(wèn),那么在該集群上的所有其他警告應(yīng)該被抑制。
靜音(默):將某些在預(yù)期內(nèi)的告警設(shè)置為靜默(即不發(fā)送告警)。靜默是基于配置匹配規(guī)則。Alertmanager會(huì)檢查從Prometheus推送過(guò)來(lái)的告警事件,看這些告警事件是否與配置的靜默規(guī)則能匹配上,如果匹配則不發(fā)送任何通知。配置靜默方法是在Alertmanager的Web界面中,也可以使用amtool工具。
總之:Alertmanager制定這一系列規(guī)則目的只有一個(gè),就是提高告警質(zhì)量。
配置Alertmanager來(lái)做告警通知主要分三個(gè)步驟:
一、安裝并配置Alertmanager
# tar -xvf alertmanager-0.20.0.linux-amd64.tar.gz
mv alertmanager-0.20.0.linux-amd64 /usr/local/alertmanager
# cat alertmanager.yml
global:
resolve_timeout: 5m
smtp_smarthost: 'smtp.mxhichina.com:465'
smtp_from: 'noreply@demo.com'
smtp_auth_username: 'noreply@demo.com'
smtp_auth_password: '1235698'
smtp_require_tls: false
templates:
- '/usr/local/alertmanager/template/default.tmpl'
route:
group_by: ['alertname']
group_wait: 30s
group_interval: 2m
repeat_interval: 10m
receiver: 'email'
# continue default is false
continue: true
receivers:
- name: 'email'
email_configs:
- to: 'cookingit222@163.com,itcooking222@163.com'
send_resolved: true
html: '{{ template "default.html" . }}'
headers: { Subject: "{{ .GroupLabels.SortedPairs.Values }} [{{ .Status | toUpper }}:{{ .Alerts.Firing | len }}]" }
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
alertmanager配置簡(jiǎn)要說(shuō)明:
global:全局配置,主要配置告警方式,如郵件、webhook等。
route:Prometheus的告警先是到達(dá)alertmanager的根路由(route),alertmanager的根路由不能包含任何匹配項(xiàng),因?yàn)楦酚墒撬懈婢娜肟邳c(diǎn)。另外,根路由需要配置一個(gè)接收器(receiver),用來(lái)處理那些沒(méi)有匹配到任何子路由的告警(如果沒(méi)有配置子路由,則全部由根路由發(fā)送告警),即缺省接收器。告警進(jìn)入到根route后開(kāi)始遍歷子route節(jié)點(diǎn),如果匹配到,則將告警發(fā)送到該子route定義的receiver中,然后就停止匹配了。因?yàn)樵趓oute中continue默認(rèn)為false,如果continue為true,則告警會(huì)繼續(xù)進(jìn)行后續(xù)子route匹配。如果當(dāng)前告警仍匹配不到任何的子route,則該告警將從其上一級(jí)(匹配)route或者根route發(fā)出(按最后匹配到的規(guī)則發(fā)出郵件)。
group_by:用于分組聚合,對(duì)告警通知按標(biāo)簽(label)進(jìn)行分組,將具有相同標(biāo)簽或相同告警名稱(chēng)(alertname)的告警通知聚合在一個(gè)組,然后作為一個(gè)通知發(fā)送。如果想完全禁用聚合,可以設(shè)置為group_by: [...]
group_wait: 當(dāng)一個(gè)新的告警組被創(chuàng)建時(shí),需要等待'group_wait'后才發(fā)送初始通知。這樣可以確保在發(fā)送等待前能聚合更多具有相同標(biāo)簽的告警,最后合并為一個(gè)通知發(fā)送。
group_interval: 當(dāng)?shù)谝淮胃婢ㄖl(fā)出后,在新的評(píng)估周期內(nèi)又收到了該分組最新的告警,則需等待'group_interval'時(shí)間后,開(kāi)始發(fā)送為該組觸發(fā)的新告警,可以簡(jiǎn)單理解為,group就相當(dāng)于一個(gè)通道(channel)。
repeat_interval: 告警通知成功發(fā)送后,若問(wèn)題一直未恢復(fù),需再次重復(fù)發(fā)送的間隔。
查看你的告警路由樹(shù),將alertmanager.yml配置文件復(fù)制到對(duì)話框,然后點(diǎn)擊"Draw Routing Tree"
https://www.prometheus.io/webtools/alerting/routing-tree-editor/
修改好配置文件后,可以使用amtool工具檢查配置
# ./amtool check-config alertmanager.yml
Checking 'alertmanager.yml' SUCCESS
# cat >/usr/lib/systemd/system/alertmanager.service <<EOF
[Unit]
Description=alertmanager
[Service]
ExecStart=/usr/local/alertmanager/alertmanager --config.file=/usr/local/alertmanager/alertmanager.yml --storage.path=/usr/local/alertmanager/data --web.listen-address=:9093 --data.retention=120h
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
# systemctl enable alertmanager
# systemctl restart alertmanager
alertmanager默認(rèn)運(yùn)行端口是:9093
alertmanager也可以同prometheus一樣熱加載配置
1)向alertmanager進(jìn)程發(fā)送SIGHUP信號(hào),如:kill -SIGHUP alertmanager_pid
2)curl -X POST http://prometheus_ip:9093/-/reload
二、修改prometheus的配置,關(guān)聯(lián)Alertmanager服務(wù),同時(shí)添加對(duì)alertmanager的監(jiān)控
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
- monitor01:9093
rule_files:
- "rules/*_rules.yml"
- "rules/*_alerts.yml"
......
- job_name: 'alertmanager'
static_configs:
- targets: ['localhost:9093']
labels:
app: alertmanager
三、修改prometheus的配置,添加記錄規(guī)則和告警規(guī)則。
# cat node_rules.yml
groups:
- name: node_rules
#interval: 15s
rules:
- record: instance:node_cpu_usage
expr: 100 - avg(irate(node_cpu_seconds_total{mode="idle"}[1m])) by (nodename) * 100
labels:
metric_type: CPU_monitor
- record: instance:node_1m_load
expr: node_load1
labels:
metric_type: load1m_monitor
- record: instance:node_mem_usage
expr: 100 - (node_memory_MemAvailable_bytes)/(node_memory_MemTotal_bytes) * 100
labels:
metric_type: Memory_monitor
- record: instance:node_root_partition_predit
expr: round(predict_linear(node_filesystem_free_bytes{device="rootfs",mountpoint="/"}[2h],12*3600)/(1024*1024*1024), 1)
labels:
metric_type: root_partition_monitor
# cat node_alerts.yml
groups:
- name: node_alerts
rules:
- alert: cpu_usage_over_threshold
expr: instance:node_cpu_usage > 80
for: 1m
labels:
severity: warning
annotations:
summary: 主機(jī) {{ $labels.nodename }} 的 CPU使用率持續(xù)1分鐘超出閾值,當(dāng)前為 {{humanize $value}} %
- alert: system_1m_load_over_threshold
expr: instance:node_1m_load > 20
for: 1m
labels:
severity: warning
annotations:
summary: 主機(jī) {{ $labels.nodename }} 的 1分負(fù)載超出閾值,當(dāng)前為 {{humanize $value}}
- alert: mem_usage_over_threshold
expr: instance:node_mem_usage > 80
for: 1m
annotations:
summary: 主機(jī) {{ $labels.nodename }} 的 內(nèi)存 使用率持續(xù)1分鐘超出閾值,當(dāng)前為 {{humanize $value}} %
- alert: root_partition_usage_over_threshold
expr: instance:node_root_partition_predit < 60
for: 1m
annotations:
summary: 主機(jī) {{ $labels.nodename }} 的 根分區(qū) 預(yù)計(jì)在12小時(shí)使用將達(dá)到 {{humanize $value}}GB,超出當(dāng)前可用空間,請(qǐng)及時(shí)擴(kuò)容!
for 表示告警持續(xù)的時(shí)長(zhǎng),若持續(xù)時(shí)長(zhǎng)小于該時(shí)間就不發(fā)給alertmanager了,大于該時(shí)間再發(fā)。for的值不要小于prometheus中的scrape_interval,例如scrape_interval為30s,for為15s,如果觸發(fā)告警規(guī)則,則再經(jīng)過(guò)for時(shí)長(zhǎng)后也一定會(huì)告警,這是因?yàn)樽钚碌亩攘恐笜?biāo)還沒(méi)有拉取,在15s時(shí)仍會(huì)用原來(lái)值進(jìn)行計(jì)算。另外,要注意的是只有在第一次觸發(fā)告警時(shí)才會(huì)等待(for)時(shí)長(zhǎng)。
例如:10:43:00 觸發(fā)了集群A中的h1告警;10:43:10又觸發(fā)了集群A中的h2告警。則在10:44:10后發(fā)生一條告警是只包含h1的郵件,在10:44:20時(shí)會(huì)收到h1和h2聚合后的告警郵件,若h1和h2持續(xù)未恢復(fù),則在repeat_interval后仍以聚合方式發(fā)送告警。
注:h1和h2告警名相同,只是在不同主機(jī)上。
完成上述配置后,主機(jī)CPU、負(fù)載、內(nèi)存、磁盤(pán)超出閾值時(shí)就發(fā)觸發(fā)郵件告警。
上述配置僅是對(duì)主機(jī)資源做了監(jiān)控,并且告警只發(fā)到了缺省聯(lián)系人組。設(shè)想一下,在實(shí)際生產(chǎn)環(huán)境中,可能會(huì)按產(chǎn)品線或業(yè)務(wù)功能進(jìn)行分組來(lái)研發(fā),不同的服務(wù)出現(xiàn)告警時(shí)只發(fā)送通知到對(duì)應(yīng)的聯(lián)系人組,其他不相關(guān)的組不需要接告警收通知,這就需要我們修改告警路由規(guī)則,而Alertmanager的核心和最復(fù)雜的地方就在告警路由規(guī)則的設(shè)置上。
Prometheus的告警規(guī)則在推送給Alertmanager前有的三種狀態(tài):
1、沒(méi)有觸發(fā)告警閾值時(shí),狀態(tài)為:inactive
2、觸發(fā)了告警閾值但未滿足告警持續(xù)時(shí)間(for)時(shí),狀態(tài)為:pending,如果不配置for或者指定for的值為0,則將跳過(guò)pending狀態(tài)。
3、已觸發(fā)告警閾值并達(dá)到告警持續(xù)時(shí)間,開(kāi)始將告警事件推送到Alertmanager,此時(shí)狀態(tài)為:firing。
配置告警靜默(silence),用于在預(yù)期內(nèi)的維護(hù)升級(jí)等操作。
當(dāng)我們?cè)趯?duì)系統(tǒng)進(jìn)行維護(hù)升級(jí)時(shí),通常不希望觸發(fā)告警通知;另外,當(dāng)上游服務(wù)出現(xiàn)異常,想讓下游的服務(wù)不觸發(fā)告警,Prometheus將用于這種配置稱(chēng)為silence(靜默)。silence用于設(shè)定一個(gè)(維護(hù))時(shí)間段,如1小時(shí),也可提前手動(dòng)觸發(fā)silence過(guò)期,表示維護(hù)結(jié)束,恢復(fù)對(duì)應(yīng)服務(wù)的告警通知功能。
設(shè)置silence的方式有以下2種:
1、登錄到alertmanager的控制臺(tái)操作
2、通過(guò)amtool命令進(jìn)行操作
配置告警模板
Alertmanager的通知模板是基于Go模板系統(tǒng),詳細(xì)可參考官網(wǎng)。
https://golang.org/pkg/text/template/
https://prometheus.io/docs/alerting/latest/notifications/#alert
https://prometheus.io/docs/prometheus/latest/configuration/template_reference/
模板配置步驟:
1、修改alertmanager.yml,配置模板地址,然后在每個(gè)receiver引用模板
templates:
- '/usr/local/alertmanager/template/default.tmpl'
...
...
html: '{{ template "default.html" . }}'
headers: { Subject: "{{ .GroupLabels.SortedPairs.Values }} [{{ .Status | toUpper }}:{{ .Alerts.Firing | len }}]" }
2、配置模板
default.tmpl
{{ define "default.html" }}
{{- if gt (len .Alerts.Firing) 0 -}}
[{{ .Status | toUpper }}:{{ .Alerts.Firing | len }}]
{{ range $i, $alert :=.Alerts }}
<pre>
告警節(jié)點(diǎn):{{ index $alert.Labels "nodename" }}
告警服務(wù):{{ index $alert.Labels "alertname" }}
報(bào)警詳情:{{ index $alert.Annotations "summary" }}
開(kāi)始時(shí)間:{{ $alert.StartsAt }}
</pre>
{{ end }}
{{ end }}
{{- if gt (len .Alerts.Resolved) 0 -}}
[{{ .Status | toUpper }}:{{ .Alerts.Resolved | len }}]
{{ range $i, $alert :=.Alerts }}
<pre>
恢復(fù)節(jié)點(diǎn):{{ index $alert.Labels "nodename" }}
恢復(fù)服務(wù):{{ index $alert.Labels "alertname" }}
狀 態(tài):{{ index $alert.Status }}
開(kāi)始時(shí)間:{{ $alert.StartsAt }}
恢復(fù)時(shí)間:{{ $alert.EndsAt }}
</pre>
{{ end }}
{{ end }}
{{- end }}
注:告警模板如果配置有問(wèn)題,會(huì)導(dǎo)致郵件發(fā)送失敗,注意觀察日志。
以下是靜默設(shè)置步驟:
1、登錄到alertmanager控制臺(tái)(http://IP:9093/#/alerts),點(diǎn)擊上方菜單欄中Alerts,可看到當(dāng)前有2個(gè)告警。
2、點(diǎn)擊右上角"New Silence",創(chuàng)建Silence任務(wù),具體設(shè)置如下,匹配規(guī)則支持正則。
注:可按instance或job_name等方式來(lái)進(jìn)行正則匹配。
3、Silence創(chuàng)建成功后,可以看到處于Active狀態(tài)的Silence
總之,多實(shí)踐。
者 | 渡渡鳥(niǎo)
分享 | 喬克
與Zabbix告警不同,Prometheus將告警分為兩個(gè)部分:Prometheus 和 Alertmanager。其中Prometheus配置告警觸發(fā)規(guī)則,對(duì)指標(biāo)進(jìn)行監(jiān)控和計(jì)算,將再將告警信息發(fā)送到Alertmanager中。Alertmanager對(duì)告警進(jìn)行管理,比如合并抑制等操作。
image.png
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。