$log = "G:\WEB-SERVER\htdocs\AttendanceHub\ZKTecoSyncService-V2\publish\logs\attendancehub_sync.log" $out = "G:\WEB-SERVER\htdocs\AttendanceHub\ZKTecoSyncService-V2\_deployment\phase9_monitor.csv" "ts,pending,sending,sent,retry,dlq,last_batch_http,last_batch_accepted,last_batch_rejected,health_line" | Set-Content $out -Encoding utf8 $off = 0 $fs = [System.IO.File]::Open($log, 'Open', 'Read', 'ReadWrite') try { $off = $fs.Length } finally { $fs.Close() } while ($true) { Start-Sleep -Seconds 60 # counts from the newest UPLOAD_CYCLE line in new log content $chunk = "" $fs2 = [System.IO.File]::Open($log, 'Open', 'Read', 'ReadWrite') try { $fs2.Seek($off, 'Begin') | Out-Null $sr = New-Object System.IO.StreamReader($fs2) $chunk = $sr.ReadToEnd() $sr.Close() $off = $fs2.Position } finally { $fs2.Close() } $pending = "?"; $sending = "?"; $sent = "?"; $retry = "?"; $dlq = "?"; $bh = "?"; $ba = "?"; $br = "?"; $health = "" $lines = $chunk -split "`r?`n" # UPLOAD_CYCLE ... success=S retry=R dlq=D pending=P sending=Sn sent=Se retryState=RS dlqState=DS ... $cycleLine = $lines | Where-Object { $_ -match 'UPLOAD_CYCLE ' } | Select-Object -Last 1 if ($cycleLine -match 'success=(\d+) retry=(\d+) dlq=(\d+) pending=(\d+) sending=(\d+) sent=(\d+) retryState=(\d+) dlqState=(\d+)') { $sent = $matches[6] # Sent-state total $retry = $matches[7] # Retry-state total (retryState) $dlq = $matches[8] # Dlq-state total (dlqState) $pending = $matches[4] $sending = $matches[5] } $batchLine = $lines | Where-Object { $_ -match 'UPLOAD_BATCH ' } | Select-Object -Last 1 if ($batchLine -match 'http=(\d+) accepted=(\d+) .* rejected=(\d+)') { $bh = $matches[1]; $ba = $matches[2]; $br = $matches[3] } $healthLine = $lines | Where-Object { $_ -match 'DUAL_HEALTH ' } | Select-Object -Last 1 if ($healthLine -match 'evDropped=(\d+).*consecFail=(\d+)') { $health = "evDropped=$($matches[1]) consecFail=$($matches[2])" } # anomaly counters (cumulative in file — cheap substring count on this chunk only) $recreate = ([regex]::Matches($chunk, 'SESSION_(LIVE|READ)_RECREATE')).Count $cross = ([regex]::Matches($chunk, 'CROSS_THREAD_COM_ACCESS')).Count $health = "$health recreate=$recreate cross=$cross" "$(Get-Date -Format o),$pending,$sending,$sent,$retryState,$dlqState,$bh,$ba,$br,$health" | Add-Content $out -Encoding utf8 # stop condition: pending=0 and sending=0 and dlq=0 and at least one batch observed if ($pending -ne "?" -and [int]$pending -eq 0 -and [int]$sending -eq 0 -and [int]$dlq -eq 0 -and $bh -ne "?") { "DRAINED at $(Get-Date -Format o)" | Add-Content $out -Encoding utf8 break } }