SMART Temperature Readout

This commit is contained in:
Lukas Heiligenbrunner 2022-05-05 09:56:08 +00:00
parent 621756115e
commit 05416bf421
2 changed files with 46 additions and 7 deletions

18
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
@ -41,7 +42,24 @@ func main() {
os.Exit(-1)
}
tmpptr := flag.Bool("temp", false, "get temperatures")
flag.Parse()
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))
for _, blockdevice := range disks.Blockdevices {
path := fmt.Sprintf("/dev/%s", blockdevice.Name)

View File

@ -37,18 +37,18 @@ type Item struct {
} `json:"raw"`
}
func checkSmartAttributes(diskpath string, isSeagate bool, isHdd bool) {
type RawAttr struct {
AtaSmartAttr struct {
Table []Item `json:"table"`
} `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")
}
type RawAttr struct {
AtaSmartAttr struct {
Table []Item `json:"table"`
} `json:"ata_smart_attributes"`
}
var rawattr RawAttr
err := json.Unmarshal(rawsmart, &rawattr)
@ -56,6 +56,27 @@ func checkSmartAttributes(diskpath string, isSeagate bool, isHdd bool) {
if err != nil {
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")
// seagate hdds have a special way to represent their error rates