使用promtool和amtool时的备注

这是一份作业备忘录。
以下是关于我在我的 Mac 上构建的 Prometheus 和 Alertmanager 的相关信息,使用 promtool 和 amtool 进行测试时的备忘录。

使用promtool/amtool检查Prometheus/Alertmanager的配置

amtool的用法- Prometheus文档

环境

    • Mac OS

 

    • Prometheus 2.33.2

 

    Alertmanager 0.23.0

使用 promtool。

当您下载Prometheus二进制文件后,可以一起使用。(使用Docker也可以,就像参考文章中一样)

$ls
LICENSE           consoles          prometheus.yml
NOTICE            data              promtool
console_libraries prometheus        rules.yml

看下帮助的话,就是这样的感觉。

 ./promtool --help
usage: promtool [<flags>] <command> [<args> ...]

Tooling for the Prometheus monitoring system.

Flags:
  -h, --help                 Show context-sensitive help (also try --help-long
                             and --help-man).
      --version              Show application version.
      --enable-feature= ...  Comma separated feature names to enable (only
                             PromQL related). See
                             https://prometheus.io/docs/prometheus/latest/feature_flags/
                             for the options and more details.

Commands:
  help [<command>...]
    Show help.

  check service-discovery [<flags>] <config-file> <job>
    Perform service discovery for the given job name and report the results,
    including relabeling.

  check config [<flags>] <config-files>...
    Check if the config files are valid or not.

  check web-config <web-config-files>...
    Check if the web config files are valid or not.

  check rules <rule-files>...
    Check if the rule files are valid or not.

  check metrics
    Pass Prometheus metrics over stdin to lint them for consistency and
    correctness.

    examples:

    $ cat metrics.prom | promtool check metrics

    $ curl -s http://localhost:9090/metrics | promtool check metrics

  query instant [<flags>] <server> <expr>
    Run instant query.

  query range [<flags>] <server> <expr>
    Run range query.

  query series --match=MATCH [<flags>] <server>
    Run series query.

  query labels [<flags>] <server> <name>
    Run labels query.

  debug pprof <server>
    Fetch profiling debug information.

  debug metrics <server>
    Fetch metrics debug information.

  debug all <server>
    Fetch all debug information.

  test rules <test-rule-file>...
    Unit tests for rules.

  tsdb bench write [<flags>] [<file>]
    Run a write performance benchmark.

  tsdb analyze [<flags>] [<db path>] [<block id>]
    Analyze churn, label pair cardinality and compaction efficiency.

  tsdb list [<flags>] [<db path>]
    List tsdb blocks.

  tsdb dump [<flags>] [<db path>]
    Dump samples from a TSDB.

  tsdb create-blocks-from openmetrics <input file> [<output directory>]
    Import samples from OpenMetrics input and produce TSDB blocks. Please refer
    to the storage docs for more details.

  tsdb create-blocks-from rules --start=START [<flags>] <rule-files>...
    Create blocks of data for new recording rules.

试着用 check config 来执行规则文件和配置文件的检查。

./promtool check config prometheus.yml
Checking prometheus.yml
  SUCCESS: 1 rule files found
 SUCCESS: prometheus.yml is valid prometheus config file syntax

Checking rules.yml
  SUCCESS: 1 rules found

好的。
看起来它还会自动检查rules.yml。
可以通过检查rules来检查单个规则。

 ./promtool check rules rules.yml
Checking rules.yml
  SUCCESS: 1 rules found

这次虽然不会尝试,但听说也可以进行单元测试。

规则的单元测试

普罗米修斯:对警报规则进行单元测试。

Prometheus文件中描述的由Prometheus执行的规则单元测试。

使用 amtool

通过使用附带在Alertmanager中的amtool工具,可以检查Alertmanager的配置文件。

 ls
LICENSE               alertmanager.yml      data
NOTICE                alertmanager.yml.back
alertmanager          amtool

看看有没有帮助。

./amtool --help
usage: amtool [<flags>] <command> [<args> ...]

View and modify the current Alertmanager state.

Config File: The alertmanager tool will read a config file in YAML format from
one of two default config locations: $HOME/.config/amtool/config.yml or
/etc/amtool/config.yml

