1 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 预处理步骤的依赖监控项:
- 在配置表单中填写依赖监控项的一般参数
- 转到“预处理”选项卡
- 选择一个 Prometheus 预处理选项(Prometheus pattern 或 Prometheus to JSON)

以下参数是 Prometheus pattern 预处理选项特有的:
| Parameter | Description | Examples |
|---|---|---|
| Pattern | 要定义所需的数据模式,你可以使用一种类似于 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 |
| Result processing | 指定是返回值、标签,还是应用相应函数(如果模式匹配多行且结果需要聚合): value - 返回监控项值(如果匹配到多行则报错) label - 返回 Label 字段中指定的标签值(如果匹配到多个监控项则报错) sum - 返回值的总和 min - 返回最小值 max - 返回最大值 avg - 返回平均值 count - 返回值的数量 此字段仅适用于 Prometheus pattern 选项。 |
另请参见下面使用参数的示例。 |
| Output | 定义标签名称(可选)。在这种情况下,将返回与该标签名称对应的值。 此字段仅适用于 Prometheus pattern 选项,并且仅在 Result processing 字段中选择了 'Label' 时可用。 |
参数使用示例
- 最常见的用法是返回 value。要返回以下内容中
/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
请使用以下参数:
- Pattern -
node_disk_usage_bytes{path="/var/db"} - Result processing - 选择 'value'
- 你也可能需要所有
node_disk_usage_bytes参数的 average 值:
- Pattern -
node_disk_usage_bytes - Result processing - 选择 'avg'
- 虽然 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
请使用以下参数:
- Pattern -
elasticsearch_cluster_health_status {cluster="elasticsearch"} == 1 - Result processing - 选择 'label'
- 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> or {__name__="<metric name>"} | <metric name> or {__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> |