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 ( 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,18 +37,18 @@ type Item struct {
} `json:"raw"` } `json:"raw"`
} }
func checkSmartAttributes(diskpath string, isSeagate bool, isHdd bool) {
rawsmart := execSystem("smartctl", "-A", "-json", diskpath)
if rawsmart == nil {
fmt.Println("error while getting smart info")
}
type RawAttr struct { 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
err := json.Unmarshal(rawsmart, &rawattr) err := json.Unmarshal(rawsmart, &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