作業メモです。
以下を参考に手元の Mac 上に構築した Prometheus 及び Alertmanager について promtool
及び amtool
を使って見た時のメモです。
promtool/amtoolでPrometheus/Alertmanagerの設定をチェックする
環境
- 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
OK。
自動的に rules.yml
もチェックしてくれてそうだ。
check 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 check-config alertmanager.yml
Checking 'alertmanager.yml' SUCCESS
Found:
- global config
- route
- 0 inhibit rules
- 1 receivers
- 0 templates
OK.
試しに他のコマンドも使ってみる。
./amtool alert
amtool: error: required flag --alertmanager.url not provided
失敗。
$HOME/.config/amtool/config.yml
or /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
成功。
Silence も確認。
# 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 させる。
# 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
OK
メモ
Proxy 経由で amtool
を使いたい、という希望が合ったが、以下を見るとマージされているが、まだリリース前の模様。