测试规则AlertManager失败:yaml:解组错误:第1行:在main.unitTestFile类型中找不到字段组

时间:2019-09-22 03:00:20

标签: alert prometheus prometheus-alertmanager

请帮助我在测试下面的警报管理器时收到错误消息

 promtool check rules /etc/prometheus/alert.rules.yml
 Checking /etc/prometheus/alert.rules.yml
 SUCCESS: 3 rules found

 promtool test rules /etc/prometheus/alert.rules.yml
 Unit Testing:  /etc/prometheus/alert.rules.yml
 FAILED:
 yaml: unmarshal errors:
 line 1: field groups not found in type main.unitTestFile

我的alert.rules配置如下:

      cat /etc/prometheus/alert.rules.yml
      groups:
      - alert: MemoryFree10%
        expr: node_exporter:node_memory_free:memory_used_percents >= 90
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "Instance {{ $labels.instance }} hight memory usage"
          description: "{{ $labels.instance }} has more than 90% of its memory used."
      - alert: DiskSpace10%Free
        expr: node_exporter:node_filesystem_free:fs_used_percents >= 90
        labels:
          severity: moderate
        annotations:
          summary: "Instance {{ $labels.instance }} is low on disk space"
          description: "{{ $labels.instance }} has only {{ $value }}% free."
      - alert: ExporterDown
        expr: up == 0
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "Exporter down (instance {{ $labels.instance }})"
          description: "Prometheus exporter down\n  VALUE = {{ $value }}\n  LABELS: {{ $labels }}"

我们的文件警报规则是否缺失或不正确?

请帮助?

谢谢

3 个答案:

答案 0 :(得分:0)

您的配置缺少规则。

    groups:
    - name: alert.rules
      rules:
      - alert: HighRequestLatency
      .....

https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/

答案 1 :(得分:0)

您正在对警报规则文件运行单元测试。您应该先编写测试文件,然后通过 promtool test rules test.yml 对测试文件运行单元测试。

这是来自 https://prometheus.io/docs/prometheus/latest/configuration/unit_testing_rules/

的演示

alerts.yml

# This is the rules file.

groups:
- name: example
  rules:

  - 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: AnotherInstanceDown
    expr: up == 0
    for: 10m
    labels:
        severity: page
    annotations:
        summary: "Instance {{ $labels.instance }} down"
        description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."

test.yml

# This is the main input for unit testing.
# Only this file is passed as command line argument.

rule_files:
    - alerts.yml

evaluation_interval: 1m

tests:
    # Test 1.
    - interval: 1m
      # Series data.
      input_series:
          - series: 'up{job="prometheus", instance="localhost:9090"}'
            values: '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'
          - series: 'up{job="node_exporter", instance="localhost:9100"}'
            values: '1+0x6 0 0 0 0 0 0 0 0' # 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
          - series: 'go_goroutines{job="prometheus", instance="localhost:9090"}'
            values: '10+10x2 30+20x5' # 10 20 30 30 50 70 90 110 130
          - series: 'go_goroutines{job="node_exporter", instance="localhost:9100"}'
            values: '10+10x7 10+30x4' # 10 20 30 40 50 60 70 80 10 40 70 100 130

      # Unit test for alerting rules.
      alert_rule_test:
          # Unit test 1.
          - eval_time: 10m
            alertname: InstanceDown
            exp_alerts:
                # Alert 1.
                - exp_labels:
                      severity: page
                      instance: localhost:9090
                      job: prometheus
                  exp_annotations:
                      summary: "Instance localhost:9090 down"
                      description: "localhost:9090 of job prometheus has been down for more than 5 minutes."
      # Unit tests for promql expressions.
      promql_expr_test:
          # Unit test 1.
          - expr: go_goroutines > 5
            eval_time: 4m
            exp_samples:
                # Sample 1.
                - labels: 'go_goroutines{job="prometheus",instance="localhost:9090"}'
                  value: 50
                # Sample 2.
                - labels: 'go_goroutines{job="node_exporter",instance="localhost:9100"}'
                  value: 50

然后你可以运行promtool test rules test.yml,你会得到这样的结果

Unit Testing:  test.yml
  SUCCESS

答案 2 :(得分:0)

使用 promtool 检查配置文件的语法时,您必须使用“./promtool check config prometheus.yml” 这个 prometheus.yml 是一个父文件,它会调用 prometheus 规则文件 prometheus_rules.yml。 因此,在使用 promtool 检查规则文件的语法时,您必须使用“./promtool 检查规则 prometheus_rules.yml