feat(device): collect static machine resource info#68
Open
swarit-stepsecurity wants to merge 1 commit into
Open
feat(device): collect static machine resource info#68swarit-stepsecurity wants to merge 1 commit into
swarit-stepsecurity wants to merge 1 commit into
Conversation
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.
There was a problem hiding this comment.
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.MachineResourcesand embeds it inmodel.Deviceand telemetryPayload. - 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) | ||
| } |
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
Implementation
model.MachineResourcesstruct embedded asDevice.Resources— single nested key, additive change.internal/device/resources.{go,_other.go,_windows.go}follow the existing platform-split convention in this package./proc/cpuinfo+/proc/meminfo(no subprocess). Graceful ARM degradation — falls back toHardware/Modellines and reportsphysical_cores: 0rather than misreporting.sysctl machdep.cpu.brand_string,hw.physicalcpu,hw.logicalcpu,hw.memsize.ProcessorNameString,GetLogicalProcessorInformation,GlobalMemoryStatusEx,GetDiskFreeSpaceEx) with PowerShellGet-CimInstanceas the only fallback. wmic is no longer available on Win11 / Server 2025 and was deliberately not used.Executor.DiskCapacityBytes(path) uint64method (syscall.Statfson unix,GetDiskFreeSpaceExon windows) so it stays mockable like every other OS interaction.Payload.SetGOOS, and missing-file degradation.Test plan
go test ./...— full suite greengo vet ./...cleannproc(4),free -h(15Gi),df -h /(29G)Get-CimInstance Win32_Processor(1c/2t Xeon 8175M) andWin32_ComputerSystem(8.36 GB / 99.5 GB)