| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package smartctl |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | type scanDevice struct { |
| 12 | name string |
| 13 | infoName string |
| 14 | typ string |
| 15 | extra bool // added via config "extra_devices" |
| 16 | } |
| 17 | |
| 18 | func (s *scanDevice) key() string { |
| 19 | return fmt.Sprintf("%s|%s", s.name, s.typ) |
| 20 | } |
| 21 | |
| 22 | func (s *scanDevice) shortName() string { |
| 23 | return strings.TrimPrefix(s.name, "/dev/") |
| 24 | } |
| 25 | |
| 26 | func (c *Collector) scanDevices() (map[string]*scanDevice, error) { |
| 27 | // Issue on Discord: https://discord.com/channels/847502280503590932/1261747175361347644/1261747175361347644 |
| 28 | // "sat" devices being identified as "scsi" with --scan, and then later |
| 29 | // code attempts to validate the type by calling `smartctl` with the "scsi" type. |
| 30 | // This validation can trigger unintended "Enabling discard_zeroes_data" messages in system logs (dmesg). |
| 31 | // To address this specific issue we use `smartctl --scan-open` as a workaround. |
| 32 | // This method reliably identifies device types. |
| 33 | scanOpen := c.NoCheckPowerMode == "never" |
| 34 | |
| 35 | resp, err := c.exec.scan(scanOpen) |
| 36 | if err != nil { |
| 37 | return nil, fmt.Errorf("failed to scan devices: %v", err) |
| 38 | } |
| 39 | |
| 40 | devices := make(map[string]*scanDevice) |
| 41 | |
| 42 | for _, d := range resp.Get("devices").Array() { |
| 43 | dev := &scanDevice{ |
| 44 | name: d.Get("name").String(), |
| 45 | infoName: d.Get("info_name").String(), |
| 46 | typ: d.Get("type").String(), |
| 47 | } |
| 48 | |
| 49 | if dev.name == "" || dev.typ == "" { |
| 50 | c.Warningf("device info missing required fields (name: '%s', type: '%s'), skipping", dev.name, dev.typ) |
| 51 | continue |
| 52 | } |
| 53 | |
| 54 | if !c.deviceSr.MatchString(dev.infoName) { |
| 55 | c.Debugf("device %s does not match selector, skipping it", dev.infoName) |
| 56 | continue |
| 57 | } |
| 58 | |
| 59 | if !scanOpen && dev.typ == "scsi" { |
| 60 | // `smartctl --scan` attempts to guess the device type based on the path, but this can be unreliable. |
| 61 | // Accurate device type information is crucial because we use the `--device` option to gather data. |
| 62 | // Using the wrong type can lead to issues. |
| 63 | // For example, using 'scsi' for 'sat' devices prevents `smartctl` from issuing the necessary ATA commands. |
| 64 | |
| 65 | c.handleGuessedScsiScannedDevice(dev) |
| 66 | } |
| 67 | |
| 68 | c.Debugf("smartctl scan found device '%s' type '%s' info_name '%s'", dev.name, dev.typ, dev.infoName) |
| 69 | |
| 70 | devices[dev.key()] = dev |
| 71 | } |
| 72 | |
| 73 | c.Debugf("smartctl scan found %d devices", len(devices)) |
| 74 | |
| 75 | for _, v := range c.ExtraDevices { |
| 76 | dev := &scanDevice{name: v.Name, typ: v.Type, extra: true} |
| 77 | |
| 78 | if _, ok := devices[dev.key()]; !ok { |
| 79 | devices[dev.key()] = dev |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if len(devices) == 0 { |
| 84 | return nil, errors.New("no devices found during scan") |
| 85 | } |
| 86 | |
| 87 | return devices, nil |
| 88 | } |
| 89 | |
| 90 | func (c *Collector) handleGuessedScsiScannedDevice(dev *scanDevice) { |
| 91 | if dev.typ != "scsi" || c.hasScannedDevice(dev) { |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | d := &scanDevice{name: dev.name, typ: "sat"} |
| 96 | |
| 97 | if c.hasScannedDevice(d) { |
| 98 | dev.typ = d.typ |
| 99 | return |
| 100 | } |
| 101 | |
| 102 | resp, _ := c.exec.deviceInfo(dev.name, "sat", c.NoCheckPowerMode) |
| 103 | if resp == nil || isExitStatusHasAnyBit(resp, 0, 1, 2) { |
| 104 | return |
| 105 | } |
| 106 | |
| 107 | attrs, ok := newSmartDevice(resp).ataSmartAttributeTable() |
| 108 | if !ok || len(attrs) == 0 { |
| 109 | return |
| 110 | } |
| 111 | |
| 112 | c.Debugf("changing device '%s' type 'scsi' -> 'sat'", dev.name) |
| 113 | dev.typ = "sat" |
| 114 | } |
| 115 | |
| 116 | func (c *Collector) hasScannedDevice(d *scanDevice) bool { |
| 117 | _, ok := c.scannedDevices[d.key()] |
| 118 | return ok |
| 119 | } |