2015-06-18 02:13:28 -07:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
|
|
|
"gopkg.in/yaml.v2"
|
2017-06-13 22:43:00 -07:00
|
|
|
|
2018-01-11 07:10:25 -08:00
|
|
|
config_util "github.com/prometheus/common/config"
|
2017-06-13 22:43:00 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2016-05-05 04:46:51 -07:00
|
|
|
"github.com/prometheus/common/version"
|
2015-06-18 02:13:28 -07:00
|
|
|
"github.com/prometheus/prometheus/config"
|
2017-06-13 22:43:00 -07:00
|
|
|
"github.com/prometheus/prometheus/pkg/rulefmt"
|
2015-06-18 02:13:28 -07:00
|
|
|
"github.com/prometheus/prometheus/promql"
|
2017-04-13 14:53:41 -07:00
|
|
|
"github.com/prometheus/prometheus/util/promlint"
|
2015-06-18 02:13:28 -07:00
|
|
|
)
|
|
|
|
|
2017-06-21 04:32:04 -07:00
|
|
|
func main() {
|
|
|
|
app := kingpin.New(filepath.Base(os.Args[0]), "Tooling for the Prometheus monitoring system.")
|
|
|
|
app.Version(version.Print("promtool"))
|
|
|
|
app.HelpFlag.Short('h')
|
|
|
|
|
|
|
|
checkCmd := app.Command("check", "Check the resources for validity.")
|
|
|
|
|
|
|
|
checkConfigCmd := checkCmd.Command("config", "Check if the config files are valid or not.")
|
|
|
|
configFiles := checkConfigCmd.Arg(
|
|
|
|
"config-files",
|
|
|
|
"The config files to check.",
|
|
|
|
).Required().ExistingFiles()
|
|
|
|
|
|
|
|
checkRulesCmd := checkCmd.Command("rules", "Check if the rule files are valid or not.")
|
|
|
|
ruleFiles := checkRulesCmd.Arg(
|
|
|
|
"rule-files",
|
|
|
|
"The rule files to check.",
|
|
|
|
).Required().ExistingFiles()
|
|
|
|
|
|
|
|
checkMetricsCmd := checkCmd.Command("metrics", checkMetricsUsage)
|
|
|
|
|
|
|
|
updateCmd := app.Command("update", "Update the resources to newer formats.")
|
|
|
|
updateRulesCmd := updateCmd.Command("rules", "Update rules from the 1.x to 2.x format.")
|
|
|
|
ruleFilesUp := updateRulesCmd.Arg("rule-files", "The rule files to update.").Required().ExistingFiles()
|
|
|
|
|
|
|
|
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
|
|
|
|
case checkConfigCmd.FullCommand():
|
|
|
|
os.Exit(CheckConfig(*configFiles...))
|
|
|
|
|
|
|
|
case checkRulesCmd.FullCommand():
|
|
|
|
os.Exit(CheckRules(*ruleFiles...))
|
|
|
|
|
|
|
|
case checkMetricsCmd.FullCommand():
|
|
|
|
os.Exit(CheckMetrics())
|
|
|
|
|
|
|
|
case updateRulesCmd.FullCommand():
|
|
|
|
os.Exit(UpdateRules(*ruleFilesUp...))
|
|
|
|
|
2015-06-18 02:13:28 -07:00
|
|
|
}
|
2017-06-21 04:32:04 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckConfig validates configuration files.
|
|
|
|
func CheckConfig(files ...string) int {
|
2015-06-18 02:13:28 -07:00
|
|
|
failed := false
|
|
|
|
|
2017-06-21 04:32:04 -07:00
|
|
|
for _, f := range files {
|
|
|
|
ruleFiles, err := checkConfig(f)
|
2015-06-18 02:13:28 -07:00
|
|
|
if err != nil {
|
2017-06-21 04:32:04 -07:00
|
|
|
fmt.Fprintln(os.Stderr, " FAILED:", err)
|
2015-06-18 02:13:28 -07:00
|
|
|
failed = true
|
|
|
|
} else {
|
2017-06-21 04:32:04 -07:00
|
|
|
fmt.Printf(" SUCCESS: %d rule files found\n", len(ruleFiles))
|
2015-06-18 02:13:28 -07:00
|
|
|
}
|
2017-06-21 04:32:04 -07:00
|
|
|
fmt.Println()
|
2015-06-18 02:13:28 -07:00
|
|
|
|
|
|
|
for _, rf := range ruleFiles {
|
2017-06-21 04:32:04 -07:00
|
|
|
if n, err := checkRules(rf); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, " FAILED:", err)
|
2015-06-18 02:13:28 -07:00
|
|
|
failed = true
|
|
|
|
} else {
|
2017-06-21 04:32:04 -07:00
|
|
|
fmt.Printf(" SUCCESS: %d rules found\n", n)
|
2015-06-18 02:13:28 -07:00
|
|
|
}
|
2017-06-21 04:32:04 -07:00
|
|
|
fmt.Println()
|
2015-06-18 02:13:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if failed {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-09-09 05:08:05 -07:00
|
|
|
func checkFileExists(fn string) error {
|
|
|
|
// Nothing set, nothing to error on.
|
|
|
|
if fn == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
_, err := os.Stat(fn)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-21 04:32:04 -07:00
|
|
|
func checkConfig(filename string) ([]string, error) {
|
|
|
|
fmt.Println("Checking", filename)
|
2015-06-18 02:13:28 -07:00
|
|
|
|
2015-08-05 09:30:37 -07:00
|
|
|
cfg, err := config.LoadFile(filename)
|
2015-06-18 02:13:28 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ruleFiles []string
|
|
|
|
for _, rf := range cfg.RuleFiles {
|
|
|
|
rfs, err := filepath.Glob(rf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-05 09:30:37 -07:00
|
|
|
// If an explicit file was given, error if it is not accessible.
|
|
|
|
if !strings.Contains(rf, "*") {
|
|
|
|
if len(rfs) == 0 {
|
|
|
|
return nil, fmt.Errorf("%q does not point to an existing file", rf)
|
|
|
|
}
|
2015-09-09 05:08:05 -07:00
|
|
|
if err := checkFileExists(rfs[0]); err != nil {
|
2015-08-05 09:30:37 -07:00
|
|
|
return nil, fmt.Errorf("error checking rule file %q: %s", rfs[0], err)
|
|
|
|
}
|
2015-06-18 02:13:28 -07:00
|
|
|
}
|
|
|
|
ruleFiles = append(ruleFiles, rfs...)
|
|
|
|
}
|
|
|
|
|
2015-08-05 09:30:37 -07:00
|
|
|
for _, scfg := range cfg.ScrapeConfigs {
|
2016-11-23 03:41:19 -08:00
|
|
|
if err := checkFileExists(scfg.HTTPClientConfig.BearerTokenFile); err != nil {
|
|
|
|
return nil, fmt.Errorf("error checking bearer token file %q: %s", scfg.HTTPClientConfig.BearerTokenFile, err)
|
2015-08-05 09:30:37 -07:00
|
|
|
}
|
|
|
|
|
2016-11-23 03:41:19 -08:00
|
|
|
if err := checkTLSConfig(scfg.HTTPClientConfig.TLSConfig); err != nil {
|
2015-09-09 05:08:05 -07:00
|
|
|
return nil, err
|
2015-09-06 16:07:44 -07:00
|
|
|
}
|
|
|
|
|
2016-11-23 03:41:19 -08:00
|
|
|
for _, kd := range scfg.ServiceDiscoveryConfig.KubernetesSDConfigs {
|
2015-09-09 05:08:05 -07:00
|
|
|
if err := checkTLSConfig(kd.TLSConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-05 09:30:37 -07:00
|
|
|
}
|
2017-08-22 15:25:30 -07:00
|
|
|
|
|
|
|
for _, filesd := range scfg.ServiceDiscoveryConfig.FileSDConfigs {
|
|
|
|
for _, file := range filesd.Files {
|
|
|
|
files, err := filepath.Glob(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(files) != 0 {
|
|
|
|
// There was at least one match for the glob and we can assume checkFileExists
|
|
|
|
// for all matches would pass, we can continue the loop.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Printf(" WARNING: file %q for file_sd in scrape job %q does not exist\n", file, scfg.JobName)
|
|
|
|
}
|
|
|
|
}
|
2015-08-05 09:30:37 -07:00
|
|
|
}
|
|
|
|
|
2015-06-18 02:13:28 -07:00
|
|
|
return ruleFiles, nil
|
|
|
|
}
|
|
|
|
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
func checkTLSConfig(tlsConfig config_util.TLSConfig) error {
|
2015-09-09 05:08:05 -07:00
|
|
|
if err := checkFileExists(tlsConfig.CertFile); err != nil {
|
|
|
|
return fmt.Errorf("error checking client cert file %q: %s", tlsConfig.CertFile, err)
|
|
|
|
}
|
|
|
|
if err := checkFileExists(tlsConfig.KeyFile); err != nil {
|
|
|
|
return fmt.Errorf("error checking client key file %q: %s", tlsConfig.KeyFile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tlsConfig.CertFile) > 0 && len(tlsConfig.KeyFile) == 0 {
|
|
|
|
return fmt.Errorf("client cert file %q specified without client key file", tlsConfig.CertFile)
|
|
|
|
}
|
|
|
|
if len(tlsConfig.KeyFile) > 0 && len(tlsConfig.CertFile) == 0 {
|
|
|
|
return fmt.Errorf("client key file %q specified without client cert file", tlsConfig.KeyFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-21 04:32:04 -07:00
|
|
|
// CheckRules validates rule files.
|
|
|
|
func CheckRules(files ...string) int {
|
2015-06-18 02:13:28 -07:00
|
|
|
failed := false
|
|
|
|
|
2017-06-21 04:32:04 -07:00
|
|
|
for _, f := range files {
|
|
|
|
if n, errs := checkRules(f); errs != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, " FAILED:")
|
2017-06-16 04:14:33 -07:00
|
|
|
for _, e := range errs {
|
2017-06-21 04:32:04 -07:00
|
|
|
fmt.Fprintln(os.Stderr, e.Error())
|
2017-06-16 04:14:33 -07:00
|
|
|
}
|
2015-06-18 02:13:28 -07:00
|
|
|
failed = true
|
|
|
|
} else {
|
2017-06-21 04:32:04 -07:00
|
|
|
fmt.Printf(" SUCCESS: %d rules found\n", n)
|
2015-06-18 02:13:28 -07:00
|
|
|
}
|
2017-06-21 04:32:04 -07:00
|
|
|
fmt.Println()
|
2015-06-18 02:13:28 -07:00
|
|
|
}
|
|
|
|
if failed {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-06-21 04:32:04 -07:00
|
|
|
func checkRules(filename string) (int, []error) {
|
|
|
|
fmt.Println("Checking", filename)
|
2015-06-18 02:13:28 -07:00
|
|
|
|
2017-06-14 01:02:26 -07:00
|
|
|
rgs, errs := rulefmt.ParseFile(filename)
|
|
|
|
if errs != nil {
|
2017-06-16 04:14:33 -07:00
|
|
|
return 0, errs
|
2015-06-18 02:13:28 -07:00
|
|
|
}
|
|
|
|
|
2017-06-14 01:02:26 -07:00
|
|
|
numRules := 0
|
|
|
|
for _, rg := range rgs.Groups {
|
|
|
|
numRules += len(rg.Rules)
|
2015-06-18 02:13:28 -07:00
|
|
|
}
|
2017-06-14 01:02:26 -07:00
|
|
|
|
|
|
|
return numRules, nil
|
2015-06-18 02:13:28 -07:00
|
|
|
}
|
|
|
|
|
2017-06-21 04:32:04 -07:00
|
|
|
// UpdateRules updates the rule files.
|
|
|
|
func UpdateRules(files ...string) int {
|
2017-06-13 22:43:00 -07:00
|
|
|
failed := false
|
|
|
|
|
2017-06-21 04:32:04 -07:00
|
|
|
for _, f := range files {
|
|
|
|
if err := updateRules(f); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, " FAILED:", err)
|
2017-06-13 22:43:00 -07:00
|
|
|
failed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if failed {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-06-21 04:32:04 -07:00
|
|
|
func updateRules(filename string) error {
|
|
|
|
fmt.Println("Updating", filename)
|
2017-06-13 22:43:00 -07:00
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rules, err := promql.ParseStmts(string(content))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
yamlRG := &rulefmt.RuleGroups{
|
|
|
|
Groups: []rulefmt.RuleGroup{{
|
|
|
|
Name: filename,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
yamlRules := make([]rulefmt.Rule, 0, len(rules))
|
|
|
|
|
|
|
|
for _, rule := range rules {
|
|
|
|
switch r := rule.(type) {
|
|
|
|
case *promql.AlertStmt:
|
|
|
|
yamlRules = append(yamlRules, rulefmt.Rule{
|
|
|
|
Alert: r.Name,
|
|
|
|
Expr: r.Expr.String(),
|
2017-06-15 22:16:21 -07:00
|
|
|
For: model.Duration(r.Duration),
|
2017-06-13 22:43:00 -07:00
|
|
|
Labels: r.Labels.Map(),
|
|
|
|
Annotations: r.Annotations.Map(),
|
|
|
|
})
|
|
|
|
case *promql.RecordStmt:
|
|
|
|
yamlRules = append(yamlRules, rulefmt.Rule{
|
|
|
|
Record: r.Name,
|
|
|
|
Expr: r.Expr.String(),
|
|
|
|
Labels: r.Labels.Map(),
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
panic("unknown statement type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
yamlRG.Groups[0].Rules = yamlRules
|
|
|
|
y, err := yaml.Marshal(yamlRG)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-21 06:08:37 -07:00
|
|
|
return ioutil.WriteFile(filename+".yml", y, 0666)
|
2017-06-13 22:43:00 -07:00
|
|
|
}
|
|
|
|
|
2017-04-13 14:53:41 -07:00
|
|
|
var checkMetricsUsage = strings.TrimSpace(`
|
|
|
|
Pass Prometheus metrics over stdin to lint them for consistency and correctness.
|
|
|
|
|
|
|
|
examples:
|
|
|
|
|
2017-06-21 04:32:04 -07:00
|
|
|
$ cat metrics.prom | promtool check metrics
|
2017-04-13 14:53:41 -07:00
|
|
|
|
2017-06-21 04:32:04 -07:00
|
|
|
$ curl -s http://localhost:9090/metrics | promtool check metrics
|
|
|
|
`)
|
2017-04-13 14:53:41 -07:00
|
|
|
|
2017-06-21 04:32:04 -07:00
|
|
|
// CheckMetrics performs a linting pass on input metrics.
|
|
|
|
func CheckMetrics() int {
|
2017-04-13 14:53:41 -07:00
|
|
|
l := promlint.New(os.Stdin)
|
|
|
|
problems, err := l.Lint()
|
|
|
|
if err != nil {
|
2017-06-21 04:32:04 -07:00
|
|
|
fmt.Fprintln(os.Stderr, "error while linting:", err)
|
2017-04-13 14:53:41 -07:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range problems {
|
2017-06-21 04:32:04 -07:00
|
|
|
fmt.Fprintln(os.Stderr, p.Metric, p.Text)
|
2017-04-13 14:53:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(problems) > 0 {
|
|
|
|
return 3
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|