Skip to content

Commit

Permalink
amtool check-config
Browse files Browse the repository at this point in the history
This is similar to `promtool check-config` and allows one
to validate the alertmanager configuration (as a git presubmit for example).

`govendor fetch github.com/spf13/{cobra,pflag}` was needed to
have support for `Args`.
  • Loading branch information
Corentin Chary committed Sep 5, 2017
1 parent d33511c commit ab677d6
Show file tree
Hide file tree
Showing 28 changed files with 1,881 additions and 1,011 deletions.
60 changes: 60 additions & 0 deletions cli/check_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cli

import (
"fmt"

"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/template"
"github.com/spf13/cobra"
)

// alertCmd represents the alert command
var checkConfigCmd = &cobra.Command{
Use: "check-config",
Args: cobra.MinimumNArgs(1),
Short: "Validate configuration files for correctness",
Long: `Validate configuration files for correctness
Will validate the syntax and schema for alertmanager config file
and associated templates. Non existing templates will not trigger
errors`,
RunE: checkConfig,
}

func init() {
RootCmd.AddCommand(checkConfigCmd)
checkConfigCmd.Flags()
}

func checkConfig(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

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)
failed += 1
} else {
fmt.Printf(" SUCCESS\n")
}

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)
failed += 1
} else {
fmt.Printf(" SUCCESS\n")
}
}
fmt.Printf("\n")
}
if failed > 0 {
return fmt.Errorf("Failed to validate %d file(s).", failed)
}
return nil
}
Loading

0 comments on commit ab677d6

Please sign in to comment.