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 7, 2017
1 parent d33511c commit 7aa8955
Show file tree
Hide file tree
Showing 31 changed files with 1,915 additions and 1,011 deletions.
65 changes: 65 additions & 0 deletions cli/check_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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
return CheckConfig(args)
}

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)
failed += 1
} 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)
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
}
17 changes: 17 additions & 0 deletions cli/check_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cli

import (
"testing"
)

func TestCheckConfig(t *testing.T) {
err := CheckConfig([]string{"testdata/conf.good.yml"})
if err != nil {
t.Fatalf("Checking valid config file failed with: %v", err)
}

err = CheckConfig([]string{"testdata/conf.bad.yml"})
if err == nil {
t.Fatalf("Failed to detect invalid file.")
}
}
1 change: 1 addition & 0 deletions cli/testdata/conf.bad.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BAD
11 changes: 11 additions & 0 deletions cli/testdata/conf.good.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
global:
smtp_smarthost: 'localhost:25'

templates:
- '/etc/alertmanager/template/*.tmpl'

route:
receiver: default

receivers:
- name: default
Loading

0 comments on commit 7aa8955

Please sign in to comment.