Skip to content

Commit

Permalink
fix: allow configuring powermode check to work better with SSDs
Browse files Browse the repository at this point in the history
fixes #91

Signed-off-by: Robin Björklin <[email protected]>
  • Loading branch information
rbjorklin committed Feb 13, 2025
1 parent aec40bf commit 9bf4ec8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package main

import (
"fmt"
"log/slog"
"net/http"
"os"
Expand Down Expand Up @@ -120,6 +121,9 @@ var (
smartctlFakeData = kingpin.Flag("smartctl.fake-data",
"The device to monitor (repeatable)",
).Default("false").Hidden().Bool()
smartctlPowerModeCheck = kingpin.Flag("smartctl.powermode-check",
"Whether or not to check powermode before fetching data",
).Default("standby").String()
)

// scanDevices uses smartctl to gather the list of available devices.
Expand Down Expand Up @@ -171,6 +175,15 @@ func buildDevicesFromFlag(devices []Device) []Device {
return devices
}

func validatePowerMode(mode string) error {
switch strings.ToLower(mode) {
case "never", "sleep", "standby", "idle":
return nil
default:
return fmt.Errorf("invalid power mode: %s. Must be one of: never, sleep, standby, idle", mode)
}
}

func main() {
metricsPath := kingpin.Flag(
"web.telemetry-path", "Path under which to expose metrics",
Expand All @@ -184,9 +197,12 @@ func main() {
kingpin.Parse()
logger := promslog.New(promslogConfig)

if err := validatePowerMode(*smartctlPowerModeCheck); err != nil {
logger.Error(err.Error())
os.Exit(1)
}
logger.Info("Starting smartctl_exporter", "version", version.Info())
logger.Info("Build context", "build_context", version.BuildContext())

var devices []Device

if len(*smartctlDevices) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion readjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func readFakeSMARTctl(logger *slog.Logger, device Device) gjson.Result {
// Get json from smartctl and parse it
func readSMARTctl(logger *slog.Logger, device Device) (gjson.Result, bool) {
start := time.Now()
var smartctlArgs = []string{"--json", "--info", "--health", "--attributes", "--tolerance=verypermissive", "--nocheck=standby", "--format=brief", "--log=error", "--device=" + device.Type, device.Name}
var smartctlArgs = []string{"--json", "--info", "--health", "--attributes", "--tolerance=verypermissive", "--nocheck=" + *smartctlPowerModeCheck, "--format=brief", "--log=error", "--device=" + device.Type, device.Name}

logger.Debug("Calling smartctl with args", "args", strings.Join(smartctlArgs, " "))
out, err := exec.Command(*smartctlPath, smartctlArgs...).Output()
Expand Down

0 comments on commit 9bf4ec8

Please sign in to comment.