Prometheus(三)Grafana部署及部署告警

x33g5p2x  于2021-12-11 转载在 其他  
字(7.0k)|赞(0)|评价(0)|浏览(476)

一、Grafana部署及模板展示

grafana是一款基于go语言开发的通用可视化工具,支持从不同的数据源加载并展示数据,可作为其数据源的部分储存系统如下所示:

TSDB:Prometheus、IfluxDB、OpenTSDB和Graphit

日志和文档存储:Loki和ElasitchSearch

分布式请求跟踪:Zipkin、Jaeger和Tenpo

SQL DB:Mysql、PostgreSQL和Microsoft SQL server

grafana基础默认监听于TCP协议的3000端口,支持集成其他认证服务,且能够通过/metrics输出内建指标

数据源(Data Source):提供用于展示的数据的储存系统
仪表盘(Dashboard):组织和管理数据的可视化面板(Panel)
团队和用户:提供了面向企业组织层级的管理能力

1.Grafan部署步骤

[root@prometheus ~]# ls
grafana-7.3.6-1.x86_64.rpm
[root@prometheus ~]# yum install grafana-7.3.6-1.x86_64.rpm -y
[root@prometheus ~]# systemctl start grafana-server.service
[root@prometheus ~]# ss -antp | grep grafana-server

二、打标签

1.重新打标定义(在job上定义)

  • 对target重新打标是在数据抓取之前动态重写target标签的强大工具,在每个数据抓取配置中,可以定义多个relabel步骤,它们将按照定义的顺序依次执行
  • 对于发现的每个target,Prometheus默认会执行如下操作:
job的标签设定为其所属的job name的值;

_address_标签的值为该target的套接字地址":"

instance标签的值为_address_的值;

_scheme_标签的值为抓取该target上指标时使用的协议(http或https) ;

_metrics _path_标签的值为抓取该target上的指标时使用URI路径,默认为/metrics;

param_标签的值为传递的URL参数中第一个名称为的参数的值

重新标记期间,还可以使用该target上以"meta "开头的元标签;

各服务发现机制为其target添加的元标签会有所不同;

重新标记完成后,该target上以"_"开头的所有标签都会被移除;

若在relabel的过程中需要临时存储标签值,则要使用tmp标签名称为前缀进行保存,以避免同Prometheus的内建标签冲突

2.relabel config(重新打标配置)

修改标签值、增加删除标签,通过调用不同参数实现自己的需求

source_labels:指定调用哪些已有的标签(可引用多个)在重新打标的时候会将这些标签对应的值给引用/提取并连接起来,例如: cpu指标{host=node1; host=node2 }

target_labels:与source_labels组合使用,可以指定使用哪个已有标签赋值给指定的新标签

separator:对应源标签的标签值使用什么连接符,默认为" ;"

regex:对于源标签,使用哪个正则表达式对源标签进行模式匹配、匹配后可以将对应的结果复制到target上,赋值方式:(引用所有正则表达式的内容进行赋值)

modulus:hash算法函数

replacement:把目标标签的值改为新的值

action <relabel_action>:表示重新打标的方式是什么,以及要实现什么功能

三、prometheus告警功能

Prometheus对指标的收集、存储同告警能力分属于Prometheus Server和AlertManager(通用的组件)两个独立的组件,前者仅负责基于"告警规则"生成告警通知,具体的告警操作则由后者完成。Alertmanager负责处理由客户端发来的告警通知客户端通常是Prometheus server,但它也支持接收来自其它工具的告警。Alertmanager对告警通知进行分组、去重后,根据路由规则将其路由到不同的receiver,如Email、短信或PagerDuty等。目前Alertmanager还不支持钉钉,那用户完全可以通过Webhook与钉钉机器人进行集成,从而通过钉钉接收告警信息。同时AltManager还提供了静默和告警抑制机制来对告警通知行为进行优化

1.告警功能概述

prometheus对指标的收集、存储与告警能力分属于Prometheus serve和alertmanager两个独立的组件,pro-server只负责通过"告警规则"生成告警通知,具体告警操作是由alertmmanager完成

2.告警规则

是由PromQL编写的布尔值表达式使用>< =与一个常用量值,比如80%进行比较,其返回值为true或false,prometheus-server对抓取到的指标序列与告警规则中做为比较的Prometheus匹配,则会把此样本值抓取过来作比较,若返回值为true则认为指标异常,不能满足false,则为正常值以上表达式为告警规则表达式。比如:筛选一个指标数据cpu使用率<0%系统异常

3.通知告警信息

一旦条件表达式为true了就会触发通知信息,送给altermanager,由alter借助特定服务的API或者访问入口,将此信息发出去一般称为告警媒介,也可以借助邮件进行告警SMTP

4.prometheus监控系统的告警逻辑

route:告警路由,分组、分类分发告警消息给不同渠道

prometheus通过alter-rule规则,生成告警通知给altermanager,altermanager会生成本地的告警路由表(第一路由默认称为根路由,所有的告警信息都需要一个根路由,没有一个匹配项,则需要设置一个默认路由)为实现将特定的信息发送给特定的用户。

例如:

按消息级别来看,严重、中等、普通级别,红色报警、蓝色报警,应用发送方

按分组:业务运维、系统运维、基础设施运维、k8s运维

5.告警功能

除了基本的告警通知能力外,Altermanager还支持对告警进行去重、分组、抑制

6.静默、抑制、分组等功能

分组 (Grouping):将相似告警合并为单个告警通知的机制,在系统因大面积故障而触发告警潮时,分组机制能避免用户被大量的告警噪声淹没,进而导致关键信息的隐没;

抑制(Inhibition):系统中某个组件或服务故障而触发告警通知后,那些依赖于该组件或服务的其它组件或服务可能也会因此而触发告警,抑制便是避免类似的级联告警的一种特性,从而让用户能将精力集中于真正的故障所在;

