在讲解prometheus的时候我们说其具有告警的特征,也就是prometheus在收集监控数据的时候会根据规则判断相应指标是否达到了告警上线然后使用推送的方式进行告警。但是要明确的一点是prometheus的仅仅是用来收集和查询监控数据的,要让我们的prometheus具有告警功能还需要prometheus体系的另一个组件altermanger,这块我们大概的讲解一下。
https://prometheus.io/download/
下载好altermanager之后,我们解压。其中的altermanager.yml是altermanager的配置文件。主要用来管理告警信息发送的规则,也就是说给谁发,用那种方式。
这块作者简单测试了一下监控mysql的线程数的告警。首先配置一下prometheus的数据收集的规则和push告警信息的地址。
代码语言:javascript复制groups:
- name: test-mysql-rule
rules:
- alert: "连接数报警"
expr: mysql_global_variables_mysqlx_max_connections > 90 #连接数大于90就告警
for: 1s
labels:
severity: warning
annotations:
summary: "服务名:{{$labels.alertname}}"
description: "业务msyql连接数不够报警: 当前值为:{{ $value }}"
value: "{{ $value }}"
在prometheus配置文件中添加这个告警规则
代码语言:javascript复制# my global config
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:
- static_configs:
- targets: ["localhost:9093"] #发送到altermanager的告警分析中
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "tianjingle_rules.yml"
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=` to any timeseries scraped from this config.
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
# 添加msyql的监控
- job_name: 'mysql'
# 静态添加node
static_configs:
# 指定监控端
- targets: ['localhost:9104']
- job_name: 'bounter-monitor'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:8081']
配置好上述配置之后对prometheus重启。并在prometheus的alter栏目中查看告警是否触发。发现已经触发了告警配置。
在配置好prometheus的告警之后,我们需要配置altermanager的告警信息路由规则。我们修改altermanager.yml文件。
代码语言:javascript复制global:
resolve_timeout: 5m
smtp_smarthost: 'smtp.qq.com:465'
smtp_from: '2695062879@qq.com'
smtp_auth_username: '2695062879@qq.com'
smtp_auth_password: '********'
smtp_require_tls: false
route:
receiver: mail
receivers:
- name: 'mail'
email_configs:
- to: '2695062879@qq.com'
配置好qq邮箱的报警提醒之后,我们重启altermanager。然后看一下是否会收到报警的邮件。稍等片刻,在邮箱中收到了邮件,如图。
早~