package linux_generic import ( "fmt" "regexp" "strconv" "strings" ) var ( nvmeSizeRe = regexp.MustCompile(`([0-9][0-9.,]*)\s*([KMGTP]?i?B)\b`) nvmeSmartHeaderRe = regexp.MustCompile(`^Smart Log for NVME device:\s*(\S+)`) ) // parseNvmeList parses the `nvme list` table. Column boundaries come from // the dashed separator line, so both the older layout and the newer one with // a "Generic" column are handled, and model names may contain spaces. func parseNvmeList(file sourceFile, inv *inventory) { lines := strings.Split(file.content, "\n") for i := 0; i+1 < len(lines); i++ { if !strings.HasPrefix(lines[i], "Node") || !strings.HasPrefix(lines[i+1], "---") { continue } columns := tableColumns(lines[i], lines[i+1]) next := i + 2 for ; next < len(lines) && strings.HasPrefix(lines[next], "/dev/"); next++ { row := tableRow(lines[next], columns) d := inv.findDrive(row["SN"], row["Node"]) if d == nil { continue } setIfEmpty(&d.model, row["Model"]) setIfEmpty(&d.firmware, row["FW Rev"]) setIfEmpty(&d.mediaType, "SSD") setIfEmpty(&d.busInterface, "NVMe") // Usage is " / ". if _, total, ok := strings.Cut(row["Usage"], "/"); ok && d.approxSizeGB == 0 { if size, ok := parseNvmeSize(total); ok { d.approxSizeGB = bytesToGB(size) } } // Format is " + ". if lba, metadata, ok := strings.Cut(row["Format"], "+"); ok && d.logicalBlock == 0 { if size, ok := parseNvmeSize(lba); ok { d.logicalBlock = size } if size, ok := parseNvmeSize(metadata); ok { d.metadataBytes = size } } } i = next - 1 } } type tableColumn struct { name string start, end int } // tableColumns derives fixed-width column spans from a dashed separator line. func tableColumns(header, separator string) []tableColumn { var columns []tableColumn for pos := 0; pos < len(separator); { if separator[pos] != '-' { pos++ continue } end := pos for end < len(separator) && separator[end] == '-' { end++ } column := tableColumn{start: pos, end: end} if pos < len(header) { column.name = strings.TrimSpace(header[pos:min(end, len(header))]) } columns = append(columns, column) pos = end } return columns } func tableRow(line string, columns []tableColumn) map[string]string { row := make(map[string]string, len(columns)) for i, column := range columns { if column.start >= len(line) { break } end := column.end if i == len(columns)-1 || end > len(line) { end = len(line) } row[column.name] = strings.TrimSpace(line[column.start:end]) } return row } func parseNvmeSize(value string) (int64, bool) { m := nvmeSizeRe.FindStringSubmatch(value) if m == nil { return 0, false } return parseSizeBytes(m[1], m[2]) } // parseNvmeSmartLog parses `nvme smart-log` blocks. A block runs until the // first blank line, so `nvme id-ctrl` fields (sn, mn, fr, tnvmcap) that a // script appends right after the log are picked up as part of it. func parseNvmeSmartLog(file sourceFile, inv *inventory) { lines := strings.Split(file.content, "\n") for i := 0; i < len(lines); i++ { m := nvmeSmartHeaderRe.FindStringSubmatch(lines[i]) if m == nil { continue } fields := make(map[string]string) next := i + 1 for ; next < len(lines) && strings.TrimSpace(lines[next]) != ""; next++ { if nvmeSmartHeaderRe.MatchString(lines[next]) { break } if key, value, ok := splitKeyValue(lines[next]); ok { fields[strings.ToLower(key)] = value } } i = next - 1 d := inv.findDrive(fields["sn"], m[1]) if d == nil { continue } setIfEmpty(&d.model, fields["mn"]) setIfEmpty(&d.firmware, fields["fr"]) setIfEmpty(&d.mediaType, "SSD") setIfEmpty(&d.busInterface, "NVMe") if size, ok := leadingInt(fields["tnvmcap"]); ok && size > 0 && d.sizeBytes == 0 { d.sizeBytes = size } applyNvmeHealth(d, fields["critical_warning"], fields["percentage_used"], fields["media_errors"]) for key, detail := range map[string]string{ "temperature": "temperature_c", "available_spare": "available_spare_pct", "power_on_hours": "power_on_hours", "power_cycles": "power_cycles", "unsafe_shutdowns": "unsafe_shutdowns", "media_errors": "media_errors", } { if n, ok := leadingInt(fields[key]); ok { d.setDetail(detail, n) } } } } // applyNvmeHealth records wear and the SMART health state on the drive. // It is snapshot state of the component, not an event. func applyNvmeHealth(d *drive, criticalWarning, percentageUsed, mediaErrors string) { if used, ok := leadingInt(percentageUsed); ok && d.usedPct == nil { pct := int(used) d.usedPct = &pct } warning, err := strconv.ParseInt(strings.TrimSpace(criticalWarning), 0, 64) if err != nil { return } switch errors, _ := leadingInt(mediaErrors); { case warning != 0: d.markStatus("Critical", fmt.Sprintf("NVMe SMART critical warning 0x%02x", warning)) case errors > 0: d.markStatus("Warning", fmt.Sprintf("NVMe SMART media errors: %d", errors)) default: d.markStatus("OK", "") } }