環境センサのデータをInfluxDBとGrafanaを使って可視化する(3)

この記事では、とりあえずGrafanaをラズパイ5にインストールしてみます。

# Grafanaのインストール

## Grafanaのリポジトリ情報を追加

Grafanaのリポジトリ情報をsources.listに追加します。

```shell:
nomura@raspberrypi5:~ $ wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
OK
nomura@raspberrypi5:~ $ echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee /etc/apt/sources.list.d/grafana.list
deb https://packages.grafana.com/oss/deb stable main
```

リスト追加ができたらapt updateを実施して更新します。

```shell:
nomura@raspberrypi5:~ $ sudo apt update
Hit:1 http://deb.debian.org/debian bookworm InRelease
Hit:2 http://deb.debian.org/debian-security bookworm-security InRelease
Hit:3 http://deb.debian.org/debian bookworm-updates InRelease
Get:4 https://packages.grafana.com/oss/deb stable InRelease [5,983 B]
Get:5 https://packages.grafana.com/oss/deb stable/main armhf Packages [138 kB]
Get:6 https://packages.grafana.com/oss/deb stable/main arm64 Packages [172 kB]
Hit:7 http://archive.raspberrypi.com/debian bookworm InRelease
Fetched 316 kB in 1s (247 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
66 packages can be upgraded. Run 'apt list --upgradable' to see them.
W: https://packages.grafana.com/oss/deb/dists/stable/InRelease: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.
```

## Grafanaのインストール

aptコマンドを使ってGrafanaをインストールします。

```shell:Grafanaのインストール
nomura@raspberrypi5:~ $ sudo apt install grafana
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  musl
The following NEW packages will be installed:
  grafana musl
0 upgraded, 2 newly installed, 0 to remove and 66 not upgraded.
Need to get 111 MB of archives.
After this operation, 424 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 https://packages.grafana.com/oss/deb stable/main arm64 grafana arm64 10.4.0 [111 MB]
Get:2 http://deb.debian.org/debian bookworm/main arm64 musl arm64 1.2.3-1 [392 kB]
Fetched 111 MB in 8s (13.1 MB/s)
Selecting previously unselected package musl:arm64.
(Reading database ... 144830 files and directories currently installed.)
Preparing to unpack .../musl_1.2.3-1_arm64.deb ...
Unpacking musl:arm64 (1.2.3-1) ...
Selecting previously unselected package grafana.
Preparing to unpack .../grafana_10.4.0_arm64.deb ...
Unpacking grafana (10.4.0) ...
Setting up musl:arm64 (1.2.3-1) ...
Setting up grafana (10.4.0) ...
Adding system user `grafana' (UID 113) ...
Adding new user `grafana' (UID 113) with group `grafana' ...
Not creating home directory `/usr/share/grafana'.
### NOT starting on installation, please execute the following statements to configure grafana to start automatically using systemd
 sudo /bin/systemctl daemon-reload
 sudo /bin/systemctl enable grafana-server
### You can start grafana-server by executing
 sudo /bin/systemctl start grafana-server
Processing triggers for man-db (2.11.2-2) ...
```

## Grafanaの起動

Grafanaインストール直後のステータスを確認してみます。

```shell:Grafanaの状態を確認
nomura@raspberrypi5:~ $ sudo systemctl status grafana-server
○ grafana-server.service - Grafana instance
     Loaded: loaded (/lib/systemd/system/grafana-server.service; disabled; preset: enabled)
     Active: inactive (dead)
       Docs: http://docs.grafana.org
```

Grafanaが起動していないようなので、起動します。
ついでにラズパイを起動したとき、Grafanaも起動するようにしておきます。

```shell:Grafanaの起動
nomura@raspberrypi5:~ $ sudo systemctl unmask grafana-server
nomura@raspberrypi5:~ $ sudo systemctl start grafana-server
nomura@raspberrypi5:~ $ sudo systemctl enable grafana-server
Synchronizing state of grafana-server.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable grafana-server
Created symlink /etc/systemd/system/multi-user.target.wants/grafana-server.service → /lib/systemd/system/grafana-server.service.
```

最後にGrafanaの状態を確認します。

```shell:Grafanaの状態を確認
nomura@raspberrypi5:~ $ sudo systemctl status grafana-server
● grafana-server.service - Grafana instance
     Loaded: loaded (/lib/systemd/system/grafana-server.service; enabled; preset: enabled)
     Active: active (running) since Sun 2024-03-10 06:30:48 JST; 11s ago
       Docs: http://docs.grafana.org
   Main PID: 4593 (grafana)
      Tasks: 14 (limit: 9250)
        CPU: 3.192s
     CGroup: /system.slice/grafana-server.service
             └─4593 /usr/share/grafana/bin/grafana server --config=/etc/grafana/grafana.ini --pidfile=/run/grafana/>

Mar 10 06:30:55 raspberrypi5 grafana[4593]: logger=ngalert.scheduler t=2024-03-10T06:30:55.510996452+09:00 level=in>
Mar 10 06:30:55 raspberrypi5 grafana[4593]: logger=ticker t=2024-03-10T06:30:55.511058138+09:00 level=info msg=star>
Mar 10 06:30:55 raspberrypi5 grafana[4593]: logger=http.server t=2024-03-10T06:30:55.512351849+09:00 level=info msg>
Mar 10 06:30:55 raspberrypi5 grafana[4593]: logger=sqlstore.transactions t=2024-03-10T06:30:55.553217104+09:00 leve>
Mar 10 06:30:55 raspberrypi5 grafana[4593]: logger=provisioning.dashboard t=2024-03-10T06:30:55.590787932+09:00 lev>
Mar 10 06:30:55 raspberrypi5 grafana[4593]: logger=provisioning.dashboard t=2024-03-10T06:30:55.590834691+09:00 lev>
Mar 10 06:30:55 raspberrypi5 grafana[4593]: logger=plugins.update.checker t=2024-03-10T06:30:55.862766928+09:00 lev>
Mar 10 06:30:55 raspberrypi5 grafana[4593]: logger=grafana.update.checker t=2024-03-10T06:30:55.914588783+09:00 lev>
Mar 10 06:30:56 raspberrypi5 grafana[4593]: logger=grafana-apiserver t=2024-03-10T06:30:56.132111946+09:00 level=in>
Mar 10 06:30:56 raspberrypi5 grafana[4593]: logger=grafana-apiserver t=2024-03-10T06:30:56.13296997+09:00 level=inf>
```

## Grafanaへアクセス

`http://raspberrypi5.local:3000/`にアクセスしてみます。
（URL とポート番号は自分の環境に合わせてください。）

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/28184/91a9e1ab-fad3-a1f1-7fd6-f78eef7974f5.png)

初期は
ユーザネーム：admin
パスワード　：admin
でログインして使えます。
ログイン直後にパスワードの変更を求められます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/28184/c2fa964a-6b30-8247-f24f-3cdcfca851ac.png)

ここでパスワードを変更すると初期画面になります。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/28184/1ab504c5-eb63-91af-4cda-1fbe82d6d8c3.png)

Grafanaのダッシュボードの作成は次回の記事で。