背景
在每天晚上21点到凌晨1点,内部会有数据任务在执行,导致相关机器的内存会出现突增,触发电话告警。为了解决这个问题,决定使用amtool工具进行定时静默操作。
环境说明
组件名称 | 组件版本 |
---|---|
alertmanager | v0.27.0 |
安装Alertmanager
- 下载合适的压缩包文件
wget -O /data/ https://github.com/prometheus/alertmanager/releases/tag/v0.27.0
- 解压
tar -zxf alertmanager-0.27.0.linux-amd64.tar.gz
- 启动alertmanager
/data/alertmanager-0.27.0/alertmanager
--config.file=alertmanager.yml #指定配置文件路径
--web.listen-address=":9093" #指定web端口
--log.level=debug #指定日志级别
--storage.path=data/ #指定数据存储目录
--web.route-prefix="/alertmanager" #指定web访问路径
使用amtool工具
创建配置文件
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
[root@xxx alertmanager] cat /etc/amtool/config.yml
alertmanager.url: "http://127.0.0.1:9093/alertmanager" #alertmanager地址
author: "auto" #这里配置的标识silence的作者
comment_required: true #是否需要添加描述内容
output: "simple" #设置输出格式
告警操作
手动创建告警
[root@xxxx alertmanager]# ./amtool alert add foo instance=bar --annotation=summary='summary of the alert' --annotation=description='description of the alert'
效果查看
查看告警列表
[root@xxx alertmanager]# ./amtool alert query
Alertname Starts At Summary State
foo 2025-08-04 16:57:50 +0800 summary of the alert active
带有条件的告警列表
[root@xxx alertmanager]# ./amtool alert query 'alertname=fo'
[root@xxxx alertmanager]# ./amtool alert query 'alertname=foo'
效果查看
silence操作
创建silence
--comment
:增加描述信息
-h
:指定静默事件
[root@xxxx alertmanager]# ./amtool silence add --comment="hello world " -d 2h alertname="foo" instance="bar"
cf90c070-f3f8-4bdc-a4ad-bd38692d4ca4
效果查看
取消静默
[root@xxxx alertmanager]# ./amtool silence expire cf90c070-f3f8-4bdc-a4ad-bd38692d4ca4 #可以影响多个
查看静默
[root@xxxx alertmanager]# ./amtool silence query
ID是这个silence的唯一ID,Matchers表示的匹配规则标识,End At表示结束时间,Create By表示创建人,Comment表示描述信息。
实战
告警的详细信息
设置定时静默效果
- 配置Crontab
0 21 * * * /data/alertmanager/amtool silence add --comment="屏蔽内存告警 " -d 1h alertname="host_memory_use_warning" idc="awsxxx" instance="xxx" namehost="xxxxx-prod-01" severity="warning"
需要注意的是:Matchers尽量全部匹配上,否则可能会有不生效的情况。
- 查看效果
可以看到,silence已经成功创建了,影响的alert信息也在下方展示了,生效时间也与配置的相同。
结语
至此,我们可以通过amtool工具进行alertmanager的定时silence,防止一些运维任务导致的告警,还要给运维打电话,可以美美睡觉了。