静默(silent):是指在一个特定的时间窗口内,即便接收到告警通知,Alertmanager也不会真正向用户发送告警信息的行为;通常,在系统例行维护期间,需要激活告警系统的静默特性;

路由(route):用于配置Alertmanager如何处理传入的特定类型的告警通知,其基本逻辑是根据路由匹配规则的匹配结果来确定处理当前告警通知的路径和行为

四、部署告警对接邮箱

在prometheus-server端定义告警规则,指定alertmanager的位置,将告警信息发送给alert处理

1.安装altermanager

[root@prometheus ~]# cd /opt/
[root@prometheus opt]# ls
alertmanager-0.23.0.linux-amd64.tar.gz
[root@prometheus opt]# tar zxf alertmanager-0.21.0.linux-amd64.tar.gz -C /usr/local/
[root@prometheus opt]# ln -s /usr/local/alertmanager-0.21.0.linux-amd64/ /usr/local/alertmanager
[root@prometheus opt]ln -s /usr/local/prometheus-0.21.0.linux-amd64/prometheus /usr/local/bin

2.查看配置文件

[root@prometheus ~]# cat /usr/local/alertmanager-0.21.0.linux-amd64/alertmanager.yml 
route:                                               #路由信息
  group_by: ['alertname']                            #分组
  group_wait: 30s                                    #分组缓冲/等待时间
  group_interval: 5m                                 #重新分组时间
  repeat_interval: 1h                                #重新告警间隔
  receiver: 'web.hook'                               #接收方/媒介
receivers:
- name: 'web.hook'
  webhook_configs:
  - url: 'http://127.0.0.1:5001/'                    #标注5001端口
inhibit_rules:                                       #抑制规则的策略
  - source_match:                                    #匹配项
      severity: 'critical'                           #严重的级别
    target_match:
      severity: 'warning'                           #target匹配warning级别
    equal: ['alertname', 'dev', 'instance']          #符合alertname、dev、instance

3.修改alertmanager的配置文件

global:
  resolve_timeout: 5m
  smtp_from: 418044336@qq.com
  smtp_auth_username: 418044336@qq.com
  smtp_auth_password:
  smtp_require_tls: false
  smtp_smarthost: 'smtp.qq.com:465'

route:
  group_by: ['alertname']
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 1h
  receiver: 'email-test'
receivers:
- name: 'email-test'
  email_configs: ossiocvhdvirbiad
  - to: 418044336@qq.com
    send_resolved: true

4.配置绑定的邮箱

登入邮箱→设置→账户→IMAP/SMTP服务→开启

5.启动alertmanager

[root@localhost alertmanager]# ./alertmanager

6.相关配置文件

[root@prometheus ~]# cd /usr/local/alertmanager
[root@prometheus alertmanager]# mkdir alert-config
[root@prometheus alertmanager]# cd alert-config/
[root@prometheus alert-config]# mkdir -p alert_rules targets
[root@prometheus alert-config]# ls
alert_rules  targets
[root@prometheus alert-config]# cd alert_rules/
[root@prometheus alert_rules]# vim instance_down.yaml
groups:
- name: AllInstances
  rules:
  - alert: InstanceDown
    # Condition for alerting
    expr: up == 0
    for: 20s
    # Annotation - additional informational labels to store more information
    annotations:
      title: 'Instance down'
      description: Instance has been down for more than 20 seconds.' # Labels - additional labels to be attached to the alert labels: severity: 'critical'
      
      ----
 [root@prometheus ~]# cd /usr/local/alertmanager/alert-config/targets/
[root@prometheus targets]# vim alertmanagers.yaml 
 
- targets:
  - 192.168.126.12:9093
  labels:
    app: alertmanager
    
    ----
[root@prometheus targets]# vim nodes-linux.yaml 
 
- targets:
  - 192.168.126.11:9100
  - 192.168.126.12:9100
  - 192.168.126.13:9100
  labels:
    app: node-exporter
    job: node    
    
    ----
[root@prometheus targets]# vim prometheus-servers.yaml 
 
- targets:
  - 192.168.126.11:9090
  labels:
    app: prometheus
    job:  prometheus

7、配置prometheus启动文件

[root@prometheus ~]# cd /usr/local/alertmanager/alert-config/
[root@prometheus alert-config]# vim prometheus.yml
# my global config
# Author: MageEdu <mage@magedu.com>
# Repo: http://gitlab.magedu.com/MageEdu/prometheus-configs/
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).
 
# Alertmanager configuration
alerting:
  alertmanagers:
  - file_sd_configs:
    - files:
      - "targets/alertmanagers*.yaml"
 
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  - "rules/*.yaml"
  - "alert_rules/*.yaml" 
 
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    static_configs:
    file_sd_configs:
    - files:                                               
      - targets/prometheus-*.yaml  
      refresh_interval: 2m 
 
  # All nodes
  - job_name: 'nodes'
    file_sd_configs:
    - files:                                               
      - targets/nodes-*.yaml  
      refresh_interval: 2m 
 
  - job_name: 'alertmanagers'
    file_sd_configs:
    - files:
      - targets/alertmanagers*.yaml
      refresh_interval: 2m

8.指定文件启动alertmanager

[root@prometheus ~]# cd /usr/local/alertmanager
[root@prometheus alertmanager]# ./alertmanager

9.指定文件启动prometheus

[root@prometheus ~]# cd /usr/local/prometheus-2.27.1.linux-amd64/
[root@prometheus prometheus-0.20.0.linux-amd64]# ./prometheus --config.file=./alert-config/prometheus.yml

10.模拟故障(停止node_exporter)

  • 查看prometheus-ui

  • 查看邮箱

相关文章