-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.go
60 lines (51 loc) · 1.3 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
package main
import (
"log"
"net/http"
"github.com/larsp/co2monitor/meter"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
device = kingpin.Arg("device", "CO2 Meter device, such as /dev/hidraw2").Required().String()
listenAddr = kingpin.Arg("listen-address", "The address to listen on for HTTP requests.").
Default(":8080").String()
)
var (
temperature = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "meter_temperature_celsius",
Help: "Current temperature in Celsius",
})
co2 = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "meter_co2_ppm",
Help: "Current CO2 level (ppm)",
})
)
func init() {
prometheus.MustRegister(temperature)
prometheus.MustRegister(co2)
}
func main() {
kingpin.Parse()
http.Handle("/metrics", promhttp.Handler())
go measure()
log.Printf("Serving metrics at '%v/metrics'", *listenAddr)
log.Fatal(http.ListenAndServe(*listenAddr, nil))
}
func measure() {
meter := new(meter.Meter)
err := meter.Open(*device)
if err != nil {
log.Fatalf("Could not open '%v'", *device)
return
}
for {
result, err := meter.Read()
if err != nil {
log.Fatalf("Something went wrong: '%v'", err)
}
temperature.Set(result.Temperature)
co2.Set(float64(result.Co2))
}
}