Merge branch 'temperature_readings' into 'master'

SMART Temperature Readout

See merge request lukas/diskhealth!1
This commit is contained in:
Lukas Heiligenbrunner 2022-05-05 09:56:08 +00:00
commit 965f9326eb
2 changed files with 46 additions and 7 deletions

18
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"flag"
"fmt" "fmt"
"log" "log"
"os" "os"
@ -41,7 +42,24 @@ func main() {
os.Exit(-1) os.Exit(-1)
} }
tmpptr := flag.Bool("temp", false, "get temperatures")
flag.Parse()
disks := getDisks() disks := getDisks()
if *tmpptr {
fmt.Println("printing temperatures")
for _, dev := range disks.Blockdevices {
path := fmt.Sprintf("/dev/%s", dev.Name)
max, min, temp := getTemp(path)
fmt.Printf("%s temp: %d°C (max: %d°C, min: %d°C)\n", path, temp, max, min)
}
return
}
fmt.Printf("Running smart check on %d disks.\n", len(disks.Blockdevices)) fmt.Printf("Running smart check on %d disks.\n", len(disks.Blockdevices))
for _, blockdevice := range disks.Blockdevices { for _, blockdevice := range disks.Blockdevices {
path := fmt.Sprintf("/dev/%s", blockdevice.Name) path := fmt.Sprintf("/dev/%s", blockdevice.Name)

View File

@ -37,16 +37,16 @@ type Item struct {
} `json:"raw"` } `json:"raw"`
} }
func checkSmartAttributes(diskpath string, isSeagate bool, isHdd bool) { type RawAttr struct {
rawsmart := execSystem("smartctl", "-A", "-json", diskpath)
if rawsmart == nil {
fmt.Println("error while getting smart info")
}
type RawAttr struct {
AtaSmartAttr struct { AtaSmartAttr struct {
Table []Item `json:"table"` Table []Item `json:"table"`
} `json:"ata_smart_attributes"` } `json:"ata_smart_attributes"`
}
func getSmartValues(diskpath string) *RawAttr {
rawsmart := execSystem("smartctl", "-A", "-json", diskpath)
if rawsmart == nil {
fmt.Println("error while getting smart info")
} }
var rawattr RawAttr var rawattr RawAttr
@ -56,6 +56,27 @@ func checkSmartAttributes(diskpath string, isSeagate bool, isHdd bool) {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
return &rawattr
}
func getTemp(diskpath string) (uint32, uint32, uint32) {
rawattr := getSmartValues(diskpath)
temp := getItemValue(rawattr.AtaSmartAttr.Table, "Temperature_Celsius")
// temp value is storead as following:
// 4bytes 4bytes 4bytes
// [----------------|----------------|---------------]
// max temp min temp curr temp
max := temp >> 32
min := (temp & 0xff00ff) >> 16
currtemp := temp & 0x0000ff
return uint32(max), uint32(min), uint32(currtemp)
}
func checkSmartAttributes(diskpath string, isSeagate bool, isHdd bool) {
rawattr := getSmartValues(diskpath)
rrerrrate := getItemValue(rawattr.AtaSmartAttr.Table, "Raw_Read_Error_Rate") rrerrrate := getItemValue(rawattr.AtaSmartAttr.Table, "Raw_Read_Error_Rate")
// seagate hdds have a special way to represent their error rates // seagate hdds have a special way to represent their error rates