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

Validate alertmanager URL
This commit is contained in:
Tobias Schmidt 2016-03-07 20:09:46 -05:00
commit 2f151d02eb
2 changed files with 34 additions and 3 deletions

View File

@ -242,10 +242,12 @@ func parse(args []string) error {
if err := parsePrometheusURL(); err != nil { if err := parsePrometheusURL(); err != nil {
return err return err
} }
if err := parseInfluxdbURL(); err != nil { if err := parseInfluxdbURL(); err != nil {
return err return err
} }
if err := validateAlertmanagerURL(); err != nil {
return err
}
cfg.remote.InfluxdbPassword = os.Getenv("INFLUXDB_PW") cfg.remote.InfluxdbPassword = os.Getenv("INFLUXDB_PW")
@ -266,7 +268,7 @@ func parsePrometheusURL() error {
} }
if ok := govalidator.IsURL(cfg.prometheusURL); !ok { 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) promURL, err := url.Parse(cfg.prometheusURL)
@ -289,7 +291,7 @@ func parseInfluxdbURL() error {
} }
if ok := govalidator.IsURL(cfg.influxdbURL); !ok { 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) url, err := url.Parse(cfg.influxdbURL)
@ -301,6 +303,23 @@ func parseInfluxdbURL() error {
return nil 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 = ` var helpTmpl = `
usage: prometheus [<args>] usage: prometheus [<args>]
{{ range $cat, $flags := . }}{{ if ne $cat "." }} == {{ $cat | upper }} =={{ end }} {{ range $cat, $flags := . }}{{ if ne $cat "." }} == {{ $cat | upper }} =={{ end }}

View File

@ -48,6 +48,18 @@ func TestParse(t *testing.T) {
input: []string{"-storage.remote.influxdb-url", "'https://some-url/'"}, input: []string{"-storage.remote.influxdb-url", "'https://some-url/'"},
valid: false, 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 { for i, test := range tests {