forked from ccremer/fronius-exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
78 lines (71 loc) · 2.36 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
package main
import (
"net/http"
"os"
"time"
"github.com/ccremer/fronius-exporter/cfg"
"github.com/ccremer/fronius-exporter/pkg/fronius"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
)
var (
version = "unknown"
commit = "dirty"
date = time.Now().String()
config = cfg.ParseConfig(version, commit, date, flag.NewFlagSet("main", flag.ExitOnError), os.Args[1:])
promHandler = promhttp.Handler()
)
func main() {
log.WithFields(log.Fields{
"version": version,
"commit": commit,
"date": date,
}).Info("Starting exporter.")
headers := http.Header{}
cfg.ConvertHeaders(config.Symo.Headers, &headers)
symoClient, err := fronius.NewSymoClient(fronius.ClientOptions{
URL: config.Symo.URL,
Headers: headers,
Timeout: config.Symo.Timeout,
PowerFlowEnabled: config.Symo.PowerFlowEnabled,
ArchiveEnabled: config.Symo.ArchiveEnabled,
SmartMeterEnabled: config.Symo.SmartMeterEnabled,
OffsetConsumed: config.Symo.OffsetConsumed,
OffsetProduced: config.Symo.OffsetProduced,
})
if err != nil {
log.WithError(err).Fatal("Cannot initialize Fronius Symo client.")
}
if !config.Symo.ArchiveEnabled && !config.Symo.PowerFlowEnabled && !config.Symo.SmartMeterEnabled {
log.Fatal("All scrape endpoints are disabled. You need enable at least one endpoint.")
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
log.WithFields(log.Fields{
"uri": r.RequestURI,
"client": r.RemoteAddr,
}).Debug("Accessed Root endpoint")
http.Redirect(w, r, "/metrics", http.StatusMovedPermanently)
})
http.HandleFunc("/liveness", func(w http.ResponseWriter, r *http.Request) {
log.WithFields(log.Fields{
"uri": r.RequestURI,
"client": r.RemoteAddr,
}).Debug("Accessed Liveness endpoint")
w.WriteHeader(http.StatusNoContent)
})
http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
log.WithFields(log.Fields{
"uri": r.RequestURI,
"client": r.RemoteAddr,
}).Debug("Accessed Metrics endpoint")
collectMetricsFromTarget(symoClient)
promHandler.ServeHTTP(w, r)
})
log.WithField("port", config.BindAddr).Info("Listening for scrapes.")
log.WithError(http.ListenAndServe(config.BindAddr, nil)).Fatal("Shutting down.")
}