Files
logpile/internal/parser/vendors/linux_generic/smartctl.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

146 lines
4.3 KiB
Go

package linux_generic
import (
"path"
"regexp"
"strings"
)
var (
smartctlBannerRe = regexp.MustCompile(`^smartctl \d+\.\d+ `)
blockDeviceRe = regexp.MustCompile(`(?:^|[^a-z0-9])(nvme\d+(?:n\d+)?|sd[a-z]+)(?:$|[^a-z0-9])`)
sectorSizeRe = regexp.MustCompile(`(\d+) bytes (logical/physical|logical|physical)`)
)
// parseSmartctl parses `smartctl -i/-a/-x` output for ATA, SAS and NVMe
// drives. One file may hold the reports of several drives; each starts with
// the smartctl version banner.
func parseSmartctl(file sourceFile, inv *inventory) {
lines := strings.Split(file.content, "\n")
var starts []int
for i, line := range lines {
if smartctlBannerRe.MatchString(line) {
starts = append(starts, i)
}
}
if len(starts) == 0 {
starts = []int{0}
}
// smartctl does not print the device node. It is taken from a command
// echo right above the banner or, for one-report files, the file name.
fileHint := ""
if len(starts) == 1 {
fileHint = blockDeviceName(path.Base(strings.ReplaceAll(file.path, `\`, "/")))
}
for n, start := range starts {
end := len(lines)
if n+1 < len(starts) {
end = starts[n+1]
}
hint := fileHint
for back := start - 1; back >= 0 && back >= start-2; back-- {
if strings.Contains(lines[back], "/dev/") {
if name := blockDeviceName(lines[back]); name != "" {
hint = name
}
break
}
}
parseSmartctlReport(lines[start:end], hint, inv)
}
}
// blockDeviceName extracts a kernel block device name (sda, nvme0n1) from a
// file name or a command echo.
func blockDeviceName(text string) string {
if m := blockDeviceRe.FindStringSubmatch(strings.ToLower(text)); m != nil {
return m[1]
}
return ""
}
func parseSmartctlReport(lines []string, nameHint string, inv *inventory) {
fields := make(map[string]string)
for _, line := range lines {
key, value, ok := splitKeyValue(line)
if !ok || value == "" {
continue
}
key = strings.ToLower(key)
if _, seen := fields[key]; !seen {
fields[key] = value
}
}
model := firstNonEmpty(fields["device model"], fields["model number"])
if model == "" && fields["product"] != "" {
// SCSI/SAS drives report vendor and product separately.
model = strings.TrimSpace(fields["vendor"] + " " + fields["product"])
}
serial := fields["serial number"]
if model == "" && serial == "" {
return
}
d := inv.findDrive(serial, nameHint)
if d == nil {
return
}
setIfEmpty(&d.model, model)
setIfEmpty(&d.firmware, firstNonEmpty(fields["firmware version"], fields["revision"]))
capacity := firstNonEmpty(fields["user capacity"], fields["total nvm capacity"], fields["namespace 1 size/capacity"])
if size, ok := leadingInt(capacity); ok && size > 0 && d.sizeBytes == 0 {
d.sizeBytes = size
}
_, isNVMe := fields["model number"]
switch rotation := strings.ToLower(fields["rotation rate"]); {
case isNVMe, strings.Contains(rotation, "solid state"):
setIfEmpty(&d.mediaType, "SSD")
case strings.Contains(rotation, "rpm"):
setIfEmpty(&d.mediaType, "HDD")
}
switch transport := strings.ToUpper(fields["transport protocol"]); {
case isNVMe:
setIfEmpty(&d.busInterface, "NVMe")
case fields["sata version is"] != "":
setIfEmpty(&d.busInterface, "SATA")
case strings.Contains(transport, "SAS"):
setIfEmpty(&d.busInterface, "SAS")
}
sectors := firstNonEmpty(fields["sector sizes"], fields["sector size"], fields["logical block size"])
for _, m := range sectorSizeRe.FindAllStringSubmatch(sectors, -1) {
size, _ := leadingInt(m[1])
if strings.Contains(m[2], "logical") && d.logicalBlock == 0 {
d.logicalBlock = size
}
if strings.Contains(m[2], "physical") && d.physicalBlock == 0 {
d.physicalBlock = size
}
}
if size, ok := leadingInt(fields["logical block size"]); ok && d.logicalBlock == 0 {
d.logicalBlock = size
}
if isNVMe {
applyNvmeHealth(d, fields["critical warning"], fields["percentage used"], fields["media and data integrity errors"])
}
health := strings.ToUpper(firstNonEmpty(fields["smart overall-health self-assessment test result"], fields["smart health status"]))
switch {
case health == "":
case strings.HasPrefix(health, "PASSED"), strings.HasPrefix(health, "OK"):
d.markStatus("OK", "")
default:
d.markStatus("Critical", "SMART health: "+health)
}
if hours, ok := leadingInt(fields["power on hours"]); ok {
d.setDetail("power_on_hours", hours)
}
}