jenningsloy318 / redfish_exporter

exporter to get metrics from redfish based hardware such as lenovo/dell/superc servers
Apache License 2.0
70 stars 61 forks source link

Add support for Basic Auth and TLS #47

Closed fschlich closed 2 years ago

fschlich commented 2 years ago

This seems to be fairly easy to add via exporter-toolkit, see for example for snmp-exporter: https://github.com/prometheus/snmp_exporter/commit/afc1c6ab31193619b89872de24f35c6b50fa5dda and https://github.com/prometheus/snmp_exporter/commit/c821d06168024f0cbd4dc2c5f432aa9d3c8c9f35

Usage instructions are at https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md

fschlich commented 2 years ago

I wanted to send a pull request, but I hit a snag: exporter-toolkit wants a logger that implements a Log() method. I don't really know any go, so there may be a smart way around this (short of converting everything to promlog)? I had to fall back on go-kit/log, and this is what works for me:

diff --git a/go.mod b/go.mod
index 5325521..bacd7c5 100644
--- a/go.mod
+++ b/go.mod
@@ -6,9 +6,11 @@ require (
        github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
        github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 // indirect
        github.com/apex/log v1.9.0
+       github.com/go-kit/log v0.2.0
        github.com/prometheus/client_golang v1.1.0
        github.com/prometheus/common v0.6.0
        github.com/stmcginnis/gofish v0.6.0
+       github.com/prometheus/exporter-toolkit v0.5.0
        gopkg.in/alecthomas/kingpin.v2 v2.2.6
        gopkg.in/yaml.v2 v2.2.2
 )
diff --git a/main.go b/main.go
index 05ff1e2..fed19cd 100755
--- a/main.go
+++ b/main.go
@@ -8,10 +8,13 @@ import (
        "syscall"

        alog "github.com/apex/log"
+       kitlog "github.com/go-kit/log"
        "github.com/jenningsloy318/redfish_exporter/collector"
        "github.com/prometheus/client_golang/prometheus"
        "github.com/prometheus/client_golang/prometheus/promhttp"
        "github.com/prometheus/common/log"
+       "github.com/prometheus/exporter-toolkit/web"
+       webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
        kingpin "gopkg.in/alecthomas/kingpin.v2"
 )

@@ -27,6 +30,7 @@ var (
                "config.file",
                "Path to configuration file.",
        ).String()
+       webConfig   = webflag.AddFlags(kingpin.CommandLine)
        listenAddress = kingpin.Flag(
                "web.listen-address",
                "Address to listen on for web interface and telemetry.",
@@ -119,6 +123,7 @@ func main() {
        log.AddFlags(kingpin.CommandLine)
        kingpin.HelpFlag.Short('h')
        kingpin.Parse()
+       kitlogger := kitlog.NewLogfmtLogger(os.Stderr)

        configLoggerCtx := rootLoggerCtx.WithField("config", *configFile)
        configLoggerCtx.Info("starting app")
@@ -178,7 +183,8 @@ func main() {
        })

        rootLoggerCtx.Infof("app started. listening on %s", *listenAddress)
-       err := http.ListenAndServe(*listenAddress, nil)
+       srv := &http.Server{Addr: *listenAddress}
+       err := web.ListenAndServe(srv, *webConfig, kitlogger)
        if err != nil {
                log.Fatal(err)
        }
jenningsloy318 commented 2 years ago

Hi @fschlich i saw there is only changes regarding go modules and main.go. do you mean this is the whole thing that need to be changed to add TLS/authentication , other code are keep same ?

fschlich commented 2 years ago

yes, with exporter-toolkit this is actually very easy, it just needs webConfig so the user can put TLS and BasicAuth details somewhere, and then the change to ListenAndServer - that's it. (plus 'go mod tidy', see here what I'm actually running with now: https://github.com/fschlich/redfish_exporter/commits/master)

jenningsloy318 commented 2 years ago

can you just create a pull request? then I can directly merge into master

fschlich commented 2 years ago

sure, here you go: https://github.com/jenningsloy318/redfish_exporter/pull/48