普遍的なシステム情報取得することを目的としたNode-Exporterに続き、今回は Pi-hole のメトリクス収集に特化した Pi-hole Exporter を導入し、 Prometheus が読み込んだメトリクスを Grafana のダッシュボードから見えるようにします。
Pi-hole Prometheus Exporterの導入
自宅で広告ブロックDNSとして活躍している、Apline Linuxベースの仮想マシンPi-holeサーバ。
前回のPrometheus構築時に、Node-Exporterを既に仕込み済みです。
更に、Pi-hole関連のメトリクス収集のために、こちらのPi-hole Exporterを導入します。
インストール方法は公開されているバイナリはダウンロードするか、go言語の環境を整えた上でソースからビルドするかの2通り。もちろん前者を試すも、Pi-holeを入れているAlpine Linux上ではエラー異常終了。
|
1 2 3 4 5 6 7 8 9 10 11 |
/home/user # wget -O /usr/local/bin/pihole-exporter https://github.com/eko/pihole-exporter/releases/latest/download/pihole_exporter-linux-amd64 Saving to: '/usr/local/bin/pihole-exporter' /usr/local/bin/pihole-exporter 100%[===============================>] 8.68M 18.9MB/s in 0.5s /home/user # chmod +x /usr/local/bin/pihole-exporter /home/user # ls -l /usr/local/bin/ -rwxr-xr-x 1 root root 18472 Mar 14 11:42 pihole -rwxr-xr-x 1 root root 9097216 Mar 3 2023 pihole-exporter /home/user # /usr/local/bin/pihole_exporter ash: /usr/local/bin/pihole_exporter: not found |
同じバイナリをUbuntu 22.04で実行すると、go言語環境が無くても動作します。
|
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 28 29 30 31 32 33 |
~/Downloads$ ./pihole_exporter-linux-amd64 INFO[0000] ------------------------------------ INFO[0000] - PI-Hole exporter configuration - INFO[0000] ------------------------------------ INFO[0000] Go version: go1.20.1 INFO[0000] PIHoleProtocol : [http] INFO[0000] PIHoleHostname : [127.0.0.1] INFO[0000] PIHolePort : [80] INFO[0000] Port : 9617 INFO[0000] Timeout : 5s INFO[0000] ------------------------------------ INFO[0000] New Prometheus metric registered: domains_blocked INFO[0000] New Prometheus metric registered: dns_queries_today INFO[0000] New Prometheus metric registered: ads_blocked_today INFO[0000] New Prometheus metric registered: ads_percentag_today INFO[0000] New Prometheus metric registered: unique_domains INFO[0000] New Prometheus metric registered: queries_forwarded INFO[0000] New Prometheus metric registered: queries_cached INFO[0000] New Prometheus metric registered: clients_ever_seen INFO[0000] New Prometheus metric registered: unique_clients INFO[0000] New Prometheus metric registered: dns_queries_all_types INFO[0000] New Prometheus metric registered: reply INFO[0000] New Prometheus metric registered: top_queries INFO[0000] New Prometheus metric registered: top_ads INFO[0000] New Prometheus metric registered: top_sources INFO[0000] New Prometheus metric registered: forward_destinations INFO[0000] New Prometheus metric registered: querytypes INFO[0000] New Prometheus metric registered: status INFO[0000] Creating client with config <Config@C0000142C0 PIHoleProtocol=http, PIHoleHostname=127.0.0.1, PIHolePort=80> INFO[0000] Starting HTTP server ^C INFO[0004] Failed to start serving HTTP requests: http: Server closed pihole-exporter HTTP server stopped |
ちなみに少し古いUbuntu 18.04ではライブラリ関連エラー。
|
1 2 3 |
~/Downloads$ ./pihole_exporter-linux-amd64 ./pihole_exporter-linux-amd64: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.32' not found (required by ./pihole_exporter-linux-amd64) ./pihole_exporter-linux-amd64: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found (required by ./pihole_exporter-linux-amd64) |
どうも公開されているバイナリが使える環境は、限られているようです。
go言語を入れてソースからビルド
Alpine Linuxのパッケージマネージャapkで、go言語をインストールします(レポジトリは既にedgeを向けています)。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
resolv11:~$ sudo df -h Filesystem Size Used Available Use% Mounted on /dev/sda3 4.1G 837.0M 3.1G 21% / resolv11:~$ sudo apk add go (1/10) Installing jansson (2.14-r4) (2/10) Installing binutils (2.42-r0) (3/10) Installing libgomp (13.2.1_git20240309-r0) (4/10) Installing libatomic (13.2.1_git20240309-r0) (5/10) Installing isl26 (0.26-r1) (6/10) Installing mpfr4 (4.2.1-r0) (7/10) Installing mpc1 (1.3.1-r1) (8/10) Installing gcc (13.2.1_git20240309-r0) (9/10) Installing musl-dev (1.2.5-r0) (10/10) Installing go (1.22.2-r0) Executing busybox-1.36.1-r21.trigger OK: 507 MiB in 168 packages resolv11:~$ sudo df -h Filesystem Size Used Available Use% Mounted on /dev/sda3 4.1G 1.1G 2.7G 29% / |
ライブラリなんかもごっそり入れられたようで、これだけで300MB程度の消費に。後から思うに、バイナリをビルドするためだけの捨て環境を同じ構成で組んで、生成されたバイナリだけ引き抜けば良かったのかも知れません。
ともあれ、そのまま go install コマンドでPi-hole Exporterをビルド。ビルドと言うより、Pythonのpipのような感覚。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
resolv11:~$ go install github.com/eko/pihole-exporter@latest go: downloading github.com/eko/pihole-exporter v0.4.0 go: downloading github.com/xonvanetta/shutdown v0.0.3 go: downloading github.com/prometheus/client_golang v1.14.0 go: downloading github.com/sirupsen/logrus v1.9.0 go: downloading golang.org/x/net v0.7.0 go: downloading github.com/heetch/confita v0.10.0 go: downloading github.com/beorn7/perks v1.0.1 go: downloading github.com/cespare/xxhash/v2 v2.2.0 go: downloading github.com/golang/protobuf v1.5.2 go: downloading github.com/prometheus/client_model v0.3.0 go: downloading github.com/prometheus/common v0.41.0 go: downloading github.com/prometheus/procfs v0.9.0 go: downloading google.golang.org/protobuf v1.28.1go: downloading golang.org/x/sys v0.5.0 go: downloading github.com/pkg/errors v0.9.1 go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.4 resolv11:~$ ls -l /go/bin/ -rwxr-xr-x 1 user user 13992086 Apr 17 10:41 pihole-exporter |
ダウンロードしたバイナリよりも大きめな実行ファイルが生成されました。
取り敢えず、引数なしで実行してみて、使えるバイナリであることを確認。このExporterのポート番号は9617番とのこと。
|
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 28 29 30 31 32 33 |
resolv11:~/go/bin$ ./pihole-exporter INFO[0000] ------------------------------------ INFO[0000] - PI-Hole exporter configuration - INFO[0000] ------------------------------------ INFO[0000] Go version: go1.22.2 INFO[0000] PIHoleProtocol : [http] INFO[0000] PIHoleHostname : [127.0.0.1] INFO[0000] PIHolePort : [80] INFO[0000] Port : 9617 INFO[0000] Timeout : 5s INFO[0000] ------------------------------------ INFO[0000] New Prometheus metric registered: domains_blocked INFO[0000] New Prometheus metric registered: dns_queries_today INFO[0000] New Prometheus metric registered: ads_blocked_today INFO[0000] New Prometheus metric registered: ads_percentag_today INFO[0000] New Prometheus metric registered: unique_domains INFO[0000] New Prometheus metric registered: queries_forwarded INFO[0000] New Prometheus metric registered: queries_cached INFO[0000] New Prometheus metric registered: clients_ever_seen INFO[0000] New Prometheus metric registered: unique_clients INFO[0000] New Prometheus metric registered: dns_queries_all_types INFO[0000] New Prometheus metric registered: reply INFO[0000] New Prometheus metric registered: top_queries INFO[0000] New Prometheus metric registered: top_ads INFO[0000] New Prometheus metric registered: top_sources INFO[0000] New Prometheus metric registered: forward_destinations INFO[0000] New Prometheus metric registered: querytypes INFO[0000] New Prometheus metric registered: status INFO[0000] Creating client with config <Config@C00004E380 PIHoleProtocol=http, PIHoleHostname=127.0.0.1, PIHolePort=80> INFO[0000] Starting HTTP server ^C pihole-exporter HTTP server stopped |
最後に、root権限で /opt 下へバイナリをコピー。以降、こちらを利用します。
|
1 2 3 |
resolv11:~/go/bin$ sudo cp pihole-exporter /opt/pihole_exporter/ resolv11:~/go/bin$ ls -l /opt/pihole_exporter/ -rwxr-xr-x 1 root root 13992086 Apr 17 10:56 pihole-exporter |
Pi-holeのAPIトークンで認証
先ず、Pi-holeのパスワード認証でPi-hole Exporterを実行、集計情報が得られました。
|
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 28 29 30 31 32 33 34 35 36 37 38 39 |
resolv11:~/go/bin$ sudo /opt/pihole_exporter/pihole-exporter -pihole_password ************ INFO[0000] ------------------------------------ INFO[0000] - PI-Hole exporter configuration - INFO[0000] ------------------------------------ INFO[0000] Go version: go1.22.2 INFO[0000] PIHoleProtocol : [http] INFO[0000] PIHoleHostname : [127.0.0.1] INFO[0000] PIHolePort : [80] INFO[0000] Pi-Hole Authentication Method : PIHolePassword INFO[0000] Port : 9617 INFO[0000] Timeout : 5s INFO[0000] ------------------------------------ INFO[0000] New Prometheus metric registered: domains_blocked INFO[0000] New Prometheus metric registered: dns_queries_today INFO[0000] New Prometheus metric registered: ads_blocked_today INFO[0000] New Prometheus metric registered: ads_percentag_today INFO[0000] New Prometheus metric registered: unique_domains INFO[0000] New Prometheus metric registered: queries_forwarded INFO[0000] New Prometheus metric registered: queries_cached INFO[0000] New Prometheus metric registered: clients_ever_seen INFO[0000] New Prometheus metric registered: unique_clients INFO[0000] New Prometheus metric registered: dns_queries_all_types INFO[0000] New Prometheus metric registered: reply INFO[0000] New Prometheus metric registered: top_queries INFO[0000] New Prometheus metric registered: top_ads INFO[0000] New Prometheus metric registered: top_sources INFO[0000] New Prometheus metric registered: forward_destinations INFO[0000] New Prometheus metric registered: querytypes INFO[0000] New Prometheus metric registered: status INFO[0000] Creating client with config <Config@C00004E338 PIHoleProtocol=http, PIHoleHostname=127.0.0.1, PIHolePort=80, PIHolePassword=*****> INFO[0000] Starting HTTP server INFO[0061] request.Header: map[Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8] Accept-Encoding:[gzip, deflate] Accept-Language:[ja,en-US;q=0.5] Connection:[keep-alive] Cookie:[PHPSESSID=cdnc06m8je7m2mtc4clc720phg] Upgrade-Insecure-Requests:[1] User-Agent:[Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0]] INFO[0061] Collecting from 127.0.0.1 INFO[0061] New tick of statistics from 127.0.0.1: 7236 ads blocked / 67097 total DNS queries ^C pihole-exporter HTTP server stopped |
次に以前、Pi-holeをHome Assistantと連携させた際に生成したAPIトークンがあるので、このトークンでPi-hole Exporterを認証、メトリクス取得を試みます。Pi-holeの設定ファイル内のトークンを読み出して、引数に連結。
|
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 28 29 30 31 32 33 34 35 36 37 38 39 40 |
resolv11:~$ API_TOKEN=$(awk -F= -v key="WEBPASSWORD" '$1==key {print $2}' /etc/pihole/setupVars.conf) resolv11:~$ sudo /opt/pihole_exporter/pihole-exporter -pihole_api_token $API_TOKEN INFO[0000] ------------------------------------ INFO[0000] - PI-Hole exporter configuration - INFO[0000] ------------------------------------ INFO[0000] Go version: go1.22.2 INFO[0000] PIHoleProtocol : [http] INFO[0000] PIHoleHostname : [127.0.0.1] INFO[0000] PIHolePort : [80] INFO[0000] Pi-Hole Authentication Method : PIHoleApiToken INFO[0000] Port : 9617 INFO[0000] Timeout : 5s INFO[0000] ------------------------------------ INFO[0000] New Prometheus metric registered: domains_blocked INFO[0000] New Prometheus metric registered: dns_queries_today INFO[0000] New Prometheus metric registered: ads_blocked_today INFO[0000] New Prometheus metric registered: ads_percentag_today INFO[0000] New Prometheus metric registered: unique_domains INFO[0000] New Prometheus metric registered: queries_forwarded INFO[0000] New Prometheus metric registered: queries_cached INFO[0000] New Prometheus metric registered: clients_ever_seen INFO[0000] New Prometheus metric registered: unique_clients INFO[0000] New Prometheus metric registered: dns_queries_all_types INFO[0000] New Prometheus metric registered: reply INFO[0000] New Prometheus metric registered: top_queries INFO[0000] New Prometheus metric registered: top_ads INFO[0000] New Prometheus metric registered: top_sources INFO[0000] New Prometheus metric registered: forward_destinations INFO[0000] New Prometheus metric registered: querytypes INFO[0000] New Prometheus metric registered: status INFO[0000] Creating client with config <Config@C0000B0320 PIHoleProtocol=http, PIHoleHostname=127.0.0.1, PIHolePort=80, PIHoleApiToken=*****> INFO[0000] Starting HTTP server INFO[0023] request.Header: map[Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8] Accept-Encoding:[gzip, deflate] Accept-Language:[ja,en-US;q=0.5] Connection:[keep-alive] Cookie:[PHPSESSID=cdnc06m8je7m2mtc4clc720phg] Upgrade-Insecure-Requests:[1] User-Agent:[Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0]] INFO[0023] Collecting from 127.0.0.1 INFO[0023] New tick of statistics from 127.0.0.1: 7308 ads blocked / 67603 total DNS queries ^C pihole-exporter HTTP server stopped |
以上で、Pi-hole Exporterの動かし方はわかりました。
Pi-hole Exporterのデーモン化
Pi-hole Exporterがシステム起動時に動いてもらうべく、Node-Exporterのinitスクリプトを元に、Pi-hole Exporter用のそれを書いてみました。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#!/sbin/openrc-run supervisor=supervise-daemon command="/opt/pihole_exporter/pihole-exporter" command_args="-pihole_hostname $HOSTNAME.lan -pihole_api_token $(awk -F= -v key="WEBPASSWORD" '$1==key {print $2}' /etc/pihole/setupVars.conf)" command_background="yes" group="prometheus" user="prometheus" logdir="/var/log/prometheus" logfile="$logdir/${SVCNAME}.log" pidfile="/var/run/${SVCNAME}.pid" start_stop_daemon_args="--stderr $logfile --user $user --group $group" depend() { need net after firewall } start_pre() { checkpath -d -o $user:$group -m755 $logdir checkpath -f -o $user:$group -m644 $logfile } |
デーモンを開始し、ランレベルを設定し終えたら、Pi-hole Exporter側の設定は終わりです。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
resolv11:/etc/init.d$ sudo vi /etc/init.d/pihole-exporter resolv11:/etc/init.d$ sudo chmod +x /etc/init.d/pihole-exporter resolv11:/etc/init.d$ ls -l /etc/init.d/*exporter -rwxr-xr-x 1 root root 478 Apr 6 22:48 /etc/init.d/node-exporter -rwxr-xr-x 1 root root 570 Apr 17 11:19 /etc/init.d/pihole-exporter resolv11:/etc/init.d$ sudo rc-service pihole-exporter start * Caching service dependencies ... [ ok ] * /var/log/prometheus/pihole-exporter.log: creating file * /var/log/prometheus/pihole-exporter.log: correcting owner * Starting pihole-exporter ... [ ok ] resolv11:/etc/init.d$ sudo rc-service pihole-exporter status * status: started resolv11:/etc/init.d$ sudo rc-update add pihole-exporter default * service pihole-exporter added to runlevel default |
Prometheusの設定
続いて、監視する側のPrometheusの設定へ移ります。
Prometheusの設定ファイル /etc/prometheus/prometheus.yml を開き、前回Node Exporterのターゲットを記述した scrape_configs: 項へ、以下の要領でPi-hole Exporterのジョブを加えます。
|
1 2 3 4 5 6 7 8 9 10 11 12 |
scrape_configs: - job_name: "prometheus" static_configs: - targets: ["localhost:9090"] - job_name: 'node' static_configs: - targets: ['localhost:9100', 'resolv11.lan:9100', 'resolv9.lan:9100', 'ubnxc.tail8d5ca.ts.net:9100', 'piaware2.lan:9100'] - job_name: 'pihole' static_configs: - targets: ['resolv11.lan:9617'] |
Prometheusサービスを再起動して設定を反映させると、
|
1 2 3 4 |
prom14:~$ sudo vi /etc/prometheus/prometheus.yml prom14:~$ sudo rc-service prometheus restart * Stopping prometheus ... [ ok ] * Starting prometheus ... [ ok ] |
Pi-hole Exporterがターゲット一覧に加わりました。
Grafanaにダッシュボード追加
Pi-hole向けのGrafanaダッシュボードは、いくつか公開されている中からこちらのダッシュボードを試してみます。
ダッシュボードIDをコピー、Grafanaへインポートしてみると、ドーナツチャートが表示されずに警告が。
|
1 |
"Panel plugin not found: grafana-piechart-panel" |
必要なプラグインを入れれば済む話なのですが、WebUI上ではなくCLIベースで行う必要があるのだそう。
|
1 2 3 4 5 6 7 8 |
prom14:~$ sudo grafana-cli plugins install grafana-piechart-panel [sudo] password for user: ✔ Downloaded and extracted grafana-piechart-panel v1.6.4 zip successfully to /var/lib/grafana/plugins/grafana-piechart-panel Please restart Grafana after installing or removing plugins. Refer to Grafana documentation for instructions if necessary. prom14:~$ sudo rc-service grafana restart * Stopping Grafana ... [ ok ] * Starting Grafana ... [ ok ] |
このプラグイン実はdeprecatedなので、このまま利用するとダッシュボード上に警告アイコンが表示されてしまいます。
Grafanaの設定ファイルに以下のオプションを追記してリロードすると、回避することができます。
|
1 2 |
[feature_toggles] autoMigrateOldPanels = true |
かくして、Pi-hole Exporterダッシュボードを表示できました。
続いてもう1つ、複数ホストを意識した、Pi-hole UIダッシュボードも入れてみました。いずれは、Pi-holeホストを増やして冗長化するつもりなので、こういうのも良いかも知れません。
次回はOpenWrtデバイスへのPrometheus Exporter導入へ進みます。









