Zabbix可以query以Prometheus行格式公开的指标。
开始收集Prometheus数据需要两个步骤:
一个HTTP master item指向 相应的数据端点,例如 https://<prometheus host>/metrics
依赖的监控项使用Prometheus预处理选项来query
required data from the metrics gathered by the master item
有两种Prometheus数据预处理选项:
批量处理支持依赖监控项. 要启用缓存和索引功能, Prometheus模式预处理必须作为第一个预处理步骤. 当Prometheus模式作为首个预处理步骤时, 解析后的Prometheus 数据会被缓存并通过Prometheus模式预处理步骤中的第一个<label>==<value>
条件 建立索引. 在处理同一批次中其他依赖监控项时会复用此缓存. 为实现最佳性能, 第一个标签应选择具有最多不同值的那个.
若在首个步骤前还需进行其他预处理, 应将其移至主监控项, 或新建一个依赖监控项作为这些依赖监控项的主监控项.
若已配置HTTP主监控项,您需要create一个 dependent item,该 链接使用Prometheus预处理步骤:
在配置表单中输入通用依赖监控项参数
转到预处理选项卡
选择Prometheus预处理选项(Prometheus模式或
Prometheus to JSON)
以下参数特定于Prometheus模式预处理选项:
参数 | 描述 | 示例 |
---|---|---|
Pattern | 要定义所需的数据模式,您可以使用与Prometheus query语言相似的query语言(参见比较表),例如: <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模式选项。 |
参见下方参数使用示例。 |
Output | 定义标签名称(可选)。在这种情况下,返回与标签名称对应的值。 此字段仅适用于Prometheus模式选项,如果在Result processing字段中选择'Label'。 |
参数使用示例
/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"}
node_disk_usage_bytes
参数的average值感兴趣:node_disk_usage_bytes
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
基于数值'1'的过滤器将匹配适当的行,而标签将返回健康状态描述(当前为'green';但也可能是'red'或'yellow')。
Prometheus的数据可用于低级发现。在这种情况下 需要JSON格式的数据,而Prometheus转JSON 预处理选项将精确返回所需格式。
更多详情,请参阅Discovery using Prometheus data。
下表列出了PromQL与Zabbix Prometheus预处理query语言之间的差异和相似之处。
PromQL instant vector selector | Zabbix Prometheus预处理 | |
---|---|---|
差异 | ||
query目标 | Prometheus服务器 | Prometheus暴露格式的纯文本 |
返回值 | 瞬时向量 | 指标或标签值(Prometheus模式) 单值JSON的指标array(Prometheus转JSON) |
标签匹配运算符 | =, !=, =~, !~ | =, !=, =~, !~ |
用于标签或指标名称匹配的正则表达式 | RE2 | PCRE |
比较运算符 | 参见list | 仅支持==(等于)进行值过滤 |
相似点 | ||
按等于string的指标名称选择 | <指标名称> 或 {__name__="<指标名称>"} | <指标名称> 或 {__name__="<指标名称>"} |
按匹配正则表达式的指标名称选择 | {__name__=~"<正则表达式>"} | {__name__=~"<正则表达式>"} |
按等于string的<标签名称>值选择 | {<标签名称>="<标签值>",...} | {<标签名称>="<标签值>",...} |
按匹配正则表达式的<标签名称>值选择 | {<标签名称>=~"<正则表达式>",...} | {<标签名称>=~"<正则表达式>",...} |
按等于string的值选择 | {__name__=~".*"} == <值> | {__name__=~".*"} == <值> |