Alertmanager告警数据持久,alertsnitch安装

【1】alertsnitch 安装

(1)golang、alertsnitch 下载

golang:https://go.dev/dl/https://golang.google.cn/dl/

  

alertsnitch 下载:https://gitlab.com/yakshaving.art/alertsnitchhttps://github.com/yakshaving-art/alertsnitch

  

(2)使用GO编译安装 alertsnitch

步骤一:配置GO环境

cd /data/software/alertsnitch

#go 文件拷贝到该文件夹
tar -zxvf go1.19.4.linux.amd64.tar.gz

# 配置 go 环境变量
cat <<eof >>/etc/profile
export GO_HOME=/data/software/alertsnitch/go
export PATH=$PATH:$GO_HOME/bin
export GOPATH=$GO_HOME/tagert
eof

 步骤二:用go编译安装 alertsnitch

tar -zxf 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

  

 如上图,这就成功了。

【2】配置启动 alertsnitch 

(1)资料查阅

  

 

 这里面写的很详细,后面我们挑启动的相关重点信息来弄。

(2)数据库初始化

  《1》建立数据库、建立账户

    

 

   第一个 sh里面有脚本,进行初始化。 test.sh 可以测试

#!/bin/bash

set -EeufCo pipefail
IFS=$'\t\n'

echo "Creating DB"
mysql --user=root --password="${MYSQL_ROOT_PASSWORD}" --host=mysql -e "CREATE DATABASE IF NOT EXISTS ${MYSQL_DATABASE};"

echo "Creating bootstrapped model"
mysql --user=root --password="${MYSQL_ROOT_PASSWORD}" --host=mysql "${MYSQL_DATABASE}" < db.d/mysql/0.0.1-bootstrap.sql

echo "Applying fingerprint model update"
mysql --user=root --password="${MYSQL_ROOT_PASSWORD}" --host=mysql "${MYSQL_DATABASE}" < db.d/mysql/0.1.0-fingerprint.sql

echo "Done creating model"

在这里面写一下变量 ,还有注意目录 db.d/mysql..... 文件位置

MYSQL_ROOT_PASSWORD、 MYSQL_DATABASE

完成后,如下图

  

(3)配置 alertsnitch 关联 mysql

我的mysql是5.7,且装载与 alertsnitch相同的机器

运行结果如下,没问题的话加开机自启什么的就不用多说了哈。/etc/rc.local  记得给权限

nohup alertsnitch -database-backend="mysql" -listen.address="10.18.194.201:9567" -dsn="root:31jradmin@tcp(127.0.0.1:3306)/alertsnitch" &

  

【3】alertsnitch 整合 alertmanager

(1)配置 alertmanager.yml

此时 alertsnitch 已经启动

   

 

 (2)alertsnitch 数据库核验

如下图,成功

   

【4】grafana接入仪表盘

(1)构建 mysql 数据源

  

 

(2)导入仪表盘 15833

  

(3)仪表展示

  

 

 【参考命令】

(1)alertsnitch mysql表查询语句

select t1.*,
    t2.Value AS "alertname",
    t3.Value AS "env",
    t4.Value AS "severity",
    t5.Value AS "az",
    t6.Value AS "instance",
    t7.Value AS "job",
    t8.Value AS "description"
from 
 (
    SELECT
    ID,  status,  startsAt,  endsAt,  TIMESTAMPDIFF(SECOND, startsAt, endsAt) AS duration
    FROM alert t1
    where startsAt BETWEEN DATE_FORMAT(DATE_ADD(now(),interval -1 month), '%Y-%m-01') and DATE_FORMAT(NOW(), '%Y-%m-01')  
    AND status="resolved"
 ) t1
  left join alertlabel t2 on t1.id=t2.AlertID and t2.Label="alertname"
  left join alertlabel t3 on t1.id=t3.AlertID and t3.Label="env"
  left join alertlabel t4 on t1.id=t4.AlertID and t4.Label="severity"
  left join alertlabel t5 on t1.id=t5.AlertID and t5.Label="az"
  left join alertlabel t6 on t1.id=t6.AlertID and t6.Label="instance"
  left join alertlabel t7 on t1.id=t7.AlertID and t7.Label="job"
  left join alertannotation t8 on t1.id=t8.AlertID and t8.Annotation="description";

    

 

 【故障处理】

(1)go install 安装失败,某些包下载链接超时

  

解决,用国内代理地址:go env -w GOPROXY=https://goproxy.cn

 

【参考文档】

https://blog.51cto.com/u_13747676/5323538?u_atoken=8ff12d11-4543-4e16-946a-3bfdfa91a2e9&u_asession=01CbUhknKvG56hLQZm5AbkuRCy_Zph8Keo_Ps7k5lR56AMmuAzHH8bc7zNEmHPlXZoX0KNBwm7Lovlpxjd_P_q4JsKWYrT3W_NKPr8w6oU7K-GGVnahFOOrgobXMSWAjXUXYclJyucqbDi6Dc_RZdAR2BkFo3NEHBv0PZUm6pbxQU&u_asig=05mlGEb2mTIXVShMltoDiqa21cwvXLFg2CVNv_DiEg9c3vWn8Yaw0Q11IZSuTUaOKWmsd76mZN-bJ6Id_OSLYWgXzxXxOsadWxBQB22ZHYghVgUt6gEvR-ibwtBA9nquA6keQerAmgPBaEoKuJ8wL9iUdjmsDRd8XGue9OMypSVMb9JS7q8ZD7Xtz2Ly-b0kmuyAKRFSVJkkdwVUnyHAIJzWBqycn3nLaNKGUzm8JwkkPMo75GDarPSX7ivWV6rz4DBP1w1dGz26Hfj9dGSf-IP-3h9VXwMyh6PgyDIVSG1W86DYp-Imb8W-5OwPBqSQ5h502J5nyU5D56YEDImjFXiO_MKJBlsdZ1WHQd3Oxtnx2CNb1klwfXPCpNOkxIN4ZhmWspDxyAEEo4kbsryBKb9Q&u_aref=FqkXi8gKe%2FLi1iIY5rE8283ZwmE%3D

posted @ 2023-02-04 16:57  郭大侠1  阅读(1550)  评论(0编辑  收藏  举报