forked from prometheus-community/smartctl_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartctl.go
88 lines (76 loc) · 2.05 KB
/
smartctl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/tidwall/gjson"
)
// SMARTDevice - short info about device
type SMARTDevice struct {
device string
proto string
serial string
family string
model string
}
// SMARTctl object
type SMARTctl struct {
ch chan<- prometheus.Metric
json gjson.Result
device SMARTDevice
}
// NewSMARTctl is smartctl constructor
func NewSMARTctl(json gjson.Result, ch chan<- prometheus.Metric) SMARTctl {
smart := SMARTctl{}
smart.ch = ch
smart.json = json
smart.device = SMARTDevice{
device: strings.TrimSpace(smart.json.Get("device.name").String()),
proto: strings.TrimSpace(smart.json.Get("device.protocol").String()),
serial: strings.TrimSpace(smart.json.Get("serial_number").String()),
family: strings.TrimSpace(smart.json.Get("model_family").String()),
model: strings.TrimSpace(smart.json.Get("model_name").String()),
}
return smart
}
// Collect metrics
func (smart *SMARTctl) Collect() {
logger.Verbose("Collecting metrics from %s: %s, %s", smart.device.device, smart.device.family, smart.device.model)
smart.mineExitStatus()
smart.mineDevice()
smart.mineCapacity()
smart.mineSmartStatus()
smart.minePowerCycleCount()
smart.minePowerOnSeconds()
smart.mineTemperatures()
smart.minePercentageUsed()
smart.mineBytesRead()
smart.mineCommandsRead()
smart.mineBytesWritten()
smart.mineCommandsWritten()
if smart.isNVMe() {
smart.mineNVMeHealthInformationLog()
} else {
smart.mineRotationRate()
smart.mineInterfaceSpeed()
smart.mineDeviceAttribute()
smart.mineDeviceSCTStatus()
smart.mineDeviceStatistics()
smart.mineDeviceErrorLog()
smart.mineDeviceSelfTestLog()
smart.mineDeviceERC()
}
}
func (smart *SMARTctl) mineExitStatus() {
smart.ch <- prometheus.MustNewConstMetric(
metricDeviceExitStatus,
prometheus.GaugeValue,
smart.json.Get("smartctl.exit_status").Float(),
smart.device.device,
smart.device.family,
smart.device.model,
smart.device.serial,
)
}
func (smart *SMARTctl) isNVMe() bool {
return smart.device.proto == "NVMe"
}