Scripts/check.ps1

#Requires -Version 5.1
# Run on the file server (powershell.exe 5.1). Checks collector health and _listen445 volume.

$ErrorActionPreference = 'Continue'
$root = Join-Path $env:ProgramData 'SMBeat'
$todayUtc = [datetime]::UtcNow.ToString('yyyy-MM-dd')
$samplePath = Join-Path $root ('samples\{0}.jsonl' -f $todayUtc)

Write-Host '=== SMBeat check ==='
Write-Host ('Computer : {0}' -f $env:COMPUTERNAME)
Write-Host ('DataRoot : {0}' -f $root)
Write-Host ('Sample : {0}' -f $samplePath)
Write-Host ''

if (Get-Module -ListAvailable -Name SMBeat) {
    Import-Module SMBeat -Force -ErrorAction SilentlyContinue
    $mod = Get-Module SMBeat
    if ($mod) {
        Write-Host ('Module : {0}' -f $mod.Version)
    }
    try {
        $st = Get-SMBeatStatus
        Write-Host ('Task : exists={0} enabled={1} state={2}' -f $st.TaskExists, $st.TaskEnabled, $st.TaskState)
        Write-Host ('Collect : IsCollecting={0} HeartbeatFresh={1}' -f $st.IsCollecting, $st.HeartbeatFresh)
        Write-Host ('LastOK : {0}' -f $st.LastSuccessUtc)
        Write-Host ('LastErr : {0}' -f $st.LastError)
        Write-Host ('Samples : {0} lines today' -f $st.SamplesToday)
    }
    catch {
        Write-Host ('Get-SMBeatStatus failed: {0}' -f $_.Exception.Message)
    }
}
else {
    Write-Host 'Module : SMBeat not installed in this session'
}

Write-Host ''
$hbPath = Join-Path $root 'heartbeat.json'
if (Test-Path -LiteralPath $hbPath) {
    Write-Host '=== heartbeat.json ==='
    Get-Content -LiteralPath $hbPath -Raw
}
else {
    Write-Host 'heartbeat.json missing - collector has not started.'
}

Write-Host ''
if (-not (Test-Path -LiteralPath $samplePath)) {
    Write-Host ('No sample file for UTC day {0}.' -f $todayUtc)
    Write-Host 'Register-SMBeatCollector -Wait, wait one flush minute, copy again.'
    return
}

$records = New-Object System.Collections.Generic.List[object]
Get-Content -LiteralPath $samplePath | ForEach-Object {
    if (-not [string]::IsNullOrWhiteSpace($_)) {
        $records.Add(($_ | ConvertFrom-Json)) | Out-Null
    }
}

$listen = @($records | Where-Object { $_.kind -eq 'tcp445' -and $_.name -eq '_listen445' })
$clients = @($records | Where-Object { $_.kind -eq 'tcp445' -and $_.name -ne '_listen445' })
$nics = @($records | Where-Object { $_.kind -eq 'nic' })

Write-Host ('=== {0} ({1} lines) ===' -f $samplePath, $records.Count)
Write-Host ('_listen445 rows : {0}' -f $listen.Count)
Write-Host ('client rows : {0}' -f $clients.Count)
Write-Host ('nic rows : {0}' -f $nics.Count)
Write-Host ''

if ($listen.Count -eq 0) {
    Write-Host 'FAIL: no kind=tcp445 name=_listen445. Watcher is not flushing TCP 445.'
    Write-Host 'Need SMBeat 0.2.0+, Unregister then Register-SMBeatCollector -Wait.'
    return
}

$listenSum = ($listen | Measure-Object -Property sentBytesDelta -Sum).Sum
$nicSum = 0
if ($nics.Count -gt 0) {
    $nicSum = ($nics | Measure-Object -Property sentBytesDelta -Sum).Sum
}

Write-Host '=== _listen445 (server sum per flush) ==='
$listen | Select-Object ts, sentBytesDelta, receivedBytesDelta | Format-Table -AutoSize
Write-Host ('_listen445 sent today : {0} bytes ({1:N3} GiB)' -f $listenSum, ($listenSum / 1GB))
if ($nics.Count -gt 0) {
    Write-Host ('NIC Out sent today : {0} bytes ({1:N3} GiB)' -f $nicSum, ($nicSum / 1GB))
}

Write-Host ''
Write-Host '=== tcp445 clients (remote IP) ==='
if ($clients.Count -eq 0) {
    Write-Host 'No client rows yet (idle or only baseline).'
}
else {
    $clients |
        Group-Object name |
        ForEach-Object {
            $sum = ($_.Group | Measure-Object -Property sentBytesDelta -Sum).Sum
            [PSCustomObject]@{
                Client   = $_.Name
                Flushes  = $_.Count
                SentGiB  = [math]::Round(($sum / 1GB), 3)
                SentBytes = $sum
            }
        } |
        Sort-Object SentBytes -Descending |
        Format-Table -AutoSize
}

Write-Host ''
$nonzero = @($listen | Where-Object { $_.sentBytesDelta -gt 1048576 })
if ($nonzero.Count -gt 0) {
    Write-Host 'OK: at least one _listen445 flush is above 1 MiB.'
    Write-Host 'A 5.5 GiB copy should show about 5.5 in the _listen445 sent-today GiB line (plus a bit of protocol).'
}
else {
    Write-Host 'WARN: _listen445 exists but sentBytesDelta stays near 0.'
    Write-Host 'First minute of a new connection is baseline. Wait one flush, or Invoke-SMBeatSample, then copy again.'
    Write-Host 'If a multi-GiB copy still stays at 0, ESTATA is not counting.'
}