prometheus记录规则Recording rules和报警规则Alerting rules的配置文件

网友投稿 300 2022-09-08

prometheus记录规则Recording rules和报警规则Alerting rules的配置文件

rometheus 支持两种可以配置然后定期评估的规则:记录规则和警报规则。要在 Prometheus 中包含规则,请创建一个包含必要规则语句的文件,并让 Prometheus 通过Prometheus 配置​​rule_files​​中的字段加载该文件。规则文件使用 YAML。

记录规则Recording rules

记录规则允许您预先计算经常需要或计算量大的表达式,并将其结果保存为一组新的时间序列。查询预先计算的结果通常比每次需要时执行原始表达式要快得多。这对于仪表板特别有用,仪表板每次刷新时都需要重复查询相同的表达式。

记录和警报规则存在于规则组中。组内的规则以固定的间隔顺序运行,具有相同的评估时间。记录规则的名称必须是 有效的指标名称。警报规则的名称必须是 有效的标签值。

在主配置文件中设置规则文件的位置:

prometheus.yml

global:rule_files: - "rules/*.yml"

创建规则文件,并设置自定义规则

groups:- name: prometheus_rule - expr: (1 - avg by (environment,instance) (irate(node_cpu_seconds_total{mode="idle"}[5m]))) * 100 record: node_exporter:cpu:total:percent labels: desc: "节点的cpu总消耗百分比" unit: "%" job: "node-exporter"

重启prometheus,检查

报警规则Alerting rules

报警规则使您可以基于Prometheus表达式定义报警条件,并将有关触发报警的通知发送到外部服务。 只要报警表达式在给定的时间点产生一个或多个向量元素,该报警就被视为以这些元素的标签集处于活跃状态。

报警规则在Prometheus中的配置方式与记录规则相同。

带有报警的示例规则文件如下:

groups:- name: node_rules rules: - alert: nodecpu5m expr: (100 - avg(irate(node_cpu_seconds_total{job="test_node",mode="idle"}[5m])) by (instance) * 100) > 80 for: 30m labels: severity: warning annotations: summary: High node cpu for 0.5 hour - alert: DiskWillFillIn4Hours expr: predict_linear(node_filesystem_free_bytes{mountpoint="/"}[1h], 4 * 3600) < 0 for: 5m labels: severity: critical annotations: summary: Disk on {{ $labels.instance }} will fill in approximately 4 hours.

可选填的​​for​​子句,可以使Prometheus在第一次遇到一个新的表达式向量元素和计数一个Firing警报的该元素之间等待一段时间。 在这种情况下,Prometheus将在每次发出警报之前评估、检查警报在10分钟内是否继续处于活跃状态。活跃但尚未触发的元素处于挂起(pending)状态。

​​labels​​子句允许指定一组附加标签来附加到警报。 任何现有的冲突标签都将被覆盖。 标签值可以模板化。

​​annotations​​子句指定一组信息标签,这些标签可用于存储更长的附加信息,例如警报描述或运行手册链接。 注释值可以模板化。

模版化 Templating

标签(labels)和注释(annotations)值可以使用​​控制台模板(console templates)​​进行模板化。 ​​$labels​​变量保存报警实例的标签键/值对。 可以通过​​$externalLabels​​变量访问已配置的外部标签。 ​​$value​​变量保存警报实例的评估值。

# To insert a firing element's label values:{{ $labels. }}# To insert the numeric expression value of the firing element:{{ $value }}

示例

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_> 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)"

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:docker学习-docker-compose常用命令以及配置测试
下一篇:信息流营销哑火,教育产品如何自救?
相关文章

 发表评论

暂时没有评论,来抢沙发吧~