我试图用Prometheus的hwmon和Grafana来绘制服务器的温度图。
与此相关的是prometheus-node-exporter提供的2个时间序列:
node_hwmon_temp_celsius,它有实际的温度;它有标签,如:{chip="platform_coretemp_0",sensor="temp1"}node_hwmon_sensor_label是一个温度传感器的辅助时间序列,它有一个名称(普罗米修斯标签名为label):{chip="platform_coretemp_0",sensor="temp1",label="core_0"}在出口商/问题/631上解释说:
并非所有传感器都可使用标签。如果你所有的传感器都有标签,你可以这样做来加入它们: Node_hwmon_temp_celsius{.}*忽略(标签)group_left(标签) node_hwmon_sensor_label
但我的一些感应器没有标签。在这种情况下,上面的PromQL查询没有帮助;在这种情况下,group_left(label)不返回任何结果。
相反,我想编写一个查询,查询具有label 标签的温度,如果缺少E 242 label E 146标签,则默认为。
我怎样才能用PromQL做到这一点呢?
发布于 2022-05-12 16:10:19
我终于找到了一个解决方案(不确定它是否最优):
(
# For all sensors that have a name (label "label"), join them with `node_hwmon_sensor_label` to get that name.
(node_hwmon_temp_celsius * ignoring(label) group_left(label) node_hwmon_sensor_label)
or
# For all sensors that do NOT a name (label "label") in `node_hwmon_sensor_label`, assign them `label="unknown-sensor-name"`.
# `label_replace()` only adds the new label, it does not remove the old one.
(label_replace((node_hwmon_temp_celsius unless ignoring(label) node_hwmon_sensor_label), "label", "unknown-sensor-name", "", ".*"))
)我是如何到达那里的:
label的时间序列,本质上等同于来自SQL的OUTER JOIN;谷歌一下,对于PromQL,我发现了有没有一种方法可以像PromQL中的查询那样执行“左外部连接”?问题,它解释了这是PromQL中的unless。label="unknown-sensor-name"。我们可以用label_replace()来实现这一点,也许令人困惑的是,它只是真正地添加了一个新的标签,它并没有删除旧的标签(如描述的这里)。我们可以将最后两个参数保留为"",因为我们只添加了一个常量字符串,因此我们可以匹配任何现有的标签。or的“如果你所有的传感器都有标签”系列。好了。
如果需要,我们还可以通过添加以下内容将其与机器的主机名连接起来:
* on(instance) group_left(nodename) node_uname_info这是在https://blog.ruanbekker.com/cheatsheets/prometheus/或在Prometheus中将实例重新标记为主机名中描述的。
这样,在Grafana中,我们可以使用{{nodename}} {{chip}} {{label}}作为图形的图例。
Grafana中的示例(显示instance而不是nodename):

发布于 2022-06-02 15:22:44
您可以利用以下事实:label_replace允许您提供regex匹配作为替换的要求,并且只有当它与空字符串匹配时才进行替换(即不存在标签):
label_replace(
<your metric>,
<label>,
<default value>,
<label>,
"^$"
)或者就你而言,具体而言:
label_replace(
<your metric>,
"label",
"unknown-sensor-name",
"label",
"^$"
)其中,^是Regexes中用来表示字符串开头的特殊字符,而$对于字符串结尾则是相同的。
https://stackoverflow.com/questions/72217597
复制相似问题