Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ type SMARTctlManagerCollector struct {

// Describe sends the super-set of all possible descriptors of metrics
func (i *SMARTctlManagerCollector) Describe(ch chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(i, ch)
for _, desc := range allMetricDescs {
ch <- desc
}
}

// Collect is called by the Prometheus registry when collecting metrics.
Expand Down
49 changes: 49 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ var (
},
nil,
)
metricDevicePowerMode = prometheus.NewDesc(
"smartctl_device_power_mode",
"Device power mode from ATA CHECK POWER MODE command (ata_value: -1=sleep, 0x00=0=standby, 0x01=1=standby_y, 0x40=64=active_nv_down, 0x41=65=active_nv_up, 0x80=128=idle, 0x81=129=idle_a, 0x82=130=idle_b, 0x83=131=idle_c, 0xff=255=active_or_idle). Source: https://github.com/smartmontools/smartmontools/blob/RELEASE_7_5/smartmontools/ataprint.cpp#L3401-L3431",
[]string{
"device",
},
nil,
)
metricDeviceStatistics = prometheus.NewDesc(
"smartctl_device_statistics",
"Device statistics",
Expand Down Expand Up @@ -354,4 +362,45 @@ var (
},
nil,
)
allMetricDescs = []*prometheus.Desc{
metricSmartctlVersion,
metricDeviceModel,
metricDeviceCount,
metricDeviceCapacityBlocks,
metricDeviceCapacityBytes,
metricDeviceTotalCapacityBytes,
metricDeviceBlockSize,
metricDeviceInterfaceSpeed,
metricDeviceAttribute,
metricDevicePowerOnSeconds,
metricDeviceRotationRate,
metricDeviceTemperature,
metricDevicePowerCycleCount,
metricDevicePercentageUsed,
metricDeviceAvailableSpare,
metricDeviceAvailableSpareThreshold,
metricDeviceCriticalWarning,
metricDeviceMediaErrors,
metricDeviceNumErrLogEntries,
metricDeviceBytesRead,
metricDeviceBytesWritten,
metricDeviceSmartStatus,
metricDeviceExitStatus,
metricDeviceState,
metricDevicePowerMode,
metricDeviceStatistics,
metricDeviceErrorLogCount,
metricDeviceSelfTestLogCount,
metricDeviceSelfTestLogErrorCount,
metricDeviceERCSeconds,
metricSCSIGrownDefectList,
metricReadErrorsCorrectedByRereadsRewrites,
metricReadErrorsCorrectedByEccFast,
metricReadErrorsCorrectedByEccDelayed,
metricReadTotalUncorrectedErrors,
metricWriteErrorsCorrectedByRereadsRewrites,
metricWriteErrorsCorrectedByEccFast,
metricWriteErrorsCorrectedByEccDelayed,
metricWriteTotalUncorrectedErrors,
}
)
13 changes: 9 additions & 4 deletions readjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ func readSMARTctl(logger *slog.Logger, device Device, wg *sync.WaitGroup) {
// Accommodate a smartmontools pre-7.3 bug
cleaned_out := strings.TrimPrefix(string(out), " Pending defect count:")
json := parseJSON(cleaned_out)
rcOk := resultCodeIsOk(logger, device, json.Get("smartctl.exit_status").Int())
exitStatus := json.Get("smartctl.exit_status").Int()
rcOk := resultCodeIsOk(logger, device, exitStatus, json)
jsonOk := jsonIsOk(logger, json)
logger.Debug("Collected S.M.A.R.T. json data", "device", device, "duration", time.Since(start))
if rcOk && jsonOk {
Expand Down Expand Up @@ -134,7 +135,7 @@ func readData(logger *slog.Logger, device Device) gjson.Result {
}

// Parse smartctl return code
func resultCodeIsOk(logger *slog.Logger, device Device, SMARTCtlResult int64) bool {
func resultCodeIsOk(logger *slog.Logger, device Device, SMARTCtlResult int64, json gjson.Result) bool {
result := true
if SMARTCtlResult > 0 {
b := SMARTCtlResult
Expand All @@ -143,8 +144,12 @@ func resultCodeIsOk(logger *slog.Logger, device Device, SMARTCtlResult int64) bo
result = false
}
if (b & (1 << 1)) != 0 {
logger.Error("Device open failed, device did not return an IDENTIFY DEVICE structure, or device is in a low-power mode", "device", device)
result = false
if json.Get("power_mode").Exists() {
logger.Info("Device in low-power mode", "device", device)
} else {
logger.Error("Device open failed, device did not return an IDENTIFY DEVICE structure, or device is in a low-power mode", "device", device)
result = false
}
}
if (b & (1 << 2)) != 0 {
logger.Warn("Some SMART or other ATA command to the disk failed, or there was a checksum error in a SMART data structure", "device", device)
Expand Down
66 changes: 66 additions & 0 deletions readjson_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2024 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"io"
"log/slog"
"os"
"testing"

"github.com/tidwall/gjson"
)

func TestResultCodeIsOkStandbyJSON(t *testing.T) {
// output from a standby hard drive:
// sudo hdparm -y /dev/sdc
// sudo smartctl --nocheck=standby /dev/sdc --json --info --health --attributes --tolerance=verypermissive --format=brief --log=error
json := readTestJSON(t, "testdata/standby-sdc.json")
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
device := Device{
Name: "/dev/sdc",
Type: "sat",
Label: "sdc",
}

if !resultCodeIsOk(logger, device, json.Get("smartctl.exit_status").Int(), json) {
t.Fatalf("expected exit status to be ok for standby json")
}
}

func TestResultCodeIsOkNonexistentDeviceJSON(t *testing.T) {
// output from a nonexistent disk:
// sudo smartctl --nocheck=standby /dev/nonexistent --json --info --health --attributes --tolerance=verypermissive --format=brief --log=error
json := readTestJSON(t, "testdata/nonexistent-device.json")
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
device := Device{
Name: "/dev/nonexistent",
Type: "auto",
Label: "nonexistent",
}

if resultCodeIsOk(logger, device, json.Get("smartctl.exit_status").Int(), json) {
t.Fatalf("expected exit status to be not ok for nonexistent device json")
}
}

func readTestJSON(t *testing.T, path string) gjson.Result {
t.Helper()

data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read json: %v", err)
}
return gjson.ParseBytes(data)
}
105 changes: 86 additions & 19 deletions smartctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (smart *SMARTctl) Collect() {
smart.logger.Debug("Collecting metrics from", "device", smart.device.device, "family", smart.device.family, "model", smart.device.model)
smart.mineExitStatus()
smart.mineDevice()
smart.minePowerMode()
smart.mineCapacity()
smart.mineBlockSize()
smart.mineInterfaceSpeed()
Expand Down Expand Up @@ -131,6 +132,23 @@ func (smart *SMARTctl) mineExitStatus() {
}

func (smart *SMARTctl) mineDevice() {
hasInfo := false
for _, key := range []string{
"model_name",
"scsi_vendor",
"scsi_product",
"serial_number",
"firmware_version",
"model_family",
} {
if smart.json.Get(key).Exists() {
hasInfo = true
break
}
}
if !hasInfo {
return
}
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceModel,
prometheus.GaugeValue,
Expand All @@ -154,22 +172,43 @@ func (smart *SMARTctl) mineDevice() {
)
}

func (smart *SMARTctl) minePowerMode() {
powerMode := smart.json.Get("power_mode")
if powerMode.Exists() {
smart.ch <- prometheus.MustNewConstMetric(
metricDevicePowerMode,
prometheus.GaugeValue,
powerMode.Get("ata_value").Float(),
smart.device.device,
)
}
}

func (smart *SMARTctl) mineCapacity() {
// The user_capacity exists only when NVMe have single namespace. Otherwise,
// for NVMe devices with multiple namespaces, when device name used without
// namespace number (exporter case) user_capacity will be absent
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceCapacityBlocks,
prometheus.GaugeValue,
smart.json.Get("user_capacity.blocks").Float(),
smart.device.device,
)
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceCapacityBytes,
prometheus.GaugeValue,
smart.json.Get("user_capacity.bytes").Float(),
smart.device.device,
)
userCapacity := smart.json.Get("user_capacity")
if userCapacity.Exists() {
blocks := userCapacity.Get("blocks")
if blocks.Exists() {
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceCapacityBlocks,
prometheus.GaugeValue,
blocks.Float(),
smart.device.device,
)
}
bytes := userCapacity.Get("bytes")
if bytes.Exists() {
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceCapacityBytes,
prometheus.GaugeValue,
bytes.Float(),
smart.device.device,
)
}
}
nvme_total_capacity := smart.json.Get("nvme_total_capacity")
if nvme_total_capacity.Exists() {
smart.ch <- prometheus.MustNewConstMetric(
Expand All @@ -183,10 +222,14 @@ func (smart *SMARTctl) mineCapacity() {

func (smart *SMARTctl) mineBlockSize() {
for _, blockType := range []string{"logical", "physical"} {
blockSize := smart.json.Get(fmt.Sprintf("%s_block_size", blockType))
if !blockSize.Exists() {
continue
}
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceBlockSize,
prometheus.GaugeValue,
smart.json.Get(fmt.Sprintf("%s_block_size", blockType)).Float(),
blockSize.Float(),
smart.device.device,
blockType,
)
Expand Down Expand Up @@ -329,55 +372,79 @@ func (smart *SMARTctl) mineDeviceSCTStatus() {
}

func (smart *SMARTctl) mineNvmePercentageUsed() {
percentageUsed := smart.json.Get("nvme_smart_health_information_log.percentage_used")
if !percentageUsed.Exists() {
return
}
smart.ch <- prometheus.MustNewConstMetric(
metricDevicePercentageUsed,
prometheus.CounterValue,
smart.json.Get("nvme_smart_health_information_log.percentage_used").Float(),
percentageUsed.Float(),
smart.device.device,
)
}

func (smart *SMARTctl) mineNvmeAvailableSpare() {
availableSpare := smart.json.Get("nvme_smart_health_information_log.available_spare")
if !availableSpare.Exists() {
return
}
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceAvailableSpare,
prometheus.CounterValue,
smart.json.Get("nvme_smart_health_information_log.available_spare").Float(),
availableSpare.Float(),
smart.device.device,
)
}

func (smart *SMARTctl) mineNvmeAvailableSpareThreshold() {
availableSpareThreshold := smart.json.Get("nvme_smart_health_information_log.available_spare_threshold")
if !availableSpareThreshold.Exists() {
return
}
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceAvailableSpareThreshold,
prometheus.CounterValue,
smart.json.Get("nvme_smart_health_information_log.available_spare_threshold").Float(),
availableSpareThreshold.Float(),
smart.device.device,
)
}

func (smart *SMARTctl) mineNvmeCriticalWarning() {
criticalWarning := smart.json.Get("nvme_smart_health_information_log.critical_warning")
if !criticalWarning.Exists() {
return
}
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceCriticalWarning,
prometheus.CounterValue,
smart.json.Get("nvme_smart_health_information_log.critical_warning").Float(),
criticalWarning.Float(),
smart.device.device,
)
}

func (smart *SMARTctl) mineNvmeMediaErrors() {
mediaErrors := smart.json.Get("nvme_smart_health_information_log.media_errors")
if !mediaErrors.Exists() {
return
}
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceMediaErrors,
prometheus.CounterValue,
smart.json.Get("nvme_smart_health_information_log.media_errors").Float(),
mediaErrors.Float(),
smart.device.device,
)
}

func (smart *SMARTctl) mineNvmeNumErrLogEntries() {
numErrLogEntries := smart.json.Get("nvme_smart_health_information_log.num_err_log_entries")
if !numErrLogEntries.Exists() {
return
}
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceNumErrLogEntries,
prometheus.CounterValue,
smart.json.Get("nvme_smart_health_information_log.num_err_log_entries").Float(),
numErrLogEntries.Float(),
smart.device.device,
)
}
Expand Down
Loading