diff --git a/ssh_exporter.go b/ssh_exporter.go index 2b8c0af..0136def 100644 --- a/ssh_exporter.go +++ b/ssh_exporter.go @@ -53,16 +53,20 @@ func metricsHandler(c *config.Config, logger log.Logger) http.HandlerFunc { if m == "" { m = "default" } + u := r.URL.Query().Get("user") module, ok := c.Modules[m] if !ok { http.Error(w, fmt.Sprintf("Unknown module %s", t), http.StatusNotFound) return } + if u == "" { + u = module.User + } level.Debug(logger).Log("msg", "Loaded module", "module", module.ModuleName) target := &config.Target{ Host: t, - User: module.User, + User: u, Password: module.Password, PrivateKey: module.PrivateKey, Certificate: module.Certificate, diff --git a/ssh_exporter_test.go b/ssh_exporter_test.go index 0753152..03e9d60 100644 --- a/ssh_exporter_test.go +++ b/ssh_exporter_test.go @@ -69,6 +69,16 @@ func TestMetricsHandler(t *testing.T) { } } +func TestMetricsHandlerUser(t *testing.T) { + body, err := queryExporter(fmt.Sprintf("target=localhost:%d&user=test", sshPort), http.StatusOK) + if err != nil { + t.Fatalf("Unexpected error GET /ssh: %s", err.Error()) + } + if !strings.Contains(body, "ssh_success 1") { + t.Errorf("Unexpected value for ssh_success\nGot:\n%s", body) + } +} + func TestMetricsHandlerNoTarget(t *testing.T) { _, _ = queryExporter("", http.StatusBadRequest) }