-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
85 lines (71 loc) · 2.32 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
package main
import (
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const SensorName = "name"
var DefaultAddressMetrics = ""
var DefaultAddressSensor = ""
func main() {
gin.SetMode(gin.ReleaseMode)
routerSensor := gin.Default()
routerSensor.Use(gin.BasicAuth(gin.Accounts{"sensor": getEnv("SENSOR_PASSWORD", "")}))
routerSensor.GET("/sensors/:sensor_name", sensorAction)
routerMetrics := gin.Default()
routerMetrics.GET("/metrics", promHandler(promhttp.Handler()))
go func() {
log.Fatal(routerMetrics.Run(getEnv("ADDRESS_METRICS", DefaultAddressMetrics)))
}()
log.Fatal(routerSensor.Run(getEnv("ADDRESS_SENSOR", DefaultAddressSensor)))
}
func getEnv(name string, defaultValue string) string {
value, exist := os.LookupEnv(name)
if exist {
return value
}
if defaultValue != "" {
return defaultValue
}
log.Fatalf("%s environment variable is missing\n", name)
return value
}
var (
temperatureSensor = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "shellyht_temperature_celsius",
Help: "Current temperature in Celsius",
}, []string{SensorName})
humiditySensor = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "shellyht_humidity_percent",
Help: "Current humidity level in %",
}, []string{SensorName})
lastSuccess = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "shellyht_last_report",
Help: "Unixtime last time the sensor has reported a value",
}, []string{SensorName})
)
type GetSensorActionParameters struct {
Humidity int `form:"hum" binding:"required,min=0,max=100"`
Temperature float64 `form:"temp" binding:"required"`
}
func sensorAction(c *gin.Context) {
sensorName := c.Param("sensor_name")
var actionParameters GetSensorActionParameters
if err := c.BindQuery(&actionParameters); err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}
labels := prometheus.Labels{SensorName: sensorName}
temperatureSensor.With(labels).Set(actionParameters.Temperature)
humiditySensor.With(labels).Set(float64(actionParameters.Humidity))
lastSuccess.With(labels).SetToCurrentTime()
}
func promHandler(handler http.Handler) gin.HandlerFunc {
return func(c *gin.Context) {
handler.ServeHTTP(c.Writer, c.Request)
}
}