Prometheus

出自 ArchWiki

Prometheus 是一个开源的指标收集和处理工具。它主要由一个时间序列数据库和一个用于访问和处理其存储的指标的查询语言组成。独立的程序执行指标暴露,Prometheus 服务器可以从中拉取指标。它开箱即用提供了一个非常简单的 Web UI。为了获得功能齐全的仪表盘系统,可以使用像 Grafana 这样的第三方工具。

安装

安装 prometheus 软件包。之后你可以启用启动 prometheus.service 服务,并通过 HTTP 在默认端口 9090 访问该应用程序。

默认配置监控 prometheus 进程本身,但除此之外不多。为了执行系统监控,你可以安装 prometheus-node-exporter,它执行从本地系统抓取指标。你可以启动并启用 prometheus-node-exporter 服务。它将默认打开端口 9100。一旦服务运行,你将需要配置 prometheus 以定期抓取 exporter 服务,以便实际收集数据。按照下面显示的添加指标步骤进行操作。

警告: prometheus 的默认配置监听 *:9090prometheus-node-exporter 监听 *:9100,因此请确保更改配置或启用相关的防火墙规则。另请参阅 Prometheus 安全模型

配置

Prometheus 配置通过 YAML 文件完成,主配置文件位于 /etc/prometheus/prometheus.yml

添加指标

你可以通过将新的抓取目标添加到 scrape_configs 数组中,来添加新的指标抓取位置。要添加本地节点 exporter 作为源,与 prometheus 进程本身一起,配置如下所示

 scrape_configs:
   - job_name: 'prometheus'
     static_configs:
       - targets: ['localhost:9090']
   - job_name: 'node'
     static_configs:
       - targets: ['localhost:9100']

Exporter

Arch Linux 仓库包含 可用 exporter 的一个子集

Exporter 被实现为服务。例如,要运行 node exporter,启用启动 prometheus-node-exporter.service

使用 UI

Prometheus 自带一个非常有限的 Web UI,用于验证配置、查询和绘制指标图表。你可以通过 https://127.0.0.1:9090 在默认情况下访问它。你可以在 Prometheus 文档中找到对 Prometheus 查询语言的深入解释。

告警

alertmanager 可以在满足某些在 /etc/prometheus/alert.rules.yml 中配置的条件时发送自定义告警,而要发送什么告警在 /etc/alertmanager/alertmanager.yml 中配置。Alertmanager 支持多种通知用户的方式,例如电子邮件、Slack 和更多。要配置电子邮件告警,请添加以下代码段

global:
  resolve_timeout: 5m
  smtp_smarthost: 'smtp.example.com:25'
  smtp_from: 'alertmanager@example.com'
route:
  group_by: ['instance', 'severity']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 3h
  receiver: team-1
receivers:
  - name: 'team-1'
    email_configs:
      - to: 'admin@example.com'

为了让 prometheus 将告警发送到 alertmanager,请在 /etc/prometheus/prometheus.yml 中包含以下代码段

alerting:
  alertmanagers:
  - static_configs:
    - targets:
      - localhost:9093

要为 systemd 单元失败配置告警,请将以下代码段添加到 /etc/prometheus/alert.rules.yml。有关更多规则,请阅读告警规则文档。

- name: systemd_unit
  interval: 15s
  rules:
  - alert: systemd_unit_failed
    expr: |
      node_systemd_unit_state{state="failed"} > 0
    for: 3m
    labels:
      severity: critical
    annotations:
      description: 'Instance : Service failed'
      summary: 'Systemd unit failed'

技巧与诀窍

使用 Telegraf 代替 Exporter

当与 Prometheus 输出插件一起使用时,可以使用 Telegraf 代替多个 exporter。与标准的 Prometheus exporter 相比,这可以将指标收集减少到单个二进制文件,并提供更灵活的配置。

参见