Merge pull request #1456 from prometheus/validate-alertmanager-url

Validate alertmanager URL
pull/1474/head
Tobias Schmidt 9 years ago
commit 2f151d02eb

@ -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 [<args>]
{{ range $cat, $flags := . }}{{ if ne $cat "." }} == {{ $cat | upper }} =={{ end }}

@ -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 {

Loading…
Cancel
Save