群晖 chat 的webhook使用参考: https://www.synology.com/en-us/knowledgebase/DSM/tutorial/Collaboration/How_to_configure_webhooks_and_slash_commands_in_Chat_Integration

脚本:

#!/bin/sh
url=$1
retry=2
delay=6s
if [ ! -n "$url" ] ;then
    echo "usage: ./ping.sh http://baidu.com"
    exit 1
fi
for((i=1;i<=$retry;i++)); do  
	http_code=`curl -Is -m 10 -w %{http_code} -o /dev/null $url`
	if [ $http_code -eq 0 ];then
		# 0=超时
		echo	"http_code\t->\t$http_code"
	elif [ $http_code -eq 200 ];then
		# 1=200 ok
		echo "http_code\t->\t$http_code"
		break;
	else
		# 2=程序错误,状态码非200
		echo "http_code\t->\t$http_code"
	fi 
# delay
if [ $i -lt $retry ];then
	echo &quot;sleep $delay&quot;
	sleep $retry 
fi

done if [ $http_code -eq 200 ];then echo "$url ONLINE!!!" # curl -X POST --data-urlencode 'payload={"text": "'$url' ONLINE!!!"}' "http://IP:PORT/chat/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2&token=TOKEN" else echo "$url OFFLINE!!!" curl -X POST --data-urlencode 'payload={"text": "'$url' OFFLINE!!!"}' "http://IP:PORT/chat/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2&token=TOKEN" fi

执行:

# 参数异常
MacBook-Pro-2:home nianzecong$ ./ping.sh 
usage: ./ping.sh http://baidu.com

服务在线

MacBook-Pro-2:home nianzecong$ ./ping.sh http://baidu.com http_code -> 200 http://baidu.com ONLINE!!!

服务异常

MacBook-Pro-2:home nianzecong$ ./ping.sh http://baidu.com/123 http_code -> 302 sleep 6s http_code -> 302 {"success":true} # 通知结果,同时,chat收到消息

群晖设置定时任务,每10分钟运行脚本,脚本所有者为root账户即可