2017-01-08 17:12:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2018-10-27 13:36:35 +00:00
|
|
|
"os"
|
2017-01-08 17:12:34 +00:00
|
|
|
|
|
|
|
netatmo "github.com/exzz/netatmo-api-go"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2018-10-27 15:07:08 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2020-06-21 13:22:19 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
log = &logrus.Logger{
|
|
|
|
Out: os.Stderr,
|
|
|
|
Formatter: &logrus.TextFormatter{
|
|
|
|
DisableTimestamp: true,
|
|
|
|
},
|
|
|
|
Level: logrus.InfoLevel,
|
|
|
|
ExitFunc: os.Exit,
|
|
|
|
ReportCaller: false,
|
|
|
|
}
|
2017-01-08 17:12:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2018-10-27 14:29:11 +00:00
|
|
|
cfg, err := parseConfig(os.Args, os.Getenv)
|
2017-01-08 17:12:34 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error in configuration: %s", err)
|
|
|
|
}
|
2020-06-21 13:39:24 +00:00
|
|
|
log.SetLevel(logrus.Level(cfg.LogLevel))
|
2017-01-08 17:12:34 +00:00
|
|
|
|
2020-06-21 13:22:19 +00:00
|
|
|
log.Infof("Login as %s", cfg.Netatmo.Username)
|
2017-01-08 17:12:34 +00:00
|
|
|
client, err := netatmo.NewClient(cfg.Netatmo)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error creating client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics := &netatmoCollector{
|
2020-06-21 16:04:42 +00:00
|
|
|
log: log,
|
|
|
|
client: client,
|
|
|
|
staleThreshold: cfg.StaleDuration,
|
2017-01-08 17:12:34 +00:00
|
|
|
}
|
|
|
|
prometheus.MustRegister(metrics)
|
|
|
|
|
2018-10-27 15:07:08 +00:00
|
|
|
http.Handle("/metrics", promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}))
|
2020-06-21 13:26:53 +00:00
|
|
|
http.Handle("/version", versionHandler(log))
|
2017-01-08 17:12:34 +00:00
|
|
|
http.Handle("/", http.RedirectHandler("/metrics", http.StatusFound))
|
|
|
|
|
2020-06-21 13:22:19 +00:00
|
|
|
log.Infof("Listen on %s...", cfg.Addr)
|
2017-01-08 17:12:34 +00:00
|
|
|
log.Fatal(http.ListenAndServe(cfg.Addr, nil))
|
|
|
|
}
|