Skip to content

Commit

Permalink
Add support for setting the port to use
Browse files Browse the repository at this point in the history
  • Loading branch information
DRuggeri committed Nov 29, 2023
1 parent 04bae3b commit 5fdea8d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,10 @@ usage: nut_exporter [<flags>]
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"
Expand All @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions collectors/nut_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type NutCollector struct {
type NutCollectorOpts struct {
Namespace string
Server string
ServerPort int
Ups string
Username string
Password string
Expand Down Expand Up @@ -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(
Expand Down
14 changes: 13 additions & 1 deletion nut_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"os"
"strconv"
"strings"

"github.com/alecthomas/kingpin/v2"
Expand All @@ -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()
Expand Down Expand Up @@ -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")
}
Expand All @@ -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
Expand Down Expand Up @@ -173,6 +184,7 @@ func main() {
collectorOpts = collectors.NutCollectorOpts{
Namespace: *metricsNamespace,
Server: *server,
ServerPort: *serverport,
Username: *nutUsername,
Password: nutPassword,
DisableDeviceInfo: *disableDeviceInfo,
Expand Down

0 comments on commit 5fdea8d

Please sign in to comment.