-
Notifications
You must be signed in to change notification settings - Fork 0
/
dht_collector.go
58 lines (50 loc) · 1.58 KB
/
dht_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
package main
import (
"fmt"
"os"
"strconv"
dht "github.com/d2r2/go-dht"
"github.com/prometheus/client_golang/prometheus"
)
const prefix = "dht_"
var (
upDesc *prometheus.Desc
tempDesc *prometheus.Desc
humidityDesc *prometheus.Desc
)
func init() {
l := []string{"location"}
upDesc = prometheus.NewDesc(prefix+"up", "Scrape was successful", l, nil)
tempDesc = prometheus.NewDesc(prefix+"temp", "Air temperature (in degrees C)", l, nil)
humidityDesc = prometheus.NewDesc(prefix+"humidity_percent", "Humidity", l, nil)
}
type dhtCollector struct {
}
func (c dhtCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- upDesc
ch <- tempDesc
ch <- humidityDesc
}
func (c dhtCollector) Collect(ch chan<- prometheus.Metric) {
sensor := dht.DHT22
if *device == 11 {
sensor = dht.DHT11
}
for gpiostr, name := range list.Names {
gpio, err := strconv.Atoi(gpiostr)
if err != nil {
fmt.Fprintln(os.Stderr, "invalid gpiod", err)
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 0, name)
continue
}
temperature, humidity, _, err := dht.ReadDHTxxWithRetry(sensor, gpio, false, 10)
if err != nil {
fmt.Fprintln(os.Stderr, "error getting sensor data", err)
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 0, name)
continue
}
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 1, name)
ch <- prometheus.MustNewConstMetric(tempDesc, prometheus.GaugeValue, float64(int(temperature*100))/100, name)
ch <- prometheus.MustNewConstMetric(humidityDesc, prometheus.GaugeValue, float64(int(humidity*100))/100, name)
}
}