...

Source file src/github.com/prometheus/alertmanager/cli/check_config.go

Documentation: github.com/prometheus/alertmanager/cli

     1  // Copyright 2018 Prometheus Team
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package cli
    15  
    16  import (
    17  	"fmt"
    18  	"os"
    19  
    20  	"gopkg.in/alecthomas/kingpin.v2"
    21  
    22  	"github.com/prometheus/alertmanager/config"
    23  	"github.com/prometheus/alertmanager/template"
    24  )
    25  
    26  // TODO: This can just be a type that is []string, doesn't have to be a struct
    27  type checkConfigCmd struct {
    28  	files []string
    29  }
    30  
    31  const checkConfigHelp = `Validate alertmanager config files
    32  
    33  Will validate the syntax and schema for alertmanager config file
    34  and associated templates. Non existing templates will not trigger
    35  errors.
    36  `
    37  
    38  func configureCheckConfigCmd(app *kingpin.Application) {
    39  	var (
    40  		c        = &checkConfigCmd{}
    41  		checkCmd = app.Command("check-config", checkConfigHelp)
    42  	)
    43  	checkCmd.Arg("check-files", "Files to be validated").ExistingFilesVar(&c.files)
    44  	checkCmd.Action(c.checkConfig)
    45  }
    46  
    47  func (c *checkConfigCmd) checkConfig(ctx *kingpin.ParseContext) error {
    48  	return CheckConfig(c.files)
    49  }
    50  
    51  func CheckConfig(args []string) error {
    52  	if len(args) == 0 {
    53  		stat, err := os.Stdin.Stat()
    54  		if err != nil {
    55  			kingpin.Fatalf("Failed to stat standard input: %v", err)
    56  		}
    57  		if (stat.Mode() & os.ModeCharDevice) != 0 {
    58  			kingpin.Fatalf("Failed to read from standard input")
    59  		}
    60  		args = []string{os.Stdin.Name()}
    61  	}
    62  
    63  	failed := 0
    64  
    65  	for _, arg := range args {
    66  		fmt.Printf("Checking '%s'", arg)
    67  		cfg, err := config.LoadFile(arg)
    68  		if err != nil {
    69  			fmt.Printf("  FAILED: %s\n", err)
    70  			failed++
    71  		} else {
    72  			fmt.Printf("  SUCCESS\n")
    73  		}
    74  
    75  		if cfg != nil {
    76  			fmt.Println("Found:")
    77  			if cfg.Global != nil {
    78  				fmt.Println(" - global config")
    79  			}
    80  			if cfg.Route != nil {
    81  				fmt.Println(" - route")
    82  			}
    83  			fmt.Printf(" - %d inhibit rules\n", len(cfg.InhibitRules))
    84  			fmt.Printf(" - %d receivers\n", len(cfg.Receivers))
    85  			fmt.Printf(" - %d templates\n", len(cfg.Templates))
    86  			if len(cfg.Templates) > 0 {
    87  				_, err = template.FromGlobs(cfg.Templates...)
    88  				if err != nil {
    89  					fmt.Printf("  FAILED: %s\n", err)
    90  					failed++
    91  				} else {
    92  					fmt.Printf("  SUCCESS\n")
    93  				}
    94  			}
    95  		}
    96  		fmt.Printf("\n")
    97  	}
    98  	if failed > 0 {
    99  		return fmt.Errorf("failed to validate %d file(s)", failed)
   100  	}
   101  	return nil
   102  }
   103  

View as plain text