File size: 5,395 Bytes
bad179a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Server Monitor - self-test for a real Windows machine (PowerShell 5.1+).
# Runs the collector once, exactly the way the apps start it (script on stdin of a second PowerShell),
# checks every section of the frame, then checks the one-shot command wrapper. Changes nothing on the machine.
# Run from the repository folder:   powershell -ExecutionPolicy Bypass -File windows-selftest.ps1
$collector = Join-Path $PSScriptRoot 'Server Monitor(web-window)\core\collector.ps1'
if (-not (Test-Path -LiteralPath $collector)) { Write-Host "collector.ps1 not found: $collector"; exit 2 }
$psArgs = '-NoLogo', '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-OutputFormat', 'Text', '-EncodedCommand',
          'aQBlAHgAIAAoAEAAKAAkAGkAbgBwAHUAdAApACAALQBqAG8AaQBuACAAWwBjAGgAYQByAF0AMQAwACkA'   # iex (@($input) -join [char]10)
$script:failed = 0
function Check($ok, $what) {
  if ($ok) { Write-Host "PASS  $what" -ForegroundColor Green } else { Write-Host "FAIL  $what" -ForegroundColor Red; $script:failed++ }
}
function Num($s) { $v = 0.0; [double]::TryParse("$s", [Globalization.NumberStyles]::Float, [Globalization.CultureInfo]::InvariantCulture, [ref]$v) }

# ---- 1. collector frame
$watch = [Diagnostics.Stopwatch]::StartNew()
$lines = @(('$iv = 1; $once = $true' + "`n" + (Get-Content -LiteralPath $collector -Raw)) | powershell.exe @psArgs)
$ms = $watch.ElapsedMilliseconds
$sec = @{}; $cur = $null
foreach ($l in $lines) {
  $l = "$l".Trim()
  if ($l.StartsWith('@@S ')) { $cur = $l.Substring(4); $sec[$cur] = New-Object System.Collections.Generic.List[string] }
  elseif ($cur -and $l -and -not $l.StartsWith('@@')) { $sec[$cur].Add($l) }
}
function KV($name) { $h = @{}; foreach ($l in $sec[$name]) { $i = $l.IndexOf('='); if ($i -gt 0) { $h[$l.Substring(0, $i)] = $l.Substring($i + 1) } }; $h }
function Rows($name, $min) { @($sec[$name] | Where-Object { $_ -and $_.Split('|').Count -ge $min }) }

Check ($lines -contains '@@SNAP_BEGIN' -and $lines -contains '@@SNAP_END') "frame is complete ($($lines.Count) lines in $ms ms)"
foreach ($s in 'info', 'os', 'cpu', 'sys', 'mem', 'net', 'tcp', 'disk', 'df', 'procs', 'sessions', 'services', 'thermal') { Check $sec.ContainsKey($s) "section '$s' is present" }
$info = KV 'info'; $os = KV 'os'; $sys = KV 'sys'; $mem = KV 'mem'
Check ($info['hostname'] -and $info['os'] -and $info['kernel'] -and $info['cpumodel']) "info: $($info['hostname']) / $($info['os']) / $($info['cpumodel'])"
Check ((Num $os['ts']) -and [double]$os['uptime'] -gt 0 -and [double]$os['memtotal'] -gt 0) "os: uptime $($os['uptime']) s, memory $($os['memtotal']) bytes, page file $($os['swaptotal']) bytes"
$total = @(Rows 'cpu' 6 | Where-Object { $_.StartsWith('_Total|') })
$cores = @(Rows 'cpu' 6 | Where-Object { -not $_.StartsWith('_Total|') })
Check ($total.Count -eq 1 -and $cores.Count -ge 1) "cpu: _Total row and $($cores.Count) core rows"
if ($total.Count -eq 1) { $f = $total[0].Split('|'); Check ((Num $f[1]) -and [double]$f[1] -ge 0 -and [double]$f[1] -le 100) "cpu: total usage $($f[1]) % (user $($f[2]) %, privileged $($f[3]) %)" }
Check ((Num $sys['queue']) -and (Num $sys['ctxt'])) "sys: queue $($sys['queue']), context switches $($sys['ctxt'])/s"
Check ((Num $mem['avail']) -and [double]$mem['avail'] -gt 0 -and (Num $mem['standby']) -and (Num $mem['pages'])) "mem: available $($mem['avail']) bytes, standby $($mem['standby']) bytes"
Check (@(Rows 'net' 10).Count -ge 1) "net: $(@(Rows 'net' 10).Count) interfaces"
Check (@($sec['tcp']).Count -ge 1) "tcp: $(@($sec['tcp']) -join ', ')"
Check (@(Rows 'disk' 6).Count -ge 1) "disk: $(@(Rows 'disk' 6).Count) physical disks"
Check (@(Rows 'df' 5).Count -ge 1) "df: $(@(Rows 'df' 5).Count) volumes"
$procs = @(Rows 'procs' 9)
Check ($procs.Count -ge 10 -and @($procs | Where-Object { [double]($_.Split('|')[2]) -gt 0 }).Count -ge 5) "procs: $($procs.Count) processes with CPU times"
Check (@(Rows 'services' 5).Count -ge 10) "services: $(@(Rows 'services' 5).Count) services"
Write-Host "info  thermal sensors: $(@($sec['thermal']).Count), GPU section: $($sec.ContainsKey('gpu')), process owners (needs administrator): $($sec.ContainsKey('users'))"

# ---- 2. one-shot command wrapper (same text the apps send)
function Wrap($cmd) {
  "[Console]::OutputEncoding = New-Object System.Text.UTF8Encoding `$false; `$ProgressPreference = 'SilentlyContinue'; `$Error.Clear(); `$global:LASTEXITCODE = 0`n& {`n$cmd`n} 2>&1 | Out-String -Width 250 -Stream`nif (`$LASTEXITCODE) { exit `$LASTEXITCODE }`nif (`$Error | Where-Object { `$_.FullyQualifiedErrorId -notlike 'NativeCommandError*' }) { exit 1 }; exit 0`n"
}
$out = @((Wrap "'hello-' + (6 * 7)") | powershell.exe @psArgs)
Check ($LASTEXITCODE -eq 0 -and ($out -join ' ') -match 'hello-42') "command output and exit code 0"
$null = (Wrap 'Get-Item C:\no-such-file-for-selftest') | powershell.exe @psArgs
Check ($LASTEXITCODE -eq 1) "failing cmdlet gives exit code 1 (got $LASTEXITCODE)"
$null = (Wrap 'cmd /c exit 3') | powershell.exe @psArgs
Check ($LASTEXITCODE -eq 3) "native exit code is passed through (got $LASTEXITCODE)"
$null = (Wrap 'cmd /c "echo warning 1>&2"') | powershell.exe @psArgs
Check ($LASTEXITCODE -eq 0) "stderr text of a program is not a failure (got $LASTEXITCODE)"

if ($script:failed) { Write-Host "`n$($script:failed) check(s) FAILED" -ForegroundColor Red; exit 1 }
Write-Host "`nAll checks passed." -ForegroundColor Green