使用prometheus实现邮件/企业微信告警(代码片段)

愿许浪尽天涯 愿许浪尽天涯     2022-12-01     669

关键词:

一、安装 AlterManager


[root@k8s-master01 ~]# wget https://github.com/prometheus/alertmanager/releases/download/v0.24.0/alertmanager-0.24.0.linux-amd64.tar.gz
[root@k8s-master01 ~]# tar xf alertmanager-0.24.0.linux-amd64.tar.gz
[root@k8s-master01 ~]# mv alertmanager-0.24.0.linux-amd64 /usr/local/alertmanager
[root@k8s-master01 ~]# nohup /usr/local/alertmanager/alertmanager &

配置文件说明:

  • 全局配置 global:用于定义一些全局配置;
  • 模板 templates:告警时的通知模板(如果没有配置,将自动使用默认的模板)
  • 告警路由 route:通过标签匹配的方式,确定当前告警应当如何处理;
  • 接收人 receivers:配合告警路由使用,定义了接收人的通信方式;
  • 抑制规则 inhibit_rules:合理设置抑制规则可以减少垃圾告警的产生;
global:
  [ resolve_timeout: <duration> | default = 5m ]
  [ smtp_from: <tmpl_string> ] 
  [ smtp_smarthost: <string> ] 
  [ smtp_hello: <string> | default = "localhost" ]
  [ smtp_auth_username: <string> ]
  [ smtp_auth_password: <secret> ]
  [ smtp_auth_identity: <string> ]
  [ smtp_auth_secret: <secret> ]
  [ smtp_require_tls: <bool> | default = true ]
  [ wechat_api_url: <string> | default = "https://qyapi.weixin.qq.com/cgi-bin/" ]
  [ wechat_api_secret: <secret> ]
  [ wechat_api_corp_id: <string> ]
  [ http_config: <http_config> ]

templates:
  [ - <filepath> ... ]

route: <route>

receivers:
  - <receiver> ...

inhibit_rules:
  [ - <inhibit_rule> ... ]

二、配置邮件告警

1)修改 AlertManager 配置文件

[root@k8s-master01 ~]# cat <<END > /usr/local/alertmanager/alertmanager.yml
global:
  resolve_timeout: 3m
  smtp_smarthost: 'smtp.163.com:25'
  smtp_from:  'ChenZhuang1217@163.com'
  smtp_auth_username: 'ChenZhuang1217@163.com'
  smtp_auth_password: '邮件授权码'
  smtp_hello: '163.com'
  smtp_require_tls: false

route:
  group_by: ['env','instance','type','group','job','alertname']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 1h
  receiver: default

  routes:
  - receiver: email
    group_wait: 10s
    match:
      severity: Error

receivers:
- name: 'default'
  email_configs:
  - to: '2085077346@qq.com'
    send_resolved: true

- name: 'email'
  email_configs:
  - to: 'ChenZhuang1217@163.com'
    send_resolved: true
END

配置说明:

  • resolve_timeout:在定义的时间内,若没有继续产生告警,才会发送恢复消息;
  • smtp_require_tls:是否使用 TLS 协议;
  • group_by:将具有相同属性的告警进行分组聚合;
  • group_wait:发送告警前的等待时间;
  • group_interval:发送告警的时间间隔;
  • repeat_interval:分组内 发送相同告警的时间间隔;
  • receiver:指定的接收器(和 receivers 配置里的名字匹配)
  • routes:子路由,通过 匹配告警规则中的标签,来发送到指定的接收人;
  • send_resolved:是否通知已经解决的告警;

[root@k8s-master01 ~]# /usr/local/alertmanager/amtool check-config alertmanager.yml			# 检查配置
[root@k8s-master01 ~]# curl -X POST http://192.168.1.1:9093/-/reload						# 加载配置文件

2)修改 Prometheus 配置文件

