搭建Prometheus监控系统
1、概述
Prometheus 是一款基于时序数据库的开源监控告警系统,Prometheus 生态系统包含了几个关键的组件:Prometheus server、Pushgateway、Alertmanager、Web UI 等,但是大多数组件都不是必需的,其中最核心的组件当然是 Prometheus server,它负责收集和存储指标数据,支持表达式查询,和告警的生成。接下来我们就来安装 Prometheus server。
2 安装Prometheus
Prometheus 可以支持多种安装方式,包括 Docker、Ansible、Chef、Puppet、Saltstack 等。下面介绍最简单的两种方式,一种是直接使用编译好的可执行文件,开箱即用
cd /usr/local
wget https://github.com/prometheus/prometheus/releases/download/v2.4.3/prometheus-2.4.3.linux-amd64.tar.gz
tar -zxvf prometheus-2.4.3.linux-amd64.tar.gz
mv prometheus-2.4.3.linux-amd64.tar.gz prometheus
cd prometheus
./prometheus —version
运行prometheus
nohup ./prometheus —config.file=prometheus.yml &
配置文件prometheus.yml
# 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:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
Prometheus 默认的配置文件分为四大块:
- global 块:Prometheus 的全局配置,比如
scrape_interval
表示 Prometheus 多久抓取一次数据,evaluation_interval
表示多久检测一次告警规则; - alerting 块:关于 Alertmanager 的配置,这个我们后面再看;
- rule_files 块:告警规则,这个我们后面再看;
- scrape_config 块:这里定义了 Prometheus 要抓取的目标,我们可以看到默认已经配置了一个名称为
prometheus
的 job,这是因为 Prometheus 在启动的时候也会通过 HTTP 接口暴露自身的指标数据,这就相当于 Prometheus 自己监控自己,虽然这在真正使用 Prometheus 时没啥用处,但是我们可以通过这个例子来学习如何使用 Prometheus;可以访问http://
192.168.83.6:9090/metrics
查看 Prometheus 暴露了哪些指标;
通过上面的步骤安装好 Prometheus 之后,我们现在可以开始体验 Prometheus 了。Prometheus 提供了可视化的 Web UI 方便我们操作,直接访问 http://
192.168.83.6:9090/
即可,它默认会跳转到 Graph 页面:http://192.168.83.6:9090/
第一次访问这个页面可能会不知所措,我们可以先看看其他菜单下的内容,比如:Alerts 展示了定义的所有告警规则,Status 可以查看各种 Prometheus 的状态信息,有 Runtime & Build Information、Command-Line Flags、Configuration、Rules、Targets、Service Discovery 等等。
实际上 Graph 页面才是 Prometheus 最强大的功能,在这里我们可以使用 Prometheus 提供的一种特殊表达式来查询监控数据,这个表达式被称为 PromQL(Prometheus Query Language)。通过 PromQL 不仅可以在 Graph 页面查询数据,而且还可以通过 Prometheus 提供的 HTTP API 来查询。查询的监控数据有列表和曲线图两种展现形式(对应上图中 Console 和 Graph 这两个标签)。
我们上面说过,Prometheus 自身也暴露了很多的监控指标,也可以在 Graph 页面查询,展开 Execute 按钮旁边的下拉框,可以看到很多指标名称,我们随便选一个,譬如:promhttp_metric_handler_requests_total
,这个指标表示 /metrics
页面的访问次数,Prometheus 就是通过这个页面来抓取自身的监控数据的。在 Console 标签中查询结果如下:
上面在介绍 Prometheus 的配置文件时,可以看到 scrape_interval
参数是 15s,也就是说 Prometheus 每 15s 访问一次 /metrics
页面,所以我们过 15s 刷新下页面,可以看到指标值会自增。在 Graph 标签中可以看得更明显:
3 **安装 Grafana**" class="reference-link">
3 **安装 Grafana**
虽然 Prometheus 提供的 Web UI 也可以很好的查看不同指标的视图,但是这个功能非常简单,只适合用来调试。要实现一个强大的监控系统,还需要一个能定制展示不同指标的面板,能支持不同类型的展现方式(曲线图、饼状图、热点图、TopN 等),这就是仪表盘(Dashboard)功能。
因此 Prometheus 开发了一套仪表盘系统 PromDash,不过很快这套系统就被废弃了,官方开始推荐使用 Grafana 来对 Prometheus 的指标数据进行可视化,这不仅是因为 Grafana 的功能非常强大,而且它和 Prometheus 可以完美的无缝融合。
Grafana 是一个用于可视化大型测量数据的开源系统,它的功能非常强大,界面也非常漂亮,使用它可以创建自定义的控制面板,你可以在面板中配置要显示的数据和显示方式,它 支持很多不同的数据源,比如:Graphite、InfluxDB、OpenTSDB、Elasticsearch、Prometheus 等,而且它也 支持众多的插件。
cd /data/local
wget https://dl.grafana.com/oss/release/grafana-6.6.0.linux-amd64.tar.gz
tar -zxvf grafana-6.6.0.linux-amd64.tar.gz
mv grafana-6.6.0 grafana
cd grafana/bin
nohup ./grafana-server &
访问 http://192.168.83.6:3000/
进入 Grafana 的登陆页面,输入默认的用户名和密码(admin/admin)即可
添加一个data source
收集服务器指标
首先我们来收集服务器的指标,这需要安装 node_exporter,这个 exporter 用于收集 *NIX 内核的系统,如果你的服务器是 Windows,可以使用 WMI exporter。
和 Prometheus server 一样,node_exporter 也是开箱即用的:
cd /usr/local
wget https://github.com/prometheus/node\_exporter/releases/download/v0.16.0/node\_exporter-0.16.0.linux-amd64.tar.gz
tar -zxvf node_exporter-0.16.0.linux-amd64.tar.gz
mv node_exporter-0.16.0.linux-amd64 node_exporter
cd node_exporter
./node_exporter
node_exporter 启动之后,我们访问下 http://192.168.83.6:9100/metrics
接口看看是否能正常获取服务器指标
如果一切 OK,我们可以修改 Prometheus 的配置文件,将服务器加到 scrape_configs
中:
vi /usr/local/prometheus/prometheus.yml
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['192.168.83.6:9090']
- job_name: 'server'
static_configs:
- targets: ['192.168.83.6:9100']
修改配置后,需要重启 Prometheus 服务,在Status的Target可以看到
收集 MySQL 指标
mysqld_exporter 是 Prometheus 官方提供的一个 exporter,我们首先 下载最新版本 并解压(开箱即用)
默认的配置文件是 ~/.my.cnf
,或者通过 --config.my-cnf
参数指定
cd /usr/local
wget https://github.com/prometheus/mysqld\_exporter/releases/download/v0.11.0/mysqld\_exporter-0.11.0.linux-amd64.tar.gz
tar -zxvf mysqld_exporter-0.11.0.linux-amd64.tar.gz
mv mysqld_exporter-0.11.0.linux-amd64 mysqld_exporter
cd mysqld_exporter
./mysqld_exporter —config.my-cnf=”.my.cnf”
node_exporter 启动之后,我们访问下 http://192.168.83.6:9104/metrics
接口看看是否能正常获取服务器指标
修改 Prometheus 的配置文件,将服务器加到 scrape_configs
中
vi /usr/local/prometheus/prometheus.yml
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['192.168.83.6:9090']
- job_name: 'server'
static_configs:
- targets: ['192.168.83.6:9100']
- job_name: 'mysql'
static_configs:
- targets: ['192.168.83.6:9104']
修改配置后,需要重启 Prometheus 服务,在Status的Target可以看到
收集JMX 指标
最后让我们来看下如何收集 Java 应用的指标,Java 应用的指标一般是通过 JMX(Java Management Extensions) 来获取的,顾名思义,JMX 是管理 Java 的一种扩展,它可以方便的管理和监控正在运行的 Java 程序。
mkdir -p /usr/local/jmx_exporter
cd /usr/local/jmx_exporter
wget https://repo1.maven.org/maven2/io/prometheus/jmx/jmx\_prometheus\_javaagent/0.3.1/jmx\_prometheus\_javaagent-0.3.1.jar
java -javaagent:jmx_prometheus_javaagent-0.3.1.jar=9404:config.yml -jar spring-boot-sample-1.0-SNAPSHOT.jar
新增配置文件config.yml
cd /usr/local/jmx_exporter/
vi config.yml
rules:
- pattern: ".*"
其中,9404 是 JMX Exporter 暴露指标的端口,config.yml
是 JMX Exporter 的配置文件,它的内容可以 参考 JMX Exporter 的配置说明
5 告警和通知
至此,我们能收集大量的指标数据,也能通过强大而美观的面板展示出来。不过作为一个监控系统,最重要的功能,还是应该能及时发现系统问题,并及时通知给系统负责人,这就是 Alerting(告警)。
Prometheus 的告警功能被分成两部分:一个是告警规则的配置和检测,并将告警发送给 Alertmanager,另一个是 Alertmanager,它负责管理这些告警,去除重复数据,分组,并路由到对应的接收方式,发出报警。常见的接收方式有:Email、PagerDuty、HipChat、Slack、OpsGenie、WebHook 等。
配置告警规则
在上面介绍 Prometheus 的配置文件时了解到,它的默认配置文件 prometheus.yml 有四大块:global
、alerting
、rule_files
、scrape_config
,其中 rule_files
块就是告警规则的配置项,alerting 块用于配置 Alertmanager,这个我们下一节再看。现在,先让我们在 rule_files
块中添加一个告警规则文件:
rule_files:
- "alert.rules"
然后参考 官方文档,创建一个告警规则文件 alert.rules
:
groups:
- name: example
rules:
# Alert for any instance that is unreachable for >5 minutes.
- alert: InstanceDown
expr: up == 0
for: 5m
labels:
severity: page
annotations:
summary: "Instance {
{ $labels.instance }} down"
description: "{
{ $labels.instance }} of job {
{ $labels.job }} has been down for more than 5 minutes."
# Alert for any instance that has a median request latency >1s.
- alert: APIHighRequestLatency
expr: api_http_request_latencies_second{quantile="0.5"} > 1
for: 10m
annotations:
summary: "High request latency on {
{ $labels.instance }}"
description: "{
{ $labels.instance }} has a median request latency above 1s (current value: {
{ $value }}s)"
这个规则文件里,包含了两条告警规则:InstanceDown
和 APIHighRequestLatency
。顾名思义,InstanceDown 表示当实例宕机时(up === 0)触发告警,APIHighRequestLatency 表示有一半的 API 请求延迟大于 1s 时(api_http_request_latencies_second{quantile="0.5"} > 1
)触发告警。
配置好后,需要重启下 Prometheus server,然后访问 http://192.168.83.6:9090/rules
可以看到刚刚配置的规则:
访问http://192.168.83.6:9090/alerts,
可以看到根据配置的规则生成的告警:
使用 Alertmanager 发送告警通知
虽然 Prometheus 的 /alerts
页面可以看到所有的告警,但是还差最后一步:触发告警时自动发送通知。这是由 Alertmanager 来完成的,我们首先 下载并安装 Alertmanager,和其他 Prometheus 的组件一样,Alertmanager 也是开箱即用的:
cd /usr/local
wget https://github.com/prometheus/alertmanager/releases/download/v0.15.2/alertmanager-0.15.2.linux-amd64.tar.gz
tar -zxvf alertmanager-0.15.2.linux-amd64.tar.gz
cd alertmanager-0.15.2.linux-amd64
./alertmanager
Alertmanager 启动后默认可以通过 http://192.168.83.6:9093/
来访问,但是现在还看不到告警,因为我们还没有把 Alertmanager 配置到 Prometheus 中,我们回到 Prometheus 的配置文件 prometheus.yml
,添加下面几行:
alerting:
alertmanagers:
- scheme: http
static_configs:
- targets:
- "192.168.83.6:9093"
重启prometheus之后访问 Alertmanager,可以看到 Alertmanager 已经接收到告警了:
下面的问题就是如何让 Alertmanager 将告警信息发送给我们了,我们打开默认的配置文件 alertmanager.ym
:
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: 'xxx' # 邮箱授权码
route:
group_by: ['alertname']
group_wait: 10s # 最初即第一次等待多久时间发送一组警报的通知
group_interval: 10s # 在发送新警报前的等待时间
repeat_interval: 1m # 发送重复警报的周期
receiver: 'mail-receiver' # 对应receivers的name属性
receivers:
- name: 'mail-receiver'
email_configs:
- to: 'xxx@163.com' #邮件接收地址
send_resolved: false
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
重启Alertmanager
紧接着,receivers 块表示告警通知的接收方式,每个 receiver 包含一个 name 和一个 xxx_configs,不同的配置代表了不同的接收方式,Alertmanager 内置了下面这些接收方式:
- email_config
- hipchat_config
- pagerduty_config
- pushover_config
- slack_config
- opsgenie_config
- victorops_config
- wechat_configs
- webhook_config
虽然接收方式很丰富,但是在国内,其中大多数接收方式都很少使用。最常用到的,莫属 email_config
和 webhook_config
,另外 wechat_configs
可以支持使用微信来告警,也是相当符合国情的了。
其实告警的通知方式是很难做到面面俱到的,因为消息软件各种各样,每个国家还可能不同,不可能完全覆盖到,所以 Alertmanager 已经决定不再添加新的 receiver 了,而是推荐使用 webhook 来集成自定义的接收方式。可以参考 这些集成的例子,譬如 将钉钉接入 Prometheus AlertManager WebHook。
还没有评论,来说两句吧...