Prometheus&Alertmanager告警推送
前言
尽管可以通过可视化数据监控系统运行状态,但我们无法时刻关注系统运行,因此需要一些实时运行的工具能够辅助监控系统运行,当系统出现运行问题时,能够通知我们,以此确保系统稳定性,告警便是作为度量指标监控中及其重要的一环。
Prometheus告警介绍
在Prometheus中,告警模块为Alertmanager,可以提供多种告警通道、方式来使得系统出现问题可以推送告警消息给相关人员。
Prometheus Server中的告警规则会向Alertmanager发送。Alertmanager管理这些告警,包括进行去重,分组和路由,以及告警的静默和抑制,通过电子邮件、实时通知系统和聊天平台等方法发送通知。
- 定义告警规则(Alerting rule)
- 告警模块消息推送(Alertmanager)
- 告警推送(Prometheus server推送告警规则到Alertmanager)
Alertmanager
Alertmanager是一个独立的告警模块,接收Prometheus server发来的告警规则,通过去重、分组、静默和抑制等处理,并将它们通过路由发送给正确的接收器。
核心概念
- 分组(Grouping): 是Alertmanager把同类型的告警进行分组,合并多条告警合并成一个通知。当系统中许多实例出问题时又或是网络延迟、延迟导致网络抖动等问题导致链接故障,会引发大量告警,在这种情况下使用分组机制,把这些被触发的告警合并为一个告警进行通知,从而避免瞬间突发性的接受大量告警通知,使得相关人员可以对问题快速定位,而不至于淹没在告警中。
- 抑制(Inhibition): 是当某条告警已经发送,停止重复发送由此告警引发的其他异常或故障的告警机制,以避免收到过多无用的告警通知。
- 静默(Silences): 可对告警进行静默处理的简单机制。对传进来的告警进行匹配检查,如果告警符合静默的配置,Alertmanager 则不会发送告警通知。
设置Alertmanager.yml
cd /opt
mkdir alertmanager
cd alertmanager
touch alertmanager.yml
配置文件主要包含以下几个部分:
- 全局配置(global):用于定义一些全局的公共参数,如全局的SMTP配置,Slack配置等内容;
- 模板(templates):用于定义告警通知时的模板,如HTML模板,邮件模板,企业微信模板等;
- 告警路由(route):根据标签匹配,确定当前告警应该如何处理;
- 接收人(receivers):接收人是一个抽象的概念,它可以是一个邮箱也可以是微信,Slack或者Webhook等,接收人一般配合告警路由使用;
配置文件内容:
global:
resolve_timeout: 2m
smtp_smarthost: 'smtp.qq.com:465'
smtp_from: your@qq.com
smtp_auth_username: your@qq.com
smtp_auth_password: 授权码
templates:
- /etc/alertmanager/template/*.tmpl
route:
group_by:
- alertname_wechat
group_wait: 10s
group_interval: 10s
receiver: wechat
repeat_interval: 1h
receivers:
- name: wechat
email_configs:
- to: otheremail@outlook.com
send_resolved: true
wechat_configs:
- corp_id: wechat_corp_id
to_party: wechat_to_party
agent_id: wechat_agent_id
api_secret: wechat_apisecret
send_resolved: true
此处设置推送消息到邮箱和企业微信,Alertmanager内置了对企业微信的支持。
https://prometheus.io/docs/alerting/latest/configuration/#wechat_config
设置消息模板
cd /opt/alertmanager
mkdir template
cd template
touch wechat.tmpl
编辑文件内容,设置模板格式
{{ define "wechat.default.message" }}
{{ range $i, $alert :=.Alerts }}
========监控报警==========
告警状态:{{ .Status }}
告警级别:{{ $alert.Labels.severity }}
告警类型:{{ $alert.Labels.alertname }}
告警应用:{{ $alert.Annotations.summary }}
告警主机:{{ $alert.Labels.instance }}
告警详情:{{ $alert.Annotations.description }}
触发阀值:{{ $alert.Annotations.value }}
告警时间:{{ $alert.StartsAt.Format "2023-02-19 10:00:00" }}
========end=============
{{ end }}
{{ end }}
部署Alertmanager
docker run -d -p 9093:9093 --name StarCityAlertmanager -v /opt/alertmanager/alertmanager.yml:/etc/alertmanager/alertmanager.yml -v /opt/alertmanager/template:/etc/alertmanager/template docker.io/prom/alertmanager:latest
告警通知测试
可通过请求Alertmanager Api模拟告警规则来推送告警通知。
curl --location 'http://Host:9093/api/v2/alerts' \
--header 'Content-Type: application/json' \
--data '[
{
"labels": {
"severity": "Warning",
"alertname": "内存使用过高",
"instance": "实例1",
"msgtype": "testing"
},
"annotations": {
"summary": "node",
"description": "请检查实例1",
"value": "0.95"
}
},
{
"labels": {
"severity": "Warning",
"alertname": "CPU使用过高",
"instance": "实例2",
"msgtype": "testing"
},
"annotations": {
"summary": "node",
"description": "请检查实例2",
"value": "0.90"
}
}
]'
发送完毕,可以在企业微信和邮件中收到告警通知,如在邮箱中收到信息。
注意:如果告警配置完毕,但测试时企业微信怎么也收不到消息,需要设置企业自建应用底部可信IP菜单
告警规则
设置告警规则
cd /opt/prometheus
touch rules.yml
告警规则内容
groups:
- name: example
rules:
- alert: InstanceDown
expr: up == 0
for: 10s
labels:
name: instance
severity: Critical
annotations:
summary: ' {{ $labels.appname }}'
description: ' The service stops running '
value: '{{ $value }}%'
- name: Host
rules:
- alert: HostMemory Usage
expr: >-
(node_memory_MemTotal_bytes - (node_memory_MemFree_bytes +
node_memory_Buffers_bytes + node_memory_Cached_bytes)) /
node_memory_MemTotal_bytes * 100 > 80
for: 10s
labels:
name: Memory
severity: Warning
annotations:
summary: ' {{ $labels.appname }} '
description: ' The instance memory usage exceeded 80%. '
value: '{{ $value }}'
- alert: HostCPU Usage
expr: >-
sum(avg without
(cpu)(irate(node_cpu_seconds_total{mode!='idle'}[5m]))) by
(instance,appname) > 0.65
for: 10s
labels:
name: CPU
severity: Warning
annotations:
summary: ' {{ $labels.appname }} '
description: The CPU usage of the instance is exceeded 65%.
value: '{{ $value }}'
- alert: HostLoad
expr: node_load5 > 4
for: 10s
labels:
name: Load
severity: Warning
annotations:
summary: '{{ $labels.appname }} '
description: ' The instance load exceeds the default value for 5 minutes.'
value: '{{ $value }}'
- alert: HostFilesystem Usage
expr: 1-(node_filesystem_free_bytes / node_filesystem_size_bytes) > 0.8
for: 10s
labels:
name: Disk
severity: Warning
annotations:
summary: ' {{ $labels.appname }} '
description: ' The instance [ {{ $labels.mountpoint }} ] partitioning is used by more than 80%.'
value: '{{ $value }}%'
- alert: HostDiskio
expr: 'irate(node_disk_writes_completed_total{job=~"Host"}[1m]) > 10'
for: 10s
labels:
name: Diskio
severity: Warning
annotations:
summary: ' {{ $labels.appname }} '
description: ' The instance [{{ $labels.device }}] average write IO load of the disk is high in 1 minute.'
value: '{{ $value }}iops'
- alert: Network_receive
expr: >-
irate(node_network_receive_bytes_total{device!~"lo|bond[0-9]|cbr[0-9]|veth.*|virbr.*|ovs-system"}[5m])
/ 1048576 > 3
for: 10s
labels:
name: Network_receive
severity: Warning
annotations:
summary: ' {{ $labels.appname }} '
description: ' The instance [{{ $labels.device }}] average traffic received by the NIC exceeds 3Mbps in 5 minutes.'
value: '{{ $value }}3Mbps'
- alert: Network_transmit
expr: >-
irate(node_network_transmit_bytes_total{device!~"lo|bond[0-9]|cbr[0-9]|veth.*|virbr.*|ovs-system"}[5m])
/ 1048576 > 3
for: 10s
labels:
name: Network_transmit
severity: Warning
annotations:
summary: ' {{ $labels.appname }} '
description: ' The instance [{{ $labels.device }}] average traffic sent by the network card exceeds 3Mbps in 5 minutes.'
value: '{{ $value }}3Mbps'
- name: Container
rules:
- alert: ContainerCPU Usage
expr: >-
(sum by(name,instance)
(rate(container_cpu_usage_seconds_total{image!=""}[5m]))*100) > 60
for: 10s
labels:
name: CPU
severity: Warning
annotations:
summary: '{{ $labels.name }} '
description: ' Container CPU usage over 60%.'
value: '{{ $value }}%'
- alert: ContainerMem Usage
expr: 'container_memory_usage_bytes{name=~".+"} / 1048576 > 1024'
for: 10s
labels:
name: Memory
severity: Warning
annotations:
summary: '{{ $labels.name }} '
description: ' Container memory usage exceeds 1GB.'
value: '{{ $value }}G'
修改prometheus.yml,增加告警规则,如下alerting和rule_files部分,重启prometheus。
global:
scrape_interval: 15s
evaluation_interval: 15s
alerting:
alertmanagers:
- static_configs:
- targets: ['Host:9093']
rule_files:
- "rules.yml"
scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- 'Prometheus Server Host:9090'
labels:
appname: Prometheus
- job_name: node
scrape_interval: 10s
static_configs:
- targets:
- 'Metrics Host:9100'
labels:
appname: node
- job_name: cadvisor
static_configs:
- targets:
- 'Metrics Host:58080'
- job_name: rabbitmq
scrape_interval: 10s
static_configs:
- targets:
- 'Metrics Host:9419'
labels:
appname: rabbitmq
再次访问prometheus可以看到告警规则
告警状态
状态 | 说明 |
---|---|
Inactive | 待激活状态,度量指标处在合适范围内。 |
Pending | 符合告警规则,但是低于配置的持续时间。这里的持续时间即rule里的FOR字段设置的时间。该状态下不发送告警通知。 |
Firing | 符合告警规则,而且超出设置的持续时间。该状态下发送告警到Alertmanager。 |
触发告警
当系统达到预定告警条件并超出设定的持续时间,则触发告警,推送告警消息到Alertmanager。
此处设置系统CPU使用率超过限定条件,可以在prometheus中看到CPU使用率告警规则达到Pending状态
当超过设定的持续时间,状态变更到Firing,消息推送到Alertmanager
在Alertmanager Web中可以看到推送过来的告警信息
在企业微信和邮箱中也收到告警信息
2023-02-23,望技术有成后能回来看见自己的脚步
Prometheus&Alertmanager告警推送的更多相关文章
- Prometheus+Grafana+Alertmanager实现告警推送教程 ----- 图文详解
前言 本文主要介绍的是Prometheus采集数据,通过Grafana加上PromQL语句实现数据可视化以及通过Alertmanage实现告警推送功能.温馨提示,本篇文章特长,2w多的文字加上几十张图 ...
- Docker系列——Grafana+Prometheus+Node-exporter微信推送(三)
在之前博文中,已经成功的实现了邮件推送.目前主流的办公终端,就是企业微信.钉钉.飞书.今天来分享下微信推送,我们具体来看. 企业微信 在配置企业微信推送时,需要有微信企业,具体如何注册.使用,另外百度 ...
- zabbix告警推送至个人微信
文章原文 自从接触zabbix后,就一直想着怎么才能把告警推送到个人微信上.有这样的想法主要是个人微信的使用频率远远要比钉钉,企业微信,邮箱,飞书等使用频率要高.比如我,就遇到过在周末的时候,因为没有 ...
- prometheus-alertmanager告警推送到钉钉
1. Prometheus告警简介 告警能力在Prometheus的架构中被划分成两个独立的部分.如下所示,通过在Prometheus中定义AlertRule(告警规则),Prometheus会周期性 ...
- pinpoint实现告警推送至钉钉和微信群
前言 在前面的文章中,我们学习了如何通过java实现将消息发送到钉钉.和将消息发送到微信群聊. 基于上述基础,我们今天来接入pinpoint的告警,发送到钉钉群. 实操前准备 开始之前,推荐阅读一下, ...
- Prometheus+alertmanager告警配置-2
prometheus 告警 prometheus 通过alertmanager进行告警 实现监控告警的步骤: 在prometheus中定义告警规则rule_files alertmanager配置告警 ...
- Docker系列——Grafana+Prometheus+Node-exporter钉钉推送(四)
近期搭建的服务器监控平台,来进行一个总结.主要分为监控平台的搭建.告警中心的配置以及消息的推送.推送的话,支持多种终端.具体详细可查看之前的博文,在这里罗列下,方便查看. Docker系列--Graf ...
- zabbix 布署实践【6 使用微信公众号-消息模版推送告警】
使用这个服务的前提是,你必须要有一个微信订阅号,或者公众号,并且是通过认证的号 因为认证过后的号才有模版消息和获取用户openid等信息的权限 ,如下,登录微信公众号的登录页后,底下有个接口权限的展示 ...
- 图文详解Prometheus监控+Grafana+Alertmanager告警安装使用
一:前言 一个服务上线了后,你想知道这个服务是否可用,需要监控.假如线上出故障了,你要先于顾客感知错误,你需要监控.还有对数据库,服务器的监控,等等各层面的监控. 近年来,微服务架构的流行,服务数越来 ...
- 一步步教你用Prometheus搭建实时监控系统系列(二)——详细分析拉取和推送两种不同模式
前言 本系列着重介绍Prometheus以及如何用它和其周边的生态来搭建一套属于自己的实时监控告警平台. 本系列受众对象为初次接触Prometheus的用户,大神勿喷,偏重于操作和实战,但是重要的概念 ...
随机推荐
- volatile关键字在并发中有哪些作用?
作者:小牛呼噜噜 | https://xiaoniuhululu.com 计算机内功.JAVA源码.职业成长.项目实战.面试相关资料等更多精彩文章在公众号「小牛呼噜噜」 前言 读过笔者之前的一篇文章J ...
- 【Shell脚本案例】案例1:服务器系统配置初始化
〇.目录 一.背景 新购买10台服务器,并安装Linux系统 目的:对操作系统进行配置的初始化 二.需求 1.设置时区并同步时间 2.禁用selinux安全机制 3.关闭防火墙(清空防火墙的默认策略, ...
- 保存sklearn中模型的两种方法(pickle、joblib)
保存sklearn中模型的两种方法(pickle.joblib) from sklearn import svm from sklearn import datasets clf = svm.SVC( ...
- 一篇文章教你实战Docker容器数据卷
在上一篇中,咱们对Docker中的容器数据卷做了介绍.已经知道了容器数据卷是什么?能干什么用.那么本篇咱们就来实战容器数据卷,Docker容器数据卷案例主要做以下三个案例 1:宿主机(也就是Docke ...
- Appium工具
1.安装 (1)jdk安装以及环境配置 a.jdk下载地址:https://www.oracle.com/cn/java/technologies/downloads/ 新建系统环境变量: b.编辑P ...
- 后疫情办公时代——你需要的多人同步协同编辑Demo(可粘贴可撤销)
新冠病毒的疫情使得在线办公成为了一个常态,这使得在线文档成为了时下的热点.其中在线协同表格是在线文档的重要一个组成部分,纯前端表格在在线协同表格上有着得天独厚的优势:本身已经实现了单人操作在线文档的基 ...
- 就聊聊不少小IT公司的技术总监
本文想告诉大家如下两个观点. 1 很多IT小公司的技术总监,论能力其实也就是相当于大公司的高级程序员. 2 程序员在职业发展过程中,绝对应该优先考虑进大厂或好公司.如果仅仅停留在小公司,由于小公司可能 ...
- [OpenCV实战]13 OpenCV中使用Mask R-CNN进行对象检测和实例分割
目录 1 背景介绍 1.1 什么是图像分割和实例分割 1.2 Mask-RCNN原理 2 Mask-RCNN在OpenCV中的使用 2.1 模型下载 2.2 模型初始化 2.3 模型加载 2.4 输出 ...
- ArcGIS Python判断数据是否存在
判断是程序编写的一个基本的操作,也是增强程序稳定性的重要方式.在ArcPy处理数据时,要保证数据存在才能做后续的操作,为源GIS提示使用arcpy自带的Exists函数可判断要素类.表.数据集.sha ...
- BBS升级版
BBS项目 项目的前期准备 1.django2.2 创建一个django目录 (需要配置环境变量和数据库) 'DIRS': [os.path.join(BASE_DIR, 'templates'), ...