前言
據了解Prometheus作為監控系統,這個系統早已經比較火了,而釘釘在很多公司作為內部通訊軟件取代了Slack,prometheus-webhook-dingtalk部署上之后,結合釘釘自定義機器人可以算是告警加推送接收的完成。
獲取釘釘機器人
如何獲取釘釘機器人, 參考文檔: https://jingyan.baidu.com/article/d3b74d640c50cc1f77e6092d.html
配置好后可以簡單測試釘釘告警機器人:
#linux命令
curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxx' \
-H 'Content-Type: application/json' \
-d '{"msgtype": "text",
"text": {
"content": "雨紛紛,舊故里草木深"
}
}'
釘釘機器人返回結果

注意事項:“https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx”:為釘釘自定義機器人配置好后的 Webhook 地址,請保管好此 Webhook 地址,不要公布在外部網站上,泄露有安全風險。
prometheus-webhook-dingtalk部署使用
#linux二進制部署
wget https://github.com/timonwong/prometheus-webhook-dingtalk/releases/download/v0.3.0/prometheus-webhook-dingtalk-0.3.0.linux-amd64.tar.gz
tar -xvf prometheus-webhook-dingtalk-0.3.0.linux-amd64.tar.gz
cd prometheus-webhook-dingtalk-0.3.0.linux-amd64/
prometheus-webhook-dingtalk使用
prometheus-webhook-dingtalk的所有配置通過命令行參數完成,工具使用特別簡單,不需要配置文件
usage: prometheus-webhook-dingtalk --ding.profile=DING.PROFILE [<flags>] Flags: -h, --help Show context-sensitive help (also try --help-long and --help-man). --web.listen-address=":8060" The address to listen on for web interface. --ding.profile=DING.PROFILE ... Custom DingTalk profile (can be given multiple times, <profile>=<dingtalk-url>). --ding.timeout=5s Timeout for invoking DingTalk webhook. --template.file="" Customized template file (see template/default.tmpl for example) --log.level=info Only log messages with the given severity or above. One of: [debug, info, warn, error] --version Show application version.
關于這里的 --ding.profile 參數:配置比較復雜,因為可以支持同時往多個釘釘自定義機器人發送報警消息,所以 --ding.profile 在命令行中指定多次,如下:
#啟動方法
nohup ./prometheus-webhook-dingtalk --ding.profile="webhook1=https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx" --ding.profile="webhook2=https://oapi.dingtalk.com/robot/send?access_token=yyyyyyyyyyyy" 2>&1 &
#啟動使用的端口默認8060
Alertmanager配置啟動
Alertmanager也注意配置兩個webhook1,webhook2的釘釘自定義機器人發送告警消息,加入相應的 receivers(注意下面的 name和url)即可:
cat alertmanager.yml
global:
resolve_timeout: 5m
route:
receiver: webhook1
group_wait: 30s
group_interval: 5m
repeat_interval: 5m
group_by: [alertname]
routes:
- receiver: webhook1
group_wait: 10s
match_re: #匹配的alertname才由此receiver發送
alertname: 'Memory Usage|CPU Usage'
- receiver: webhook2
group_wait: 10s
match_re: #匹配的alertname才由此receiver發送
alertname: 'Server Status|Disk Usage'
receivers:
- name: webhook1
webhook_configs:
- url: http://192.168.134.131:8060/dingtalk/webhook1/send
send_resolved: true
- name: webhook2
webhook_configs:
- url: http://192.168.134.131:8060/dingtalk/webhook2/send
send_resolved: true
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
啟動Alertmanager
#Linux命令
nohup ./alertmanager --config.file="/software/alertmanager-0.23.0.linux-amd64/alertmanager.yml" 2>&1 &
ss -alntup | grep -i 9093 #確認9093端口是否啟動,此時命令應該有輸出。
Alertmanager啟動默認端口為9093,啟動后訪問http://ip:9093/查看alertmanager是否啟動成功,如下:

Prometheus配置啟動
如何配置Prometheus配置文件和告警規則,可通過參考文檔http://www.sunline.cc/db/334564的“prometheus配置及告警規則配置”章節
prometheus關聯alertmanager
prometheus.yml中的alerting標簽下配置上alertmanager的地址即可,配置如下:
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets: ['192.168.134.131:9093']
prometheus啟動
#Linux命令
nohup ./prometheus --config.file=/software/prometheus-2.33.3.linux-amd64/etc/prometheus.yml 2>&1 &
ss -alntup | grep -i 9090 #確認9090端口是否啟動,此時命令應該有輸出。
啟動后訪問http://ip:9090/查看prometheus是否啟動成功,點擊alerts就能看到配置的報警信息了,如下:

測試釘釘接收
磁盤測試
說明:使用dd命令創建一個大文件,文件一定要占用盤存儲空間的80%以上,磁盤告警由webhook2發出。
dd if=/dev/zero of=/test bs=1024M count=12
釘釘接收消息截圖

主機down測試
說明:直接對一個監控節點進行關機操作,主機狀態告警由webhook2發出。
halt -p
釘釘接收消息截圖

CPU測試
說明:下載stress軟件,如果你Linux系統總的CPU線程有1個,那么設置測試的CPU一定要超過這個值。一般設置為2-3倍。CPU告警由webhook1發出。
stress --cpu 3 --timeout 6000
釘釘接收消息截圖

內存測試
說明:下載memtester軟件,根據你實際的內存量設置合適的內存測試值,如你的主機有10G,那么按照設置告警的規則為80%才告警,你可以設置測試內存為9G。測試單位有B K M G自選。CPU告警由webhook1發出。
./memtester 700M
釘釘接收消息截圖





