2021-08-18 21:35:22 -04:00
|
|
|
//go:build linux
|
2020-10-04 13:18:46 -04:00
|
|
|
// +build linux
|
|
|
|
|
2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2020-10-04 13:18:46 -04:00
|
|
|
|
|
|
|
package smart
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/dswarbrick/smart/drivedb"
|
|
|
|
"github.com/dswarbrick/smart/ioctl"
|
|
|
|
"github.com/dswarbrick/smart/scsi"
|
|
|
|
"github.com/dswarbrick/smart/utils"
|
2021-05-06 11:52:02 -04:00
|
|
|
"github.com/minio/madmin-go"
|
2020-10-04 13:18:46 -04:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetInfo - gets info about device
|
2021-05-06 11:52:02 -04:00
|
|
|
func GetInfo(device string) (madmin.SmartInfo, error) {
|
|
|
|
info := madmin.SmartInfo{
|
2020-10-04 13:18:46 -04:00
|
|
|
Device: device,
|
|
|
|
}
|
|
|
|
|
|
|
|
var db drivedb.DriveDb
|
2021-11-16 12:28:29 -05:00
|
|
|
dec := yaml.NewDecoder(bytes.NewReader(MustAsset("drivedb.yaml")))
|
2020-10-04 13:18:46 -04:00
|
|
|
|
|
|
|
err := dec.Decode(&db)
|
|
|
|
if err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, d := range db.Drives {
|
|
|
|
db.Drives[i].CompiledRegexp, _ = regexp.Compile(d.ModelRegex)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(device, "/dev/nvme") {
|
|
|
|
d := NewNVMeDevice(device)
|
|
|
|
if err := d.Open(); err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
nvmeInfo, err := getNvmeInfo(d)
|
|
|
|
if err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
info.Nvme = nvmeInfo
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := scsi.OpenSCSIAutodetect(device)
|
|
|
|
if err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch dev := d.(type) {
|
|
|
|
case *scsi.SCSIDevice:
|
|
|
|
scsiInfo, err := getScsiInfo(dev)
|
|
|
|
if err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
info.Scsi = scsiInfo
|
|
|
|
case *scsi.SATDevice:
|
|
|
|
ataInfo, err := getAtaInfo(dev)
|
|
|
|
if err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
info.Ata = ataInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
2021-05-06 11:52:02 -04:00
|
|
|
func getNvmeInfo(d *NVMeDevice) (*madmin.SmartNvmeInfo, error) {
|
2020-10-04 13:18:46 -04:00
|
|
|
buf := make([]byte, 4096)
|
2021-05-06 11:52:02 -04:00
|
|
|
nvmeInfo := &madmin.SmartNvmeInfo{}
|
2020-10-04 13:18:46 -04:00
|
|
|
|
|
|
|
cmd := nvmePassthruCommand{
|
|
|
|
opcode: NvmeAdminIdentify,
|
|
|
|
nsid: 0, // Namespace 0, since we are identifying the controller
|
|
|
|
addr: uint64(uintptr(unsafe.Pointer(&buf[0]))),
|
|
|
|
dataLen: uint32(len(buf)),
|
|
|
|
cdw10: 1, // Identify controller
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioctl.Ioctl(uintptr(d.fd), nvmeIoctlAdminCmd, uintptr(unsafe.Pointer(&cmd))); err != nil {
|
|
|
|
return nvmeInfo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var controller nvmeIdentController
|
2021-11-16 12:28:29 -05:00
|
|
|
binary.Read(bytes.NewReader(buf), utils.NativeEndian, &controller)
|
2020-10-04 13:18:46 -04:00
|
|
|
|
|
|
|
nvmeInfo.VendorID = strings.TrimSpace(fmt.Sprintf("%#04x", controller.VendorID))
|
|
|
|
nvmeInfo.ModelNum = strings.TrimSpace(fmt.Sprintf("%s", controller.ModelNumber))
|
|
|
|
nvmeInfo.SerialNum = strings.TrimSpace(fmt.Sprintf("%s", controller.SerialNumber))
|
|
|
|
nvmeInfo.FirmwareVersion = strings.TrimSpace(fmt.Sprintf("%s", controller.Firmware))
|
|
|
|
nvmeInfo.MaxDataTransferPages = 1 << controller.Mdts
|
|
|
|
|
|
|
|
buf2 := make([]byte, 512)
|
|
|
|
|
|
|
|
// Read SMART log
|
|
|
|
if err := d.readLogPage(0x02, &buf2); err != nil {
|
|
|
|
return nvmeInfo, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var sl nvmeSMARTLog
|
2021-11-16 12:28:29 -05:00
|
|
|
binary.Read(bytes.NewReader(buf2), utils.NativeEndian, &sl)
|
2020-10-04 13:18:46 -04:00
|
|
|
|
|
|
|
unitsRead := le128ToBigInt(sl.DataUnitsRead)
|
|
|
|
unitsWritten := le128ToBigInt(sl.DataUnitsWritten)
|
|
|
|
|
|
|
|
nvmeInfo.CriticalWarning = fmt.Sprintf("%x", sl.CritWarning)
|
|
|
|
nvmeInfo.Temperature = fmt.Sprintf("%d Celsius",
|
|
|
|
((uint16(sl.Temperature[1])<<8)|uint16(sl.Temperature[0]))-273) // Kelvin to degrees Celsius
|
|
|
|
nvmeInfo.SpareAvailable = fmt.Sprintf("%d%%", sl.AvailSpare)
|
|
|
|
nvmeInfo.SpareThreshold = fmt.Sprintf("%d%%", sl.SpareThresh)
|
|
|
|
nvmeInfo.DataUnitsReadBytes = unitsRead
|
|
|
|
nvmeInfo.DataUnitsWrittenBytes = unitsWritten
|
|
|
|
nvmeInfo.HostReadCommands = le128ToBigInt(sl.HostReads)
|
|
|
|
nvmeInfo.HostWriteCommands = le128ToBigInt(sl.HostWrites)
|
|
|
|
nvmeInfo.ControllerBusyTime = le128ToBigInt(sl.CtrlBusyTime)
|
|
|
|
nvmeInfo.PowerCycles = le128ToBigInt(sl.PowerCycles)
|
|
|
|
nvmeInfo.PowerOnHours = le128ToBigInt(sl.PowerOnHours)
|
|
|
|
nvmeInfo.UnsafeShutdowns = le128ToBigInt(sl.UnsafeShutdowns)
|
|
|
|
nvmeInfo.MediaAndDataIntegrityErrors = le128ToBigInt(sl.MediaErrors)
|
|
|
|
|
|
|
|
return nvmeInfo, nil
|
|
|
|
}
|
|
|
|
|
2021-05-06 11:52:02 -04:00
|
|
|
func getScsiInfo(d *scsi.SCSIDevice) (*madmin.SmartScsiInfo, error) {
|
|
|
|
return &madmin.SmartScsiInfo{}, nil
|
2020-10-04 13:18:46 -04:00
|
|
|
}
|
|
|
|
|
2021-05-06 11:52:02 -04:00
|
|
|
func getAtaInfo(d *scsi.SATDevice) (*madmin.SmartAtaInfo, error) {
|
|
|
|
return &madmin.SmartAtaInfo{}, nil
|
2020-10-04 13:18:46 -04:00
|
|
|
}
|