dingtalk-work-alert.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/bin/bash
  2. #--------------------------------------------------------------------------------------------------
  3. # 使用企业微信群机器人发送告警的脚本
  4. # 腾讯云数仓机器人:-key=19e30ec1-d001-4437-ac41-63dc07f78520
  5. # 小可爱:-key=cc3653b1-78cb-465a-bf95-bf5f5303a37a
  6. #--------------------------------------------------------------------------------------------------
  7. BASE_DIR=$(
  8. cd "$(dirname "$(realpath "$0")")/.." || exit
  9. pwd
  10. )
  11. . "${BASE_DIR}"/bin/common/init.sh
  12. function usage() {
  13. echo -e "${NORM_MGT}Usage: $0
  14. ${NORM_CYN}\t[-h/-H/--h/--H/--help] 打印脚本使用方法${DO_RESET}"
  15. echo -e "${NORM_MGT}Usage: $0
  16. ${NORM_GRN}\t<-key[ /=] robot hook key> 机器人url后的key(lxy/common/alerter_constants.py中有记录)
  17. ${NORM_GRN}\t<-msg[=/] message need to send> 要发送的消息
  18. ${NORM_GRN}\t<-f[=/] file message need to send> 要发送的文件消息
  19. ${DO_RESET}"
  20. exit "$1"
  21. }
  22. function parse_args() {
  23. for index in $(seq 1 $#); do
  24. arg=${*:index:1}
  25. case $arg in
  26. -key)
  27. index=$((index + 1))
  28. KEY="${*:index:1}"
  29. ;;
  30. -key=*)
  31. KEY="${arg#*=}"
  32. ;;
  33. -msg)
  34. index=$((index + 1))
  35. MSG+=("${*:index:1}")
  36. ;;
  37. -msg=*)
  38. MSG+=("${arg#*=}")
  39. ;;
  40. -f)
  41. index=$((index + 1))
  42. FILE_PATH+=("${*:index:1}")
  43. ;;
  44. -f=*)
  45. FILE_PATH+=("${arg#*=}")
  46. ;;
  47. -h | -H | --help)
  48. usage 0
  49. ;;
  50. *) ;;
  51. esac
  52. done
  53. }
  54. function build_message() {
  55. if [ -z "${KEY}" ] || [ "${#MSG[@]}" -eq 0 ]; then
  56. usage 1
  57. fi
  58. msg=${MSG[0]}
  59. for ((i = 1; i < ${#MSG[@]}; i++)); do
  60. msg="${msg}\n${MSG[$i]}"
  61. done
  62. message=("{
  63. \"msgtype\": \"text\",
  64. \"text\": {
  65. \"content\": \"异常告警:\n${msg[*]}\"
  66. },
  67. \"at\":{
  68. \"isAtAll\":true
  69. }
  70. }")
  71. url="http://m1.node.cdh/dingtalk/api/robot/send?access_token=${KEY}"
  72. }
  73. # shellcheck disable=SC2034
  74. AT=()
  75. MSG=()
  76. parse_args "${@}"
  77. build_message
  78. #echo -e "${NORM_GRN} Send message using ${RED}${url}${DO_RESET}"
  79. curl "$url" -H 'Content-Type: application/json' -d "${message[*]}"