ルータやWiFiアクセスポイントとして運用している様々な OpenWrt デバイスへ Prometheus Exporter をインストールして、 Grafana のダッシュボード上で集中監視できるようにしました。
パッケージ群のインストール
LuCI上でPrometheus関連パッケージを検索すると、大量にヒットしてしまうのですが、
(下図は、H3C NX30 Proに入れたOpenWrt 23.05.2の例)
必要なパッケージは以下の6つ。
- prometheus-node-exporter-lua
- prometheus-node-exporter-lua-nat_traffic
- prometheus-node-exporter-lua-netstat
- prometheus-node-exporter-lua-openwrt
- prometheus-node-exporter-lua-wifi
- prometheus-node-exporter-lua-wifi_stations
OpenWrtのパッケージマネージャ opkg は、複数パッケージを空白区切りで連結しての一括インストールができず、一つ一つ地道にインストールするのが地味に面倒。
そんな中、改行区切りにして複数行を一括実行する小技が、Grafana Blogにありました(Great Works!!)。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
root@R2S:~# opkg install prometheus-node-exporter-lua \ prometheus-node-exporter-lua-nat_traffic \ prometheus-node-exporter-lua-netstat \ prometheus-node-exporter-lua-openwrt \ prometheus-node-exporter-lua-wifi \ prometheus-node-exporter-lua-wifi_stations Installing prometheus-node-exporter-lua (2022.04.18-1) to root... Downloading https://downloads.openwrt.org/releases/21.02.2/packages/aarch64_generic/packages/prometheus-node-exporter-lua_2022.04.18-1_all.ipkInstalling luasocket (2019-04-21-733af884-1) to root... Downloading https://downloads.openwrt.org/releases/21.02.2/packages/aarch64_generic/packages/luasocket_2019-04-21-733af884-1_aarch64_generic.ipk Installing prometheus-node-exporter-lua-nat_traffic (2022.04.18-1) to root... Downloading https://downloads.openwrt.org/releases/21.02.2/packages/aarch64_generic/packages/prometheus-node-exporter-lua-nat_traffic_2022.04.18-1_all.ipk Installing prometheus-node-exporter-lua-netstat (2022.04.18-1) to root... Downloading https://downloads.openwrt.org/releases/21.02.2/packages/aarch64_generic/packages/prometheus-node-exporter-lua-netstat_2022.04.18-1_all.ipk Installing prometheus-node-exporter-lua-openwrt (2022.04.18-1) to root... Downloading https://downloads.openwrt.org/releases/21.02.2/packages/aarch64_generic/packages/prometheus-node-exporter-lua-openwrt_2022.04.18-1_all.ipk Installing prometheus-node-exporter-lua-wifi (2022.04.18-1) to root... Downloading https://downloads.openwrt.org/releases/21.02.2/packages/aarch64_generic/packages/prometheus-node-exporter-lua-wifi_2022.04.18-1_all.ipk Installing prometheus-node-exporter-lua-wifi_stations (2022.04.18-1) to root... Downloading https://downloads.openwrt.org/releases/21.02.2/packages/aarch64_generic/packages/prometheus-node-exporter-lua-wifi_stations_2022.04.18-1_all.ipk Configuring luasocket. Configuring prometheus-node-exporter-lua. Configuring prometheus-node-exporter-lua-netstat. Configuring prometheus-node-exporter-lua-wifi. Configuring prometheus-node-exporter-lua-openwrt. Configuring prometheus-node-exporter-lua-wifi_stations. Configuring prometheus-node-exporter-lua-nat_traffic. |
インストール後、Prometheus Exporter Luaサービスが開始されるのですが、その仕組みの違いからNetdataのエージェントと比べ、プロセスのリソース消費が圧倒的に少ないことには驚かされました。
|
1 2 3 4 5 6 7 8 9 10 |
root@vWrt:~# ps | grep -e 'PID' -e 'prometheus' -e 'netdata' | grep -v grep PID USER VSZ STAT COMMAND 3482 root 1868 S /usr/sbin/uhttpd -f -c /dev/null -l / -L /usr/bin/prometheus-node-exporter-lua -k 70 -p 192.168.51.5:9100 5424 root 355m SN /opt/netdata/bin/srv/netdata -D -c /opt/netdata/etc/netdata/netdata.conf 5767 root 23296 SN /opt/netdata/bin/srv/netdata --special-spawn-server 6394 root 1260m SN /opt/netdata/usr/libexec/netdata/plugins.d/go.d.plugin 1 6420 root 10000 SN /opt/netdata/usr/libexec/netdata/plugins.d/ebpf.plugin 1 6426 root 3220 SN /opt/netdata/usr/libexec/netdata/plugins.d/apps.plugin 1 13873 root 416 SN /opt/netdata/usr/libexec/netdata/plugins.d/debugfs.plugin 1 13909 root 400 SN /opt/netdata/usr/libexec/netdata/plugins.d/nfacct.plugin 1 |
|
1 2 3 4 5 6 7 8 |
root@R2S:~# ps | grep -e 'PID' -e 'prometheus' -e 'netdata' | grep -v grep PID USER VSZ STAT COMMAND 2536 root 349m SN /opt/netdata/bin/srv/netdata -D -c /opt/netdata/etc/netdata/netdata.conf 2711 root 22116 SN /opt/netdata/bin/srv/netdata --special-spawn-server 2888 root 3088 SN /opt/netdata/usr/libexec/netdata/plugins.d/apps.plugin 1 2904 root 1258m SN /opt/netdata/usr/libexec/netdata/plugins.d/go.d.plugin 1 5175 root 424 SN /opt/netdata/usr/libexec/netdata/plugins.d/debugfs.plugin 1 6358 root 2760 S {prometheus-node} /usr/bin/lua /usr/bin/prometheus-node-exporter-lua --bind 192.168.51.2 --port 9100 |
この事象に関し、こちらの比較レビュー記事を目にしたことがきっかけとなり、NetdataからPrometheusへの乗り換えを踏み切った次第です(鳴謝!!)。
設定ファイルの編集
インストール直後、サービスは開始されているものの、デフォルトではローカルホストの待ち受けに限定されているので、リモートホストからの利用には設定変更が必要。
Prometheus Exporter LuaにはGUIによる設定が無いので、設定ファイル /etc/config/prometheus-node-exporter-lua を直接編集します。
|
1 2 3 4 5 6 7 8 9 10 11 |
【変更前】 config prometheus-node-exporter-lua 'main' option listen_interface 'loopback' option listen_ipv6 '0' option listen_port '9100' 【変更後】 config prometheus-node-exporter-lua 'main' option listen_interface 'lan' option listen_ipv6 '0' option listen_port '9100' |
但し、古いOpenWrtのパッケージでは待ち受けの記述方法が以下の通り、異なっていました。
|
1 2 3 4 5 6 7 8 9 |
【変更前】 config prometheus-node-exporter-lua 'main' option listen_address '::1' option listen_port '9100' 【変更後】 config prometheus-node-exporter-lua 'main' option listen_address '0.0.0.0' option listen_port '9100' |
サービスを再起動して設定を反映させた後、ブラウザからメトリクスが開けることを確認します。
|
1 2 3 4 5 6 7 8 |
~# /etc/init.d/prometheus-node-exporter-lua status running ~# /etc/init.d/prometheus-node-exporter-lua restart ~# /etc/init.d/prometheus-node-exporter-lua status running ~# netstat -lpnt | grep 9100 tcp 0 0 192.168.51.4:9100 0.0.0.0:* LISTEN 8995/uhttpd |
Prometheus側の設定
続いてPrometheusサーバへ移り、設定ファイルへOpenWrt用のjobを以下のように記述しました。
|
1 2 3 4 5 6 7 8 9 10 |
scrape_configs: - job_name: 'openwrt' scrape_interval: 15s static_configs: - targets: ['vWRT.lan:9100'] - targets: ['R2S.lan:9100'] - targets: ['nx30proa9.lan:9100'] - targets: ['gl-ar750se4.lan:9100'] - targets: ['192.168.1.251:9100'] - targets: ['192.168.1.250:9100'] |
Prometheusサービスの再起動後、PrometheusのWebUI上で、ターゲットに追加されているのを確認。
|
1 2 3 4 |
prom14:~$ sudo vi /etc/prometheus/prometheus.yml prom14:~$ sudo rc-service prometheus restart * Stopping prometheus ... [ ok ] * Starting prometheus ... [ ok ] |
Grafanaダッシュボード
Grafanaのダッシュボードには、汎用のNode Exporter Fullをベースにしたと謳われている、こちらのOpenWrt向けのダッシュボードをインポートしてみました。
Import dashboard にて、ダッシュボードIDを入力してロード。
データソースにPrometheusを選択してインポート実行。
ダッシュボードレイアウトは、確かにほぼNode Exporter Fullでした。なお、Root FSが N/A になるのは、ディスク関連メトリクスが得られていないため(各デバイス共通)。
複数の同じようなデバイスをどう見せるか考えた結果、取り敢えずデバイスの数だけ同じダッシュボードを複製し、設定を含めて保存することで対処。それらをプレイリストに登録して、紙芝居のように順繰りに表示させる形にしました。
ディスク関連メトリクスの追加
ディスク関連メトリクスが N/A となってしまう問題は、OpenWrtパッケージのGitHubでも指摘されているのですが、その中でメトリクス収集に必要な disk.lua ファイルを組みあげている猛者がいらっしゃいました(Great Works!!)。
Prometheus Node Exporter Luaが計測に使うコレクタプローブは、 /usr/lib/lua/prometheus-collectors/ に収められています。そこへ、次のファイルを新規作成、実行権限を付与の上、サービスを再起動。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
local function scrape() local df = io.popen("df -T") local line = df:read("*l") line = df:read("*l") while (line ~= nil) do local size = space_split(line)[3] * 1024 local available = space_split(line)[5] * 1024 local labels = { fstype = space_split(line)[2], mountpoint = space_split(line)[7] } metric("node_filesystem_avail_bytes", "gauge", labels, available) metric("node_filesystem_size_bytes", "gauge", labels, size) line = df:read("*l") end df:close() end return { scrape = scrape } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
root@vWrt:~# vi /usr/lib/lua/prometheus-collectors/disk.lua root@vWrt:~# chmod +x /usr/lib/lua/prometheus-collectors/disk.lua root@vWrt:~# ls -l /usr/lib/lua/prometheus-collectors -rwxr-xr-x 1 root root 420 Oct 3 2023 conntrack.lua -rwxr-xr-x 1 root root 1429 Oct 3 2023 cpu.lua -rwxr-xr-x 1 root root 547 May 8 13:01 disk.lua -rwxr-xr-x 1 root root 350 Oct 3 2023 entropy.lua -rwxr-xr-x 1 root root 243 Oct 3 2023 filefd.lua -rwxr-xr-x 1 root root 266 Oct 3 2023 loadavg.lua -rwxr-xr-x 1 root root 345 Oct 3 2023 meminfo.lua -rwxr-xr-x 1 root root 1251 Oct 3 2023 nat_traffic.lua -rwxr-xr-x 1 root root 3068 Oct 3 2023 netclass.lua -rwxr-xr-x 1 root root 1077 Oct 3 2023 netdev.lua -rwxr-xr-x 1 root root 690 Oct 3 2023 netstat.lua -rwxr-xr-x 1 root root 475 Oct 3 2023 openwrt.lua -rwxr-xr-x 1 root root 129 Oct 3 2023 time.lua -rwxr-xr-x 1 root root 660 Oct 3 2023 uname.lua -rwxr-xr-x 1 root root 1541 Oct 3 2023 wifi.lua -rwxr-xr-x 1 root root 2753 Oct 3 2023 wifi_stations.lua root@vWrt:~# /etc/init.d/prometheus-node-exporter-lua restart |
Grafanaダッシュボードに、Root FSの情報が正しく表示されました。
以上で情報を収集するPrometheus側の設定は完了し、今後の課題はGrafanaでどう見せるかに。Netdataのノード一覧のような感じにしたいので、ダッシュボードデザインを調べてみるつもりです。













