Skip to content

Commit

Permalink
Merge pull request #70 from Momeno/master
Browse files Browse the repository at this point in the history
Enable providing mqtt login details via the environment
  • Loading branch information
hikhvar committed Sep 20, 2021
2 parents 4418b5a + 83413b1 commit e4d37ac
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,30 @@ metrics:
error_value: 1
```

### Environment Variables

Having the MQTT login details in the config file runs the risk of publishing them to a version control system. To avoid this, you can supply these parameters via environment variables. MQTT2Prometheus will look for `MQTT2PROM_MQTT_USER` and `MQTT2PROM_MQTT_PASSWORD` in the local environment and load them on startup.

#### Example use with Docker

Create a file to store your login details, for example at `~/secrets/mqtt2prom`:
```SHELL
#!/bin/bash
export MQTT2PROM_MQTT_USER="myUser"
export MQTT2PROM_MQTT_PASSWORD="superpassword"
```

Then load that file into the environment before starting the container:
```SHELL
source ~/secrets/mqtt2prom && \
docker run -it \
-e MQTT2PROM_MQTT_USER \
-e MQTT2PROM_MQTT_PASSWORD \
-v "$(pwd)/examples/config.yaml:/config.yaml" \
-p 9641:9641 \
ghcr.io/hikhvar/mqtt2prometheus:latest
```


## Best Practices
The exporter can only listen to one topic_path per instance. If you have to listen to two different topic_paths it is
Expand Down
11 changes: 11 additions & 0 deletions cmd/mqtt2prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ func main() {
if err != nil {
logger.Fatal("Could not load config", zap.Error(err))
}

mqtt_user := os.Getenv("MQTT2PROM_MQTT_USER")
mqtt_password := os.Getenv("MQTT2PROM_MQTT_PASSWORD")

if mqtt_user != "" {
cfg.MQTT.User = mqtt_user
}
if mqtt_password != "" {
cfg.MQTT.Password = mqtt_password
}

mqttClientOptions := mqtt.NewClientOptions()
mqttClientOptions.AddBroker(cfg.MQTT.Server).SetCleanSession(true)
mqttClientOptions.SetAutoReconnect(true)
Expand Down

0 comments on commit e4d37ac

Please sign in to comment.