使用 Grafana 監控磁碟空間與調整 Node Exporter 設定

Kanglin WuKanglin Wu
1 min read

前言

在運維系統時,磁碟空間管理是非常重要的一環,特別是當磁碟快要滿時,可能會影響服務的穩定性。因此,透過 GrafanaPrometheus Node Exporter 來監控磁碟使用情況,並設定適當的告警機制,是一個必要的步驟。

最近,我在設定 Grafana 監控時,遇到了一些挑戰,尤其是在處理磁碟使用率的查詢與告警上。本文將分享我的經驗,從 發現問題修改 Node Exporter 設定,並提供一些最佳實踐。


問題發現

在一開始設定 Grafana Panel 時,我使用了 PromQL 來查詢磁碟空間的使用率,類似如下:

100 - ((node_filesystem_avail_bytes{instance="$node",job="$job",device!~"rootfs"} * 100) / node_filesystem_size_bytes)

這段查詢的目標是計算不同 mount points 的磁碟使用百分比,並在 Grafana Panel 中顯示。然而,在實際監控時,我發現了一個異常:

  • 為什麼會撈到 /var/snap/firefox/common/host-hunspell:這個掛載點本來不是我關心的監控對象,但它卻出現在 Metrics 裡。

這促使我進一步調查 Node Exporter 的行為,並對其設定進行調整。


深入調查 Node Exporter

1. Node Exporter 是如何收集 Metrics 的?

Node Exporter 會自動掃描 /proc/mounts 來獲取掛載資訊,並根據 /proc/filesystems 判斷是否應該收集對應的 metrics。

執行以下命令可以檢視目前的掛載資訊:

cat /proc/mounts

這份輸出顯示了系統目前所有的掛載點,包含了 /var/snap/firefox/common/host-hunspell,這說明這個目錄可能是由 Snap 應用程式自動掛載,而 Node Exporter 預設不會忽略它,導致它出現在 Metrics 裡。


調整 Node Exporter 設定以過濾不必要的掛載點

因為 node exporter 我是透過 service run 起來,因此修改 /etc/systemd/system/node_exporter.service 裡的 ExecStart 區塊後面加上 --collector.filesystem.mount-points-exclude 搭配正規將不必要的 mount point 排除

[Unit]
Description=Prometheus Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter --web.listen-address=:9100 --collector.filesystem.mount-points-exclude "^/(sys|proc|dev|run|tmpfs|overlay|snap)($|/)"

[Install]
WantedBy=multi-user.target

設定完成後,重啟 Node Exporter 以套用變更:

systemctl restart node_exporter

然後再檢查 metrics,確保新數據已正確出現。

curl http://<node_exporter_host>:9100/metrics | grep node_filesystem

結論

透過這次的調整,我成功解決了如何客製化 exporter 要撈取的 metrics,也讓告警機制更為準確。

  1. Node Exporter 依據 /proc/mounts 來決定要監控哪些目錄。

  2. 適當使用 mount-points-exclude 來完全忽略特定目錄。

希望這篇文章能幫助到有類似需求的朋友!

0
Subscribe to my newsletter

Read articles from Kanglin Wu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kanglin Wu
Kanglin Wu