Files
ai_wht_wechat/go_backend/monitor_service.bat
2026-01-06 19:36:42 +08:00

84 lines
2.1 KiB
Batchfile
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

@echo off
chcp 65001 >nul
REM 服务监控脚本 - Windows版本
REM 用于外部监控服务状态
setlocal enabledelayedexpansion
set "SERVICE_NAME=AI小红书服务"
set "ALERT_PHONE=15707023967"
set "HEARTBEAT_FILE=%TEMP%\ai_xhs_service_heartbeat.json"
set "CHECK_INTERVAL=120"
echo ========================================
echo 服务监控检查 - %date% %time%
echo ========================================
echo.
REM 检查心跳文件是否存在
if not exist "%HEARTBEAT_FILE%" (
echo [错误] 心跳文件不存在: %HEARTBEAT_FILE%
echo [错误] 服务可能未启动或已宕机
goto :SEND_ALERT
)
echo [信息] 心跳文件: %HEARTBEAT_FILE%
REM 读取心跳文件内容
for /f "delims=" %%i in ('powershell -Command "Get-Content '%HEARTBEAT_FILE%' | ConvertFrom-Json | Select-Object -ExpandProperty last_heartbeat"') do (
set "LAST_HEARTBEAT=%%i"
)
if "!LAST_HEARTBEAT!"=="" (
echo [错误] 无法读取心跳信息
goto :SEND_ALERT
)
echo [信息] 上次心跳: !LAST_HEARTBEAT!
REM 计算时间差使用PowerShell
for /f %%i in ('powershell -Command "$now=[DateTime]::Now; $last=[DateTime]::Parse('!LAST_HEARTBEAT!'); ($now - $last).TotalSeconds"') do (
set "TIME_DIFF=%%i"
)
REM 去除小数点
for /f "tokens=1 delims=." %%a in ("!TIME_DIFF!") do set "TIME_DIFF_INT=%%a"
echo [信息] 距离上次心跳: !TIME_DIFF_INT!
REM 检查是否超时
if !TIME_DIFF_INT! GTR %CHECK_INTERVAL% (
echo [错误] 服务可能已宕机(超过%CHECK_INTERVAL%秒未更新心跳)
goto :SEND_ALERT
)
echo [信息] 服务运行正常
echo.
echo ========================================
echo 检查完成 - 状态正常
echo ========================================
exit /b 0
:SEND_ALERT
echo.
echo [警告] 检测到服务异常,正在发送通知...
echo.
REM 发送宕机通知
cd /d %~dp0
go run test_service_alert.go
if %ERRORLEVEL% EQU 0 (
echo [信息] 宕机通知发送成功
) else (
echo [错误] 宕机通知发送失败
)
echo.
echo ========================================
echo 检查完成 - 服务异常
echo ========================================
pause
exit /b 1