# Grafana 配置 Prometheus Proxmox VE Exporter

本文不做基础知识的扫盲。

我一直想将 PVE 上全量的虚拟机纳入 Grafana 中展示，然而又不想一台台的配置 node\_exporter。偶然的机会听说了 Prometheus Proxmox VE Exporter 插件，索性配置一下这个插件，将 PVE 的数据接入 Grafana。

整体架构如下图：

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1748024336266/fa5f8422-b95e-41e5-af8f-f6d98d35f540.png align="center")

# 1\. 配置 PVE 用户

网上的很多教程都是直接拿 `root@pam` 账户来进行操作，但特权账号用作监控其实是安全风险蛮高的一件事情。这里我们单独建立一个审计（`PVEAuditor`）账户来执行操作。

建立账户很简单，登录网页控制台，在数据中心（Datacenter） → 权限（Permissions） → 用户（Users）中，直接添加一个用户即可。我这里使用的用户名是 `monitor`，鉴权方式（Realm）选择的是 PVE 鉴权。

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1748022137312/37db4b52-5836-4317-93f9-b90a99febf4e.png align="left")

之后要给用户配置审计角色（Role），这里很搞的是，PVE 没有办法通过 WebUI 来配置，需要登录到控制台执行命令：`pveum aclmod / -user monitor@pve -role PVEAuditor` 来实现。

命令执行成功后没有任何回显，但可以在 WebUI 中点击 Permissions 按钮就可以看到用户对应的权限。

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1748022379116/b7e862f8-94d3-4a77-973a-d26db95409e0.png align="center")

# 2\. 创建配置文件

在本地创建一个 pve.yml 文件，用于保存 PVE 的访问凭证。

可以使用密码或者 token，我这里使用的是密码，Token 的配置可参考项目官方 Github：[https://github.com/prometheus-pve/prometheus-pve-exporter?tab=readme-ov-file#authentication](https://github.com/prometheus-pve/prometheus-pve-exporter?tab=readme-ov-file#authentication)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1748022635401/77379a0d-9cd5-4006-a82d-cbca14fa1c6c.png align="center")

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">我的 PVE 绑定了域名并配置了有效的证书，如果你使用的是自签名证书，请不要开启 SSL 校验。</div>
</div>

# 3\. 拉起插件的 Docker 镜像

最开始我其实是希望在本地直接用 Python 虚拟环境启动的，结果在使用 `pip install prometheus-pve-exporter` 的过程中，有一个依赖始终找不到，后面懒得去找了，索性直接拉 Docker 镜像。

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1748022932065/04a4e759-3efd-41cb-84e5-ba422393eac3.png align="center")

拉镜像也很简单，直接命令执行 `docker run --init --name prometheus-pve-exporter -d -p 127.0.0.1:9221:9221 -v /root/pve.yml:/etc/prometheus/pve.yml prompve/prometheus-pve-exporter` 即可。

<div data-node-type="callout">
<div data-node-type="callout-emoji">⚠</div>
<div data-node-type="callout-text">注：我的 prometheus-pve-exporter 和 prometheus、Grafana 是放在同一台机器上的，如果你的 exporter 是放在 PVE 宿主机上的，监听请监听 0.0.0.0 而非 127.0.0.1。</div>
</div>

拉起来之后可以通过命令 `curl http://localhost:9221/pve?target=<PVE IP地址>` 试一下，看看能不能读取到 metrics。

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1748023220581/5dd5d198-b025-4a42-9391-11a90828fe15.png align="center")

# 4\. 添加 **Prometheus 配置**

这一步直接跟着官网的操作，在Prometheus 的配置文件 `scrape_configs` 下新增一项即可（注意不要出现两个 `scrape_configs:` ，同时记得调整 `Proxmox VE node` 的地址）。

```yaml
scrape_configs:
  - job_name: 'pve'
    static_configs:
      - targets:
        - 192.168.1.2  # Proxmox VE node.
        - 192.168.1.3  # Proxmox VE node.
    metrics_path: /pve
    params:
      module: [default]
      cluster: ['1']
      node: ['1']
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 127.0.0.1:9221  # PVE exporter.
```

如果是部署在 PVE 上，则无需配置 `relabel_configs` 相关字段，具体可参考官方 Github：[https://github.com/prometheus-pve/prometheus-pve-exporter?tab=readme-ov-file#prometheus-configuration](https://github.com/prometheus-pve/prometheus-pve-exporter?tab=readme-ov-file#prometheus-configuration)

添加完成后，执行 `systemctl restart prometheus` 重启 Prometheus。

后续可以访问 Prometheus 网页，通过 `Status` → `Target health` 查看配置项的运行状态。

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1748023636777/557c8956-afd6-43c1-b8af-27168d304428.png align="center")

# 5\. Grafana 中添加 Dashboard

一方面传统的 Dashboard 依然可以使用，同时也可以添加针对 PVE 定制的 Dashboard，个人比较推荐：[https://grafana.com/grafana/dashboards/10347-proxmox-via-prometheus/](https://grafana.com/grafana/dashboards/10347-proxmox-via-prometheus/)

添加完成后即可在仪表盘中查看对应的监控信息。

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1748024074833/e2001e85-c794-4630-8018-3230aef9e99a.png align="center")
