最近、Grafanaを使っていると、画面更新時に「Unauthorized」というメッセージでログアウトしてしまう問題事象が頻発していました。いろいろとGittHub Q&Aとかで調べていると、Grafanaでデフォルトで使用されているデータベース(sqlite3)に関するパフォーマンスの問題であることが濃厚のようでした。 そのため、データベースをPostgreSQLに変更することにしましたが、併せて以下構成の見直しを行うことにしました。
・Grafana Ver7から最新版のVer8へ変更(ダッシュボードは移行)
・計測システムアプリケーションを2台のRaspberry Pi へ分散配置

⇒前回記事:「車庫屋根ソーラー発電システム30(コントローラ変更)
⇒関連記事:「車庫屋根ソーラー発電システム29(計測システム改善)

Grafana01


【2台のRaspberry Pi へ分散配置の構成について】

 Raspberry Pi 3 Model B+(以降PI3と略)にすべてのアプリケーションを導入していました。
kousei1-2
 Raspberry Pi 4 Model B(以降PI4と略)にGrafana Ver8 を新規導入し、PI3のGrafanaは停止します。また、PI4自体を監視するためにPrometheusも新規導入します。さらに、効果は今後検証ですが、Prometheus Server をPI3、PI4 両方に配置し、SmartBMSと充放電コントローラの計測データの冗長化を試みています。
kousei2-2


【構築ログ】

・Postgre SQL のインストール

# apt install postgresql
 ・・・
debian found in ID_LIKE, treating as Debian
Adding user postgres to group ssl-cert
 ・・・
/usr/lib/postgresql/11/bin/initdb -D /var/lib/postgresql/11/main --auth-local peer --auth-host md5
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/11/main ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Asia/Tokyo
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

Success. You can now start the database server using:

    pg_ctlcluster 11 main start

Ver Cluster Port Status Owner    Data directory              Log file
11  main    5432 down   postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
update-alternatives: using /usr/share/postgresql/11/man/man1/postmaster.1.gz to provide /usr/share/man/man1/postmaster.1.gz (postmaster.1.gz) in auto mode
 ・・・
Processing triggers for man-db (2.8.5-2) ...
Processing triggers for libc-bin (2.28-10+rpi1) ...

・Postgre SQL バージョンの確認

# psql --version
psql (PostgreSQL) 11.12 (Raspbian 11.12-0+deb10u1)

・Postgre SQL 起動環境の確認

# systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
   Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
   Active: active (exited) since Sat 2021-07-24 23:30:30 JST; 1min 7s ago
 Main PID: 32717 (code=exited, status=0/SUCCESS)
    Tasks: 0 (limit: 4915)
   CGroup: /system.slice/postgresql.service
 ・・・

# cat /etc/passwd
 ・・・
postgres:x:117:126:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash
 ・・・

# passwd postgres
New password: 
Retype new password: 
passwd: password updated successfully

# vi /etc/postgresql/11/main/pg_hba.conf
 ・・・
# "local" is for Unix domain socket connections only
local   all             all                                     peer
 ・・・
 ↓変更
 ・・・
# "local" is for Unix domain socket connections only
local   all             all                                     md5
 ・・・

# systemctl restart postgresql

・Postgre SQL grafanaユーザ、grafanaデータべース登録

# su - postgres
$ createuser grafana
$ createdb grafana

$ psql
psql (11.12 (Raspbian 11.12-0+deb10u1))
Type "help" for help.

postgres=# ALTER ROLE grafana PASSWORD 'xxxxxxx'; ⇒あとでgrafanaに設定するパスワード
ALTER ROLE

postgres=# exit

・Postgre SQL grafanaユーザによる garafanaデータベースへの接続確認

$ psql -U grafana grafana
Password for user grafana: 
psql (11.12 (Raspbian 11.12-0+deb10u1))
Type "help" for help.

grafana=> exit

・prometheus サーバ と node exporter インストール

# apt install prometheus prometheus-node-exporter
 ・・・
Processing triggers for systemd (241-7~deb10u6+rpi1) ...
 ・・・

・prometheus バージョン確認

# prometheus --version
prometheus, version 2.7.1+ds (branch: debian/sid, revision: 2.7.1+ds-3)
  build user:       pkg-go-maintainers@lists.alioth.debian.org
  build date:       20190210-18:10:02
  go version:       go1.11.5

・prometheus 起動環境の確認

