2017-09-07 08:58:58 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-12-22 10:17:13 +00:00
|
|
|
"github.com/alecthomas/kingpin"
|
2017-09-07 08:58:58 +00:00
|
|
|
"github.com/prometheus/alertmanager/config"
|
|
|
|
"github.com/prometheus/alertmanager/template"
|
|
|
|
)
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
// TODO: This can just be a type that is []string, doesn't have to be a struct
|
|
|
|
type checkConfigCmd struct {
|
|
|
|
files []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func configureCheckConfigCmd(app *kingpin.Application, longHelpText map[string]string) {
|
|
|
|
var (
|
|
|
|
c = &checkConfigCmd{}
|
|
|
|
checkCmd = app.Command("check-config", "Validate alertmanager config files")
|
|
|
|
)
|
|
|
|
checkCmd.Arg("check-files", "Files to be validated").ExistingFilesVar(&c.files)
|
2017-12-22 10:17:13 +00:00
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
checkCmd.Action(c.checkConfig)
|
2017-12-22 10:17:13 +00:00
|
|
|
longHelpText["check-config"] = `Validate alertmanager config files
|
2017-09-07 08:58:58 +00:00
|
|
|
|
|
|
|
Will validate the syntax and schema for alertmanager config file
|
|
|
|
and associated templates. Non existing templates will not trigger
|
2017-12-22 10:17:13 +00:00
|
|
|
errors`
|
2017-09-07 08:58:58 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 11:34:16 +00:00
|
|
|
func (c *checkConfigCmd) checkConfig(element *kingpin.ParseElement, ctx *kingpin.ParseContext) error {
|
|
|
|
return CheckConfig(c.files)
|
2017-09-07 08:58:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func CheckConfig(args []string) error {
|
|
|
|
failed := 0
|
|
|
|
|
|
|
|
for _, arg := range args {
|
|
|
|
fmt.Printf("Checking '%s'", arg)
|
|
|
|
config, _, err := config.LoadFile(arg)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf(" FAILED: %s\n", err)
|
2017-11-01 22:08:34 +00:00
|
|
|
failed++
|
2017-09-07 08:58:58 +00:00
|
|
|
} else {
|
|
|
|
fmt.Printf(" SUCCESS\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config != nil {
|
|
|
|
fmt.Printf("Found %d templates: ", len(config.Templates))
|
|
|
|
if len(config.Templates) > 0 {
|
|
|
|
_, err = template.FromGlobs(config.Templates...)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf(" FAILED: %s\n", err)
|
2017-11-01 22:08:34 +00:00
|
|
|
failed++
|
2017-09-07 08:58:58 +00:00
|
|
|
} else {
|
|
|
|
fmt.Printf(" SUCCESS\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
if failed > 0 {
|
2017-11-12 16:43:48 +00:00
|
|
|
return fmt.Errorf("failed to validate %d file(s)", failed)
|
2017-09-07 08:58:58 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|