All flags can be given in the config file, but the following are the suited for
static configuration:

  alertmanager.url
        Set a default alertmanager url for each request

  author
        Set a default author value for new silences. If this argument is not
        specified then the username will be used

  require-comment
        Bool, whether to require a comment on silence creation. Defaults to true

  output
        Set a default output type. Options are (simple, extended, json)

  date.format
        Sets the output format for dates. Defaults to "2006-01-02 15:04:05 MST"

  tls.insecure.skip.verify
        Skips TLS certificate verification for all HTTPS requests.
        Defaults to false.

Flags:
  -h, --help           Show context-sensitive help (also try --help-long and
                       --help-man).
      --date.format="2006-01-02 15:04:05 MST"
                       Format of date output
  -v, --verbose        Verbose running information
      --alertmanager.url=ALERTMANAGER.URL
                       Alertmanager to talk to
  -o, --output=simple  Output formatter (simple, extended, json)
      --timeout=30s    Timeout for the executed command
      --tls.insecure.skip.verify
                       Skip TLS certificate verification
      --version-check  Check alertmanager version. Use --no-version-check to
                       disable.
      --version        Show application version.

Commands:
  help [<command>...]
  alert
    query* [<flags>] [<matcher-groups>...]
    add [<flags>] [<labels>...]
  silence
    add [<flags>] [<matcher-groups>...]
    expire [<silence-ids>...]
    import [<flags>] [<input-file>]
    query* [<flags>] [<matcher-groups>...]
    update [<flags>] [<update-ids>...]
  check-config [<check-files>...]
  cluster
    show*
  config
    show*
    routes [<flags>]
      show*
      test [<flags>] [<labels>...]
  template
    render --template.glob=TEMPLATE.GLOB --template.text=TEMPLATE.TEXT [<flags>]

alertmanager 的使用方式在其 GitHub 的 README 文件中有详细介绍。

amtool可以用于处理ASN.1信息,支持各种ASN.1编码和解码操作。

试试检查一下警报文件。

 ./amtool check-config alertmanager.yml
Checking 'alertmanager.yml'  SUCCESS
Found:
 - global config
 - route
 - 0 inhibit rules
 - 1 receivers
 - 0 templates

好的。

试着尝试使用其他命令吧。

./amtool alert
amtool: error: required flag --alertmanager.url not provided

似乎需要配置 $HOME/.config/amtool/config.yml 或 /etc/amtool/config.yml 才能成功。

mkdir -p $HOME/.config/amtool/
echo 'alertmanager.url: "http://localhost:9093"' >> $HOME/.config/amtool/config.yml

# View all currently firing alerts
 ./amtool alert
Alertname       Starts At                Summary  State
DeadMansSwitch  2022-02-11 22:29:38 UTC  テストアラート  active

成功。
沉默也得到确认。

# View silences
./amtool silence query
ID  Matchers  Ends At  Created By  Comment

# Silence an alert
./amtool silence add alertname=DeadMansSwitch

# コメントがないのでエラー
 ./amtool silence add alertname=DeadMansSwitch
amtool: error: comment required by config

 # help をチェック
 ./amtool silence add --help

# Silence
 ./amtool silence add alertname=DeadMansSwitch -c fromCLI
49965d0a-1b10-4110-b23f-f0df0dc0078e

# OK
./amtool silence query
ID                                    Matchers                    Ends At                  Created By  Comment
49965d0a-1b10-4110-b23f-f0df0dc0078e  alertname="DeadMansSwitch"  2022-02-14 22:05:27 UTC  toshihirock     fromCLI

# デフォルトで1時間後に expire になる模様
date -u
2022年 2月14日 月曜日 21時07分33秒 UTC

过期

# Expire
./amtool silence expire 49965d0a-1b10-4110-b23f-f0df0dc0078e

# Expire したので表示されない
 ./amtool silence query
ID  Matchers  Ends At  Created By  Comment

# Alert に表示される
./amtool alert
Alertname       Starts At                Summary  State
DeadMansSwitch  2022-02-11 22:29:38 UTC  テストアラート  active

记事

虽然有希望通过代理使用amtool,但从下面的情况看,它似乎已经与主分支合并,但尚未发布。

amtool 支持使用 http_config 访问 alertmanager

广告
将在 10 秒后关闭
bannerAds