-
Notifications
You must be signed in to change notification settings - Fork 2
/
onewire_collector.go
134 lines (122 loc) · 2.88 KB
/
onewire_collector.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
126
127
128
129
130
131
132
133
134
package main
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
const prefix = "onewire_"
var (
upDesc *prometheus.Desc
tempDesc *prometheus.Desc
)
func init() {
upDesc = prometheus.NewDesc(prefix+"up", "Scrape was successful", nil, nil)
tempDesc = prometheus.NewDesc(prefix+"temp", "Air temperature (in degrees C)", []string{"id", "name"}, nil)
}
type Temp struct {
ID string
Value float64
}
type onewireCollector struct {
}
func getTemperatureFromDevice(device os.FileInfo) Temp {
reg, err := regexp.Compile("[^0-9]+")
if err != nil {
log.Fatal(err)
}
for i := 1; i <= 5; i++ {
content, err := ioutil.ReadFile("/sys/bus/w1/devices/" + device.Name() + "/w1_slave")
if err != nil {
log.Infof("Error reading device %s\n", device.Name())
continue
}
lines := strings.Split(string(content), "\n")
if len(lines) != 3 {
log.Infof("Unknown format for device %s\n", device.Name())
continue
}
if !strings.Contains(lines[0], "YES") {
log.Infof("CRC invalid for device %s\n", device.Name())
continue
}
data := strings.SplitAfter(lines[1], "t=")
if len(data) != 2 {
log.Infof("Temp value not found for device %s\n", device.Name())
continue
}
strValue := reg.ReplaceAllString(data[1], "")
tempInt, err := strconv.ParseFloat(strValue, 64)
if err != nil {
continue
}
if tempInt == 85000 {
continue
}
return Temp{
ID: device.Name(),
Value: tempInt / 1000.0,
}
}
return Temp{}
}
func getTemperatures() ([]Temp, error) {
devices, err := ioutil.ReadDir("/sys/bus/w1/devices/")
if err != nil {
return nil, err
}
var wg sync.WaitGroup
valueChan := make(chan Temp)
for _, device := range devices {
if _, err := os.Stat("/sys/bus/w1/devices/" + device.Name() + "/w1_slave"); err != nil {
continue
}
wg.Add(1)
go func(device os.FileInfo) {
defer wg.Done()
valueChan <- getTemperatureFromDevice(device)
}(device)
}
go func() {
wg.Wait()
close(valueChan)
}()
var values []Temp
for t := range valueChan {
if t == (Temp{}) {
continue
}
values = append(values, t)
}
return values, nil
}
func (c onewireCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- upDesc
ch <- tempDesc
}
func (c onewireCollector) Collect(ch chan<- prometheus.Metric) {
values, err := getTemperatures()
if err != nil {
fmt.Fprintln(os.Stderr, "error getting sensor data", err)
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 0)
} else {
for _, sensor := range values {
n := list.Names[sensor.ID]
if n == "" {
if *ignoreUnknown == true {
log.Infof("Ingoring unknown device %s\n", sensor.ID)
continue
} else {
n = sensor.ID
}
}
l := []string{sensor.ID, n}
ch <- prometheus.MustNewConstMetric(tempDesc, prometheus.GaugeValue, float64(sensor.Value), l...)
}
}
}