使用Zabbix收集Prometheus Exporter的值
我们已经进入了Zabbix Advent Calendar的中期。还有很多空位。
今天的主题是Zabbix和Prometheus。
在最近的Zabbix Conference Japan 2018上,Alexey先生在他的演讲中宣布,今后将开展支持Prometheus的Exporter的开发工作。
- 講演資料(pdf)
在Zabbix Roadmap中,4.2版本部分列举了以下内容。
- https://www.zabbix.com/roadmap#v4_2
Out of the box support of Prometheus exporters
It is already possible to extract data from arbitrary data sources using regular expressions, which is not always easy to configure.
This functionality is about native support of high-performance data collection from Prometheus exporters.
因此,很可能在未来,Zabbix将在Exporter格式方面与Zabbix进行原生兼容。
我們將在4.0版本中試著確定如何從Exporter收集資訊。
安装并运行节点导出器。
首先,我们将引入用于监控服务器节点资源状况等的Node Exporter,作为监视目标。
因为Go的已构建二进制文件在Github上发布,所以请下载并解压它。
$ wget https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz
$ tar xvzf node_exporter-0.17.0.linux-amd64.tar.gz
在展开的文件夹中运行node_exporter命令。
$ ./node_exporter-0.17.0.linux-amd64/node_exporter
通过此方式,默认配置下,node-exporter将在9100端口/TCP上启动并返回各种服务器内的信息。我们可以尝试使用curl进行获取。
$ curl -XGET http://サーバIP:9100/metrics
・・・略
# HELP node_cpu_seconds_total Seconds the cpus spent in each mode.
# TYPE node_cpu_seconds_total counter
node_cpu_seconds_total{cpu="0",mode="idle"} 305464.49
node_cpu_seconds_total{cpu="0",mode="iowait"} 1462.92
node_cpu_seconds_total{cpu="0",mode="irq"} 0
node_cpu_seconds_total{cpu="0",mode="nice"} 0
node_cpu_seconds_total{cpu="0",mode="softirq"} 633.15
node_cpu_seconds_total{cpu="0",mode="steal"} 0
node_cpu_seconds_total{cpu="0",mode="system"} 3749.97
node_cpu_seconds_total{cpu="0",mode="user"} 7824.49
node_cpu_seconds_total{cpu="1",mode="idle"} 307983.81
node_cpu_seconds_total{cpu="1",mode="iowait"} 1068.86
node_cpu_seconds_total{cpu="1",mode="irq"} 0
node_cpu_seconds_total{cpu="1",mode="nice"} 0
node_cpu_seconds_total{cpu="1",mode="softirq"} 214.7
node_cpu_seconds_total{cpu="1",mode="steal"} 0
node_cpu_seconds_total{cpu="1",mode="system"} 3425.68
node_cpu_seconds_total{cpu="1",mode="user"} 7742.01
・・・略
大概就是这个样子。
注册Zabbix的HTTP Agent项目。
接下来,我们将在Zabbix中进行项目注册。这次我们将使用Zabbix的HTTP代理功能,尝试以无需编写程序代码的方式挑战。我们将注册一个针对node-exporter的HTTP请求并获取响应的HTTP代理类型的项目,具体如下所示。
-
- 名前: Node Exporter (任意)
-
- タイプ: HTTPエージェント
-
- キー: prom.node.exporter (任意)
-
- URL: http://監視対象サーバIP:9100/metrics
-
- データ型: テキスト
- 更新間隔: 1m
然后,大量的节点信息将以文本的形式被记录下来。

注册Zabbix的依赖项
由于只有从Node Exporter获得的文本信息是无法处理的,所以我们会从Exporter的文本信息中提取所需信息并注册到监视项中。这需要利用依赖项。
在创建依赖项目之前,先了解 Prometheus Exporter 的格式。就像这里所描述的那样,它采用了文本基础格式。
总的来说,简单来说就是以下这样的感觉。
メトリクス名{ラベル=ラベルバリュー} 値 タイムスタンプ
根据这个格式,逐步选择要导入Zabbix监控项的内容。
例如,如果节点的负载平均值为1分钟,则感觉如下所示。
帮助:节点加载量1分钟负载平均值。
类型 节点加载1 测量
node_load1 是0.45。
以下是将Zabbix设置为以下依赖项目的方式。
-
- 名前: node_load1 data (任意)
-
- タイプ: 依存
-
- キー: prom.node.exporter.node_load1 (任意)
-
- マスターアイテム: Node Exporter (先程登録したHTTPエージェントアイテム)
-
- データ型: 数値(浮動小数)
-
- 保存前処理:
正規表現 =>
パターン: \nnode_load1\s(\S*)
出力: \1
正規表現的指定在保存前处理中是关键。
通过node-exporter的/metrics接口获取的数据以换行符(\n)分割,以文本形式保存多个信息。因此,我们使用正则表达式来提取紧跟着\n之后记录node_load1的部分,并通过后向引用来输出其后的value,并进行监控。

总结
现状来看,要使用正则表达式进行指定可能有些困难,协调也不太容易。根据Issue Tracker上的这个问题,似乎正在进行Zabbix 4.2的开发,所以需要特别关注开发进展。