-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
31 changed files
with
1,915 additions
and
1,011 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
BAD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.