[root@k8s-master01 ~]# cat <<END > /usr/local/prometheus/prometheus.yml
global:
  scrape_interval: 15s
  evaluation_interval: 15s

alerting:
  alertmanagers:
  - static_configs:
    - targets: ["192.168.1.1:9093"]

rule_files:
- "/usr/local/prometheus/rules.yml"

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]

  - job_name: "Linux"
    static_configs:
      - targets: ["192.168.1.1:9100"]
END

3)编写告警规则

  • alert:告警名称;
  • expr:PromQL 查询语句,查询是否满足告警条件;
  • for:评估等待时间,当条件成立一段时间后,才会发送告警(在此期间处于告警处于 Pending 状态)
  • labels:自定义标签,用户可以通过该标签进行路由匹配、告警展示等功能;
  • annotations:附加信息,只能用于告警展示;
[root@k8s-master01 ~]# cat <<"END" > /usr/local/prometheus/rules.yml
groups:
- name: 主机监控
  rules:
  - alert: TargetDown
    expr: upjob="Linux" == 0
    for: 15s
    labels:
      severity: Error
    annotations:
      summary: " $labels.job  主机已经超过 15s 未响应"
      description: " $labels.instance  主机宕机"
  
  - alert: NodeFilesystemUsage
    expr: 100 - (node_filesystem_free_bytesfstype=~"ext4|xfs" / node_filesystem_size_bytesfstype=~"ext4|xfs" * 100) > 80
    for: 1m
    labels:
      severity: Warning
    annotations:
      Summary: "Instance  $labels.instance :  $labels.mountpoint  分区使用率过高"
      Description: " $labels.instance :  $labels.mountpotint  分组使用大于 80% (当前值:  $value )"
END
  • 上面定义的那些告警规则,其实都是通过 PromQL 语句在 Prometheus 里查询出来的结果。

[root@k8s-master01 ~]# /usr/local/prometheus/promtool check config /usr/local/prometheus/prometheus.yml
[root@k8s-master01 ~]# curl -X POST http://192.168.1.1:9090/-/reload

验证:

1)验证时,我们只需要将 node_exporter 关闭即可。

2)再重新打开 node_exporter

Prometheus Alerts 三种状态:

  • Inactive:正常状态,表示还未产生告警。
  • Pending:等待状态。
  • Firing:告警状态,通过 AlertManager 配置文件中定义的信息,发送给指定的接收者。

三、配置企业微信告警

1)注册企业微信:地址

2)创建应用


3)配置可信 IP(如果不配置可信 IP 的话,将导致无法发送告警消息

[root@k8s-master01 ~]# curl httpbin.org/ip									# 获取当前主机的公网 IP

4)查看企业ID


1)修改 AlertManager 配置文件

[root@k8s-master01 ~]# cat <<END > /usr/local/alertmanager/alertmanager.yml
global:
  resolve_timeout: 3m
  wechat_api_url: "https://qyapi.weixin.qq.com/cgi-bin/"
  wechat_api_secret: "Secret"
  wechat_api_corp_id: "企业微信ID"

templates:
  - '/usr/local/alertmanager/wechat.tmpl'

route:
  group_by: ['env','instance','type','group','job','alertname']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 1h
  receiver: wechat

receivers:
- name: 'wechat'
  wechat_configs:
  - agent_id: "AgentId"
    to_party: "部门ID"
    message: ' template "wechat.default.message" . '
    send_resolved: true
END
  • 因为上面我们在配置邮件告警的时候,已经将告警规则配置上了,所以下面我们直接配置告警模板即可。

2)配置告警模板

[root@k8s-master01 ~]# cat <<"END" > /usr/local/alertmanager/wechat.tmpl
 define "wechat.default.message" 
