forked from ccremer/fronius-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (68 loc) · 2.18 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
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,
})
if err != nil {
log.WithError(err).Fatal("Cannot initialize Fronius Symo client.")
}
if !config.Symo.ArchiveEnabled && !config.Symo.PowerFlowEnabled {
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.")
}