-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
73 lines (64 loc) · 1.93 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
package main
import (
"flag"
"log"
"net/http"
"github.com/morus12/dht22"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
listenAddress = flag.String("listen-address", ":9543", "The address to listen on for HTTP requests.")
metricsPath = flag.String("metrics-path", "/metrics", "The path of the metrics endpoint.")
gpioPort = flag.String("gpio-port", "4", "The GPIO port where DHT22 is connected.")
)
func main() {
flag.Parse()
if err := prometheus.Register(prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Subsystem: "dht22",
Name: "temperature_celsius",
Help: "Temperature in Celsius",
},
func() float64 {
sensor := dht22.New(*gpioPort)
temperature, err := sensor.Temperature()
if err != nil {
log.Fatalf("error reading temperature %v", err)
}
return float64(temperature)
},
)); err != nil {
log.Fatalf("error registering gaugefunc: %v", err)
}
log.Println("GaugeFunc 'temperature_celsius', registered.")
if err := prometheus.Register(prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Subsystem: "dht22",
Name: "humidity_percent",
Help: "Humidity in percent",
},
func() float64 {
sensor := dht22.New(*gpioPort)
humidity, err := sensor.Humidity()
if err != nil {
log.Fatalf("error reading humidity %v", err)
}
return float64(humidity)
},
)); err != nil {
log.Fatalf("error registering gaugefunc: %v", err)
}
log.Println("GaugeFunc 'temperature_celsius', registered.")
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>DHT22 Exporter</title></head>
<body>
<h1>DHT22 Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}