104 lines
3.4 KiB
PowerShell
104 lines
3.4 KiB
PowerShell
# 只读:拉服务器新系统 8910 智能体日志中的企微相关行与错误行。
|
||
# 用法(仓库根或任意目录):
|
||
# .\deploy\Watch-Agent8910.ps1 # 持续监听(Ctrl+C 结束)
|
||
# .\deploy\Watch-Agent8910.ps1 -Once # 只看最近一段
|
||
# .\deploy\Watch-Agent8910.ps1 -All # 不过滤关键字,整行刷出
|
||
#
|
||
# 说明:
|
||
# - 盯的是 E:\wwwroot\logs\agent-http.*.log(新系统 8910),不碰现网 8810/8811。
|
||
# - 切流前公网 /wecom/callback 仍进现网;本脚本主要看到 inject、出站、本机调 8910 的错误。
|
||
# - 依赖本机 SSH 密钥 ~/.ssh/id_ed25519_ytd。
|
||
|
||
[CmdletBinding()]
|
||
param(
|
||
[switch]$Once,
|
||
[switch]$All,
|
||
[int]$Tail = 80
|
||
)
|
||
|
||
$ErrorActionPreference = 'Stop'
|
||
$Key = Join-Path $env:USERPROFILE '.ssh\id_ed25519_ytd'
|
||
$Remote = 'ytd_extdev_0903@129.211.181.80'
|
||
$LogOut = 'E:\wwwroot\logs\agent-http.out.log'
|
||
$LogErr = 'E:\wwwroot\logs\agent-http.err.log'
|
||
|
||
# 企微回调 / 出站 / 注入 / 常见失败码;-All 时不使用
|
||
$Pattern = 'wecom|callback|inject|outbox|inbox|errcode|errmsg|60020|ERROR|WARNING|Traceback|Exception|health|sender|touser|crypto'
|
||
|
||
if (-not (Test-Path $Key)) {
|
||
throw "缺少 SSH 密钥: $Key"
|
||
}
|
||
|
||
Write-Host "目标: ${Remote}"
|
||
Write-Host "日志: $LogOut"
|
||
Write-Host " $LogErr"
|
||
if ($All) {
|
||
Write-Host '过滤: 关闭(全部输出)'
|
||
} else {
|
||
Write-Host "过滤: $Pattern"
|
||
}
|
||
Write-Host '---'
|
||
|
||
# 远程脚本:先打 health,再 Tail;-Wait 仅在非 Once 时启用
|
||
$waitLiteral = if ($Once) { '$false' } else { '$true' }
|
||
$allLiteral = if ($All) { '$true' } else { '$false' }
|
||
|
||
$remote = @"
|
||
`$ErrorActionPreference = 'Continue'
|
||
`$out = '$LogOut'
|
||
`$err = '$LogErr'
|
||
`$tail = $Tail
|
||
`$wait = $waitLiteral
|
||
`$all = $allLiteral
|
||
`$pat = '$Pattern'
|
||
|
||
Write-Output '=== remote health 8910 ==='
|
||
try {
|
||
`$h = Invoke-WebRequest -Uri 'http://127.0.0.1:8910/health' -UseBasicParsing -TimeoutSec 5
|
||
Write-Output `$h.Content
|
||
} catch {
|
||
Write-Output ('health_fail: ' + `$_.Exception.Message)
|
||
}
|
||
Write-Output '=== log tail ==='
|
||
|
||
`$files = @()
|
||
if (Test-Path `$out) { `$files += `$out } else { Write-Output ('MISSING ' + `$out) }
|
||
if (Test-Path `$err) { `$files += `$err } else { Write-Output ('MISSING ' + `$err) }
|
||
if (`$files.Count -eq 0) { throw 'no agent-http log files' }
|
||
|
||
function Show-Line([string]`$line) {
|
||
if (`$all) {
|
||
Write-Output `$line
|
||
return
|
||
}
|
||
if (`$line -match `$pat) {
|
||
Write-Output `$line
|
||
}
|
||
}
|
||
|
||
if (-not `$wait) {
|
||
foreach (`$f in `$files) {
|
||
Write-Output ('--- ' + `$f + ' ---')
|
||
Get-Content -Path `$f -Tail `$tail -ErrorAction SilentlyContinue | ForEach-Object { Show-Line `$_ }
|
||
}
|
||
exit 0
|
||
}
|
||
|
||
Write-Output 'following... (Ctrl+C to stop)'
|
||
Get-Content -Path `$files -Tail `$tail -Wait -ErrorAction SilentlyContinue | ForEach-Object { Show-Line `$_ }
|
||
"@
|
||
|
||
$remoteFile = Join-Path $env:TEMP 'ytd-watch-agent8910-remote.ps1'
|
||
Set-Content -Path $remoteFile -Value $remote -Encoding UTF8
|
||
|
||
$incoming = 'C:/Users/ytd_extdev_0903/incoming/_watch_agent8910.ps1'
|
||
& scp -o BatchMode=yes -o ConnectTimeout=12 -i $Key $remoteFile "${Remote}:${incoming}"
|
||
if ($LASTEXITCODE -ne 0) { throw "scp failed: $LASTEXITCODE" }
|
||
|
||
& ssh -o BatchMode=yes -o ConnectTimeout=12 -i $Key $Remote `
|
||
"powershell -NoProfile -ExecutionPolicy Bypass -File C:\Users\ytd_extdev_0903\incoming\_watch_agent8910.ps1"
|
||
if ($LASTEXITCODE -ne 0 -and -not $Once) {
|
||
# Ctrl+C 常非 0,不当作部署失败
|
||
exit $LASTEXITCODE
|
||
}
|