Skip to content

Commit

Permalink
Merge pull request #154 from k0ste/help2
Browse files Browse the repository at this point in the history
Small fixes & added NVMe total capacity metric
  • Loading branch information
NiceGuyIT committed Aug 26, 2023
2 parents 8ab045d + 1ab518e commit f3588bb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 53 deletions.
23 changes: 8 additions & 15 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ var (
},
nil,
)
metricDeviceTotalCapacityBytes = prometheus.NewDesc(
"smartctl_device_nvme_capacity_bytes",
"NVMe device total capacity bytes",
[]string{
"device",
},
nil,
)
metricDeviceBlockSize = prometheus.NewDesc(
"smartctl_device_block_size",
"Device block size",
Expand Down Expand Up @@ -274,9 +282,6 @@ var (
"Device SCSI grown defect list counter",
[]string{
"device",
"model_family",
"model_name",
"serial_number",
},
nil,
)
Expand All @@ -285,9 +290,6 @@ var (
"Read Errors Corrected by ReReads/ReWrites",
[]string{
"device",
"model_family",
"model_name",
"serial_number",
},
nil,
)
Expand All @@ -296,9 +298,6 @@ var (
"Read Total Uncorrected Errors",
[]string{
"device",
"model_family",
"model_name",
"serial_number",
},
nil,
)
Expand All @@ -307,9 +306,6 @@ var (
"Write Errors Corrected by ReReads/ReWrites",
[]string{
"device",
"model_family",
"model_name",
"serial_number",
},
nil,
)
Expand All @@ -318,9 +314,6 @@ var (
"Write Total Uncorrected Errors",
[]string{
"device",
"model_family",
"model_name",
"serial_number",
},
nil,
)
Expand Down
70 changes: 32 additions & 38 deletions smartctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewSMARTctl(logger log.Logger, json gjson.Result, ch chan<- prometheus.Metr
device: SMARTDevice{
device: strings.TrimPrefix(strings.TrimSpace(json.Get("device.name").String()), "/dev/"),
serial: strings.TrimSpace(json.Get("serial_number").String()),
family: strings.TrimSpace(json.Get("model_family").String()),
family: strings.TrimSpace(GetStringIfExists(json, "model_family", "unknown")),
model: strings.TrimSpace(json.Get("model_name").String()),
},
}
Expand All @@ -60,6 +60,7 @@ func (smart *SMARTctl) Collect() {
smart.mineExitStatus()
smart.mineDevice()
smart.mineCapacity()
smart.mineBlockSize()
smart.mineInterfaceSpeed()
smart.mineDeviceAttribute()
smart.minePowerOnSeconds()
Expand All @@ -71,14 +72,14 @@ func (smart *SMARTctl) Collect() {
smart.mineDeviceErrorLog()
smart.mineDeviceSelfTestLog()
smart.mineDeviceERC()
smart.minePercentageUsed()
smart.mineAvailableSpare()
smart.mineAvailableSpareThreshold()
smart.mineCriticalWarning()
smart.mineMediaErrors()
smart.mineNumErrLogEntries()
smart.mineBytesRead()
smart.mineBytesWritten()
smart.mineNvmePercentageUsed()
smart.mineNvmeAvailableSpare()
smart.mineNvmeAvailableSpareThreshold()
smart.mineNvmeCriticalWarning()
smart.mineNvmeMediaErrors()
smart.mineNvmeNumErrLogEntries()
smart.mineNvmeBytesRead()
smart.mineNvmeBytesWritten()
smart.mineSmartStatus()
smart.mineSCSIGrownDefectList()
smart.mineSCSIErrorCounterLog()
Expand Down Expand Up @@ -114,19 +115,30 @@ func (smart *SMARTctl) mineDevice() {
}

func (smart *SMARTctl) mineCapacity() {
capacity := smart.json.Get("user_capacity")
// 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,
capacity.Get("blocks").Float(),
smart.json.Get("user_capacity.blocks").Float(),
smart.device.device,
)
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceCapacityBytes,
prometheus.GaugeValue,
capacity.Get("bytes").Float(),
smart.json.Get("user_capacity.bytes").Float(),
smart.device.device,
)
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceTotalCapacityBytes,
prometheus.GaugeValue,
smart.json.Get("nvme_total_capacity").Float(),
smart.device.device,
)
}

