Skip to content

Commit

Permalink
extend self-test log processing
Browse files Browse the repository at this point in the history
  • Loading branch information
aritas1 committed Aug 20, 2023
1 parent 75c76b3 commit 528396b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
11 changes: 11 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,17 @@ var (
},
nil,
)
metricDeviceSelfTest = prometheus.NewDesc(
"smartctl_device_self_test_log_seconds",
"Device SMART self test log execution lifetime seconds",
[]string{
"device",
"self_test_log_type",
"self_test_passed",
},
nil,
)

metricDeviceSelfTestLogCount = prometheus.NewDesc(
"smartctl_device_self_test_log_count",
"Device SMART self test log count",
Expand Down
2 changes: 1 addition & 1 deletion readjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func readFakeSMARTctl(logger log.Logger, device string) gjson.Result {
// Get json from smartctl and parse it
func readSMARTctl(logger log.Logger, device string) (gjson.Result, bool) {
level.Debug(logger).Log("msg", "Collecting S.M.A.R.T. counters", "device", device)
out, err := exec.Command(*smartctlPath, "--json", "--info", "--health", "--attributes", "--tolerance=verypermissive", "--nocheck=standby", "--format=brief", "--log=error", device).Output()
out, err := exec.Command(*smartctlPath, "--json", "--info", "--health", "--attributes", "--tolerance=verypermissive", "--nocheck=standby", "--format=brief", "--log=error", "--log=selftest", device).Output()
if err != nil {
level.Warn(logger).Log("msg", "S.M.A.R.T. output reading", "err", err, "device", device)
}
Expand Down
46 changes: 46 additions & 0 deletions smartctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package main

import (
"fmt"
"strconv"
"strings"

"github.com/go-kit/log"
Expand Down Expand Up @@ -69,6 +70,7 @@ func (smart *SMARTctl) Collect() {
smart.mineDeviceSCTStatus()
smart.mineDeviceStatistics()
smart.mineDeviceErrorLog()
smart.mineDeviceSelfTest()
smart.mineDeviceSelfTestLog()
smart.mineDeviceERC()
smart.minePercentageUsed()
Expand Down Expand Up @@ -399,6 +401,50 @@ func (smart *SMARTctl) mineDeviceErrorLog() {
}
}

func (smart *SMARTctl) mineDeviceSelfTest() {
validTypes := map[int]string{
255: "vendor",
129: "short_captive",
2: "long",
1: "short",
}

// assume the table will always be in descending order
processedTypes := make(map[string]bool)

for _, logEntry := range smart.json.Get("ata_smart_self_test_log.standard.table").Array() {
testType := int(logEntry.Get("type.value").Int())
testTime := float64(logEntry.Get("lifetime_hours").Int())
testRunningIndicator := int(logEntry.Get("status.value").Int())
testStatus := strconv.FormatBool(logEntry.Get("status.passed").Bool())

// stick with seconds
testTime = testTime * 60 * 60

// skip running tests
if testRunningIndicator != 0 {
continue
}

logTestType, exists := validTypes[testType]
if !exists {
logTestType = "unknown"
}

if !processedTypes[logTestType] {
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceSelfTest,
prometheus.GaugeValue,
testTime,
smart.device.device,
logTestType,
testStatus,
)
processedTypes[logTestType] = true
}
}
}

func (smart *SMARTctl) mineDeviceSelfTestLog() {
for logType, status := range smart.json.Get("ata_smart_self_test_log").Map() {
smart.ch <- prometheus.MustNewConstMetric(
Expand Down

0 comments on commit 528396b

Please sign in to comment.