#!/bin/bash #-------------------------------------------------------------------------------------------------- # 使用企业微信群机器人发送告警的脚本 # 腾讯云数仓机器人:-key=19e30ec1-d001-4437-ac41-63dc07f78520 # 小可爱:-key=cc3653b1-78cb-465a-bf95-bf5f5303a37a #-------------------------------------------------------------------------------------------------- BASE_DIR=$( cd "$(dirname "$(realpath "$0")")/.." || exit pwd ) . "${BASE_DIR}"/bin/common/init.sh function usage() { echo -e "${NORM_MGT}Usage: $0 ${NORM_CYN}\t[-h/-H/--h/--H/--help] 打印脚本使用方法${DO_RESET}" echo -e "${NORM_MGT}Usage: $0 ${NORM_GRN}\t<-key[ /=] robot hook key> 机器人url后的key(lxy/common/alerter_constants.py中有记录) ${NORM_GRN}\t<-msg[=/] message need to send> 要发送的消息 ${NORM_GRN}\t<-f[=/] file message need to send> 要发送的文件消息 ${DO_RESET}" exit "$1" } function parse_args() { for index in $(seq 1 $#); do arg=${*:index:1} case $arg in -key) index=$((index + 1)) KEY="${*:index:1}" ;; -key=*) KEY="${arg#*=}" ;; -msg) index=$((index + 1)) MSG+=("${*:index:1}") ;; -msg=*) MSG+=("${arg#*=}") ;; -f) index=$((index + 1)) FILE_PATH+=("${*:index:1}") ;; -f=*) FILE_PATH+=("${arg#*=}") ;; -h | -H | --help) usage 0 ;; *) ;; esac done } function build_message() { if [ -z "${KEY}" ] || [ "${#MSG[@]}" -eq 0 ]; then usage 1 fi msg=${MSG[0]} for ((i = 1; i < ${#MSG[@]}; i++)); do msg="${msg}\n${MSG[$i]}" done message=("{ \"msgtype\": \"text\", \"text\": { \"content\": \"异常告警:\n${msg[*]}\" }, \"at\":{ \"isAtAll\":true } }") url="http://m1.node.cdh/dingtalk/api/robot/send?access_token=${KEY}" } # shellcheck disable=SC2034 AT=() MSG=() parse_args "${@}" build_message #echo -e "${NORM_GRN} Send message using ${RED}${url}${DO_RESET}" curl "$url" -H 'Content-Type: application/json' -d "${message[*]}"