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>
348 lines
9.2 KiB
Go
348 lines
9.2 KiB
Go
package linux_generic
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.mchus.pro/mchus/logpile/internal/models"
|
|
)
|
|
|
|
type dmiFields map[string]string
|
|
|
|
// dimm keeps the dmidecode Bank Locator next to the DIMM: it disambiguates
|
|
// boards that reuse the same Locator string in several banks.
|
|
type dimm struct {
|
|
models.MemoryDIMM
|
|
bank string
|
|
}
|
|
|
|
// dmiSectionParsers maps a dmidecode section title to its handler. Titles
|
|
// are matched exactly, so both full `dmidecode` output (with "Handle ..."
|
|
// lines) and grep-filtered excerpts that keep only the title and a few
|
|
// indented fields are recognized.
|
|
var dmiSectionParsers = map[string]func(s *dmiScan, fields dmiFields){
|
|
"System Information": (*dmiScan).system,
|
|
"Base Board Information": (*dmiScan).baseBoard,
|
|
"Chassis Information": (*dmiScan).chassis,
|
|
"BIOS Information": (*dmiScan).bios,
|
|
"Processor Information": (*dmiScan).processor,
|
|
"Memory Device": (*dmiScan).memoryDevice,
|
|
"System Power Supply": (*dmiScan).powerSupply,
|
|
}
|
|
|
|
// dmiScan holds the per-file state of one dmidecode pass. CPUs and PSUs have
|
|
// no reliable cross-file key, so they are collected per file and the most
|
|
// complete file wins.
|
|
type dmiScan struct {
|
|
inv *inventory
|
|
cpus []models.CPU
|
|
cpuSockets int
|
|
psus []models.PSU
|
|
psuSeen int
|
|
}
|
|
|
|
func parseDMI(file sourceFile, inv *inventory) {
|
|
scan := &dmiScan{inv: inv}
|
|
lines := strings.Split(file.content, "\n")
|
|
|
|
for i := 0; i < len(lines); i++ {
|
|
handler, ok := dmiSectionParsers[strings.TrimRight(lines[i], " \t")]
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
fields := make(dmiFields)
|
|
next := i + 1
|
|
for ; next < len(lines) && isIndented(lines[next]); next++ {
|
|
key, value, ok := splitKeyValue(lines[next])
|
|
if !ok {
|
|
continue
|
|
}
|
|
if _, seen := fields[key]; !seen {
|
|
fields[key] = cleanDMIValue(value)
|
|
}
|
|
}
|
|
handler(scan, fields)
|
|
i = next - 1
|
|
}
|
|
|
|
if len(scan.cpus) > len(inv.cpus) {
|
|
inv.cpus = scan.cpus
|
|
}
|
|
if len(scan.psus) > len(inv.psus) {
|
|
inv.psus = scan.psus
|
|
}
|
|
}
|
|
|
|
// cleanDMIValue drops the placeholders firmware vendors leave in unset
|
|
// SMBIOS strings.
|
|
func cleanDMIValue(value string) string {
|
|
value = strings.TrimSpace(value)
|
|
switch strings.ToLower(value) {
|
|
case "not specified", "not provided", "not present", "unknown", "none", "n/a", "null",
|
|
"to be filled by o.e.m.", "default string", "no dimm", "<out of spec>",
|
|
"system product name", "system serial number", "system manufacturer",
|
|
"no asset tag", "not settable", "<bad index>":
|
|
return ""
|
|
}
|
|
return value
|
|
}
|
|
|
|
func setIfEmpty(dst *string, value string) {
|
|
if *dst == "" {
|
|
*dst = value
|
|
}
|
|
}
|
|
|
|
func (s *dmiScan) system(fields dmiFields) {
|
|
board := &s.inv.board
|
|
setIfEmpty(&board.Manufacturer, fields["Manufacturer"])
|
|
setIfEmpty(&board.ProductName, fields["Product Name"])
|
|
setIfEmpty(&board.Version, fields["Version"])
|
|
setIfEmpty(&board.SerialNumber, fields["Serial Number"])
|
|
setIfEmpty(&board.UUID, fields["UUID"])
|
|
}
|
|
|
|
func (s *dmiScan) baseBoard(fields dmiFields) {
|
|
s.addFRU("Base Board", fields)
|
|
}
|
|
|
|
func (s *dmiScan) chassis(fields dmiFields) {
|
|
fru := s.addFRU("Chassis", fields)
|
|
if fru != nil {
|
|
fru.ChassisType = fields["Type"]
|
|
}
|
|
}
|
|
|
|
// addFRU records a dmidecode board/chassis section as a FRU entry. These are
|
|
// not merged into BoardInfo: a base board serial is not the system serial.
|
|
func (s *dmiScan) addFRU(description string, fields dmiFields) *models.FRUInfo {
|
|
fru := models.FRUInfo{
|
|
Description: description,
|
|
Manufacturer: fields["Manufacturer"],
|
|
ProductName: fields["Product Name"],
|
|
SerialNumber: fields["Serial Number"],
|
|
PartNumber: fields["Part Number"],
|
|
Version: fields["Version"],
|
|
AssetTag: fields["Asset Tag"],
|
|
}
|
|
if fru.Manufacturer == "" && fru.ProductName == "" && fru.SerialNumber == "" {
|
|
return nil
|
|
}
|
|
for i := range s.inv.fru {
|
|
if s.inv.fru[i].Description == description {
|
|
return &s.inv.fru[i]
|
|
}
|
|
}
|
|
s.inv.fru = append(s.inv.fru, fru)
|
|
return &s.inv.fru[len(s.inv.fru)-1]
|
|
}
|
|
|
|
func (s *dmiScan) bios(fields dmiFields) {
|
|
version := fields["Version"]
|
|
if version == "" {
|
|
return
|
|
}
|
|
for _, fw := range s.inv.firmware {
|
|
if fw.DeviceName == "BIOS" {
|
|
return
|
|
}
|
|
}
|
|
s.inv.firmware = append(s.inv.firmware, models.FirmwareInfo{
|
|
DeviceName: "BIOS",
|
|
Description: fields["Vendor"],
|
|
Version: version,
|
|
BuildTime: fields["Release Date"],
|
|
})
|
|
}
|
|
|
|
func (s *dmiScan) processor(fields dmiFields) {
|
|
socket := s.cpuSockets
|
|
s.cpuSockets++
|
|
|
|
status := strings.ToLower(fields["Status"])
|
|
if strings.Contains(status, "unpopulated") || fields["Version"] == "" {
|
|
return
|
|
}
|
|
|
|
cpu := models.CPU{
|
|
Socket: socket,
|
|
Model: fields["Version"],
|
|
Description: fields["Socket Designation"],
|
|
SerialNumber: fields["Serial Number"],
|
|
Cores: atoi(fields["Core Count"]),
|
|
Threads: atoi(fields["Thread Count"]),
|
|
FrequencyMHz: parseMHz(fields["Current Speed"]),
|
|
MaxFreqMHz: parseMHz(fields["Max Speed"]),
|
|
}
|
|
s.cpus = append(s.cpus, cpu)
|
|
}
|
|
|
|
func (s *dmiScan) memoryDevice(fields dmiFields) {
|
|
sizeMB := parseDMIMemorySizeMB(fields["Size"])
|
|
if sizeMB == 0 {
|
|
// "No Module Installed" and sizeless stubs are empty slots.
|
|
return
|
|
}
|
|
|
|
slot := fields["Locator"]
|
|
module := dimm{
|
|
MemoryDIMM: models.MemoryDIMM{
|
|
Slot: slot,
|
|
Location: slot,
|
|
Present: true,
|
|
SizeMB: sizeMB,
|
|
Type: fields["Type"],
|
|
Technology: fields["Type Detail"],
|
|
MaxSpeedMHz: int(leadingNumber(fields["Speed"])),
|
|
CurrentSpeedMHz: int(leadingNumber(fields["Configured Memory Speed"])),
|
|
Manufacturer: fields["Manufacturer"],
|
|
SerialNumber: fields["Serial Number"],
|
|
PartNumber: fields["Part Number"],
|
|
Ranks: atoi(fields["Rank"]),
|
|
},
|
|
bank: fields["Bank Locator"],
|
|
}
|
|
if module.CurrentSpeedMHz == 0 {
|
|
module.CurrentSpeedMHz = int(leadingNumber(fields["Configured Clock Speed"]))
|
|
}
|
|
|
|
if existing := s.inv.findDIMM(module); existing != nil {
|
|
existing.merge(module)
|
|
return
|
|
}
|
|
s.inv.dimms = append(s.inv.dimms, &module)
|
|
}
|
|
|
|
// findDIMM returns the already-recorded module that is the same physical
|
|
// DIMM: same serial, or the same slot with no conflicting serial or bank.
|
|
func (inv *inventory) findDIMM(module dimm) *dimm {
|
|
for _, existing := range inv.dimms {
|
|
sameSerial := existing.SerialNumber != "" && existing.SerialNumber == module.SerialNumber
|
|
serialConflict := existing.SerialNumber != "" && module.SerialNumber != "" && !sameSerial
|
|
bankConflict := existing.bank != "" && module.bank != "" && existing.bank != module.bank
|
|
|
|
if existing.Slot == module.Slot && module.Slot != "" {
|
|
if !serialConflict && !bankConflict {
|
|
return existing
|
|
}
|
|
continue
|
|
}
|
|
if sameSerial && (existing.Slot == "" || module.Slot == "") {
|
|
return existing
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (d *dimm) merge(other dimm) {
|
|
setIfEmpty(&d.Slot, other.Slot)
|
|
setIfEmpty(&d.Location, other.Location)
|
|
setIfEmpty(&d.bank, other.bank)
|
|
setIfEmpty(&d.Type, other.Type)
|
|
setIfEmpty(&d.Technology, other.Technology)
|
|
setIfEmpty(&d.Manufacturer, other.Manufacturer)
|
|
setIfEmpty(&d.SerialNumber, other.SerialNumber)
|
|
setIfEmpty(&d.PartNumber, other.PartNumber)
|
|
if d.MaxSpeedMHz == 0 {
|
|
d.MaxSpeedMHz = other.MaxSpeedMHz
|
|
}
|
|
if d.CurrentSpeedMHz == 0 {
|
|
d.CurrentSpeedMHz = other.CurrentSpeedMHz
|
|
}
|
|
if d.Ranks == 0 {
|
|
d.Ranks = other.Ranks
|
|
}
|
|
}
|
|
|
|
func (s *dmiScan) powerSupply(fields dmiFields) {
|
|
s.psuSeen++
|
|
status := strings.ToLower(fields["Status"])
|
|
if strings.HasPrefix(status, "not present") {
|
|
return
|
|
}
|
|
psu := models.PSU{
|
|
Slot: firstNonEmpty(fields["Location"], "PSU"+strconv.Itoa(s.psuSeen)),
|
|
Present: true,
|
|
Model: fields["Name"],
|
|
Vendor: fields["Manufacturer"],
|
|
SerialNumber: fields["Serial Number"],
|
|
PartNumber: fields["Model Part Number"],
|
|
Firmware: fields["Revision"],
|
|
WattageW: int(leadingNumber(fields["Max Power Capacity"])),
|
|
}
|
|
if psu.Model == "" && psu.SerialNumber == "" && psu.PartNumber == "" {
|
|
return
|
|
}
|
|
switch {
|
|
case strings.Contains(status, "critical") && !strings.Contains(status, "non-critical"):
|
|
psu.Status = "Critical"
|
|
case strings.Contains(status, "non-critical"):
|
|
psu.Status = "Warning"
|
|
case strings.HasSuffix(status, "ok"):
|
|
psu.Status = "OK"
|
|
}
|
|
s.psus = append(s.psus, psu)
|
|
}
|
|
|
|
// parseDMIMemorySizeMB parses a dmidecode memory size ("64 GB", "32768 MB").
|
|
func parseDMIMemorySizeMB(value string) int {
|
|
fields := strings.Fields(value)
|
|
if len(fields) != 2 {
|
|
return 0
|
|
}
|
|
size, err := strconv.Atoi(fields[0])
|
|
if err != nil || size <= 0 {
|
|
return 0
|
|
}
|
|
switch strings.ToUpper(fields[1]) {
|
|
case "MB":
|
|
return size
|
|
case "GB":
|
|
return size * 1024
|
|
case "TB":
|
|
return size * 1024 * 1024
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// parseMHz parses a dmidecode frequency ("3800 MHz", "2.0 GHz").
|
|
func parseMHz(value string) int {
|
|
fields := strings.Fields(value)
|
|
if len(fields) != 2 {
|
|
return 0
|
|
}
|
|
speed, err := strconv.ParseFloat(fields[0], 64)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
switch strings.ToUpper(fields[1]) {
|
|
case "MHZ":
|
|
return int(speed)
|
|
case "GHZ":
|
|
return int(speed * 1000)
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// leadingNumber parses the leading number of a "<number> <unit>" value.
|
|
func leadingNumber(value string) float64 {
|
|
fields := strings.Fields(value)
|
|
if len(fields) == 0 {
|
|
return 0
|
|
}
|
|
n, err := strconv.ParseFloat(fields[0], 64)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return n
|
|
}
|
|
|
|
func atoi(value string) int {
|
|
n, err := strconv.Atoi(strings.TrimSpace(value))
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return n
|
|
}
|