Prometheus告警记录持久化
Prometheus将基于告警规则生成的告警存储为时间序列,不会将Alertmanager的告警信息持久化存储,
那么针对历史告警的检索、统计等需求就无法实现。因此需要一种持久化机制用于存储历史告警信息,
本文主要探究基于alertmanager告警的开源持久化方案。
涉及技术栈:Prometheus+Alertmanager+AlertSnitch+Grafana
1.alertsnitch下载
下载地址:https://gitlab.com/yakshaving.art/alertsnitch
2.go安装
go的安装请参考linux环境下go安装-CSDN博客,我使用的go版本为:1.21.0
3.go编译安装
tar -xzvf alertsnitch-0.2.1.tar.gz
go env -w GOPROXY=https://goproxy.cn #该步骤是切换国内代理,不然github可能很多包无法下载
cd alertsnitch-0.2.1
go install#成功与否见下,成功后复制该alertsnitch命令到环境变量
cp -r $GOPATH/bin/alertsnitch /usr/local/bin
[root@monitor12011 go]# ll /root/go/tagert/
total 0
drwxr-xr-x. 2 root root 25 Dec 24 10:48 bin
drwxr-xr-x. 3 root root 17 Dec 23 16:30 pkg
[root@monitor12011 go]# ll /root/go/tagert/bin/
total 11756
-rwxr-xr-x. 1 root root 12035858 Dec 24 10:46 alertsnitch
4.初始化mysql数据库
# 将变量写入到/etc/profile中
cat >>/etc/profile<<'EOF'
# alertsnitch
export MYSQL_ROOT_PASSWORD=123456
export MYSQL_DATABASE=alertsnitch
export ALERTSNITCH_ADDR=xx.xx.xx.xx:9567
export ALERTSNITCH_BACKEND="mysql"
export ALERTSNITCH_DSN="xxxx:xxx@tcp(xx.xx.xx.xx:3306)/alertsnitch"
EOF
- ALERTSNITCH_DSN:用户名:密码@协议类型(数据库所在主机地址:数据库端口)/数据库名称
- MYSQL_ROOT_PASSWORD:数据库连接密码
- MYSQL_DATABASE:数据库名称
- ALERTSNITCH_ADDR:alertsnitch监听IP地址和端口
vi /root/alertsnitch/alertsnitch-0.2.1/script.d/bootstrap_mysql.sh
# 将--host配置值改为真实数据库ip
# 将--user=配置值改为真实数据库用户
# 将sql文件路径改为绝对路径
# 执行bootstrap_mysql.sh脚本
[root@monitor12011 script.d]# sh /root/alertsnitch/alertsnitch-0.2.1/script.d/bootstrap_mysql.sh
Creating DB
Creating bootstrapped model
Applying fingerprint model update
Done creating model
# 数据库验证
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| alertsnitch |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
8 rows in set (0.00 sec)mysql> use alertsnitch;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> show tables;
+-----------------------+
| Tables_in_alertsnitch |
+-----------------------+
| alert |
| alertannotation |
| alertgroup |
| alertlabel |
| commonannotation |
| commonlabel |
| grouplabel |
| model |
+-----------------------+
8 rows in set (0.00 sec)
5.alertmanager配置
vi /xxx/xx/xx/alertmanager.yml
# 告警路由配置
route:group_by:- instancegroup_interval: 5mgroup_wait: 30sreceiver: teamworkrepeat_interval: 1hroutes:- continue: truegroup_interval: 1mgroup_wait: 30sreceiver: alertsnitchrepeat_interval: 10m
# 告警接收人配置
receivers:
- name: alertsnitchwebhook_configs:- url: http://xx.xx.xx.xx:9567/webhook
6.启动alertsnitch
vi /etc/systemd/system/alertsnitch.service
# 添加以下内容
[Unit]
Description=Alertsnitch Server
After=network.target[Service]
Restart=on-failure
WorkingDirectory=/root/alertsnitch
ExecStart=alertsnitch -database-backend="mysql" -listen.address="xx.xx.xx.xx:9567" -dsn="xxx:xx@tcp(xx.xx.xx.xx:3306)/alertsnitch"[Install]
WantedBy=multi-user.targetsystemctl start alertsnitch # 启动
systemctl status alertsnitch # 状态
systemctl enable alertsnitch # 开机自启
7.grafana接入
7.1 数据源创建
7.2 导入仪表板
模板id:15833
8.参考
- Prometheus告警记录持久化_alertsnitch
- Alertmanager告警数据持久,alertsnitch安装