Prometheus 检查

概述

Zabbix 可以查询以 Prometheus 行格式暴露的监控指标。

开始收集 Prometheus 数据需要两个步骤:

  • 一个指向相应数据端点的 HTTP 主监控项,例如 https://<prometheus host>/metrics
  • 使用 Prometheus 预处理选项的依赖监控项,用于从主监控项收集的监控指标中查询所需数据

有两种 Prometheus 数据预处理选项:

  • Prometheus pattern - 用于普通监控项,以查询 Prometheus 数据
  • Prometheus to JSON - 用于普通监控项和低级别发现。在这种情况下,查询到的 Prometheus 数据会以 JSON 格式返回。
批量处理

依赖监控项支持批量处理。为了启用缓存和索引,Prometheus pattern 预处理必须是第一个预处理步骤。
当 Prometheus pattern 是第一个预处理步骤时,已解析的 Prometheus 数据会根据 Prometheus pattern 预处理步骤中的第一个 <label>==<value> 条件进行缓存和索引。处理此批次中的其他依赖监控项时,会复用该缓存。为了获得最佳性能,第一个标签应选择取值差异最多的那个。

如果在第一个步骤之前还需要执行其他预处理,则应将其移到主监控项,或者移到一个新的依赖监控项中,并将该监控项用作这些依赖监控项的主监控项。

配置

在配置好 HTTP 主监控项的前提下,您需要创建一个使用 Prometheus 预处理步骤的依赖监控项:

  1. 在配置表单中输入常规依赖监控项参数。
  2. 转到预处理选项卡。
  3. 选择 Prometheus 预处理选项(Prometheus 模式或 Prometheus 转 JSON)。

以下参数专用于 Prometheus 模式预处理选项:

参数 描述 示例
模式 要定义所需的数据模式,可以使用类似于 Prometheus 查询语言的查询语言(请参阅比较表),例如:
<metric name> - 按指标名称选择
{__name__="<metric name>"} - 按指标名称选择
{__name__=~"<regex>"} - 选择与正则表达式匹配的指标名称
{<label name>="<label value>",...} - 按标签名称选择
{<label name>=~"<regex>",...} - 选择与正则表达式匹配的标签名称
{__name__=~".*"}==<value> - 按指标值选择
或者组合使用上述条件:
<metric name>{<label1 name>="<label1 value>",<label2 name>=~"<regex>",...}==<value>

标签值可以是任意 UTF-8 字符序列,但反斜杠、双引号和换行符必须分别转义为 \\、\" 和 \n;其他字符不应转义。
wmi_os_physical_memory_free_bytes
cpu_usage_system{cpu="cpu-total"}
cpu_usage_system{cpu=~".*"}
cpu_usage_system{cpu="cpu-total",host=~".*"}
wmi_service_state{name="dhcp"}==1
wmi_os_timezone{timezone=~".*"}==1
结果处理 指定返回值、标签,或应用适当的函数(如果模式匹配多行且需要聚合结果):
value - 返回指标值(匹配多行时出错)
label - 返回标签字段中指定标签的值(匹配多个指标时出错)
sum - 返回值的总和
min - 返回最小值
max - 返回最大值
avg - 返回平均值
count - 返回值的数量
此字段仅适用于 Prometheus 模式选项。
另请参阅下面的参数使用示例。
输出 定义标签名称(可选)。在这种情况下,将返回与该标签名称对应的值。
仅当在结果处理字段中选择“Label”时,此字段才适用于 Prometheus 模式选项。

参数使用示例

  1. 最常见的用例是返回值。要从以下数据中返回 /var/db 的值:

node_disk_usage_bytes{path="/var/cache"} 2.1766144e+09
node_disk_usage_bytes{path="/var/db"} 20480
node_disk_usage_bytes{path="/var/dpkg"} 8192
node_disk_usage_bytes{path="/var/empty"} 4096

请使用以下参数:

  • 模式 - node_disk_usage_bytes{path="/var/db"}
  • 结果处理 - 选择“value”
  1. 您可能还需要所有 node_disk_usage_bytes 参数的平均值:
  • 模式 - node_disk_usage_bytes
  • 结果处理 - 选择“avg”
  1. 虽然 Prometheus 仅支持数值数据,但通常可以通过变通方式同时返回相关的文本描述。 这可以通过使用过滤器并指定标签来实现。 因此,要从以下数据中返回“color”标签的值:

elasticsearch_cluster_health_status{cluster="elasticsearch",color="green"} 1
elasticsearch_cluster_health_status{cluster="elasticsearch",color="red"} 0
elasticsearch_cluster_health_status{cluster="elasticsearch",color="yellow"} 0

请使用以下参数:

  • 模式 - elasticsearch_cluster_health_status {cluster="elasticsearch"} == 1
  • 结果处理 - 选择“label”
  • 标签 - 指定“color”

基于数值“1”的过滤器将匹配相应的行,而标签将返回健康状态描述(当前为“green”,但也可能是“red”或“yellow”)。

Prometheus 转 JSON

Prometheus 中的数据可用于低级别发现。在这种情况下,需要 JSON 格式的数据,而 Prometheus to JSON 预处理选项将正好返回这种格式。

有关更多详细信息,请参见 使用 Prometheus 数据进行发现。

查询语言比较

下表列出了 PromQL 和 Zabbix Prometheus 预处理查询语言之间的差异和相似之处。

PromQL instant vector selector Zabbix Prometheus preprocessing
差异
查询目标 Prometheus 服务器 Prometheus exposition 格式的纯文本
返回值 瞬时向量 监控项或标签值(Prometheus 模式)
JSON 中单个值的监控项数组(Prometheus to JSON)
标签匹配运算符 =, !=, =~, !~ =, !=, =~, !~
标签或监控项名称匹配中使用的正则表达式 RE2 PCRE
比较运算符 参见 列表 仅支持 ==(等于)用于值过滤
相似之处
按等于字符串的监控项名称选择 <metric name> 或 {__name__="<metric name>"} <metric name> 或 {__name__="<metric name>"}
按匹配正则表达式的监控项名称选择 {__name__=~"<regex>"} {__name__=~"<regex>"}
按等于字符串的 <label name> 值选择 {<label name>="<label value>",...} {<label name>="<label value>",...}
按匹配正则表达式的 <label name> 值选择 {<label name>=~"<regex>",...} {<label name>=~"<regex>",...}
按等于字符串的值选择 {__name__=~".*"} == <value> {__name__=~".*"} == <value>