#!/bin/bash

# 企业微信群市场质量零容忍一群机器人的Webhook URL
WEBHOOK_URL="https://open.feishu.cn/open-apis/bot/v2/hook/29913eb9-e057-4cb2-972b-b4cb94d113b5"

# 解析参数
while getopts "a:b:c:" opt; do
  case $opt in
    a) ip="$OPTARG";;
    b) servername="$OPTARG";;
    c) port="$OPTARG";;
    \?) echo "Invalid option: -$OPTARG" >&2
        exit 1;;
  esac
done

# 获取系统时间
datetime=$(date +"%Y-%m-%d %H:%M:%S")

# 检查参数是否齐全
#if [ -z "$cartype" ] || [ -z "$schjcount" ]  || [ -z "$changetype" ] || [ -z "$component" ]; then
#  echo "Usage: $0 -t <title> -n <schjcount>"
#  exit 1
#fi

# 构建Markdown格式的消息体
markdown_message=$(cat <<EOF
{
	"msg_type": "interactive",
	"card": {
		"header": {
			"template": "orange",
			"title": {
				"tag": "plain_text",
				"content": "JAVA服务异常！！！"
			}
		},
		"config": {
			"wide_screen_mode": true,
			"enable_forward": true
		},
		"elements": [{
			"tag": "markdown",
			"content": "<font color='yellow'>**告警时间：${datetime}** </font\n<font color='yellow'>**服务器IP：${ip}** </font\n<font color='yellow'>**服务名称：${servername}** </font\n<font color='yellow'>**端口号：${port}** </font\n<font color='green'>**请及时跟进核实!!！** </font>"
		}]
	}
}
EOF
)

# 发送消息到企业微信机器人
curl -s -X POST "$WEBHOOK_URL" -H "Content-Type: application/json" -d "$markdown_message"

echo "Alert sent successfully!"
