springboot+Prometheus+grafana+PrometheusAlert一条龙监控

Prometheus

安装

cd /usr/local/src
tar zvxf prometheus-2.14.0.linux-amd64.tar.gz -C /opt/
ln -s /opt/prometheus-2.14.0.linux-amd64/ /opt/prometheus
useradd -s /sbin/nologin -M prometheus 
mkdir /data/prometheus -p
chown -R prometheus:prometheus /opt/prometheus/
chown -R prometheus:prometheus /data/prometheus/
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
User=prometheus
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --storage.tsdb.path=/data/prometheus
Restart=on-failure

[Install]
WantedBy=multi-user.target
systemctl enable prometheus
systemctl start prometheus
systemctl status prometheus

启动

nohup ./prometheus --web.enable-lifecycle & 

![在这里插入图片描述](https://img-blog.csdnimg.cn/6332cf4cafc4432c9f79be7d846d424e.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5a2m5b6S6a2P6I-x5bu2,size_20,color_FFFFFF,t_70,g_se,x_16热启动

curl -XPOST http://localhost:9090/-/reload

springboot应用接入

management:
  endpoints:
    web:
      base-path: /management
      exposure:
        include: ['configprops', 'env', 'health', 'info', 'jhimetrics', 'logfile', 'loggers', 'prometheus', 'threaddump']
  endpoint:
    health:
      show-details: when_authorized
      roles: 'ROLE_ADMIN'
    jhimetrics:
      enabled: true
  info:
    git:
      mode: full
  health:
    mail:
      enabled: false # When using the MailService, configure an SMTP server and set this to true
  metrics:
    export:
      # Prometheus is the default metrics backend
      prometheus:
        enabled: true
        step: 60
    enable:
      http: true
      jvm: true
      logback: true
      process: true
      system: true
    distribution:
      percentiles-histogram:
        all: true
      percentiles:
        all: 0, 0.5, 0.75, 0.95, 0.99, 1.0
    tags:
      application: ${spring.application.name}
    web:
      server:
        auto-time-requests: true

prometheus配置

- job_name: 'springboot'
  metrics_path: '/management/prometheus'
  static_configs:
  	- targets: ['10.237.xxx.xx:7700']

grafana

grafana-7.3.3-1.x86_64.rpm

下载: https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/

  1. 上传grafana​​​​​​​离线包,上传离线包到/usr/local/grafana-rpm目录
mkdir -p /usr/local/grafana-rpm
  1. 安装
cd /usr/local/grafana-rpm/
yum clean all ; yum localinstall –y --skip-broken ./*
grafana-server -v       //查看版本

在这里插入图片描述
3.配置文件

# vim /etc/grafana/grafana.ini

# grep -n '^'[[a-Z] /etc/grafana/grafana.ini   //查看配置内容方便
  1. 启动grafana-server,并设置开机启动
# systemctl restart grafana-server      //重启
# systemctl enable grafana-server      //加入开机启动
# systemctl status grafana-server        //查看启动状态
  1. 启动Grafana,配置Prometheus数据源

添加Prometheus数据源
在这里插入图片描述
Add data source选择Prometheus
在这里插入图片描述
在这里插入图片描述
最后点击Save
3、配置Grafana数据展示模板

至此Prometheus和Grafana已经打通了,接下来需要添加个图形模板以便展示数据。

Grafana里有很多的模板可以直接使用(https://grafana.com/dashboards),我这里使用的是https://grafana.com/dashboards/8919,其中8919是ID号,可以在Grafana中Import,选择数据源为Prometheus
这里我们新建一个:
在这里插入图片描述
在这里插入图片描述
填写采集的指标点

在这里插入图片描述
注意: 这里的指标点不能随便填,必须是已有的可以在 Prometheus看到
在这里插入图片描述
选择图表样式
在这里插入图片描述
填写标题描述
在这里插入图片描述
最后点击右上角的保存,输入Dashboad的名称即可。
在这里插入图片描述

Grafana告警
设置分为两部分:

Notification配置
Alert配置
1、Notification配置:其实就是配置告警途径,Grafana支持多种方式的告警:Email、webhook等。由于webhook比较通用,我们重点说下如何配置webhook。
在这里插入图片描述
之后点击New Channel进入配置页面:
在这里插入图片描述
保存后即可。
2、alert配置:
1)进入dashboards,选择一个pannel,然后edit,进入alert选项卡,添加alert配置。
在这里插入图片描述

在alert config页面设置报警阈值,和报警条件等。
2)设置notification:
然后再notification页面设置上一步配置的notification,以及报警信息。
3、编写webhook接口:
1)grafana发送的数据结构如下:

{
    "ruleName": "webhooktest",
    "state": "alerting",
    "message": "This is my webhook send test ,my target is to get the alert data.",
    "ruleId": 4,
    "title": "[Alerting] webhooktest",
    "ruleUrl": "http://localhost:3000/dashboard/db/webhookdatatest?fullscreen&edit&tab=alert&panelId=1&orgId=1",
    "evalMatches": [
        {
            "metric": "mytest.age { myName: pangkun2 }",
            "value": 27,
            "tags": null
        },
        {
            "metric": "mytest.age { myName: pangkun3 }",
            "value": 26,
            "tags": null
        }
    ]
}

2)在接口中,可以通过request.getparamater(“key”)来获取上面结构中的数据
PromethuesAlert
在这里插入图片描述
在这里插入图片描述

PrometheusAlert

https://github.com/feiyu563/PrometheusAlert
在linux系统中部署

#打开PrometheusAlert releases页面,根据需要选择需要的版本下载到本地解压并进入解压后的目录
如linux版本(https://github.com/feiyu563/PrometheusAlert/releases/download/v4.4.0/linux.zip)
# wget https://github.com/feiyu563/PrometheusAlert/releases && unzip linux.zip && cd linux/
#,下载好后解压并进入解压后的文件夹
/
#运行PrometheusAlert
# ./PrometheusAlert (#后台运行请执行 nohup ./PrometheusAlert &)
#启动后可使用浏览器打开以下地址查看:http://127.0.0.1:8080
#默认登录帐号和密码在app.conf中有配置

接口说明
PrometheusAlert 目前提供以下几类接口,分别对应各自接入端,负责解析各自接口传入或者传出的消息。

prometheusalert自定义模版接口

/prometheusalert?type=${type}&tpl=${template}[&rr=true][&ddurl=${ddur}][&wxurl=${wxurl}][&fsurl=${fsurl}][&phone=${phonenumber}]   自定义模版接口,可通过Dashboard自定义模版后,支持任意WebHook接入

metrics接口

/metrics           展示PrometheusAlert指标信息

prometheus固定模版接口

/prometheus/alert   处理Prometheus告警消息转发到默认接口/prometheus/router  处理Prometheus AlertManager router消息指定接收端接口

zabbix接口

/zabbix/alert  处理Zabbix告警消息转发默认接口

grafana固定模版接口 (即将废弃,今后统一切换至自定义模板接口)

/grafana/phone     处理Grafana告警消息转发到腾讯云电话接口(v3.0版本将废弃)
/grafana/dingding  处理Grafana告警消息转发到钉钉接口
/grafana/weixin    处理Grafana告警消息转发到微信接口
/grafana/feishu    处理Grafana告警消息转发到飞书接口
/grafana/txdx      处理Grafana告警消息转发到腾讯云短信接口
/grafana/txdh      处理Grafana告警消息转发到腾讯云电话接口
/grafana/hwdx      处理Grafana告警消息转发到华为云短信接口
/grafana/alydx     处理Grafana告警消息转发到阿里云短信接口
/grafana/alydh     处理Grafana告警消息转发到阿里云电话接口
/grafana/rlydh     处理Grafana告警消息转发到容联云电话接口
/grafana/email     处理Grafana告警消息转发到Email接口
/grafana/bddx      处理Grafana告警消息转发到百度云短信接口
/grafana/tg        处理Grafana告警消息转发到telegram接口
/grafana/workwechat处理Grafana告警消息转发到企业微信应用接口
/grafana/ruliu     处理Grafana告警消息转发到百度Hi(如流)接口

配置文件说明
PrometheusAlert配置文件中定义了丰富的配置项,默认配置即可满足开箱即用。并且配置文件中每项配置都加入了详细的注释,非常明确的指出了每个配置项的功能和定义。这里重点关心webhook配置

#---------------------↓全局配置-----------------------
appname = PrometheusAlert
#登录用户名
login_user=prometheusalert
#登录密码
login_password=prometheusalert
#监听地址
httpaddr = "0.0.0.0"
#监听端口
httpport = 8080
runmode = dev
#设置代理 proxy = http://123.123.123.123:8080
proxy =
#开启JSON请求
copyrequestbody = true
#告警消息标题
title=PrometheusAlert
#链接到告警平台地址
GraylogAlerturl=http://graylog.org
#钉钉告警 告警logo图标地址
logourl=https://raw.githubusercontent.com/feiyu563/PrometheusAlert/master/doc/alert-center.png
#钉钉告警 恢复logo图标地址
rlogourl=https://raw.githubusercontent.com/feiyu563/PrometheusAlert/master/doc/alert-center.png
#短信告警级别(等于3就进行短信告警) 告警级别定义 0 信息,1 警告,2 一般严重,3 严重,4 灾难
messagelevel=3
#电话告警级别(等于4就进行语音告警) 告警级别定义 0 信息,1 警告,2 一般严重,3 严重,4 灾难
phonecalllevel=4
#默认拨打号码(页面测试短信和电话功能需要配置此项)defaultphone=xxxxxxxx
#故障恢复是否启用电话通知0为关闭,1为开启
phonecallresolved=0
#自动告警抑制(自动告警抑制是默认同一个告警源的告警信息只发送告警级别最高的第一条告警信息,其他消息默认屏蔽,这么做的目的是为了减少相同告警来源的消息数量,防止告警炸弹,0为关闭,1为开启)
silent=0
#是否前台输出file or console
logtype=file
#日志文件路径
logpath=logs/prometheusalertcenter.log
#转换Prometheus,graylog告警消息的时区为CST时区(如默认已经是CST时区,请勿开启)
prometheus_cst_time=0
#数据库驱动,支持sqlite3,mysql,postgres如使用mysql或postgres,请开启db_host,db_port,db_user,db_password,db_name的注释
db_driver=sqlite3
#db_host=127.0.0.1
#db_port=3306
#db_user=root
#db_password=root
#db_name=prometheusalert

#---------------------↓webhook-----------------------
#是否开启钉钉告警通道,可同时开始多个通道0为关闭,1为开启
open-dingding=1
#默认钉钉机器人地址
ddurl=https://oapi.dingtalk.com/robot/send?access_token=xxxxx
#是否开启 @所有人(0为关闭,1为开启)
dd_isatall=1
#是否开启微信告警通道,可同时开始多个通道0为关闭,1为开启
open-weixin=1
#默认企业微信机器人地址
wxurl=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxx
#是否开启飞书告警通道,可同时开始多个通道0为关闭,1为开启
open-feishu=0
#默认飞书机器人地址
fsurl=https://open.feishu.cn/open-apis/bot/hook/xxxxxxxxx

其他配置
另外 PrometheusAlert 同时支持按照日期发送告警到不同号码,并且已经加入告警失败或者被告警人未接听电话后转联系默认联系人(defaultphone),只需新建user.csv文件,并将文件放到程序运行目录下即可自动加载
同时该文件也是电话回调接口所必需文件(如回调接口返回非0状态,则会在此文件中寻找下一号码进行拨打,如开启回调功能,请务必创建该文件)

ps:目前grafana/graylog的电话和短信告警依赖于该文件,prometheus电话和短信告警优先从rules的Mobile字段读取,如未配置号码,则会从user.csv中读取,如user.csv中也未配置,则会直接发送到defaultphone

关于user.csv中值班时间切换问题,默认每日上午10点进行切换

csv文件格式如下:

2019年4月10日,15888888881,小张,15999999999,备用联系人小陈,15999999998,备用联系人小赵
2019年4月11日,15888888882,小李,15999999999,备用联系人小陈,15999999998,备用联系人小赵
2019年4月12日,15888888883,小王,15999999999,备用联系人小陈,15999999998,备用联系人小赵
2019年4月13日,15888888884,小宋,15999999999,备用联系人小陈,15999999998,备用联系人小赵

使用Dashboard对配置进行测试
PrometheusAlert提供简洁的Dashboard,目前Dashboard主要用于测试配置文件中的配置是否正确,Dashboard上提供所有消息接收渠道的测试按钮,只需要点击对应渠道的按钮即可发送一条测试消息到该渠道,如果配置文件正确的情况,用户就会立即收到测试的告警消息。如果长时间未收到消息,那么就需要检查下配置文件的配置的正确性了。
dashboard
在这里插入图片描述
钉钉告警配置
PrometheusAlert全家桶钉钉配置说明
1、开启钉钉机器人
打开钉钉,进入钉钉群中,选择群设置–>智能群助手–>添加机器人–>自定义,可参下图:
在这里插入图片描述
在这里插入图片描述
新版本的钉钉加了安全设置,只需选择安全设置中的 自定义关键词 即可,并将关键词设置为 Prometheus或者app.conf中设置的title值均可,参考下图
在这里插入图片描述
在这里插入图片描述

复制图中的Webhook地址,并填入PrometheusAlert配置文件app.conf中对应配置项即可。
PS: 钉钉机器人目前已经支持 @某人 ,使用该功能需要取得对应用户的钉钉关联手机号码,如下图:
在这里插入图片描述
测试自定义机器人
通过以下方法,可以快速验证自定义机器人是否可以正常工作:

使用命令行工具curl。

说明 为避免出错,将以下命令逐行复制到命令行,需要将xxxxxxxx替换为真实access_token;若测试出错,请检查复制的命令是否和测试命令一致,多特殊字符会报错。

curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx' \
 -H 'Content-Type: application/json' \
 -d '{"msgtype": "text","text": {"content":"我就是我, 是不一样的烟火"}}'

钉钉目前支持只支持markdown语法的子集

钉钉相关配置

#---------------------↓全局配置-----------------------
#告警消息标题
title=PrometheusAlert
#钉钉告警 告警logo图标地址
logourl=https://raw.githubusercontent.com/feiyu563/PrometheusAlert/master/doc/alert-center.png
#钉钉告警 恢复logo图标地址
rlogourl=https://raw.githubusercontent.com/feiyu563/PrometheusAlert/master/doc/alert-center.png
#---------------------↓webhook-----------------------
#是否开启钉钉告警通道,可同时开始多个通道0为关闭,1为开启
open-dingding=1
#默认钉钉机器人地址
ddurl=https://oapi.dingtalk.com/robot/send?access_token=xxxxx
#是否开启 @所有人(0为关闭,1为开启)
dd_isatall=1

企业微信告警配置
打开企业微信,进入企业微信群中,选择群设置–>群机器人–>添加,可参下图:
在这里插入图片描述

在这里插入图片描述
检测

curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=920f1aa6-c05b-4796-861c-5f08ba9a69ee
   -H 'Content-Type: application/json' \
   -d '
   {
        "msgtype": "text",
        "text": {
            "content": "hello world"
        }
   }'

告警源配置
这里重点记录Grafana接入配置
Grafana 接入配置
首先使用管理员或者具有告警配置权限的帐号登录进Grafana管理页面,登录后进入notification channels配置。
在这里插入图片描述
注意此处的地址需要去PrometheusAlert的模版页面获取,如发送给钉钉的grafana模版

http://[prometheusalert_url]:8080/prometheusalert?type=dd&tpl=grafana-dd&ddurl=钉钉机器人地址&at=18888888888

在这里插入图片描述

在这里插入图片描述
【WebHook】
PrometheusAlert WebHook是实现将PrometheusAlert收到的消息转发给除了默认支持的告警渠道之外的系统,如部分企业内部系统或接口;同时通过自定义模板实现由用户自行指定发送的json内容。
使用前提:接收WebHook消息的接口需支持POST。且使用该功能需要使用者对go语言的template模版有一些初步了解,可以参考默认模版的一些语法来进行自定义。
模版数据等信息均存储在程序目录的下的db/PrometheusAlertDB.db中。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值