# systemctl status prometheus
● prometheus.service - Monitoring system and time series database
   Loaded: loaded (/lib/systemd/system/prometheus.service; enabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: https://prometheus.io/docs/introduction/overview/

# systemctl start prometheus-node-exporter
# systemctl start prometheus

・prometheus サーバ設定(データ取得先exporterにBMSとsolarshedを追加)

# cat /etc/prometheus/prometheus.yml
 ・・・
    # Override the global default and scrape targets from this job every 5 seconds.
    # scrape_interval: 60s ⇒コメントアウト
    # scrape_timeout: 5s ⇒コメントアウト
 ・・・
 ↓最終行の下に追加
  - job_name: solarshed
    static_configs:
      - targets: ['<PI3のIPアドレス>:5000']
  - job_name: bms
    static_configs:
      - targets: ['<PI3のIPアドレス>:8000']

・prometheus サーバ TSDB 保存期間の変更(15d⇒90d)

# vi /etc/default/prometheus
 ・・・
# Set the command-line arguments to pass to the server.
ARGS=""
 ・・・
 ↓変更
 ・・・
# Set the command-line arguments to pass to the server.
ARGS="--storage.tsdb.retention=90d --web.enable-admin-api"
 ・・・

・prometheus サーバ再起動による設定反映と起動状況の確認

# systemctl restart prometheus
# systemctl status prometheus
● prometheus.service - Monitoring system and time series database
   Loaded: loaded (/lib/systemd/system/prometheus.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2021-07-25 00:14:08 JST; 11s ago
     Docs: https://prometheus.io/docs/introduction/overview/
 Main PID: 2926 (prometheus)
    Tasks: 17 (limit: 4915)
   CGroup: /system.slice/prometheus.service
           mq2926 /usr/bin/prometheus
 ・・・

・grafana インストール

# apt install -y adduser libfontconfig1
# wget https://dl.grafana.com/oss/release/grafana_8.0.6_armhf.deb
--2021-07-24 23:50:47--  https://dl.grafana.com/oss/release/grafana_8.0.6_armhf.deb
Resolving dl.grafana.com (dl.grafana.com)... 151.101.198.217
Connecting to dl.grafana.com (dl.grafana.com)|151.101.198.217|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 52067418 (50M) [application/vnd.debian.binary-package]
Saving to: ‘grafana_8.0.6_armhf.deb’
 ・・・

# dpkg -i grafana_8.0.6_armhf.deb
Selecting previously unselected package grafana.
 ・・・

・grafana 起動環境の確認

# systemctl status grafana-server
● grafana-server.service - Grafana instance
   Loaded: loaded (/lib/systemd/system/grafana-server.service; disabled; vendor preset: enabled)
   Active: active (running) since Sat 2021-07-24 23:51:44 JST; 16s ago
     Docs: http://docs.grafana.org
 Main PID: 2733 (grafana-server)
    Tasks: 13 (limit: 4915)
   CGroup: /system.slice/grafana-server.service
           mq2733 /usr/sbin/grafana-server --config=/etc/grafana/grafana.ini --pidfile=/var/run/grafana/grafana-server.pid --packaging=deb cfg:default.path
 ・・・
# systemctl enable grafana-server

・grafana 設定ファイルの変更(Postgre SQLデータベースへの変更)

# vi /etc/grafana/grafana.ini
 ・・・
[database]
 ・・・
;type = sqlite3
;host = 127.0.0.1:3306
;name = grafana
;user = root
;password =
 ↓変更
type = postgres
host = 127.0.0.1:5432
name = grafana
user = grafana
password = xxxxxxx

・grafana 再起動と起動状況の確認

# systemctl restart grafana-server
# systemctl status grafana-server
● grafana-server.service - Grafana instance
   Loaded: loaded (/lib/systemd/system/grafana-server.service; disabled; vendor preset: enabled)
   Active: active (running) since Sat 2021-07-24 23:51:44 JST; 46min ago
     Docs: http://docs.grafana.org
 Main PID: 2733 (grafana-server)
    Tasks: 13 (limit: 4915)
   CGroup: /system.slice/grafana-server.service
           mq2733 /usr/sbin/grafana-server --config=/etc/grafana/grafana.ini --pidfile=/var/run/grafana/grafana-server.pid --packaging=deb cfg:default.path
 ・・・

・grafana ブラウザからログイン

 ⇒再起動後しばらく待ってから http://<PI4のIPアドレス>:3000/ にアクセス
 ※再起動後テーブル生成しているのか、すぐにはログインできませんでした。
 ⇒デフォルトのadmin/admin でログイン後すぐにパスワードを変更します。

grafana データソース「Prometheus-1」を登録

 ⇒トップページで左メニューの「歯車」>「Data Sources」
 ⇒「Add data source」
 ⇒データソースの一覧から「Prometheus」を選択
 ⇒以下設定値で「Save&Test」
  Name:Prometheus-1
  Default:有効
  HTTP URL:http://<PI3のIPアドレス>:9090
  ※その他デフォルト値

grafana データソース「Prometheus-PI4」を登録

 ⇒トップページで左メニューの「歯車」>「Data Sources」
 ⇒「Add data source」
 ⇒データソースの一覧から「Prometheus」を選択
 ⇒以下設定値で「Save&Test」
  Name:Prometheus-PI4
  Default:デフォルト(無効)
  HTTP URL:http://localhost:9090
  ※その他デフォルト値

・grafana ver7 でのダッシュボードのエクスポート

 ⇒http://<PI3のIPアドレス>:3000/ にアクセス
 ⇒移行対象のダッシュボードを表示
 ⇒上部左寄りにあるボタン「Share dashboard or panel」
 ⇒「Export」タブを選択
 ⇒「Save to file」でファイルにエクスポート
 ※移行対象のダッシュボードすべてについて以上の手順を繰り返す。

・grafana ver8 でのダッシュボードのインポート

 ⇒http://<PI4のIPアドレス>:3000/ にアクセス
 ⇒トップページで左メニューの「+」>「Import」
 ⇒「Upload JSON file」でエクスポートしたファイルを選んでインポートする
 ※移行対象のダッシュボードすべてについて以上の手順を繰り返す。
Grafana02
 以上、問題事象はでなくなりました。

・grafana ver7 の停止

# systemctl stop grafana-server
# systemctl disable grafana-server

・node-exporter 用の grafana テンプレート をインポート

 各Raspberry PIの監視用ダッシュボードについて grafana テンプレート を利用してみます。
 ⇒http://<PI4のIPアドレス>:3000/ にアクセス
 ⇒トップページで左メニューの「+」>「Import」
 ⇒「Import via grafana.com」にIDとして「11074」を入れて「Load」
 ⇒以下値を設定し「Import」
  Name:適当な名前をつける
  VictoriaMetrics:「Prometheus-1」(PI3用)もしくは「Prometheus-PI4」を指定
  その他デフォルト
Grafana03