开源监控系统Prometheus配置说明

Prometheus可以通过命令行参数和配置文件进行配置。虽然命令行参数可以配置一些不可变的系统参数(例如存储位置,保留在磁盘和内存中的数据量等),但配置文件能够定义与抓取作业及其实例相关的所有内容,以及哪些规则文件可以被加载等。

要查看所有可用的命令行参数,请运行./prometheus -h。

Prometheus可以在运行时重新加载其配置。如果新配置的格式不正确,则不会应用更改。如果想要重载Prometheus配置,可以给Prometheus的主进程发送SIGHUP信号或者发送post请求给指定端点 /-/reload(前提条件是在启动时加上--web.enable-lifecycle启动参数)

Prometheus的配置文件是YAML格式,当我们运行prometheus二进制文件(windows是prometheus.exe可执行文件)时,我们通过参数可指定一个配置文件。

./prometheus --config.file=prometheus.yml

Prometheus的解压包里自带了一个默认的配置文件prometheus.yml。让我们来看一下:

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']

在这个缺省的配置文件里定义了4个单元:global、alerting、rule_files和scrape_configs。

Global

global包含用于控制prometheus服务器行为的全局设置。

scrape_interval参数指定Prometheus抓取应用程序数据的间隔为15秒。

可以为特定的服务设定不同的参数来覆盖这个全局参数。但是最好不要这样做,在整个服务器上保持一个全局性间隔。这样可以确保所有时间序列数据具有相同的采集间隔,可以组合在一起计算。如果覆盖全局采集间隔,则可能由于尝试比较不同间隔收集的数据而导致结果不一致。

evaluation_interval参数指定Prometheus评估规则的频率。

规则可以分为两大类:记录规则和警报规则:

  • 记录规则-允许您根据预先记录表达式抓取监控数据,并将其结果保存为派生的时间序列数据。
  • 警报规则-允许你定义警报条件。

通过这个参数,Prometheus将每隔15秒(重新)评估这些规则。

Altering

altering配置Prometheus的警报服务器。Prometheus的警报由一个名为AlertManager的独立工具提供。
AlertManager是一个可以集群化的独立警报管理工具。

alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

Prometheus还支持对AlertManager的服务发现,例如,你可以查询外部源(如Consul服务器)以返回可用的AlertManager列表,而不是单独指定每个AlertManager。

Rule files

rule_files指定了一组规则文件,可以包含记录规则或警报规则。

规则文件的语法是:

groups:
  [ - <rule_group> ]

一个简单的记录规则文件是:

groups:
  - name: example
    rules:
    - record: job:http_inprogress_requests:sum
      expr: sum(http_inprogress_requests) by (job)

警报规则的示例文件是:

groups:
- name: example
  rules:
  - alert: HighErrorRate
    expr: job:request_latency_seconds:mean5m{job="myjob"} > 0.5
    for: 10m
    labels:
      severity: page
    annotations:
      summary: High request latency

要在不启动Prometheus服务器的情况下快速检查规则文件在语法上是否正确,请安装并运行Prometheus的promtool命令行工具:

go get github.com/prometheus/prometheus/cmd/promtool
promtool check rules /path/to/example.rules.yml

Scrape configuration

scrape_configs具体说明了Prometheus想要抓取的目标。

Prometheus通过访问获取端点来抓取数据。为了抓取一个端点,普罗米修斯定义了一个称为目标的配置。这是执行抓取所需的信息,例如,需要应用的标签、连接所需的任何身份验证,或者定义抓取将如何发生的其他信息。目标组称为作业。在作业内部,每个目标都有一个名为instance的标签,该标签唯一地标识目标对象。

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

在缺省配置中有一个名为prometheus的作业,它里面包含一个static_config配置项,列出了这个作业将要抓取的目标。这个要抓取的目标列表可以手动第静态配置或通过服务发现来设置。

这里Prometheus将监控自己,它将抓取localhost的9090端口的服务的监控指标。Prometheus默认从 /metrics 端口抓取数据,因此它将访问的地址是
http://localhost:9090/metrics

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章