Documentation
¶
Overview ¶
Package smart reads S.M.A.R.T. disk health via smartctl (smartmontools) through an injected exec.Runner.
r, _ := exec.NewRunner(exec.Direct) // smartctl needs root
c, err := smart.New(r)
if err != nil { ... }
devs, _ := c.Scan(ctx)
for _, d := range devs {
info, _ := c.Device(ctx, d.Name)
fmt.Println(info.Name, info.Healthy, info.TemperatureC)
}
smart is a single-implementation, read-only Collector (§3.8): smartctl is the one tool. It exposes an interface for shape-uniformity with the rest of the SDK. Probes escalate through the Runner (smartctl needs root to open a device).
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidDevice = errors.New("smart: invalid device path")
ErrInvalidDevice is returned when a device path is unsafe to pass to smartctl (empty, not absolute, contains "..", or flag-shaped).
Functions ¶
This section is empty.
Types ¶
type Collector ¶
type Collector interface {
// Scan lists the devices smartctl can inspect.
Scan(ctx context.Context) ([]ScanDevice, error)
// Device returns the health summary for one device path.
Device(ctx context.Context, dev string) (Device, error)
}
Collector is the S.M.A.R.T. read surface.
func New ¶
New builds a smart Collector driven by runner. A nil runner is rejected. New is pure — it does not probe for smartctl (a missing binary surfaces at call time).
Example ¶
ExampleNew scans for devices and prints each one's health.
package main
import (
"context"
"fmt"
"log"
"github.com/manchtools/power-manage-sdk/sys/exec"
"github.com/manchtools/power-manage-sdk/sys/smart"
)
func main() {
r, err := exec.NewRunner(exec.Direct) // smartctl needs root
if err != nil {
log.Fatal(err)
}
c, err := smart.New(r)
if err != nil {
log.Fatal(err)
}
devs, err := c.Scan(context.Background())
if err != nil {
log.Fatal(err)
}
for _, d := range devs {
info, err := c.Device(context.Background(), d.Name)
if err != nil {
continue
}
fmt.Printf("%s healthy=%v %d°C\n", info.Name, info.Healthy, info.TemperatureC)
}
}
Output:
type Device ¶
type Device struct {
Name string // the device passed to Device()
Model string
Serial string
Healthy bool // smart_status.passed
TemperatureC int // temperature.current (0 if unreported)
PowerOnHours int // power_on_time.hours (0 if unreported)
}
Device is the parsed health summary for one device.
type ScanDevice ¶
type ScanDevice struct {
Name string // e.g. /dev/sda
Type string // smartctl device type, e.g. "sat", "nvme"
}
ScanDevice is one entry from `smartctl --scan`.