Download dev/collector.ps1 from mchuca/ServerMonitor: direct link, hf CLI and curl.
- Browser
- Download file 6.1 kB
-
https://huggingface.co/mchuca/ServerMonitor/resolve/main/dev/collector.ps1
- Command line
-
hf download hf://mchuca/ServerMonitor/dev/collector.ps1
-
curl -L -o collector.ps1 https://huggingface.co/mchuca/ServerMonitor/resolve/main/dev/collector.ps1
6.1 kB
| # Server Monitor collector for Windows (PowerShell 5.1+, ASCII only). | |
| # The caller prepends "$iv = <seconds>; $once = $false". One frame per interval: | |
| # "@@SNAP_BEGIN", "@@S <section>" + rows (key=value or |-separated), "@@SNAP_END". | |
| $ErrorActionPreference = 'SilentlyContinue' | |
| $ProgressPreference = 'SilentlyContinue' | |
| [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding $false | |
| function Cim($class, $ns = 'root/cimv2') { Get-CimInstance -ClassName $class -Namespace $ns } | |
| function Clean($s) { ("$s" -replace '[|\r\n]', ' ').Trim() } | |
| $epoch = [DateTime]'1970-01-01' | |
| $smi = Get-Command nvidia-smi.exe | Select-Object -First 1 -ExpandProperty Path | |
| # Console.Out reports no error when the pipe is closed, so the collector also ends when its parent process is gone. | |
| $parent = Get-Process -Id (Get-CimInstance Win32_Process -Filter "ProcessId = $PID").ParentProcessId | |
| $n = 0 | |
| while ($true) { | |
| $t0 = Get-Date | |
| $out = New-Object System.Collections.Generic.List[string] | |
| $out.Add('@@SNAP_BEGIN') | |
| $os = Cim Win32_OperatingSystem | |
| if ($n % 15 -eq 0) { | |
| $cs = Cim Win32_ComputerSystem | |
| $cpu = @(Cim Win32_Processor)[0] | |
| $virt = Clean ($cs.Manufacturer + ' ' + $cs.Model) | |
| $admin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | |
| $out.Add('@@S info') | |
| $out.Add("hostname=$($cs.Name)") | |
| $out.Add("os=$(Clean $os.Caption)") | |
| $out.Add("kernel=$($os.Version)") | |
| $out.Add("arch=$($os.OSArchitecture)") | |
| $out.Add("cpumodel=$(Clean $cpu.Name)") | |
| $out.Add("mhz=$($cpu.MaxClockSpeed)") | |
| $out.Add("virt=$virt") | |
| $out.Add("admin=$([int]$admin)") | |
| if ($admin) { | |
| $out.Add('@@S users') | |
| Get-Process -IncludeUserName | ForEach-Object { $out.Add("$($_.Id)|$($_.UserName)") } | |
| } | |
| } | |
| $out.Add('@@S os') | |
| $out.Add("ts=$([long](([DateTime]::UtcNow - $epoch).TotalMilliseconds))") | |
| $out.Add("uptime=$([long](((Get-Date) - $os.LastBootUpTime).TotalSeconds))") | |
| $out.Add("memtotal=$([long]$os.TotalVisibleMemorySize * 1024)") | |
| $out.Add("swaptotal=$([long]$os.SizeStoredInPagingFiles * 1024)") | |
| $out.Add("swapfree=$([long]$os.FreeSpaceInPagingFiles * 1024)") | |
| $out.Add('@@S cpu') | |
| Cim Win32_PerfFormattedData_PerfOS_Processor | ForEach-Object { | |
| $out.Add("$($_.Name)|$($_.PercentProcessorTime)|$($_.PercentUserTime)|$($_.PercentPrivilegedTime)|$([long]$_.PercentInterruptTime + [long]$_.PercentDPCTime)|$($_.InterruptsPersec)") | |
| } | |
| $sys = Cim Win32_PerfFormattedData_PerfOS_System | |
| $out.Add('@@S sys') | |
| $out.Add("queue=$($sys.ProcessorQueueLength)") | |
| $out.Add("ctxt=$($sys.ContextSwitchesPersec)") | |
| $out.Add("threads=$($sys.Threads)") | |
| $pm = Cim Win32_PerfFormattedData_PerfOS_Memory | |
| $out.Add('@@S mem') | |
| $out.Add("avail=$($pm.AvailableBytes)") | |
| $out.Add("free=$($pm.FreeAndZeroPageListBytes)") | |
| $out.Add("standby=$([long]$pm.StandbyCacheCoreBytes + [long]$pm.StandbyCacheNormalPriorityBytes + [long]$pm.StandbyCacheReserveBytes)") | |
| $out.Add("syscache=$($pm.CacheBytes)") | |
| $out.Add("modified=$($pm.ModifiedPageListBytes)") | |
| $out.Add("pool=$([long]$pm.PoolPagedBytes + [long]$pm.PoolNonpagedBytes)") | |
| $out.Add("commit=$($pm.CommittedBytes)") | |
| $out.Add("commitlimit=$($pm.CommitLimit)") | |
| $out.Add("pages=$($pm.PagesPersec)") | |
| $out.Add('@@S net') | |
| [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() | ForEach-Object { | |
| $s = $null | |
| $s = $_.GetIPStatistics() | |
| if ($s) { $out.Add("$(Clean $_.Name)|$($_.OperationalStatus)|$([long]($_.Speed / 1000000))|$($_.NetworkInterfaceType)|$($s.BytesReceived)|$($s.BytesSent)|$($s.UnicastPacketsReceived)|$($s.UnicastPacketsSent)|$([long]$s.IncomingPacketsWithErrors + [long]$s.OutgoingPacketsWithErrors)|$([long]$s.IncomingPacketsDiscarded + [long]$s.OutgoingPacketsDiscarded)|$(Clean $_.Description)") } | |
| } | |
| $ip = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties() | |
| $out.Add('@@S tcp') | |
| $ip.GetActiveTcpConnections() | Group-Object State | ForEach-Object { $out.Add("$($_.Name) $($_.Count)") } | |
| $out.Add("Listen $(@($ip.GetActiveTcpListeners()).Count)") | |
| $out.Add('@@S disk') | |
| Cim Win32_PerfFormattedData_PerfDisk_PhysicalDisk | Where-Object { $_.Name -ne '_Total' } | ForEach-Object { | |
| $out.Add("$(Clean $_.Name)|$($_.DiskReadBytesPersec)|$($_.DiskWriteBytesPersec)|$($_.DiskReadsPersec)|$($_.DiskWritesPersec)|$($_.PercentIdleTime)") | |
| } | |
| $out.Add('@@S df') | |
| [System.IO.DriveInfo]::GetDrives() | Where-Object { $_.IsReady -and $_.DriveType -eq 'Fixed' } | ForEach-Object { | |
| $out.Add("$($_.Name)|$(Clean $_.VolumeLabel)|$($_.DriveFormat)|$($_.TotalSize)|$($_.AvailableFreeSpace)") | |
| } | |
| $sessions = @{} | |
| $out.Add('@@S procs') | |
| Cim Win32_Process | ForEach-Object { | |
| $born = 0 | |
| if ($_.CreationDate) { $born = [long](($_.CreationDate.ToUniversalTime() - $epoch).TotalSeconds) } | |
| if ($_.Name -eq 'explorer.exe') { $sessions[$_.SessionId] = 1 } | |
| $out.Add("$($_.ProcessId)|$($_.ParentProcessId)|$([long]$_.KernelModeTime + [long]$_.UserModeTime)|$($_.Priority)|$($_.ThreadCount)|$born|$($_.VirtualSize)|$($_.WorkingSetSize)|$(Clean $_.Name)") | |
| } | |
| $out.Add('@@S sessions') | |
| $out.Add("$($sessions.Count)") | |
| if ($n % 5 -eq 0) { | |
| $out.Add('@@S services') | |
| Cim Win32_Service | ForEach-Object { $out.Add("$($_.Name)|$($_.State)|$($_.StartMode)|$($_.ExitCode)|$(Clean $_.DisplayName)") } | |
| } | |
| $out.Add('@@S thermal') | |
| Cim MSAcpi_ThermalZoneTemperature 'root/wmi' | ForEach-Object { $out.Add("$(Clean $_.InstanceName)|$(($_.CurrentTemperature - 2732) / 10)") } | |
| if ($smi -and $n % 3 -eq 0) { | |
| $out.Add('@@S gpu') | |
| & $smi '--query-gpu=index,name,utilization.gpu,memory.used,memory.total,temperature.gpu,power.draw,fan.speed' '--format=csv,noheader,nounits' | ForEach-Object { $out.Add("$_") } | |
| } | |
| $out.Add('@@SNAP_END') | |
| try { [Console]::Out.Write(($out -join "`n") + "`n"); [Console]::Out.Flush() } catch { exit } | |
| if ($once -or $parent.HasExited) { break } | |
| $n++ | |
| $left = $iv * 1000 - ((Get-Date) - $t0).TotalMilliseconds | |
| if ($left -gt 0) { Start-Sleep -Milliseconds ([int]$left) } | |
| } | |