-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
99 lines (82 loc) · 2.13 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"errors"
"fmt"
"net/http"
"os"
"runtime"
"github.com/jessevdk/go-flags"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
"github.com/webdevops/shelly-plug-exporter/config"
"github.com/webdevops/shelly-plug-exporter/discovery"
)
const (
Author = "webdevops.io"
UserAgent = "shelly-plug-exporter/"
)
var (
argparser *flags.Parser
Opts config.Opts
// Git version information
gitCommit = "<unknown>"
gitTag = "<unknown>"
)
func main() {
initArgparser()
initLogger()
logger.Infof("starting shellyplug-plug-exporter v%s (%s; %s; by %v)", gitTag, gitCommit, runtime.Version(), Author)
logger.Info(string(Opts.GetJson()))
initSystem()
logger.Infof("starting http server on %s", Opts.Server.Bind)
startHttpServer()
}
// init argparser and parse/validate arguments
func initArgparser() {
argparser = flags.NewParser(&Opts, flags.Default)
_, err := argparser.Parse()
// check if there is an parse error
if err != nil {
var flagsErr *flags.Error
if ok := errors.As(err, &flagsErr); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
fmt.Println()
argparser.WriteHelp(os.Stdout)
os.Exit(1)
}
}
}
// start and handle prometheus handler
func startHttpServer() {
mux := http.NewServeMux()
// healthz
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
if _, err := fmt.Fprint(w, "Ok"); err != nil {
logger.Error(err)
}
})
// readyz
mux.HandleFunc("/readyz", func(w http.ResponseWriter, r *http.Request) {
if _, err := fmt.Fprint(w, "Ok"); err != nil {
logger.Error(err)
}
})
discovery.EnableDiscovery(
logger.With(zap.String("module", "discovery")),
Opts.Shelly.ServiceDiscovery.Refresh,
Opts.Shelly.ServiceDiscovery.Timeout,
Opts.Shelly.Host.ShellyPlug,
Opts.Shelly.Host.ShellyPlus,
Opts.Shelly.Host.ShellyPro,
)
mux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/probe", shellyProbeDiscovery)
srv := &http.Server{
Addr: Opts.Server.Bind,
Handler: mux,
ReadTimeout: Opts.Server.ReadTimeout,
WriteTimeout: Opts.Server.WriteTimeout,
}
logger.Fatal(srv.ListenAndServe())
}