前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >prometheus alertmanager 部署监控(二)

prometheus alertmanager 部署监控(二)

作者头像
怀朔
发布2022-05-25 13:56:19
4140
发布2022-05-25 13:56:19
举报
文章被收录于专栏:运维入门时间

上回已经讲好快速部署prometheus alertmanager 这回接着如果配置报警,本文我主要以接入mysql报警为案例 进行全面的讲解 软加载监控报警

1、首先讲解一下prometheus.yml文件

代码语言:javascript
复制
global:
  scrape_interval:     1s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 1s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration 主要连接alertmanage
alerting:
  alertmanagers:
  - static_configs:
    - targets:
       - localhost:9093   
# 这里我设置了默认的alertmanager装在同一机器上
# -localhost:9093 
# 大家可以替换成 alertmanager_server_ip:9093      

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
# 装载的 具体报警规则
rule_files:
  -  /usr/local/prometheus/rules/*.rules
  # - "first_rules.yml"
  # - "second_rules.yml"
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'
    static_configs:
    - targets: ['172.17.0.159:9090']
  - job_name: mysql
  # 这里的加载方式主要用了file_sd_configs 
  #  软加载了配置文件 consul方法更佳  欢迎大神普及
    file_sd_configs:
     - files: ['/usr/local/prometheus/mysql/*.json']

2、讲解一下alertmanager文件

可以用邮箱 webhook 企业微信等等 我这里以钉钉为例

代码语言:javascript
复制
global:
  resolve_timeout: 1m
  smtp_smarthost: 'smtp.qiye.aliyun.com:465'
  smtp_from: 'monitor@xx.com'
  smtp_auth_username: 'monitor@xx.com'
  smtp_auth_password: 'xxxxxxxx#'
  smtp_require_tls: false
route:
  group_by: ['alertname']
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 10m
  receiver: 'webhook'
receivers:
- name: 'webhook'
 # email_configs:
 # - to: 'wml@zmjx.com'
  webhook_configs:
  #  没有什么可以讲的主要讲一下这个URL
  - url: 'http://localhost:8060/dingtalk/webhook1/send'
    send_resolved: true
inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['alertname', 'dev', 'instance']

2.1 prometheus-webhook-dingtalk git仓库地址

我躺了很多坑了 当然要感谢作者无限付出 希望作者能努力优化

1、推荐大家不要用会转义的字符

2、postman时候先试一下

3、最后使用releases 自己编译很坑 用docker启动可能会有转义问题(笔者测试的时候坑了 无数次 )

rpm -ivh prometheus-webhook-dingtalk-0.3.0.linux-amd64.tar.gz

2.2 添加机器人获取webhook地址 (该机器需要连接钉钉接口外网)

代码语言:javascript
复制
nohup ./prometheus-webhook-dingtalk --ding.profile="webhook1=https://oapi.dingtalk.com/robot/send?access_token=你机器人的token" &

这样就启动获取了一个URL地址:注意这个webhoo1 很关键的URL路径

代码语言:javascript
复制
http://启动机器的ip:8060/dingtalk/webhook1/send

3、配置服务器的规则及客户端的agent(可以配置任何一台机器)

3.1 prometheus 下面配置如下

代码语言:javascript
复制
     ###在该文件夹下添加json
     ##     - files: ['/usr/local/prometheus/mysql/*.json']
     ###   不要修改其他文件名 必须以*.json为结尾不然动态加载不出来
[root@grafana prometheus]# cat /usr/local/prometheus/mysql/usercenter.json
[
  {
    "targets": [
      "IP:9104"
    ],
    "labels": {
      "alias": "usercenter-master-mysql",
      "group": "mysql"
    }
  },
  {
    "targets": [
      "IP:10000"
    ],
    "labels": {
      "alias": "usercenter-slave-mysql",
      "group": "mysql"
    }
  }
]

3.2下载mysql_export(根据自己的需求,下载不同的版本)

代码语言:javascript
复制
 https://github.com/prometheus/mysqld_exporter

3.3启动agent代理配置

代码语言:javascript
复制
nohup /usr/local/prometheus/mysql/mysqld_exporter-0.12.0.linux-amd64/mysqld_exporter --config.my-cnf="/usr/local/prometheus/mysql/.trade.11000.master.cnf" --web.listen-address=":11000" &
代码语言:javascript
复制
[root@grafana prometheus]# cat /usr/local/prometheus/mysql/.my.cnf
[client]
user=DB_USER
password=DB_PASSWORD
host=DB_URL

3.4 查看prometheus下面的状态 监控的数据就会出现 state UP 状态证明prometheus已经捕获得DB信息 证明已经连接成功

3.5 测试报警webhook报警

首先这个需要要使用prometheus这个语法 以up为例

代码语言:javascript
复制
http://prometheus_server_ip:9093/graph?g0.range_input=1h&g0.expr=mysql_up&g0.tab=

使用例子

mysql_up

3.6最后设置rule规则

(不同的prometheus版本rule规则不一样 大家需要注意)

给个一个样本 大家自己参照

代码语言:javascript
复制
groups:
- name: OS info #规则组名称
  rules:
  - alert: MYSQL_UP
    expr: mysql_up < 1
    for: 1m
    labels:
      project: alias
    annotations:
      summary: " mysql is down {{$labels.alias}}"
      description: "mysql is down  VALUE = {{ $value }}\n  LABELS: {{ $labels }}"

3.7 钉钉出现报警 解决完出现 RESOLVED

另外推荐下面二个rule地址 非常全面 涉及覆盖点linux zk jmx mysql 希望能帮助 大家

代码语言:javascript
复制
https://awesome-prometheus-alerts.grep.to/rules.html
https://github.com/samber/awesome-prometheus-alerts

结合上下文总结 本方案有如下几点缺点

1、rule不能做到动态加载 需要重启prometheus

2、file_sd_config 方法手工配置较为麻烦 随着机器数量增加不足 较为麻烦

3、prometheus本身 对分布式支持不太友好(笔者自己认为)

最后谢谢过程中 帮助我的 好朋友 好基友 我这次踩坑也踩的比较严重!!

兑吧-小徐 二维火-糯米 微拍堂-男神等等基友 过程帮我解决问题的朋友!!!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-08-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 运维入门时间 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档