Skip to content

Commit

Permalink
Merge pull request prometheus#1456 from prometheus/validate-alertmana…
Browse files Browse the repository at this point in the history
…ger-url

Validate alertmanager URL
  • Loading branch information
grobie committed Mar 8, 2016
2 parents 1bd4c92 + 7763bbd commit 2f151d0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
25 changes: 22 additions & 3 deletions cmd/prometheus/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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 [<args>]
{{ range $cat, $flags := . }}{{ if ne $cat "." }} == {{ $cat | upper }} =={{ end }}
Expand Down
12 changes: 12 additions & 0 deletions cmd/prometheus/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2f151d0

Please sign in to comment.