- if gt (len .Alerts.Firing) 0 -
- range $index, $alert := .Alerts -
- if eq $index 0 
======== 监控报警 ========
告警状态:   .Status 
告警级别: .Labels.severity 
告警类型: .Labels.alertname 
故障主机: .Labels.instance 
告警主题: .Annotations.summary 
告警详情: .Annotations.description ;
故障时间: .StartsAt.Format "2006-01-02 15:04:05" 
======== = END = ========
- end 
- end 
- end 

- if gt (len .Alerts.Resolved) 0 -
- range $index, $alert := .Alerts -
- if eq $index 0 
======== 异常恢复 ========
告警类型: .Labels.alertname 
告警状态: .Status 
告警主题: .Annotations.summary 
告警详情: .Annotations.description ;
故障时间: .StartsAt.Format "2006-01-02 15:04:05" 
恢复时间: .EndsAt.Format "2006-01-02 15:04:05" 
- if gt (len $alert.Labels.instance) 0 
实例信息: $alert.Labels.instance 
- end 
======== = END = ========
- end 
- end 
- end 
- end 
END
  • 如果告警时间显示的是 UTC 时区,可以将其配置为 (.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05"

3)验证

[root@k8s-master01 ~]# curl -X POST http://192.168.1.1:9093/-/reload

重新打开 node_exporter

使用prometheus实现邮件/企业微信告警(代码片段)

一、安装AlterManager如果没有安装Prometheus以及监控客户端的话,可以看博主前面的文章:Prometheus(普罗米修斯)监控系统[root@k8s-master01~]#wgethttps://github.com/prometheus/alertmanager/releases/download/v0.24.0/alertmanager-0.24.0.linu... 查看详情

三分钟实现prometheus电话短信邮件钉钉飞书企业微信报警

Prometheus是现在企业用的比较多的开源监控系统,Prometheus电话短信报警更是运维不可缺少的报警渠道,Spug推送助手针对Prometheus内置好了报警模板,可以通过简单的配置就可以实现Prometheus电话、短信、邮件、钉钉、飞书、企业微... 查看详情

prometheus实现邮件告警(代码片段)

一:Prometheus告警机制简介警报一直是整个监控系统中的重要组成部分,Prometheus监控系统中,采集与警报是分离的。警报规则在Prometheus定义,警报规则触发以后,才会将信息转发到给独立的组件Alertmanager,... 查看详情

prometheus实现邮件告警(代码片段)

一:Prometheus告警机制简介警报一直是整个监控系统中的重要组成部分,Prometheus监控系统中,采集与警报是分离的。警报规则在Prometheus定义,警报规则触发以后,才会将信息转发到给独立的组件Alertmanager,... 查看详情

prometheus实现邮件告警(代码片段)

一:Prometheus告警机制简介警报一直是整个监控系统中的重要组成部分,Prometheus监控系统中,采集与警报是分离的。警报规则在Prometheus定义,警报规则触发以后,才会将信息转发到给独立的组件Alertmanager,... 查看详情

极客时间运维进阶训练营第二十周作业-待完成

1、Prometheus基于consul实现服务发现,并总结服务发现过程2、Prometheus监控JAVA服务(Tomcat)、Redis、MySQL、HAProxy3、总结Prometheus基于exporter进行指标数据采集的流程4、Prometheus集合AlertManager实现邮件、钉钉、微信告警基于钉钉告警模板与... 查看详情

技术分享|如何使用prometheus实现系统监控报警邮件通知(代码片段)

上一篇关于Prometheus的文章中说到了Prometheus是如何实现进程监控。在实际的线上环境中,当系统进程出现异常后需要实时通知到值班运维人员,去检查系统是否还正常运转。下面我们就介绍下基于Prometheus如何实现监控报... 查看详情

k8s系列-prometheus使用邮件告警(代码片段)

...服务名称为alertmanager,端口为9093。告警规则文件为“/etc/prometheus/rules/”目录下的所有规则文件。global:scrape_interval:15s#Setthescrapeintervaltoevery15seconds.Defaultisevery1minute.evaluation_interval:15s#Evaluaterulesevery15seconds.Thedefaultisevery1minute.#scrape_t... 查看详情

