-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (79 loc) · 3.04 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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Package main parses config arguments and starts the exporter
package main
import (
"log/slog"
"net/http"
"os"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/hooklift/gowsdl/soap"
"github.com/mrueg/netcupscp-exporter/pkg/metrics"
"github.com/mrueg/netcupscp-exporter/pkg/scpclient"
"github.com/prometheus/client_golang/prometheus"
cversion "github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promslog"
"github.com/prometheus/common/promslog/flag"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
)
var (
loginName = kingpin.Flag("login-name", "User ID").Envar("SCP_LOGINNAME").Default("").String()
password = kingpin.Flag("password", "API Password").Envar("SCP_PASSWORD").Default("").String()
addr = kingpin.Flag("listen-address", "The address to listen on for HTTP requests.").Envar("SCP_LISTENADDRESS").Default(":9757").String()
tlsConfig = kingpin.Flag("tls-config", "Path to TLS config file.").Envar("SCP_TLSCONFIG").Default("").String()
)
const netcupWSUrl = "https://www.servercontrolpanel.de/SCP/WSEndUser" //nolint:gosec
func main() {
promslogConfig := &promslog.Config{}
flag.AddFlags(kingpin.CommandLine, promslogConfig)
kingpin.Version(version.Version + " git " + version.Revision)
kingpin.Parse()
var logger *slog.Logger
var metricsPath = "/metrics"
logger = promslog.New(promslogConfig)
logger.Debug("Starting SCP Exporter version " + version.Version + " git " + version.Revision)
client := soap.NewClient(netcupWSUrl)
wsclient := scpclient.NewWSEndUser(client)
scpCollector := metrics.NewScpCollector(wsclient, logger, loginName, password)
prometheus.DefaultRegisterer.MustRegister(scpCollector)
prometheus.DefaultRegisterer.MustRegister(cversion.NewCollector("scp"))
metricsServer := http.Server{
ReadHeaderTimeout: 5 * time.Second}
landingConfig := web.LandingConfig{
Name: "Netcup SCP Exporter",
Description: "Exporting Metrics from Netcup's ServerControlPanel",
Version: version.Version + " git " + version.Revision,
Links: []web.LandingLinks{
{
Address: metricsPath,
Text: "Metrics",
},
},
}
landingPage, err := web.NewLandingPage(landingConfig)
if err != nil {
logger.Error("failed to create landing page", "error", err.Error())
os.Exit(1)
}
http.Handle(metricsPath, promhttp.HandlerFor(
prometheus.DefaultGatherer,
promhttp.HandlerOpts{
// Opt into OpenMetrics to support exemplars.
EnableOpenMetrics: true,
}))
http.Handle("/", landingPage)
flags := web.FlagConfig{
WebListenAddresses: &[]string{*addr},
WebSystemdSocket: new(bool),
WebConfigFile: tlsConfig,
}
err = web.ListenAndServe(&metricsServer, &flags, logger)
if err != nil {
logger.Error("Run into bad state", "error", err)
os.Exit(1)
}
}