Files
logpile/internal/parser/vendors/linux_generic/inventory.go
T
Mikhail ChusavitinandClaude Opus 5.5 b4b15e58a2 feat(parser): linux-generic content-driven parser and RAR archive support
Field bundles from ad-hoc service scripts (first sample: Giga G894-SD3 .rar
with dmidecode/lscpu/lspci/nvme/lsblk output, no fixed layout). ADL-078:
- linux-generic: recognizes utility output by content, merges records by
  stable key, low detect confidence (20-30) below all vendor parsers
- pciids.LookupByDescription: resolve a textual lspci description back to
  vendor/device IDs and manufacturer/model
- archive: accept .rar via the in-repo internal/rar5 reader (RAR 5 / RAR 7,
  single volume, solid; encrypted, multi-volume, SFX and RAR 4.x rejected),
  same per-file and total limits as tar/zip
- UI: .rar in the upload accept list

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 15:00:38 +03:00

281 lines
7.0 KiB
Go

package linux_generic
import (
"sort"
"strconv"
"strings"
"git.mchus.pro/mchus/logpile/internal/models"
)
// inventory accumulates what the utility parsers find. The same component is
// commonly reported by several files (full `lspci` plus per-device `lspci -vv`
// excerpts, `nvme list` plus `smartctl`), so records are merged by a stable
// key and the first non-empty value of each field wins.
type inventory struct {
board models.BoardInfo
fru []models.FRUInfo
firmware []models.FirmwareInfo
cpus []models.CPU
lscpu lscpuInfo
dimms []*dimm
psus []models.PSU
pci map[string]*pciDevice
drives []*drive
}
func newInventory() *inventory {
return &inventory{pci: make(map[string]*pciDevice)}
}
// drive is one block device assembled from nvme-cli, smartctl and lsblk.
type drive struct {
name string // kernel block device name: nvme0n1, sda
controller string // NVMe controller name: nvme0
model string
serial string
firmware string
mediaType string // SSD | HDD
busInterface string // NVMe | SATA | SAS | ...
sizeBytes int64 // exact capacity, when a source reports bytes
approxSizeGB int // capacity from the `nvme list` usage column (3 significant digits)
roughSizeGB int // capacity from the lsblk SIZE column (binary units, 2 significant digits)
logicalBlock int64
physicalBlock int64
metadataBytes int64
usedPct *int
status string
errorText string
details map[string]any
}
func (d *drive) setDetail(key string, value any) {
if d.details == nil {
d.details = make(map[string]any)
}
if _, exists := d.details[key]; !exists {
d.details[key] = value
}
}
// markStatus keeps the most severe status reported for the drive.
func (d *drive) markStatus(status, errorText string) {
if statusRank(status) <= statusRank(d.status) {
return
}
d.status = status
d.errorText = errorText
}
func statusRank(status string) int {
switch status {
case "Critical":
return 3
case "Warning":
return 2
case "OK":
return 1
}
return 0
}
// findDrive returns the record for a drive, matching by serial number first
// and by kernel device / NVMe controller name second, creating it when the
// drive was not seen yet.
func (inv *inventory) findDrive(serial, name string) *drive {
serial = strings.TrimSpace(serial)
name = strings.TrimPrefix(strings.TrimSpace(name), "/dev/")
controller := nvmeControllerName(name)
if serial != "" {
for _, d := range inv.drives {
if d.serial == serial {
return d.identify(serial, name, controller)
}
}
}
if name != "" {
for _, d := range inv.drives {
if d.serial != "" && serial != "" {
continue
}
if d.name == name || (controller != "" && d.controller == controller) {
return d.identify(serial, name, controller)
}
}
}
if serial == "" && name == "" {
return nil
}
d := &drive{}
inv.drives = append(inv.drives, d)
return d.identify(serial, name, controller)
}
func (d *drive) identify(serial, name, controller string) *drive {
if d.serial == "" {
d.serial = serial
}
// A namespace device name (nvme0n1) is more specific than the bare
// controller name (nvme0) a SMART log is keyed by.
if name != "" && name != controller && (d.name == "" || d.name == d.controller) {
d.name = name
}
if d.name == "" {
d.name = name
}
if d.controller == "" {
d.controller = controller
}
return d
}
// nvmeControllerName returns "nvme0" for "nvme0", "nvme0n1" and "nvme0n1p2".
func nvmeControllerName(name string) string {
if !strings.HasPrefix(name, "nvme") {
return ""
}
end := len("nvme")
for end < len(name) && name[end] >= '0' && name[end] <= '9' {
end++
}
if end == len("nvme") {
return ""
}
return name[:end]
}
// hardware assembles the normalized hardware config from the merged records.
func (inv *inventory) hardware() *models.HardwareConfig {
hw := &models.HardwareConfig{
BoardInfo: inv.board,
Firmware: inv.firmware,
CPUs: inv.buildCPUs(),
PowerSupply: inv.psus,
}
for _, d := range inv.dimms {
hw.Memory = append(hw.Memory, d.MemoryDIMM)
}
hw.Storage = inv.buildStorage()
inv.appendPCIDevices(hw)
return hw
}
func (inv *inventory) buildStorage() []models.Storage {
drives := make([]*drive, len(inv.drives))
copy(drives, inv.drives)
sort.SliceStable(drives, func(i, j int) bool { return naturalLess(drives[i].name, drives[j].name) })
storage := make([]models.Storage, 0, len(drives))
for _, d := range drives {
// Most precise source wins: exact bytes, then nvme list, then lsblk.
sizeGB := d.roughSizeGB
if d.approxSizeGB > 0 {
sizeGB = d.approxSizeGB
}
if d.sizeBytes > 0 {
sizeGB = bytesToGB(d.sizeBytes)
}
item := models.Storage{
Slot: firstNonEmpty(d.name, d.serial),
Type: d.mediaType,
Model: d.model,
SizeGB: sizeGB,
SerialNumber: d.serial,
Firmware: d.firmware,
Interface: d.busInterface,
Present: true,
LogicalBlockSizeBytes: d.logicalBlock,
PhysicalBlockSizeBytes: d.physicalBlock,
MetadataBytesPerBlock: d.metadataBytes,
Status: d.status,
ErrorDescription: d.errorText,
Details: d.details,
}
if d.usedPct != nil {
remaining := 100 - *d.usedPct
if remaining < 0 {
remaining = 0
}
item.RemainingEndurancePct = &remaining
}
storage = append(storage, item)
}
return storage
}
// bytesToGB rounds a byte count to decimal gigabytes.
func bytesToGB(size int64) int {
return int((size + 500_000_000) / 1_000_000_000)
}
// naturalLess orders names with embedded numbers numerically
// (nvme2n1 before nvme10n1).
func naturalLess(a, b string) bool {
for a != "" && b != "" {
aDigits, bDigits := leadingDigits(a), leadingDigits(b)
if aDigits > 0 && bDigits > 0 {
an, _ := strconv.ParseUint(a[:aDigits], 10, 64)
bn, _ := strconv.ParseUint(b[:bDigits], 10, 64)
if an != bn {
return an < bn
}
a, b = a[aDigits:], b[bDigits:]
continue
}
if a[0] != b[0] {
return a[0] < b[0]
}
a, b = a[1:], b[1:]
}
return len(a) < len(b)
}
func leadingDigits(s string) int {
n := 0
for n < len(s) && s[n] >= '0' && s[n] <= '9' {
n++
}
return n
}
// parseSizeBytes parses a "<number> <unit>" capacity. SI units are decimal
// (nvme-cli, smartctl) and IEC units binary.
func parseSizeBytes(number, unit string) (int64, bool) {
value, err := strconv.ParseFloat(strings.ReplaceAll(strings.TrimSpace(number), ",", ""), 64)
if err != nil || value < 0 {
return 0, false
}
var multiplier float64
switch strings.ToUpper(strings.TrimSpace(unit)) {
case "B", "BYTES":
multiplier = 1
case "KB":
multiplier = 1e3
case "MB":
multiplier = 1e6
case "GB":
multiplier = 1e9
case "TB":
multiplier = 1e12
case "PB":
multiplier = 1e15
case "KIB":
multiplier = 1 << 10
case "MIB":
multiplier = 1 << 20
case "GIB":
multiplier = 1 << 30
case "TIB":
multiplier = 1 << 40
case "PIB":
multiplier = 1 << 50
default:
return 0, false
}
return int64(value*multiplier + 0.5), true
}