3.prometheus+grafana实现可视化告警(代码片段)

3.Prometheus+Grafana实现可视化、告警3.1.什么是Grafana3.2.快速入门3.3.Grafana的下载3.4.启动grafana3.5.配置数据源3.6.配置面板3.7.邮件通道配置3.8.指标告警配置3.9.Dashboards模板下载3.Prometheus+Grafana实现可视化、告警转自:https://www.... 查看详情

prometheus+grafana+alertmanager+邮件+钉钉告警

Prometheus+Grafana+alertmanager+邮件+钉钉告警本文模拟生产环境一ansible部署ansbile部署在线安装yuminstallansible-y离线安装#离线环境,提前在有网络的服务器上下载好需要的软件包mkdir-p/home/ansibleyuminstallansible-y--downloadonly--downloaddir/home/ansib... 查看详情

prometheus配置alertmanager告警邮件通知

参考技术A位置:/etc/prometheus/prometheus.yml修改以下内容,targets填入alertmanager的ip和端口位置:/etc/alertmanager/alertmanager.yml注意:这里用的是163的邮箱测试的,开启tls加密一直没调通,不开tls可以,不开tls的smtp端口是25位置:/etc/alert... 查看详情

devops之prometheus实现优雅的告警

参考技术A目前prometheus的告警,常用的有grafana自带的告警和prometheus插件alertmanger的告警两种,这里测试下alertmanger的告警功能。综合考虑,配合上prometheusoperator,使用alertmanger,能够使监控告警这块的工作更加devops。prometheusoperato... 查看详情

prometheus通过企业微信机器人报警

一、prometheus告警逻辑prometheus主服务通过警报规则(rules)去推送到alertmanager,这些规则将使用我们收集的指标并在指定的阈值或标准上触发警报,收到警报后, Alertmanager 会处理警报并根据其标签进行路由。一旦路径确... 查看详情

zabbix实现企业微信(wechat)告警(代码片段)

根据https://www.linuxprobe.com/zabbix-alert-with-wechat.html搭建成功大致记录1.环境不一致,导致执行python脚本不成功,后更新阿里yum源解决2.python脚本使用参考地址https://github.com/X-Mars/Zabbix-Alert-WeChat3.配置zabbix用户要注意权限 查看详情

prometheusalertmanager的使用(邮件和微信告警)

...置二进制包下载解压后即可使用,官网地址:​​https://prometheus.io/download/​​1.上传下载的alertmanager-0.24.0.linux-amd64.tar.gz到、/user/localcd2.解压缩tarzxvfalertmanager-0.24.0.linux-amd64.tar.gz-C/usr/local3.改名mv/usr/local/alertmanager-0.24.0.linux-amd64/us... 查看详情

prometheus告警功能

prometheus告警功能Prometheus对指标的收集、存储同告警能力分属于PrometheusServer和AlertManager(通用的组件,可由企业自行开发)两个独立的组件,前者仅负责基于"告警规则"生成告警通知,具体的告警操作则由后者完成... 查看详情

如何使用 prometheus 的 AlertManager 设置/触发电子邮件警报

】如何使用prometheus的AlertManager设置/触发电子邮件警报【英文标题】:Howtoset/triggeremailalertusingAlertManagerofprometheus【发布时间】:2019-07-2018:21:00【问题描述】:如何使用Prometheus的Alert-Manager设置/触发电子邮件警报Prometheus的Alert-Manag... 查看详情

k8s结合prometheus构建企业级监控告警系统

深度解读Prometheus什么是Prometheus?Prometheus是一个开源的系统监控和报警系统,现在已经加入到CNCF基金会,成为继k8s之后第二个在CNCF托管的项目,在kubernetes容器管理系统中,通常会搭配prometheus进行监控,... 查看详情