当前位置: 首页>后端>正文

grafana配置prometheus提示Bad Gateway grafana prometheus模板

Prometheus 是一套开源的系统监控报警框架。
本文只要讲述如何部署:Prometheus+Grafana+AlertManager

简介

Prometheus 特性:
  • 高维度数据模型
  • 自定义查询语言
  • 可视化数据展示
  • 高效的存储策略
  • 易于运维
  • 提供各种客户端开发库
  • 警告和报警
  • 数据导出
Prometheus 组成及架构

grafana配置prometheus提示Bad Gateway grafana prometheus模板,grafana配置prometheus提示Bad Gateway grafana prometheus模板_spring,第1张

大致过程:从各job中获取metrics,或者接收metrics,将收集到的metrics存储到本地,并根据定义好的alert.rule来进行处理,向Alertmanager 推送警报。Alertmanager接收到警报侯发送邮件等信息。

Springboot集成Prometheus
pom.xml中引入
<!--pom.xml中引入-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
	<groupId>io.micrometer</groupId>
	<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
application.yml中配置
server:
  port: 15113
  servlet:
      context-path: /communtiy/
spring:
  application:
    name: prometheus-test-server
management:
  endpoints:
    web:
      exposure:
        include: 'prometheus'
  metrics:
    tags:
      application: ${spring.application.name}
编写一个测试接口
@RestController
public class GetQxController {
    @Resource
    Test1Mapper test1Mapper;
    @RequestMapping("/getQxByUser")
    public Object getQxByUser(){
        return test1Mapper.getAll();
    }
}
测试集成成功

http://localhost:15113/communtiy/actuator/prometheus

grafana配置prometheus提示Bad Gateway grafana prometheus模板,grafana配置prometheus提示Bad Gateway grafana prometheus模板_后台运行_02,第2张

安装Prometheus

官方下载地址 prometheus-2.19.2.darwin-amd64.tar.gz

安装prometheus步骤

解压

tar xvfz prometheus-*.tar.gz
cd prometheus-*

修改Prometheus的配置文件prometheus.yml

alerting:   #配置Alertmanager相关信息
  alertmanagers:
  - static_configs:
    - targets: ["118.24.167.82:9093"]
rule_files:
  - "../rule/*.yml" # 警告的规则
scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 5s
    static_configs:
    - targets: ['localhost:9090'] # prometheus的默认端口为9090
  - job_name: 'test_15113' #自定义job名称
    scrape_interval: 5s # 多久抓取一次信息
    metrics_path: '/communtiy/actuator/prometheus' # 获取监控信息的接口
    static_configs:
      - targets: ['192.168.84.47:15113'] # 需要监听的项目ip+port

创建告警规则文件…/rule/84_47_15113.yml

cd ..
mkdir rule
touch 84_47_15113.yml
vim 84_47_15113.yml

84_47_15113.yml内容

groups:
- name: node_status #自定义
  rules: 
  - alert: serverDown # alertName
    expr: up{job="test_15113"}  ==  0 # job名称test_15113宕机
    # expr: up  ==  0 # job宕机
    for: 30s
    labels:
     severity: warning
    annotations: # 警告内容
     description: "{{$labels.instance}} down!"
     summary: "{{$labels.instance}} of job {{$labels.job}} down!"

启动Prometheus
后台运行,将日志打印到log.txt中

./prometheus --config.file=prometheus.yml >log.txt 2>&1 &

访问Prometheus

http://ip:9090

安装AlertManager步骤

解压

tar xvfz alertmanager-*.tar.gz
cd alertmanager-*

修改Alertmanager的配置文件AlertManager.yml

global:
  resolve_timeout: 5m
  smtp_smarthost: 'smtp.exmail.qq.com:465' # 发送邮箱的smtp,腾讯企业邮箱端口465
  smtp_from: 'xxxxx.com' # 发送邮箱账号
  smtp_auth_username: 'xxxxx.com' # 发送邮箱账号
  smtp_auth_password: 'xxxxxx' # 发送邮箱密码
  smtp_require_tls: false # 关闭tls
  wechat_api_url: "https://qyapi.weixin.qq.com/cgi-bin/" #发送企业微信的接口
  wechat_api_secret: '*********************' #企业微信获取
  wechat_api_corp_id: '**************' #企业微信获取