func (smart *SMARTctl) mineBlockSize() {
for _, blockType := range []string{"logical", "physical"} {
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceBlockSize,
Expand Down Expand Up @@ -245,7 +257,7 @@ func (smart *SMARTctl) mineDeviceSCTStatus() {
}
}

func (smart *SMARTctl) minePercentageUsed() {
func (smart *SMARTctl) mineNvmePercentageUsed() {
smart.ch <- prometheus.MustNewConstMetric(
metricDevicePercentageUsed,
prometheus.CounterValue,
Expand All @@ -254,7 +266,7 @@ func (smart *SMARTctl) minePercentageUsed() {
)
}

func (smart *SMARTctl) mineAvailableSpare() {
func (smart *SMARTctl) mineNvmeAvailableSpare() {
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceAvailableSpare,
prometheus.CounterValue,
Expand All @@ -263,7 +275,7 @@ func (smart *SMARTctl) mineAvailableSpare() {
)
}

func (smart *SMARTctl) mineAvailableSpareThreshold() {
func (smart *SMARTctl) mineNvmeAvailableSpareThreshold() {
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceAvailableSpareThreshold,
prometheus.CounterValue,
Expand All @@ -272,7 +284,7 @@ func (smart *SMARTctl) mineAvailableSpareThreshold() {
)
}

func (smart *SMARTctl) mineCriticalWarning() {
func (smart *SMARTctl) mineNvmeCriticalWarning() {
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceCriticalWarning,
prometheus.CounterValue,
Expand All @@ -281,7 +293,7 @@ func (smart *SMARTctl) mineCriticalWarning() {
)
}

func (smart *SMARTctl) mineMediaErrors() {
func (smart *SMARTctl) mineNvmeMediaErrors() {
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceMediaErrors,
prometheus.CounterValue,
Expand All @@ -290,7 +302,7 @@ func (smart *SMARTctl) mineMediaErrors() {
)
}

func (smart *SMARTctl) mineNumErrLogEntries() {
func (smart *SMARTctl) mineNvmeNumErrLogEntries() {
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceNumErrLogEntries,
prometheus.CounterValue,
Expand All @@ -299,7 +311,7 @@ func (smart *SMARTctl) mineNumErrLogEntries() {
)
}

func (smart *SMARTctl) mineBytesRead() {
func (smart *SMARTctl) mineNvmeBytesRead() {
blockSize := smart.json.Get("logical_block_size").Float()
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceBytesRead,
Expand All @@ -311,7 +323,7 @@ func (smart *SMARTctl) mineBytesRead() {
)
}

func (smart *SMARTctl) mineBytesWritten() {
func (smart *SMARTctl) mineNvmeBytesWritten() {
blockSize := smart.json.Get("logical_block_size").Float()
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceBytesWritten,
Expand Down Expand Up @@ -346,9 +358,6 @@ func (smart *SMARTctl) mineDeviceStatistics() {
prometheus.GaugeValue,
statistic.Get("value").Float(),
smart.device.device,
smart.device.family,
smart.device.model,
smart.device.serial,
table,
strings.TrimSpace(statistic.Get("name").String()),
strings.TrimSpace(statistic.Get("flags.string").String()),
Expand Down Expand Up @@ -438,9 +447,6 @@ func (smart *SMARTctl) mineSCSIGrownDefectList() {
prometheus.GaugeValue,
scsi_grown_defect_list.Float(),
smart.device.device,
smart.device.family,
smart.device.model,
smart.device.serial,
)
}
}
Expand All @@ -453,36 +459,24 @@ func (smart *SMARTctl) mineSCSIErrorCounterLog() {
prometheus.GaugeValue,
SCSIHealth.Get("read.errors_corrected_by_rereads_rewrites").Float(),
smart.device.device,
smart.device.family,
smart.device.model,
smart.device.serial,
)
smart.ch <- prometheus.MustNewConstMetric(
metricReadTotalUncorrectedErrors,
prometheus.GaugeValue,
SCSIHealth.Get("read.total_uncorrected_errors").Float(),
smart.device.device,
smart.device.family,
smart.device.model,
smart.device.serial,
)
smart.ch <- prometheus.MustNewConstMetric(
metricWriteErrorsCorrectedByRereadsRewrites,
prometheus.GaugeValue,
SCSIHealth.Get("write.errors_corrected_by_rereads_rewrites").Float(),
smart.device.device,
smart.device.family,
smart.device.model,
smart.device.serial,
)
smart.ch <- prometheus.MustNewConstMetric(
metricWriteTotalUncorrectedErrors,
prometheus.GaugeValue,
SCSIHealth.Get("write.total_uncorrected_errors").Float(),
smart.device.device,
smart.device.family,
smart.device.model,
smart.device.serial,
)
}
}

0 comments on commit f3588bb

Please sign in to comment.