Prometheus alertmanager告警 搭建监控系统六(基础篇)

配置报警规则

Alertmanager部署

  • 下载二进制包
https://prometheus.io/download/

安装alertmanager

  • 安装步骤
[root@prometheus ~]# tar xf alertmanager-0.20.0-rc.0.linux-amd64.tar.gz -C /usr/local/
[root@prometheus ~]# ln -sv /usr/local/alertmanager-0.20.0-rc.0.linux-amd64/ /usr/local/Prometheus_alertmanager/
[root@prometheus ~]# cd /usr/local/Prometheus_alertmanager/

配置alertmanager邮件告警

  • 修改alertmanager配置文件
[root@prometheus Prometheus_alertmanager]# cp alertmanager.yml alertmanager.yml.bak
[root@prometheus Prometheus_alertmanager]# vim alertmanager.yml
global:
  resolve_timeout: 5m
  smtp_smarthost: 'smtp.163.com:25'      # smtp地址
  smtp_from: 'xxx@163.com'               # 谁发邮件
  smtp_auth_username: 'xxx@163.com'      # 邮箱用户
  smtp_auth_password: 'xxxxx'            # 邮箱客户端授权密码
  smtp_require_tls: false
  
route:                                   # route用来设置报警的分发策略
  group_by: ["alertname"]                # 分组名
  group_wait: 30s                        # 当收到告警的时候,等待三十秒看是否还有告警,如果有就一起发出去
  group_interval: 30s                    # 发送警告间隔时间
  repeat_interval: 20m                   # 重复报警的间隔时间
  receiver: Node_warning                 # 设置默认接收人,如果想分组接收,把下面这段的注释去掉
#  routes:                                # 可以指定哪些组接收哪些消息 
#  - receiver: 'Node_warning'
#    continue: true
#    group_wait: 10s
#    match_re:
#      service: mysql|cassandra           # 所有service=mysql或者service=cassandra的告警分配到数据库接收端
#  - receiver: 'MySQL_warning'  
#    group_wait: 10s
#    match_re:                            # 根据标签分组,匹配标签dest=szjf的为fping-receiver组
#      serverity: warning

receivers:                               # 定义接收者,将告警发送给谁
- name: 'Node_warning'
  email_configs:
  - to: 'xxx@126.com'
#- name: 'MySQL_warning'
#  email_configs:
#  - to: 'xxx@qq.com'

测试

  • 检查配置
[root@prometheus Prometheus_alertmanager]# ./amtool check-config alertmanager.yml
Checking 'alertmanager.yml'  SUCCESS
Found:
 - global config
 - route
 - 0 inhibit rules
 - 1 receivers
 - 0 templates

测试成功

  • 配置systemd启动alertmanager
[root@prometheus ~]# vim /lib/systemd/system/alertmanager.service
[Unit]
Description=Alertmanager
After=network.target

[Service]
ExecStart=/usr/local/Prometheus_alertmanager/alertmanager --config.file='/usr/local/Prometheus_alertmanager/alertmanager.yml'

[Install]
WantedBy=multi-user.target

# 重载配置并设置开机自启
[root@prometheus ~]# systemctl daemon-reload
[root@prometheus ~]# systemctl start alertmanager
[root@prometheus ~]# systemctl enable alertmanager

在web中使用url:http://alertmanager_ip:9093,会显示alertmanager的界面

建立通信

  • 修改prometheus配置文件,添加alertmanager通讯地址
[root@prometheus ~]# cd /usr/local/Prometheus
[root@prometheus Prometheus]# vim prometheus.yml
# 建立通信
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      - 127.0.0.1:9093

# 规则读取路径(可配置多个)
rule_files:
  - "rules/node_rules.yml"
#   - "rules/mysql_rules.yml"
  • 配置告警规则
[root@prometheus Prometheus]# mkdir rules
[root@prometheus Prometheus]# vim rules/node_rules.yml
groups:
- name: test
  rules:
  - alert: 内存使用率过高
    expr: 100-(node_memory_Buffers_bytes+node_memory_Cached_bytes+node_memory_MemFree_bytes)/node_memory_MemTotal_bytes*100 > 90
    for: 30s                     # 告警持续时间,超过这个时间才会发送给alertmanager
    labels:
      severity: warning
    annotations:
      summary: "Instance {{ $labels.instance }} 内存使用率过高"
      description: "{{ $labels.instance }} of job {{$labels.job}}内存使用率超过80%,当前使用率[{{ $value }}]."

  - alert: cpu使用率过高
    expr: 100-avg(irate(node_cpu_seconds_total{mode="idle"}[5m])) by(instance)*100 > 90
    for: 30s
    labels:
      severity: warning
    annotations:
      summary: "Instance {{ $labels.instance }} cpu使用率过高"
      description: "{{ $labels.instance }} of job {{$labels.job}}cpu使用率超过80%,当前使用率[{{ $value }}]."

测试规则

  • 检查告警规则,重载prometheus
[root@prometheus Prometheus]# curl -XPOST http://localhost:9090/-/reload
  • 在prometheus界面的alert可以看到告警状态
- 绿色表示正常。
- 红色状态为PENDING表示alerts还没有发送至Alertmanager,因为rules里面配置了for: 30s。
- 30s后状态由PENDING变为FIRING,此时,prometheus才将告警发给alertmanager,在Alertmanager中可以看到有一个alert。 
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,406评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,732评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,711评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,380评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,432评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,301评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,145评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,008评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,443评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,649评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,795评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,501评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,119评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,731评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,865评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,899评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,724评论 2 354

推荐阅读更多精彩内容