Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"net"
"os"
goruntime "runtime"

Expand All @@ -27,6 +28,9 @@ func main() {
var port int
pflag.IntVar(&port, "port", metrics.DefaultMetricsPort,
"Prometheus metrics-exporter port number")
var bindAddress net.IP
pflag.IPVar(&bindAddress, "address", bindAddress,
"Prometheus metrics-exporter bind address")
var noProfile bool
pflag.BoolVar(&noProfile, "no-profile", false,
"Run without collecting profile information")
Expand Down Expand Up @@ -57,7 +61,12 @@ func main() {
}
log.Info("Located smbstatus", "path", loc, "version", ver)

err = metrics.RunSmbMetricsExporter(log, port, !noProfile)
var bindAddrs []net.IP
if len(bindAddress) > 0 {
bindAddrs = append(bindAddrs, bindAddress)
log.Info("User supplied bind addresses", "bindAddrs", bindAddrs)
}
err = metrics.RunSmbMetricsExporter(log, port, bindAddrs, !noProfile)
if err != nil {
os.Exit(1)
}
Expand Down
40 changes: 26 additions & 14 deletions internal/metrics/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,23 @@ var (
)

type smbMetricsExporter struct {
log logr.Logger
reg *prometheus.Registry
mux *http.ServeMux
port int
profile bool
log logr.Logger
reg *prometheus.Registry
mux *http.ServeMux
port int
bindAddresses []net.IP
profile bool
}

func newSmbMetricsExporter(log logr.Logger, port int, profile bool) *smbMetricsExporter {
func newSmbMetricsExporter(
log logr.Logger, port int, bindAddresses []net.IP, profile bool) *smbMetricsExporter {
return &smbMetricsExporter{
log: log,
reg: prometheus.NewRegistry(),
mux: http.NewServeMux(),
port: port,
profile: profile,
log: log,
reg: prometheus.NewRegistry(),
mux: http.NewServeMux(),
port: port,
bindAddresses: bindAddresses,
profile: profile,
}
}

Expand All @@ -43,7 +46,15 @@ func (sme *smbMetricsExporter) init() error {
}

func (sme *smbMetricsExporter) serve() error {
addr := fmt.Sprintf(":%d", sme.port)
var addr string
switch len(sme.bindAddresses) {
case 0:
addr = fmt.Sprintf(":%d", sme.port)
case 1:
addr = fmt.Sprintf("%s:%d", sme.bindAddresses[0], sme.port)
default:
return fmt.Errorf("too many listen addresses")
}
sme.log.Info("serve metrics", "addr", addr)

handler := promhttp.HandlerFor(sme.reg, promhttp.HandlerOpts{})
Expand All @@ -65,11 +76,12 @@ func (sme *smbMetricsExporter) serve() error {

// RunSmbMetricsExporter executes an HTTP server and exports SMB metrics to
// Prometheus.
func RunSmbMetricsExporter(log logr.Logger, port int, profile bool) error {
func RunSmbMetricsExporter(
log logr.Logger, port int, bindAddresses []net.IP, profile bool) error {
if port <= 0 {
port = DefaultMetricsPort
}
sme := newSmbMetricsExporter(log, port, profile)
sme := newSmbMetricsExporter(log, port, bindAddresses, profile)
err := sme.init()
if err != nil {
return err
Expand Down
Loading