diff --git a/README.md b/README.md index bced941..cd84539 100644 --- a/README.md +++ b/README.md @@ -199,8 +199,10 @@ usage: nut_exporter [] Flags: -h, --[no-]help Show context-sensitive help (also try --help-long and --help-man). --nut.server="127.0.0.1" Hostname or IP address of the server to connect to. ($NUT_EXPORTER_SERVER) ($NUT_EXPORTER_SERVER) + --nut.serverport=3493 Port on the NUT server to connect to. ($NUT_EXPORTER_SERVER) ($NUT_EXPORTER_SERVER) --nut.username=NUT.USERNAME - If set, will authenticate with this username to the server. Password must be set in NUT_EXPORTER_PASSWORD environment variable. ($NUT_EXPORTER_USERNAME) ($NUT_EXPORTER_USERNAME) + If set, will authenticate with this username to the server. Password must be set in NUT_EXPORTER_PASSWORD environment variable. ($NUT_EXPORTER_USERNAME) + ($NUT_EXPORTER_USERNAME) --[no-]nut.disable_device_info A flag to disable the generation of the device_info meta metric. ($NUT_EXPORTER_DISABLE_DEVICE_INFO) ($NUT_EXPORTER_DISABLE_DEVICE_INFO) --nut.vars_enable="battery.charge,battery.voltage,battery.voltage.nominal,input.voltage,input.voltage.nominal,ups.load,ups.status" @@ -219,7 +221,8 @@ Flags: --[no-]web.systemd-socket Use systemd socket activation listeners instead of port listeners (Linux only). --web.listen-address=:9199 ... Addresses on which to expose metrics and web interface. Repeatable for multiple addresses. - --web.config.file="" [EXPERIMENTAL] Path to configuration file that can enable TLS or authentication. See: https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md + --web.config.file="" [EXPERIMENTAL] Path to configuration file that can enable TLS or authentication. See: + https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md --web.telemetry-path="/ups_metrics" Path under which to expose the UPS Prometheus metrics ($NUT_EXPORTER_WEB_TELEMETRY_PATH) ($NUT_EXPORTER_WEB_TELEMETRY_PATH) --web.exporter-telemetry-path="/metrics" diff --git a/collectors/nut_collector.go b/collectors/nut_collector.go index cbfd789..a0872e6 100644 --- a/collectors/nut_collector.go +++ b/collectors/nut_collector.go @@ -24,6 +24,7 @@ type NutCollector struct { type NutCollectorOpts struct { Namespace string Server string + ServerPort int Ups string Username string Password string @@ -81,8 +82,8 @@ func NewNutCollector(opts NutCollectorOpts, logger log.Logger) (*NutCollector, e } func (c *NutCollector) Collect(ch chan<- prometheus.Metric) { - level.Debug(c.logger).Log("msg", "Connecting to server", "server", c.opts.Server) - client, err := nut.Connect(c.opts.Server) + level.Debug(c.logger).Log("msg", "Connecting to server", "server", c.opts.Server, "port", c.opts.ServerPort) + client, err := nut.Connect(c.opts.Server, c.opts.ServerPort) if err != nil { level.Error(c.logger).Log("err", err) ch <- prometheus.NewInvalidMetric( diff --git a/nut_exporter.go b/nut_exporter.go index 1399f5b..5a41cea 100644 --- a/nut_exporter.go +++ b/nut_exporter.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "os" + "strconv" "strings" "github.com/alecthomas/kingpin/v2" @@ -26,6 +27,10 @@ var ( "nut.server", "Hostname or IP address of the server to connect to. ($NUT_EXPORTER_SERVER)", ).Envar("NUT_EXPORTER_SERVER").Default("127.0.0.1").String() + serverport = kingpin.Flag( + "nut.serverport", "Port on the NUT server to connect to. ($NUT_EXPORTER_SERVER)", + ).Envar("NUT_EXPORTER_SERVER").Default("3493").Int() + nutUsername = kingpin.Flag( "nut.username", "If set, will authenticate with this username to the server. Password must be set in NUT_EXPORTER_PASSWORD environment variable. ($NUT_EXPORTER_USERNAME)", ).Envar("NUT_EXPORTER_USERNAME").String() @@ -89,6 +94,12 @@ func (h *metricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { thisCollectorOpts.Server = r.URL.Query().Get("server") } + if r.URL.Query().Get("serverport") != "" { + if port, err := strconv.Atoi(r.URL.Query().Get("serverport")); err != nil { + thisCollectorOpts.ServerPort = port + } + } + if r.URL.Query().Get("username") != "" { thisCollectorOpts.Username = r.URL.Query().Get("username") } @@ -106,7 +117,7 @@ func (h *metricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } var promHandler http.Handler - cacheName := fmt.Sprintf("%s/%s", thisCollectorOpts.Server, thisCollectorOpts.Ups) + cacheName := fmt.Sprintf("%s:%d/%s", thisCollectorOpts.Server, thisCollectorOpts.ServerPort, thisCollectorOpts.Ups) if tmp, ok := h.handlers[cacheName]; ok { level.Debug(logger).Log("msg", fmt.Sprintf("Using existing handler for UPS `%s`", cacheName)) promHandler = *tmp @@ -173,6 +184,7 @@ func main() { collectorOpts = collectors.NutCollectorOpts{ Namespace: *metricsNamespace, Server: *server, + ServerPort: *serverport, Username: *nutUsername, Password: nutPassword, DisableDeviceInfo: *disableDeviceInfo,