templates:
- '/usr/local/listen_ip/alertmanager-0.21.0/temp/*.tmpl' #模板地址
route:
  group_by: ['alertname']# 采用哪个标签来作为分组依据
  group_wait: 30s # 组告警等待时间。也就是告警产生后等待30s,如果有同组告警一起发出
  group_interval: 3m # 两组告警的间隔时间
  repeat_interval: 4h # 重复告警的间隔时间,减少相同邮件的发送频率
  receiver: 'email' #默认发送email
  routes:
  - receiver: 'wechat' # 如果是error 发送微信
    match:
      severity: error
receivers:
- name: 'email'
  email_configs:
  - to: '**************@qq.com'
    html: '{{template "email.to.html" . }}' #邮件模板
    headers: { Subject: "[WARN] 报警邮件"}
#  - to: '*************@qq.com,*****************@qq.com' 发送多个人
- name: 'wechat'
  wechat_configs:
  - send_resolved: true
    to_user: "@all" # 发送所有人
    to_party: '1' # 微信获取
    agent_id: '1000002' # 微信获取
    corp_id: '********' # 微信获取
    message: '{{template "wechat.to.message" . }}' #微信模板

email.tmpl模板

{{ define "email.to.html" }}
{{ range .Alerts }}
 =============start=============<br>
告警等级:{{ .Labels.severity }}<br>
告警类型:{{ .Labels.alertname }}<br>
告警主机:{{ .Labels.instance }}<br>
告警主题:{{ .Annotations.summary }}<br>
告警备注:{{ .Annotations.description }}<br>
触发时间:{{ (.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}<br>
==============end================
{{ end }}
{{ end }}

wechat.tmpl

{{ define "wechat.to.message" }}
{{ if gt (len .Alerts.Firing) 0}}
{{ range $i, $alert := .Alerts }}
===start===
@警报
告警等级:{{ .Labels.severity }}
告警类型:{{ .Labels.alertname }}
告警主机:{{ .Labels.instance }}
告警主题:{{ .Annotations.summary }}
告警备注:{{ .Annotations.description }}
触发时间:{{ (.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
===end===
{{ end }}
{{ end }}
{{ if gt (len .Alerts.Resolved) 0 }}
{{ range $i, $alert := .Alerts }}
===start===
@恢复
告警等级:{{ .Labels.severity }}
告警类型:{{ .Labels.alertname }}
告警项目:{{ .Labels.instance }}
触发时间:{{ (.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
恢复时间:{{ (.EndsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
===end===
{{ end }}
{{ end }}
{{ end }}

启动Alertmanager
后台运行,将日志打印到log.txt中

./alertmanager --config.file=alertmanager.yml >log.txt 2>&1 &

访问Alertmanager

http://ip:9093

安装Grafana步骤

解压

tar xvfz grafana-*.tar.gz
cd grafana-*
cd bin

启动Grafana
后台运行,将日志打印到log.txt中

./grafana-server >log.txt 2>&1 &

访问Alertmanager

http://ip:3000

注:服务器需要将9090,9093,3000等端口打开
注:建议将三个服务都配置完毕后逐一运行
Grafana的使用

默认username/pwd:admin/admin

选择:配置数据源

grafana配置prometheus提示Bad Gateway grafana prometheus模板,grafana配置prometheus提示Bad Gateway grafana prometheus模板_spring_03,第3张

选择: Add data source-Prometheus

grafana配置prometheus提示Bad Gateway grafana prometheus模板,grafana配置prometheus提示Bad Gateway grafana prometheus模板_微信_04,第4张

填写URL

grafana配置prometheus提示Bad Gateway grafana prometheus模板,grafana配置prometheus提示Bad Gateway grafana prometheus模板_spring_05,第5张

创建可视化监听

grafana配置prometheus提示Bad Gateway grafana prometheus模板,grafana配置prometheus提示Bad Gateway grafana prometheus模板_后台运行_06,第6张

grafana配置prometheus提示Bad Gateway grafana prometheus模板,grafana配置prometheus提示Bad Gateway grafana prometheus模板_spring_07,第7张

监听接口的配置:
Metrics:http_server_requests_seconds_count{instance=“192.168.84.47:15113”,status=“200”,uri!~"/actuator.*"}
Legend:
{{uri}}



https://www.xamrdz.com/backend/3hc1937478.html

相关文章: