Support configuration using environment variables (#5)

This commit is contained in:
herver 2018-10-27 15:36:35 +02:00 committed by Robert Jacob
parent 95b0de08dd
commit 6eb05643c9
3 changed files with 31 additions and 7 deletions

View file

@ -4,6 +4,11 @@ This changelog contains the changes made between releases. The versioning follow
## Unreleased ## Unreleased
### Added
- Support for battery and RF-link status
- Support for configuration via environment variables
## 1.1.0 (2018-09-02) ## 1.1.0 (2018-09-02)
### Added ### Added

View file

@ -38,6 +38,18 @@ Usage of netatmo-exporter:
After starting the server will offer the metrics on the `/metrics` endpoint, which can be used as a target for prometheus. After starting the server will offer the metrics on the `/metrics` endpoint, which can be used as a target for prometheus.
### Passing secrets
You can pass credentials either via command line arguments (see next section) or by populating the following environment variables:
* `NETATMO_EXPORTER_ADDR` Address to listen on
* `NETATMO_CLIENT_ID` Client ID for NetAtmo app
* `NETATMO_CLIENT_SECRET` Client secret for NetAtmo app
* `NETATMO_CLIENT_USERNAME` Username of NetAtmo account
* `NETATMO_CLIENT_PASSWORD` Password of NetAtmo account
### Scrape interval
The exporter will query the Netatmo API every time it is scraped by prometheus. It does not make sense to scrape the Netatmo API with a small interval as the sensors only update their data every few minutes, so don't forget to set a slower scrape interval for this exporter: The exporter will query the Netatmo API every time it is scraped by prometheus. It does not make sense to scrape the Netatmo API with a small interval as the sensors only update their data every few minutes, so don't forget to set a slower scrape interval for this exporter:
```yml ```yml
@ -46,5 +58,4 @@ scrape_configs:
scrape_interval: 90s scrape_interval: 90s
static_configs: static_configs:
- targets: ['localhost:9210'] - targets: ['localhost:9210']
``` ```

18
main.go
View file

@ -4,6 +4,7 @@ import (
"errors" "errors"
"log" "log"
"net/http" "net/http"
"os"
netatmo "github.com/exzz/netatmo-api-go" netatmo "github.com/exzz/netatmo-api-go"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@ -17,11 +18,18 @@ type config struct {
func parseConfig() (config, error) { func parseConfig() (config, error) {
cfg := config{} cfg := config{}
pflag.StringVarP(&cfg.Addr, "addr", "a", ":9210", "Address to listen on.")
pflag.StringVarP(&cfg.Netatmo.ClientID, "client-id", "i", "", "Client ID for NetAtmo app.") // Set default port to bind to
pflag.StringVarP(&cfg.Netatmo.ClientSecret, "client-secret", "s", "", "Client secret for NetAtmo app.") addr := ":9210"
pflag.StringVarP(&cfg.Netatmo.Username, "username", "u", "", "Username of NetAtmo account.") if envAddr := os.Getenv("NETATMO_EXPORTER_ADDR"); envAddr != "" {
pflag.StringVarP(&cfg.Netatmo.Password, "password", "p", "", "Password of NetAtmo account.") addr = envAddr
}
pflag.StringVarP(&cfg.Addr, "addr", "a", addr, "Address to listen on.")
pflag.StringVarP(&cfg.Netatmo.ClientID, "client-id", "i", os.Getenv("NETATMO_CLIENT_ID"), "Client ID for NetAtmo app.")
pflag.StringVarP(&cfg.Netatmo.ClientSecret, "client-secret", "s", os.Getenv("NETATMO_CLIENT_SECRET"), "Client secret for NetAtmo app.")
pflag.StringVarP(&cfg.Netatmo.Username, "username", "u", os.Getenv("NETATMO_CLIENT_USERNAME"), "Username of NetAtmo account.")
pflag.StringVarP(&cfg.Netatmo.Password, "password", "p", os.Getenv("NETATMO_CLIENT_PASSWORD"), "Password of NetAtmo account.")
pflag.Parse() pflag.Parse()
if len(cfg.Addr) == 0 { if len(cfg.Addr) == 0 {