CH-J ToolsSSH によるサーバー管理
メニュー
公開済みのソース

CH-J Tools

特定のアプリケーションリリースのディレクトリとファイルを閲覧できます。

ソースの ZIP をダウンロード
CH-J / Server Manager 872 ファイル
core-20261030T220507Z-61e4558d889f 追加日時: · ビルド日時:
windows_metrics.ps1 追加日時: 日付の記録なし 8.9 KB · 94 行
コード ファイルをダウンロード
1param([string]$ArgsJson)
2$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'
3$A = $ArgsJson | ConvertFrom-Json
4$out = [ordered]@{ errors = @() }
5function Try-Cim([string]$Class, [string]$Filter) {
6 try { if ($Filter) { return @(Get-CimInstance -ClassName $Class -Filter $Filter -ErrorAction Stop) } else { return @(Get-CimInstance -ClassName $Class -ErrorAction Stop) } }
7 catch { $script:out.errors += "$($Class): $($_.Exception.Message)"; return $null }
8}
9if ($A.static) {
10 $nt = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -ErrorAction SilentlyContinue
11 $cpuKey = Get-ItemProperty 'HKLM:\HARDWARE\DESCRIPTION\System\CentralProcessor\0' -ErrorAction SilentlyContinue
12 $static = [ordered]@{ caption = [string]$nt.ProductName; build = [string]$nt.CurrentBuildNumber; ubr = [string]$nt.UBR; displayVersion = [string]$nt.DisplayVersion
13 installationType = [string]$nt.InstallationType; hostname = [Environment]::MachineName; architecture = [string]$env:PROCESSOR_ARCHITECTURE
14 cpuModel = ([string]$cpuKey.ProcessorNameString).Trim(); logical = [Environment]::ProcessorCount; physical = $null; sockets = $null; memoryTotal = $null }
15 $os = Try-Cim 'Win32_OperatingSystem'
16 if ($os) { $static.caption = [string]$os[0].Caption; $static.memoryTotal = [double]$os[0].TotalVisibleMemorySize * 1024 }
17 $cpu = Try-Cim 'Win32_Processor'
18 if ($cpu) { $static.sockets = $cpu.Count; $static.physical = [int](($cpu | Measure-Object -Property NumberOfCores -Sum).Sum); $static.logical = [int](($cpu | Measure-Object -Property NumberOfLogicalProcessors -Sum).Sum) }
19 $video = Try-Cim 'Win32_VideoController'
20 if ($video) { $static.gpus = @($video | ForEach-Object { [ordered]@{ name = [string]$_.Name; vendor = [string]$_.AdapterCompatibility; driverVersion = [string]$_.DriverVersion; pnp = [string]$_.PNPDeviceID; adapterRam = $(if ($null -ne $_.AdapterRAM) { [double]$_.AdapterRAM } else { $null }) } }) }
21 if (-not $static.memoryTotal) { try { Add-Type -AssemblyName Microsoft.VisualBasic; $static.memoryTotal = [double](New-Object Microsoft.VisualBasic.Devices.ComputerInfo).TotalPhysicalMemory } catch { $out.errors += "ComputerInfo: $($_.Exception.Message)" } }
22 $out.static = $static
24# Uptime: CIM boot time, otherwise the monotonic performance counter since boot.
25$out.uptimeSeconds = $null
26$os = Try-Cim 'Win32_OperatingSystem'
27if ($os) {
28 $out.uptimeSeconds = [math]::Floor(((Get-Date) - $os[0].LastBootUpTime).TotalSeconds)
29 $out.memoryTotal = [double]$os[0].TotalVisibleMemorySize * 1024; $out.memoryAvailable = [double]$os[0].FreePhysicalMemory * 1024
30} else {
31 $out.uptimeSeconds = [math]::Floor([Diagnostics.Stopwatch]::GetTimestamp() / [Diagnostics.Stopwatch]::Frequency)
32 try { Add-Type -AssemblyName Microsoft.VisualBasic; $ci = New-Object Microsoft.VisualBasic.Devices.ComputerInfo; $out.memoryTotal = [double]$ci.TotalPhysicalMemory; $out.memoryAvailable = [double]$ci.AvailablePhysicalMemory } catch { $out.errors += "ComputerInfo: $($_.Exception.Message)" }
34# Raw CPU counter; Core computes the usage from the difference between two refreshes.
35$cpu = Try-Cim 'Win32_PerfRawData_PerfOS_Processor' "Name='_Total'"
36if ($cpu) { $out.cpuRaw = [ordered]@{ busyInverse = [string]$cpu[0].PercentProcessorTime; time = [string]$cpu[0].Timestamp_Sys100NS } }
37# Each CPU package separately.
38$packages = Try-Cim 'Win32_Processor'
39if ($packages) { $out.cpuPackages = @($packages | ForEach-Object { [ordered]@{ id = [string]$_.DeviceID; name = ([string]$_.Name).Trim(); cores = [int]$_.NumberOfCores; logical = [int]$_.NumberOfLogicalProcessors; load = $(if ($null -ne $_.LoadPercentage) { [double]$_.LoadPercentage } else { $null }); mhz = [double]$_.CurrentClockSpeed } }) }
40# GPU metrics: nvidia-smi where installed, Windows GPU performance counters otherwise (absent on systems without WDDM counters).
41$smi = $null
42try { $smi = (Get-Command 'nvidia-smi.exe' -ErrorAction Stop).Source } catch { $candidate = Join-Path ([string]$env:ProgramFiles) 'NVIDIA Corporation\NVSMI\nvidia-smi.exe'; if (Test-Path -LiteralPath $candidate) { $smi = $candidate } }
43if ($smi) {
44 try {
45 $ErrorActionPreference = 'Continue'
46 $out.nvidia = @(& $smi '--query-gpu=pci.bus_id,name,utilization.gpu,utilization.memory,memory.total,memory.used,temperature.gpu,power.draw,power.limit,clocks.current.graphics,fan.speed,driver_version' '--format=csv,noheader,nounits' 2>$null | ForEach-Object { [string]$_ })
47 } catch {} finally { $ErrorActionPreference = 'Stop' }
49$engines = $null; $adapterMemory = $null
50try { $engines = @(Get-CimInstance -ClassName Win32_PerfFormattedData_GPUPerformanceCounters_GPUEngine -ErrorAction Stop); $adapterMemory = @(Get-CimInstance -ClassName Win32_PerfFormattedData_GPUPerformanceCounters_GPUAdapterMemory -ErrorAction Stop) } catch {}
51if ($adapterMemory) {
52 $busy = @{}
53 foreach ($engine in $engines) {
54 if ($engine.Name -match '(luid_0x[0-9A-Fa-f]+_0x[0-9A-Fa-f]+).*engtype_(.*)$') { $key = "$($Matches[1])|$($Matches[2])"; $busy[$key] = [double]$busy[$key] + [double]$engine.UtilizationPercentage }
55 }
56 $out.gpuCounters = @($adapterMemory | ForEach-Object {
57 $luid = if ($_.Name -match '(luid_0x[0-9A-Fa-f]+_0x[0-9A-Fa-f]+)') { $Matches[1] } else { [string]$_.Name }
58 $peak = $null; foreach ($key in $busy.Keys) { if ($key.StartsWith("$luid|") -and ($null -eq $peak -or $busy[$key] -gt $peak)) { $peak = $busy[$key] } }
59 [ordered]@{ luid = $luid; usage = $peak; dedicated = [double]$_.DedicatedUsage; shared = [double]$_.SharedUsage } })
61$system = Try-Cim 'Win32_PerfRawData_PerfOS_System'
62if ($system) { $out.processorQueueLength = [int]$system[0].ProcessorQueueLength; $out.processes = [int]$system[0].Processes }
63# Physical memory lists (gauges, no second sample needed).
64$memory = Try-Cim 'Win32_PerfRawData_PerfOS_Memory'
65if ($memory) {
66 $m = $memory[0]
67 $out.memoryCounters = [ordered]@{ available = [double]$m.AvailableBytes; cache = [double]$m.CacheBytes; systemCacheResident = [double]$m.SystemCacheResidentBytes
68 standbyCore = [double]$m.StandbyCacheCoreBytes; standbyNormal = [double]$m.StandbyCacheNormalPriorityBytes; standbyReserve = [double]$m.StandbyCacheReserveBytes
69 modified = [double]$m.ModifiedPageListBytes; freeZero = [double]$m.FreeAndZeroPageListBytes; committed = [double]$m.CommittedBytes; commitLimit = [double]$m.CommitLimit
70 poolPaged = [double]$m.PoolPagedResidentBytes; poolNonpaged = [double]$m.PoolNonpagedBytes; systemCode = [double]$m.SystemCodeResidentBytes; systemDriver = [double]$m.SystemDriverResidentBytes
71 pagesInput = [string]$m.PagesInputPersec; pagesOutput = [string]$m.PagesOutputPersec; time = [string]$m.Timestamp_Sys100NS; frequency = [string]$m.Frequency_Sys100NS }
73# Pagefiles: allocated size, current and peak usage (MB as reported by Windows).
74$usage = Try-Cim 'Win32_PageFileUsage'
75if ($null -ne $usage) { $out.pagefiles = @($usage | ForEach-Object { [ordered]@{ name = [string]$_.Name; allocatedMb = [double]$_.AllocatedBaseSize; currentMb = [double]$_.CurrentUsage; peakMb = [double]$_.PeakUsage; temporary = [bool]$_.TempPageFile } }) }
76# Fixed disks through .NET (no WMI rights required).
77$out.disks = @([IO.DriveInfo]::GetDrives() | Where-Object { $_.DriveType -eq 'Fixed' -and $_.IsReady } | ForEach-Object { [ordered]@{ name = $_.Name.TrimEnd('\'); total = [double]$_.TotalSize; available = [double]$_.AvailableFreeSpace; free = [double]$_.TotalFreeSpace; format = [string]$_.DriveFormat; label = [string]$_.VolumeLabel } })
78$out.systemDrive = [string]$env:SystemDrive
79# Network adapters through .NET: status, MAC, unicast addresses and cumulative byte counters.
80$adapters = @()
81foreach ($nic in [Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()) {
82 if ($nic.NetworkInterfaceType -in @('Loopback', 'Tunnel')) { continue }
83 $props = $nic.GetIPProperties()
84 $stats = $null; try { $stats = $nic.GetIPStatistics() } catch {}
85 $addresses = @($props.UnicastAddresses | Where-Object { -not $_.Address.IsIPv6LinkLocal } | ForEach-Object { "$($_.Address.IPAddressToString)/$($_.PrefixLength)" })
86 $mac = ($nic.GetPhysicalAddress().GetAddressBytes() | ForEach-Object { $_.ToString('x2') }) -join ':'
87 $adapters += [ordered]@{ name = $nic.Name; description = $nic.Description; state = [string]$nic.OperationalStatus; mac = $mac; speed = [double]$nic.Speed
88 rx = $(if ($stats) { [double]$stats.BytesReceived } else { $null }); tx = $(if ($stats) { [double]$stats.BytesSent } else { $null })
89 rxPackets = $(if ($stats) { [double]$stats.UnicastPacketsReceived + [double]$stats.NonUnicastPacketsReceived } else { $null })
90 txPackets = $(if ($stats) { [double]$stats.UnicastPacketsSent + [double]$stats.NonUnicastPacketsSent } else { $null })
91 addresses = $addresses; gateway = [bool](@($props.GatewayAddresses | Where-Object { $_.Address.ToString() -notin @('0.0.0.0', '::') }).Count) }
93$out.adapters = $adapters
94[Console]::Out.WriteLine('CHJ_RESULT:' + [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -InputObject $out -Depth 6 -Compress))))
SHA-256496a32c5cea43268225a5239ac0906e2ca47ab7ce0ef50a058e1151fb9faa2a9

CH-J Proprietary Software License 1.14 · ソースは CH-J Proprietary Software License 1.14 に基づいて提供されています。公開されていても、ライセンス条項が変わったり、追加の権利が付与されたりすることはありません。

アーカイブの SHA-25677380d4805485ffa4027fbe7858b5836f197a4aa9d52358a43e2d12d018744df
ファイル