diff --git a/cmd/prometheus/config.go b/cmd/prometheus/config.go index 19858d1cd..35f8c0b8d 100644 --- a/cmd/prometheus/config.go +++ b/cmd/prometheus/config.go @@ -242,10 +242,12 @@ func parse(args []string) error { if err := parsePrometheusURL(); err != nil { return err } - if err := parseInfluxdbURL(); err != nil { return err } + if err := validateAlertmanagerURL(); err != nil { + return err + } cfg.remote.InfluxdbPassword = os.Getenv("INFLUXDB_PW") @@ -266,7 +268,7 @@ func parsePrometheusURL() error { } if ok := govalidator.IsURL(cfg.prometheusURL); !ok { - return fmt.Errorf("Invalid Prometheus URL: %s", cfg.prometheusURL) + return fmt.Errorf("invalid Prometheus URL: %s", cfg.prometheusURL) } promURL, err := url.Parse(cfg.prometheusURL) @@ -289,7 +291,7 @@ func parseInfluxdbURL() error { } if ok := govalidator.IsURL(cfg.influxdbURL); !ok { - return fmt.Errorf("Invalid InfluxDB URL: %s", cfg.influxdbURL) + return fmt.Errorf("invalid InfluxDB URL: %s", cfg.influxdbURL) } url, err := url.Parse(cfg.influxdbURL) @@ -301,6 +303,23 @@ func parseInfluxdbURL() error { return nil } +func validateAlertmanagerURL() error { + if cfg.notifier.AlertmanagerURL == "" { + return nil + } + if ok := govalidator.IsURL(cfg.notifier.AlertmanagerURL); !ok { + return fmt.Errorf("invalid Alertmanager URL: %s", cfg.notifier.AlertmanagerURL) + } + url, err := url.Parse(cfg.notifier.AlertmanagerURL) + if err != nil { + return err + } + if url.Scheme == "" { + return fmt.Errorf("missing scheme in Alertmanager URL: %s", cfg.notifier.AlertmanagerURL) + } + return nil +} + var helpTmpl = ` usage: prometheus [] {{ range $cat, $flags := . }}{{ if ne $cat "." }} == {{ $cat | upper }} =={{ end }} diff --git a/cmd/prometheus/config_test.go b/cmd/prometheus/config_test.go index 4f5e8c112..a77eaf207 100644 --- a/cmd/prometheus/config_test.go +++ b/cmd/prometheus/config_test.go @@ -48,6 +48,18 @@ func TestParse(t *testing.T) { input: []string{"-storage.remote.influxdb-url", "'https://some-url/'"}, valid: false, }, + { + input: []string{"-alertmanager.url", ""}, + valid: true, + }, + { + input: []string{"-alertmanager.url", "http://alertmanager.company.com"}, + valid: true, + }, + { + input: []string{"-alertmanager.url", "alertmanager.company.com"}, + valid: false, + }, } for i, test := range tests {