2018-05-14 12:36:24 +00:00
|
|
|
// Copyright 2018 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-09-07 08:58:58 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-07-09 17:27:04 +00:00
|
|
|
"os"
|
2017-09-07 08:58:58 +00:00
|
|
|
|
2022-03-25 16:59:51 +00:00
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-04-24 07:35:15 +00:00
|
|
|
const checkConfigHelp = `Validate alertmanager config files
|
|
|
|
|
|
|
|
Will validate the syntax and schema for alertmanager config file
|
|
|
|
and associated templates. Non existing templates will not trigger
|
|
|
|
errors.
|
|
|
|
`
|
|
|
|
|
|
|
|
func configureCheckConfigCmd(app *kingpin.Application) {
|
2018-04-13 11:34:16 +00:00
|
|
|
var (
|
|
|
|
c = &checkConfigCmd{}
|
2018-04-24 07:35:15 +00:00
|
|
|
checkCmd = app.Command("check-config", checkConfigHelp)
|
2018-04-13 11:34:16 +00:00
|
|
|
)
|
|
|
|
checkCmd.Arg("check-files", "Files to be validated").ExistingFilesVar(&c.files)
|
|
|
|
checkCmd.Action(c.checkConfig)
|
2017-09-07 08:58:58 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 07:35:15 +00:00
|
|
|
func (c *checkConfigCmd) checkConfig(ctx *kingpin.ParseContext) error {
|
2018-04-13 11:34:16 +00:00
|
|
|
return CheckConfig(c.files)
|
2017-09-07 08:58:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func CheckConfig(args []string) error {
|
2018-07-09 17:27:04 +00:00
|
|
|
if len(args) == 0 {
|
2018-08-05 13:38:25 +00:00
|
|
|
stat, err := os.Stdin.Stat()
|
|
|
|
if err != nil {
|
|
|
|
kingpin.Fatalf("Failed to stat standard input: %v", err)
|
|
|
|
}
|
2018-07-09 17:27:04 +00:00
|
|
|
if (stat.Mode() & os.ModeCharDevice) != 0 {
|
|
|
|
kingpin.Fatalf("Failed to read from standard input")
|
|
|
|
}
|
|
|
|
args = []string{os.Stdin.Name()}
|
|
|
|
}
|
|
|
|
|
2017-09-07 08:58:58 +00:00
|
|
|
failed := 0
|
|
|
|
|
|
|
|
for _, arg := range args {
|
|
|
|
fmt.Printf("Checking '%s'", arg)
|
2019-08-26 12:01:40 +00:00
|
|
|
cfg, err := config.LoadFile(arg)
|
2017-09-07 08:58:58 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2018-05-15 07:17:51 +00:00
|
|
|
if cfg != nil {
|
|
|
|
fmt.Println("Found:")
|
|
|
|
if cfg.Global != nil {
|
|
|
|
fmt.Println(" - global config")
|
|
|
|
}
|
|
|
|
if cfg.Route != nil {
|
|
|
|
fmt.Println(" - route")
|
|
|
|
}
|
|
|
|
fmt.Printf(" - %d inhibit rules\n", len(cfg.InhibitRules))
|
|
|
|
fmt.Printf(" - %d receivers\n", len(cfg.Receivers))
|
|
|
|
fmt.Printf(" - %d templates\n", len(cfg.Templates))
|
|
|
|
if len(cfg.Templates) > 0 {
|
|
|
|
_, err = template.FromGlobs(cfg.Templates...)
|
2017-09-07 08:58:58 +00:00
|
|
|
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
|
|
|
|
}
|