-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
125 lines (115 loc) · 3.38 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
"git.dolansoft.org/dolansoft/smart-exporter/drivedb"
"git.dolansoft.org/dolansoft/smart-exporter/smart"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
collectorDurationValue = prometheus.NewHistogram(prometheus.HistogramOpts{Name: "ata_smart_collector_duration_seconds", Buckets: prometheus.ExponentialBuckets(0.01, 2.0, 10)})
)
var (
sdRegex = regexp.MustCompile("^sd[a-z]+$")
)
var (
listenAddr = flag.String("listen-addr", ":9541", "Address the SMART exporter should listen on")
)
type smartCollector struct {
smartValue *prometheus.Desc
smartRawValue *prometheus.Desc
db drivedb.DriveDb
}
func newSMARTCollector() *smartCollector {
db, err := drivedb.OpenDriveDb("drivedb.yaml")
if err != nil {
panic(err)
}
return &smartCollector{
smartValue: prometheus.NewDesc("ata_smart_value", "ATA SMART normalized value", []string{"dev", "serial", "model", "family", "attr_id", "attr_name"}, nil),
smartRawValue: prometheus.NewDesc("ata_smart_raw_value", "ATA SMART raw decoded value", []string{"dev", "serial", "model", "family", "attr_id", "attr_name"}, nil),
db: db,
}
}
func (c *smartCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.smartValue
ch <- c.smartRawValue
}
func (c *smartCollector) Collect(ch chan<- prometheus.Metric) {
devs, err := ioutil.ReadDir("/dev")
if err != nil {
log.Printf("Failed to list devices: %v", err)
return
}
var diskDevs []string
for _, dev := range devs {
if sdRegex.MatchString(dev.Name()) {
diskDevs = append(diskDevs, dev.Name())
}
}
wg := sync.WaitGroup{}
for _, disk := range diskDevs {
wg.Add(1)
go func(disk string) {
startTime := time.Now()
defer func() {
duration := time.Now().Sub(startTime)
collectorDurationValue.Observe(duration.Seconds())
}()
defer wg.Done()
diskHandle, err := os.Open("/dev/" + disk)
if err != nil {
log.Printf("Failed to open device %v: %v", disk, err)
return
}
defer diskHandle.Close()
id, err := smart.Identify(diskHandle)
if err != nil {
return
}
if id.Word85&1 == 1 { // Disk has SMART
pages, err := smart.SmartReadData(diskHandle)
if err != nil {
log.Printf("Failed to get SMART data: %v", err)
return
}
model := c.db.LookupDrive(id.ModelNumber())
for _, attr := range pages.Attrs {
attrIdStr := strconv.Itoa(int(attr.Id))
if attr.Id == 0 {
continue
}
conv, ok := model.Presets[attrIdStr]
labels := []string{disk, strings.TrimSpace(string(id.SerialNumber())), strings.TrimSpace(string(id.ModelNumber())), model.Family, attrIdStr, conv.Name}
if ok {
metricValue := attr.RawValue(conv.Conv)
if metricValue >= 0.0 {
ch <- prometheus.MustNewConstMetric(c.smartRawValue, prometheus.UntypedValue, metricValue, labels...)
}
}
ch <- prometheus.MustNewConstMetric(c.smartValue, prometheus.UntypedValue, float64(attr.Value), labels...)
}
}
}(disk)
}
wg.Wait()
}
func main() {
smartC := newSMARTCollector()
prometheus.MustRegister(smartC)
prometheus.MustRegister(collectorDurationValue)
flag.Parse()
http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(*listenAddr, nil); err != nil {
log.Fatalf("failed to listen: %v", err)
}
}