shell脚本调用钉钉机器人发送告警
一个用shell脚本发送钉钉告警的脚本,这里使用加签方式,关键字方式比较简单这里就不写了,机器人的说明这里也不写了,自己百度一下:
参考文档:
https://open.dingtalk.com/document/robots/customize-robot-security-settings
#!/bin/bash# 钉钉机器人配置
DINGDING_WEBHOOK="https://oapi.dingtalk.com/robot/send?access_token=59721d282a5ad6d0c585506e42342190e741667917178077d1357f32adc739cf607b62"
SECRET="SEC33aebaba1dc013c13a2f647a8620522fc916def32fc709e7345a9e2bc1a63d8cf4988"MESSAGE="THIS IS TEST MESSAGE"
AT_MOBILES="1252542424 1324242424" # phone numbertimestamp=$(date +%s%3N)
string_to_sign=$(printf "%s\n%s" "${timestamp}" "${SECRET}")#sha256 and base64
sign=$(echo -n "${string_to_sign}" | openssl dgst -sha256 -hmac "${SECRET}" -binary | base64)
# URL编码签名
sign=$(python3 -c "import urllib.parse; print(urllib.parse.quote_plus('${sign}'))")echo -e "签名字符串(含换行):\n${sign}" # 调试:打印拼接结果# full Webhook URL
webhook_url="${DINGDING_WEBHOOK}×tamp=${timestamp}&sign=${sign}"echo -e "\n最终请求URL(部分隐藏):${webhook_url:0:500}...\n" # 避免泄露完整token#phone array ["xxx","xxx"]
at_mobiles_json=$(echo "${AT_MOBILES}" | sed 's/ /", "/g' | awk '{print "[\"" $0 "\"]"}')echo $at_mobiles_json# 告警消息内容
message="{'msgtype': 'text','text': {'content': '$MESSAGE'},'at': {'atMobiles': '${at_mobiles_json}','isAtAll': false}
}"# 发送告警
response=$(curl -s -H "Content-Type: application/json" -X POST -d "$message" "$webhook_url")# 检查发送结果
if [ $? -eq 0 ]; thenecho "告警发送成功"
elseecho "告警发送失败"echo "响应: $response"
fi