-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.go
87 lines (78 loc) · 1.95 KB
/
metrics.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
package main
import (
"regexp"
"sync"
"github.com/prometheus/client_golang/prometheus"
)
// SMARTMetric wraps particular SMART metric and provides Prometheus integration
type SMARTMetric struct {
// Regexp holds regular expression used to parse metric value from smartctl
// output.
Regexp *regexp.Regexp
// Desc provides Prometheus metric descriptor.
Desc *prometheus.Desc
// Vals contains metric values per label (that is device). Vals are updated
// periodiclly in a separate goroutine.
Vals map[string]float64
// This lock guards access to Vals map
sync.RWMutex
}
func (m *SMARTMetric) Collect(ch chan<- prometheus.Metric) {
// Report metric with read lock because they're updated in a separate
// goroutine
m.RLock()
for label, val := range m.Vals {
ch <- prometheus.MustNewConstMetric(
m.Desc,
prometheus.GaugeValue,
val,
label,
)
}
m.RUnlock()
}
func (m *SMARTMetric) Describe(ch chan<- *prometheus.Desc) {
ch <- m.Desc
}
var metrics = []*SMARTMetric{
&SMARTMetric{
Regexp: regexp.MustCompile(`Reallocated_Sector_Ct`),
Desc: prometheus.NewDesc(
"smart_reallocated_sectors_total",
"Number of reallocated sectors",
[]string{"device"},
nil,
),
Vals: make(map[string]float64),
},
&SMARTMetric{
Regexp: regexp.MustCompile(`^Elements in grown defect list:`),
Desc: prometheus.NewDesc(
"smart_grown_defect_list_total",
"Number of elements in grown defect list",
[]string{"device"},
nil,
),
Vals: make(map[string]float64),
},
&SMARTMetric{
Regexp: regexp.MustCompile(`^read:`),
Desc: prometheus.NewDesc(
"smart_read_uncorrected_errors_total",
"Number of uncorrected read errors",
[]string{"device"},
nil,
),
Vals: make(map[string]float64),
},
&SMARTMetric{
Regexp: regexp.MustCompile(`^write:`),
Desc: prometheus.NewDesc(
"smart_write_uncorrected_errors_total",
"Number of uncorrected write errors",
[]string{"device"},
nil,
),
Vals: make(map[string]float64),
},
}