生活随笔
收集整理的這篇文章主要介紹了
Promethues微信告警部署
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
原創(chuàng)作者:運維工程師 謝晉
Promethues微信告警
- 使用Prometheus實現(xiàn)企微信報警
使用Prometheus實現(xiàn)企微信報警
? ?Prometheus可以通過多種方式把告警信息發(fā)送到指定人,常用的有郵件,短信報警方式,但是越來越多的企業(yè)開始使用Prometheus結(jié)合微信作為主要的告警方式,這樣可以及時有效的把告警信息推送到接收人,方便告警的及時處理。
部署Alertmanager
Alertmanager是Prometheus告警組件,需要通過Alertmanager結(jié)合郵件、微信、釘釘?shù)冗M行告警
安裝Alertmanager組件
官網(wǎng)下載Alertmanager包
[Unit
]Description=alertmanager System
Documentation=alertmanager System
[Service
]ExecStart=/usr/local/alertmanager/alertmanager
\--config.file
=/usr/local/alertmanager/alertmanager.yml
[Install
]WantedBy=multi-user.target
訪問Alertmanager網(wǎng)頁9093端口
檢查alertmanager.yml語法
/usr/local/alertmanager
Checking
'alertmanager.yml' SUCCESSFound:- global config- route-
1 inhibit rules-
1 receivers-
0 templates
企業(yè)微信ID獲取
登陸企業(yè)微信https://work.weixin.qq.com/,需企業(yè)管理員賬號登陸
點擊應(yīng)用管理
這里添加應(yīng)用名稱和告警需要通知到的人或組
這里要記住3個值,Agentld、Secret、企業(yè)ID,后續(xù)創(chuàng)建報警需要
創(chuàng)建告警配置文件
global:resolve_timeout: 5mtemplates: -
'/usr/local/alertmanager/wechat.tmpl'route: group_by:
['linux'] group_wait: 10s group_interval: 10s repeat_interval: 1m receiver:
'wechat' receivers:- name:
'wechat'wechat_configs:- send_resolved:
trueagent_id:
'1000003' to_party:
'2' api_secret:
'zdevZA7hR-gsc4N0aeKYSjSFMz1aL38YLQnkeGuaVIY' corp_id:
'wwc5982c624dfffbb9' message:
'{{ template "wechat.tmpl" . }}'inhibit_rules:- source_match:severity:
'critical'target_match:severity:
'warning'
將企業(yè)微信剛剛獲取到的三個值填入即可
配置Prometheus.yml
將Alertmanager信息添加到prometheus.yml文件內(nèi)
alerting:alertmanagers:- static_configs:- targets:
['192.168.20.106:9093']
報警規(guī)則配置
groups:- name: 實例存活告警規(guī)則rules:- alert: 實例存活告警expr: up
{job
="linux"} == 0 or up
{job
="linux"} == 0for: 1mlabels:user: prometheusseverity: Disasterannotations:summary:
"Instance {{ $labels.instance }} is down"description:
"Instance {{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minutes."value:
"{{ $value }}"- name: 內(nèi)存告警規(guī)則rules:- alert:
"內(nèi)存使用率告警"expr:
(node_memory_MemTotal_bytes -
(node_memory_MemFree_bytes+node_memory_Buffers_bytes+node_memory_Cached_bytes
)) / node_memory_MemTotal_bytes *
100 > 75for: 1mlabels:user: prometheusseverity: warningannotations:summary:
"服務(wù)器: {{$labels.alertname}} 內(nèi)存報警"description:
"{{ $labels.alertname }} 內(nèi)存資源利用率大于75%!(當(dāng)前值: {{ $value }}%)"value:
"{{ $value }}"- name: CPU報警規(guī)則rules:- alert: CPU使用率告警expr:
100 -
(avg by
(instance
)(irate
(node_cpu_seconds_total
{mode
="idle"}[1m
]) )) *
100 > 70for: 1mlabels:user: prometheusseverity: warningannotations:summary:
"服務(wù)器: {{$labels.alertname}} CPU報警"description:
"服務(wù)器: CPU使用超過70%!(當(dāng)前值: {{ $value }}%)"value:
"{{ $value }}"- name: 磁盤報警規(guī)則rules:- alert: 磁盤使用率告警expr:
(node_filesystem_size_bytes - node_filesystem_avail_bytes
) / node_filesystem_size_bytes *
100 > 80for: 1mlabels:user: prometheusseverity: warningannotations:summary:
"服務(wù)器: {{$labels.alertname}} 磁盤報警"description:
"服務(wù)器:{{$labels.alertname}},磁盤設(shè)備: 使用超過80%!(掛載點: {{ $labels.mountpoint }} 當(dāng)前值: {{ $value }}%)"value:
"{{ $value }}"
在添加告警監(jiān)控腳本位置
rule_files:-
"rules/node_status.yml"
啟用alertmanager
啟動服務(wù)
設(shè)置開機自啟動
測試告警推送結(jié)果
將監(jiān)控的虛擬機關(guān)閉一臺做主機宕機測試,企業(yè)微信收到告警
若未配置告警模板,將會收到這樣的默認(rèn)告警信息。配置告警模板
{{ define
"wechat.tmpl" }}{{-
if gt
(len .Alerts.Firing
) 0 -
}}{{- range
$index,
$alert :
= .Alerts -
}}{{-
if eq
$index 0 }}========= 監(jiān)控報警
=========告警狀態(tài):
{{ .Status
}}告警級別:
{{ .Labels.severity
}}告警類型:
{{ $alert.Labels.alertname
}}故障主機:
{{ $alert.Labels.instance
}}告警主題:
{{ $alert.Annotations.summary
}}告警詳情:
{{ $alert.Annotations.message
}}{{ $alert.Annotations.description
}};觸發(fā)閥值:
{{ .Annotations.value
}}故障時間:
{{ ($alert.StartsAt.Add 28800e9
).Format
"2006-01-02 15:04:05" }}========= = end
= ========={{- end
}}{{- end
}}{{- end
}}{{-
if gt
(len .Alerts.Resolved
) 0 -
}}{{- range
$index,
$alert :
= .Alerts -
}}{{-
if eq
$index 0 }}========= 異常恢復(fù)
=========告警類型:
{{ .Labels.alertname
}}告警狀態(tài):
{{ .Status
}}告警主題:
{{ $alert.Annotations.summary
}}告警詳情:
{{ $alert.Annotations.message
}}{{ $alert.Annotations.description
}};故障時間:
{{ ($alert.StartsAt.Add 28800e9
).Format
"2006-01-02 15:04:05" }}恢復(fù)時間:
{{ ($alert.EndsAt.Add 28800e9
).Format
"2006-01-02 15:04:05" }}
{{-
if gt
(len
$alert.Labels.instance
) 0 }}實例信息:
{{ $alert.Labels.instance
}}{{- end
}}========= = end
= ========={{- end
}}{{- end
}}{{- end
}}{{- end
}}
在alertmanager.yml文件內(nèi)告警模板指向wechat.tmpl文件
receivers:- name:
'wechat'wechat_configs:- send_resolved:
trueagent_id:
'1000002' to_party:
'2' api_secret:
'NZvnlNNFvnpk4k-0_YNmE-ULRynAEU8PYkyT1k_MTm8' corp_id:
'wwc5982c624dfffbb9' message:
'{{ template "wechat.tmpl" . }}'
測試報警
總結(jié)
以上是生活随笔為你收集整理的Promethues微信告警部署的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。