admin管理员组

文章数量:1529441

目录

前言

告警功能概述

                告警规则

                通知告警信息

                prometheus监控系统的告警逻辑

                告警功能:

 部署告警对接邮箱

                故障模拟


前言

        Prometheus对指标的收集、存储同告警能力分属于Prometheus Server和AlertManager(通用的组件)两个独立的组件,前者仅负责基于"告警规则"生成告警通知,具体的告警操作则由后者完成;

        Alertmanager负责处理由客户端发来的告警通知客户端通常是Prometheus server,但它也支持接收来自其它工具的告警;

        Alertmanager对告警通知进行分组、去重后,根据路由规则将其路由到不同的receiver,如Email、短信或PagerDuty等;

        目前Alertmanager还不支持钉钉,那用户完全可以通过Webhook与钉钉机器人进行集成,从而通过钉钉接收告警信息。同时AltManager还提供了静默和告警抑制机制来对告警通知行为进行优化

PS:webhook是一个APr概念, webhook是一种web回调或者http的push APT.Webhook作为一个轻量的事件处理应用

告警功能概述

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

                告警规则

        是由PromQL编写的布尔值表达式使用>< =与一个常用量值,比如80%进行比较,其返回值为true或false

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

                通知告警信息

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

                prometheus监控系统的告警逻辑

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

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

例如:按消息级别来看,严重、中等、普通级别,红色报警、蓝色报警,应用发送方,按分组:业务运维、系统运维、基础设施运维、k8s运维

                告警功能:

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

静默、抑制、分组等功能;

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

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

        静默(silent):是指在一个特定的时间窗口内,即便接收到告警通知,Alertmanager也不会真正向用户发送告警信息的行为;通常,在系统例行维护期间,需要激活告警系统的静默特性;路由(route):用于配置Alertmanager如何处理传入的特定类型的告警通知,其基本逻辑是根据路由匹配规则的匹配结果来确定处理当前告警通知的路径和行为

 部署告警对接邮箱

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

上传软件包

[root@prometheus ~]# tar -zxf alertmanager-0.22.2.linux-amd64.tar.gz -C /usr/local/
[root@prometheus ~]# ln -s /usr/local/alertmanager-0.22.2.linux-amd64/ /usr/local/alertmanager

查看配置文件

[root@prometheus /usr/local/alertmanager]# cat 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

修改alertmanager的配置文件

自己的邮箱开启smtp服务,获得授权码

 

[root@prometheus /usr/local/alertmanager]# cp alertmanager.yml alertmanager.yml.bak
[root@prometheus /usr/local/alertmanager]# vim alertmanager.yml

global:
  resolve_timeout: 5m
  smtp_from: 1973207956@qq
  smtp_auth_username: 1973207956@qq
  smtp_auth_password: tedkmlljybrheiei
  smtp_require_tls: false
  smtp_smarthost: 'smtp.qq:465'
route:
  group_by: ['alertname']
  group_wait: 10s
  group_interval: 30s
  repeat_interval: 1h
  receiver: 'email-demo'
receivers:
- name: 'email-demo'
  email_configs:
  - to: 1973207956@qq
    send_resolved: true

启动alertmanager

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

相关的配置文件

[root@prometheus /usr/local/alertmanager]# cd ../prometheus-2.27.1.linux-amd64/
[root@prometheus /usr/local/prometheus-2.27.1.linux-amd64]# ls
console_libraries  consoles  consul-sd  data  file_sd  LICENSE  NOTICE  prometheus  promtool
[root@prometheus /usr/local/prometheus-2.27.1.linux-amd64]# mkdir alert-config
[root@prometheus /usr/local/prometheus-2.27.1.linux-amd64]# cd alert-config/
[root@prometheus /usr/local/prometheus-2.27.1.linux-amd64/alert-config]# mkdir alert_rules targets
[root@prometheus /usr/local/prometheus-2.27.1.linux-amd64/alert-config]# cd alert_rules/
[root@prometheus /usr/local/prometheus-2.27.1.linux-amd64/alert-config/alert_rules]# vim demo-down.yml
groups:
- name: AllInstances
  rules:
  - alert: InstanceDown
    #节点服务挂掉
    # Condition for alerting
    expr: up == 0
    #up状态为0时
    for: 1m
    # Annotation - additional informational labels to store more information
    annotations:
      title: 'Instance down'
      description: Instance has been down for more than 1 minute.'
    # Labels - additional labels to be attached to the alert
    labels:
      severity: 'critical'
        #告警级别
~

编写yml文件


[root@prometheus /usr/local/prometheus-2.27.1.linux-amd64/alert-config/targets]# vim alertmanagers.yml
[root@prometheus /usr/local/prometheus-2.27.1.linux-amd64/targets]# cat alertmanagers.yml
- targets:
  - 192.168.37.100:9093
  labels:
    app: alertmanager

[root@prometheus /usr/local/prometheus-2.27.1.linux-amd64/alert-config/targets]# vim nodes-centos.yml
- targets:
  - 192.168.37.100:9100
  - 192.168.37.101:9100
  - 192.168.37.102:9100
  - 192.168.37.107:9100
  labels:
    app: node-exporter
    job: node
[root@prometheus /usr/local/prometheus-2.27.1.linux-amd64/alert-config/targets]# vim prometheus-servers.yml
- targets:
  - 192.168.37.100:9090
  labels:
    app: prometheus
    job: prometheus

目录结构图 

[root@prometheus /usr/local/prometheus-2.27.1.linux-amd64/alert-config]# tree
.
├── alert_rules
│   └── demo-down.yml
├── prometheus.yml
└── targets
    ├── alertmanagers.yml
    ├── nodes-centos.yml
    └── prometheus-servers.yml

 prometheus启动文件

[root@prometheus /usr/local/prometheus-2.27.1.linux-amd64/alert-config]# vim prometheus.yml
# my global config
# Author: MageEdu <mage@magedu>
# Repo: http://gitlab.magedu/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*.yml"

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  - "rules/*.yml"
  - "alert_rules/*.yml"

   # 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'.
    file_sd_configs:
    - files:
      - targets/prometheus-*.yml
      refresh_interval: 2m

    # All nodes
  - job_name: 'nodes'
    file_sd_configs:
    - files:
      - targets/nodes-*.yml
      refresh_interval: 2m
  - job_name: 'alertmanagers'
    file_sd_configs:
    - files:
      - targets/alertmanagers*.yml
      refresh_interval: 2m

指定文件启动prometheus

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

                故障模拟

关闭node——exporer

先看ui界面,此时断开状态

 查看邮箱

大功告成了!!!! 

本文标签: 邮箱功能prometheus