一、docker部署
#!/bin/bash
# Uptime Kuma Docker 部署脚本(机器A)
set -e
# 配置变量
WEIXIN_WEBHOOK="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d402043a-3389-4e0f-8626-f05d429b2227"
PORT=3001
DATA_DIR="/opt/uptime-kuma"
# 安装Docker
if ! command -v docker &> /dev/null; then
echo "安装Docker..."
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo systemctl enable docker
fi
sudo tee /etc/docker/daemon.json <<-EOF
{
"registry-mirrors": ["https://nu4ynvqu.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
EOF
sudo systemctl restart docker
# 部署Uptime Kuma
sudo mkdir -p $DATA_DIR
sudo chown -R $USER:$USER $DATA_DIR
docker run -d \
--restart=always \
-p $PORT:3001 \
-v $DATA_DIR:/app/data \
--name uptime-kuma \
louislam/uptime-kuma:1
# 发送部署通知
curl -s -X POST $WEIXIN_WEBHOOK \
-H "Content-Type: application/json" \
-d '{
"msgtype": "markdown",
"markdown": {
"content": "**Uptime Kuma Docker 版部署成功**\n> 主机:`'$(hostname)'`\n> IP:`'$(hostname -I | awk '{print $1}')'`\n> 端口:`'$PORT'`\n> 数据目录:`'$DATA_DIR'`"
}
}'
echo "部署完成!访问地址:http://$(hostname -I | awk '{print $1}'):$PORT"
二、源码部署
#!/bin/bash
# Uptime Kuma Node.js 部署脚本(修复版)
set -e
# 配置变量
WEIXIN_WEBHOOK="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d402043a-3389-4e0f-8626-f05d429b2227"
PORT=3002
INSTALL_DIR="/opt/uptime-kuma"
# 安装依赖
echo "安装系统依赖..."
sudo apt update
sudo apt install -y git curl build-essential python3-minimal
# 安装Node.js
if ! command -v node &> /dev/null || [ "$(node -v | cut -d'.' -f1 | tr -d 'v')" -lt 18 ]; then
echo "安装Node.js 18..."
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
fi
# 安装PM2
if ! command -v pm2 &> /dev/null; then
echo "安装PM2..."
sudo npm install -g pm2
fi
# 部署应用
echo "部署Uptime Kuma..."
sudo mkdir -p $INSTALL_DIR
sudo chown -R $USER:$USER $INSTALL_DIR
cd $INSTALL_DIR
if [ ! -d "$INSTALL_DIR/uptime-kuma" ]; then
git clone https://github.com/louislam/uptime-kuma.git
cd uptime-kuma
# 关键修复:先全局安装vite,再安装项目依赖
npm install -g vite
npm ci --production --include=dev
npm run build
else
cd uptime-kuma
git reset --hard
npm install -g vite
npm ci --production --include=dev
npm run build
fi
# 配置环境变量
export NODE_ENV=production
# 启动服务
echo "启动服务..."
pm2 delete uptime-kuma 2> /dev/null || true
pm2 start npm --name uptime-kuma -- run start-server -- --port=$PORT
pm2 save
pm2 startup | grep -v "sudo" | bash
# 发送部署通知
echo "发送通知..."
curl -s -X POST $WEIXIN_WEBHOOK \
-H "Content-Type: application/json" \
-d '{
"msgtype": "markdown",
"markdown": {
"content": "**Uptime Kuma Node 版部署成功**\n> 主机:`'$(hostname)'`\n> IP:`'$(hostname -I | awk '{print $1}')'`\n> 端口:`'$PORT'`\n> 安装目录:`'$INSTALL_DIR'`\n> Node版本:`'$(node -v)'`\n> 构建时间:`'$(date +"%Y-%m-%d %H:%M:%S")'`"
}
}'
echo "部署完成!访问地址:http://$(hostname -I | awk '{print $1}'):$PORT"
验证部署
检查服务状态:
pm2 list
查看日志:
pm2 logs uptime-kuma
访问页面:
http://<服务器IP>:3002
三、配置双向监控
- 在机器A监控机器B
登录机器A的 Uptime Kuma 控制台(http:/
/<机器A_IP>:3001)
添加监控项:
名称:Machine-B-NodeJS
类型:HTTP
URL:http://<机器B_IP>:3002
监控间隔:60秒3
- 在机器B监控机器A
登录机器B的 Uptime Kuma 控制台(http://<机器B_IP>:3002)
添加监控项:
名称:Machine-A-Docker
类型:HTTP
URL:http://<机器A_IP>:3001
监控间隔:60秒
四、配置企业微信告警
- 在两台机器的 Uptime Kuma 中配置:
进入 Settings → Notifications
选择 Webhook 通知方式
填写以下参数:
名称:企微机器人
Webhook URL:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d402043a-3389-4e0f-8626-f05d429b2227
Body Template(JSON 格式):
json
{
“msgtype”: “markdown”,
“markdown”: {
“content”: “[[{{monitor_name}}]]\n> 状态: {{status}}\n> 时间: {{time}}\n> 详情: {{message}}”
}
}
点击 Test 发送测试通知39