alertmanager釘釘報警之@我
一、前言
首先說下為什么會折騰自己搞釘釘報警,還要專門@我。原因有以下幾點:
(1)線上部署了好幾個prometheus+grafana的監控系統,報警是有的,grafana自帶的(如下圖), 我想群上的人,包括作為運維的我,連看的欲望都木有= =(心底話,自己都過不了自己的審美的),想改良它!!!
(2)監控群上很多人,很多業務交流/開發技術研討,我是不太需要關心的,所以我平時一般設置免擾模式。如何快速甄選我關心的消息? ——》 @我咯
(3)源於之前的一個需求(還沒搞完):監控計划任務進程存活
(4)深入學習alertmanager(配置很靈活,方便,好處多多,只會越來越普及)+ 滿足好奇心
二、問題探索
第一次弄的時候參考的鏈接:https://my.oschina.net/u/4408208/blog/4685334
部署起來很成功,但是@不到人。效果不盡如人意。。。放棄
解壓目錄結構如下,倒是挺簡單的:
然后各種查,谷歌翻了好幾頁終於找到這個:https://wi1dcard.dev/posts/alertmanager-dingtalk-integration-tips/
照着做,是源於這個github項目的【作者:timonwong】:
https://github.com/timonwong/prometheus-webhook-dingtalk
下載鏈接:https://github.com/timonwong/prometheus-webhook-dingtalk/releases
實踐證明,不管編不編譯都能用!!!(一開始以為@不了我,是因為我沒有編譯= =)
編譯安裝請參考:https://blog.csdn.net/Buster_ZR/article/details/105848811
不編譯直接來(我特意把上面的編譯環境干掉來證明這樣是ok滴【go、yarn,nodejs等】) ——》 推薦,下面介紹具體實現方法
三、具體實現
感謝:github作者 + 某小伙子有些關鍵地方的提醒(不然搞崩我= =)
1、下載釘釘插件:prometheus-webhook-dingtalk這個git項目,解壓改名
1 wget https://github.com/timonwong/prometheus-webhook-dingtalk/releases/download/v1.4.0/prometheus-webhook-dingtalk-1.4.0.linux-amd64.tar.gz 2 3 tar -xvf prometheus-webhook-dingtalk-1.4.0.linux-amd64.tar.gz 4 mv prometheus-webhook-dingtalk-1.4.0.linux-amd64 prometheus-webhook-dingtalk
解壓后非常精簡:配置文件+二進制可執行程序+模板參考目錄(contrib)

2、配置模板文件:/config/example.tmpl
直接抄這個:https://soulchild.cn/2168.html
注意:alertmanager、grafana等鏈接地址要改成你自己的
3、修改配置文件:
## Request timeout # timeout: 5s ## Customizable templates path templates: - /config/example.tmpl targets: webhook1: &target_base url: https://oapi.dingtalk.com/robot/send?access_token=xxxx message: title: '{{ template "example.title" . }}' text: '{{ template "example.content" . }}' webhook2: <<: *target_base mention: # 此處必須聲明 Mention 的號碼... mobiles: ["我的電話號碼"] message: text: | @我的電話號碼 {{ template "example.content" . }}
因為我的機器人webhook是很久以前申請的了, 那會還不需要加簽或者告警關鍵字之類,直接寫就好
這文件有個注意點: @電話這里要放出來!!!參考文章沒有寫清楚
4、確認告警規則文件的標簽

記下來 severity: warning
5、修改:alertmanager.yml
1 route: 2 group_by: ['severity','alertname'] 3 group_wait: 10s 4 repeat_interval: 1m #重復發報警時間 5 receiver: webhook1 6 routes: 7 - match_re: 8 severity: warning 9 receiver: webhook2 10 11 receivers: 12 - name: 'webhook1' 13 webhook_configs: 14 - &dingtalk_config 15 send_resolved: false 16 url: http://localhost:8060/dingtalk/webhook1/send 17 18 - name: 'webhook2' 19 webhook_configs: 20 - <<: *dingtalk_config 21 url: http://localhost:8060/dingtalk/webhook2/send
match_re 匹配了上面的標簽
dingtalk插件默認端口是8060,文章中那個地址是不對的
alertmanager的路由配置說明可以參考這文檔:https://blog.csdn.net/weixin_44723434/article/details/104556121
6、啟動alertmanager和dingtalk插件
## 啟動 alertmanager
1 cd /usr/local/src/monitor/alertmanager 2 ./alertmanager --config.file alertmanager.yml
1 ## 啟動 dingtalk插件 2 cd /usr/local/src/monitor/test/prometheus-webhook-dingtalk 3 ./prometheus-webhook-dingtalk --config.file=config.example.yml
最終效果圖:
這個東西后續其實還挺多需要改善的地方,比如
(1)告警內容不夠直觀,可讀性不強
(2)日志默認是沒有的,我現在啟動是直接放前台觀察的
。。。
不過都算開了個好頭,想想還是頗高興的呢~~ 后續可以大做文章,為所欲為,哈哈哈哈~~~~ ^__^