Skip to content

Commit

Permalink
Merge pull request #1 from niklasdoerfler/feature/add-mqtt-exporter
Browse files Browse the repository at this point in the history
Feature: Add mqtt exporter
  • Loading branch information
niklasdoerfler authored Jan 19, 2022
2 parents c6f0dd9 + 9a9a262 commit 37bde5d
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 28 deletions.
50 changes: 25 additions & 25 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,34 @@ jobs:
# Learn more about CodeQL language support at https://git.io/codeql-language-support

steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Checkout repository
uses: actions/checkout@v2

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release
#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ follows:

This exporter provides prometheus compatible metrics on the metrics endpoint (`/metrics`).

__TODO: Finish implementing gauges for all available measurement data.__

## Config

The service can be configured by a `config.yaml` file placed next to the binary.
Expand All @@ -78,18 +76,29 @@ The following represents an example config:
```yaml
webserverPort: 8080

logLevel: info

jsonExporter:
enabled: true

prometheusExporter:
enabled: true

influxDbExporter:
enabled: true
enabled: false
server: 1.2.3.4
port: 8086
user: user
password: password
database: database
measurement: measurement

mqtt:
enabled: false
brokerAddress: 1.2.3.4
brokerPort: 1883
username: username
password: password
clientId: weather-exporter
topicPrefix: weather
```
11 changes: 11 additions & 0 deletions config.sample.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
webserverPort: 8080

logLevel: info

jsonExporter:
enabled: true

Expand All @@ -14,3 +16,12 @@ influxDbExporter:
password: password
database: database
measurement: measurement

mqtt:
enabled: false
brokerAddress: 1.2.3.4
brokerPort: 1883
username: username
password: password
clientId: weather-exporter
topicPrefix: weather
70 changes: 70 additions & 0 deletions exporter/mqtt/mqttExporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package mqtt

import (
"bresser-weather-exporter/model"
"bresser-weather-exporter/model/configuration"
"encoding/json"
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
log "github.com/sirupsen/logrus"
"strings"
)

var (
mqttClient mqtt.Client
topicPrefix string
)

var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
log.Debugf("Received message: %s from topic: %s\n", msg.Payload(), msg.Topic())
}

var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
rOps := client.OptionsReader()
servers := rOps.Servers()
log.Infof("Connected to mqtt broker: %s:%s", servers[0].Host, servers[0].Port())
}

var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
log.Warnf("Connect to mqtt broker lost: %v", err)
}

func SetupMqttConnection(config *configuration.Configuration) {
if config.Mqtt.Enabled {
opts := mqtt.NewClientOptions()
opts.AddBroker(fmt.Sprintf("tcp://%s:%d", config.Mqtt.BrokerAddress, config.Mqtt.BrokerPort))
opts.SetClientID(config.Mqtt.ClientId)
opts.SetUsername(config.Mqtt.Username)
opts.SetPassword(config.Mqtt.Password)
opts.SetDefaultPublishHandler(messagePubHandler)
opts.OnConnect = connectHandler
opts.OnConnectionLost = connectLostHandler
topicPrefix = strings.TrimSuffix(config.Mqtt.TopicPrefix, "/")
mqttClient = mqtt.NewClient(opts)
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
log.Error("Unable to connect to mqtt broker:", token.Error())
}
}
}

func PublishData(data *model.WeatherData) {
if mqttClient != nil && mqttClient.IsConnectionOpen() {
jsonData, _ := json.Marshal(data)
publishMessage("json", string(jsonData))

var inInterface map[string]interface{}
err := json.Unmarshal(jsonData, &inInterface)
if err == nil {
for field, value := range inInterface {
publishMessage(field, fmt.Sprintf("%v", value))
}
}
}
}

func publishMessage(topic string, message string) {
fullTopic := topicPrefix + "/" + topic
log.Debugf("Publish message on topic %s: '%s'", fullTopic, message)
token := mqttClient.Publish(fullTopic, 0, false, message)
token.Wait()
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/deepmap/oapi-codegen v1.8.2 // indirect
github.com/eclipse/paho.mqtt.golang v1.3.5 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect
github.com/magiconair/properties v1.8.5 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/deepmap/oapi-codegen v1.8.2 h1:SegyeYGcdi0jLLrpbCMoJxnUUn8GBXHsvr4rbzjuhfU=
github.com/deepmap/oapi-codegen v1.8.2/go.mod h1:YLgSKSDv/bZQB7N4ws6luhozi3cEdRktEqrX88CvjIw=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/eclipse/paho.mqtt.golang v1.3.5 h1:sWtmgNxYM9P2sP+xEItMozsR3w0cqZFlqnNN1bdl41Y=
github.com/eclipse/paho.mqtt.golang v1.3.5/go.mod h1:eTzb4gxwwyWpqBUHGQZ4ABAV7+Jgm1PklsYT/eo8Hcc=
github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
github.com/getkin/kin-openapi v0.61.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
Expand Down Expand Up @@ -55,6 +57,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/influxdata/influxdb-client-go/v2 v2.6.0 h1:bIOaGTgvvv1Na2hG+nIvqyv7PK2UiU2WrJN1ck1ykyM=
Expand Down Expand Up @@ -165,6 +169,7 @@ golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
Expand Down
1 change: 1 addition & 0 deletions model/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ type Configuration struct {
JsonExporter JsonExporterConfiguration
PrometheusExporter PrometheusExporterConfiguration
InfluxDbExporter InfluxExporterConfiguration
Mqtt MqttConfiguration
}
11 changes: 11 additions & 0 deletions model/configuration/mqtt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package configuration

type MqttConfiguration struct {
Enabled bool
BrokerAddress string
BrokerPort int
Username string
Password string
ClientId string
TopicPrefix string
}
3 changes: 3 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bresser-weather-exporter/exporter/influxDb"
"bresser-weather-exporter/exporter/mqtt"
"bresser-weather-exporter/exporter/prometheus"
"bresser-weather-exporter/model"
"bresser-weather-exporter/model/configuration"
Expand Down Expand Up @@ -83,6 +84,7 @@ func weatherStationEventHandler(w http.ResponseWriter, req *http.Request) {
parseDataRecord(keys)
prometheus.UpdatePromGauges(weatherData)
influxDb.WriteDataToDb(weatherData)
mqtt.PublishData(weatherData)

_, err := io.WriteString(w, "success")
if err != nil {
Expand Down Expand Up @@ -124,5 +126,6 @@ func main() {
log.Infof("Hello Weather Exporter %s! ☀ (Build: %s)", BuildVersion, BuildTime)
loadConfig()
influxDb.SetupInfluxDb(&config)
mqtt.SetupMqttConnection(&config)
runWebserver()
}

0 comments on commit 37bde5d

Please sign in to comment.