Skip to content

feat(device): collect static machine resource info#68

Open
swarit-stepsecurity wants to merge 1 commit into
step-security:mainfrom
swarit-stepsecurity:swarit/feat/wt/machine-resource-info
Open

feat(device): collect static machine resource info#68
swarit-stepsecurity wants to merge 1 commit into
step-security:mainfrom
swarit-stepsecurity:swarit/feat/wt/machine-resource-info

Conversation

@swarit-stepsecurity
Copy link
Copy Markdown
Member

Summary

Adds CPU model / physical+logical cores / architecture, total RAM, and root volume capacity to the device snapshot reported on every scan. High-signal answer to "how much resource does this machine have" — no volatile fields (free RAM, per-process load) that would muddy a static snapshot.

DEVICE
  Hostname    EC2AMAZ-HJS7CVB
  Serial      ec241363-fe37-f981-a408-72c4cfdf51c7
  Windows     10.0.26100
  User        EC2AMAZ-HJS7CVB\Administrator
  CPU         Intel(R) Xeon(R) Platinum 8175M CPU @ 2.50GHz (1c / 2t, amd64)
  Memory      7 GB
  Disk        99 GB

Implementation

  • New model.MachineResources struct embedded as Device.Resources — single nested key, additive change.
  • internal/device/resources.{go,_other.go,_windows.go} follow the existing platform-split convention in this package.
    • Linux: /proc/cpuinfo + /proc/meminfo (no subprocess). Graceful ARM degradation — falls back to Hardware/Model lines and reports physical_cores: 0 rather than misreporting.
    • macOS: sysctl machdep.cpu.brand_string, hw.physicalcpu, hw.logicalcpu, hw.memsize.
    • Windows: native Win32 APIs (registry ProcessorNameString, GetLogicalProcessorInformation, GlobalMemoryStatusEx, GetDiskFreeSpaceEx) with PowerShell Get-CimInstance as the only fallback. wmic is no longer available on Win11 / Server 2025 and was deliberately not used.
  • Disk capacity goes through a new Executor.DiskCapacityBytes(path) uint64 method (syscall.Statfs on unix, GetDiskFreeSpaceEx on windows) so it stays mockable like every other OS interaction.
  • Surfaced in pretty + HTML output and added to the enterprise telemetry Payload.
  • Tests cover parsers (x86, ARM, multi-socket, malformed), all three OS code paths via SetGOOS, and missing-file degradation.

Test plan

  • go test ./... — full suite green
  • go vet ./... clean
  • Cross-compile verified for darwin/amd64+arm64, linux/amd64+arm64, windows/amd64
  • macOS host (M4 Pro MBP): numbers match local hardware
  • Linux (Fedora 42 EC2 t3.xlarge): matches nproc (4), free -h (15Gi), df -h / (29G)
  • Windows (Server 2025 EC2 t3.large): matches Get-CimInstance Win32_Processor (1c/2t Xeon 8175M) and Win32_ComputerSystem (8.36 GB / 99.5 GB)

Adds CPU model / physical+logical cores / architecture, total RAM, and
root volume capacity to the device snapshot reported on every scan.
Answers "how much resource does this machine have" without including
volatile signals like free memory or per-process load.

Linux reads /proc/cpuinfo and /proc/meminfo (no subprocess); macOS uses
sysctl; Windows uses native Win32 APIs (registry CPU name,
GetLogicalProcessorInformation, GlobalMemoryStatusEx, GetDiskFreeSpaceEx)
with PowerShell Get-CimInstance as fallback — wmic is unavailable on
Win11 / Server 2025. Disk capacity routes through a new
Executor.DiskCapacityBytes method so it stays mockable like everything
else.

Surfaced in pretty + HTML output and the enterprise telemetry payload.
Verified end-to-end against ground truth on macOS, Fedora 42 EC2, and
Server 2025 EC2.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds static machine resource capacity (CPU model/cores/arch, total RAM, root volume capacity) to the device snapshot so it can be surfaced in CLI outputs and enterprise telemetry payloads.

Changes:

  • Introduces model.MachineResources and embeds it in model.Device and telemetry Payload.
  • Implements best-effort resource collection across Linux/macOS/Windows with platform-split device helpers and a new mockable Executor.DiskCapacityBytes.
  • Surfaces resources in pretty + HTML reports and adds parser-focused unit tests.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
internal/telemetry/telemetry.go Adds resources to enterprise telemetry payload and populates it from the gathered device snapshot.
internal/output/pretty.go Prints CPU/memory/disk in pretty output; adds formatting helpers for CPU + byte sizes.
internal/output/html.go Adds CPU/memory/disk fields to the HTML report and registers formatting helpers for templates.
internal/model/model.go Adds MachineResources and embeds it in Device JSON output.
internal/executor/executor.go Extends Executor interface with DiskCapacityBytes(path) uint64.
internal/executor/executor_unix.go Implements unix disk capacity via statfs.
internal/executor/executor_windows.go Implements Windows disk capacity via GetDiskFreeSpaceEx.
internal/executor/mock.go Adds mock support for disk capacity stubbing for tests.
internal/executor/user_aware.go Forwards DiskCapacityBytes through UserAwareExecutor.
internal/device/device.go Populates Device.Resources during device gather.
internal/device/resources.go Core cross-platform resource gathering + Linux parsers + shared Windows CIM parsing helpers.
internal/device/resources_windows.go Windows-native CPU/memory discovery with PowerShell CIM fallbacks.
internal/device/resources_other.go Non-Windows stubs to exercise Windows resource collection path in tests via SetGOOS("windows").
internal/device/resources_test.go Adds unit tests for Linux parsers and resource gather behavior across simulated OS paths.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +18 to +24
func (r *Real) DiskCapacityBytes(path string) uint64 {
var stat syscall.Statfs_t
if err := syscall.Statfs(path, &stat); err != nil {
return 0
}
return uint64(stat.Blocks) * uint64(stat.Bsize)
}
Comment thread internal/output/pretty.go
return parts[0]
}
if len(parts) == 0 {
return "(" + strings.Join(detail, " / ") + ")"
Comment on lines +17 to +19
// counts cores via GetLogicalProcessorInformation. Falls back to wmic if
// either step fails — VMs and stripped Server Core images occasionally lack
// one or the other.
Comment on lines +66 to +67
// kernel32 export is wrapped via syscall.NewLazyDLL so we don't need a build-
// time binding for it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants