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

以前、PostgreSQLとMetabaseを利用して環境センサのデータを見えるかしたのですが、今回は僕の周りで話題のInfluxDBとGrafanaを使って可視化していきます。

この記事では、InfluxDBのインストールと初期設定をおこなっていきます。

# InfluxDBのインストール

今回は、この記事を参考にさせていただきました。

https://qiita.com/h-kiyo/items/3a032b97b8768e47eb34

上記の記事では、InfluxDBのリポジトリを登録の手順から書かれていましたが、現状は必要ないようです。

```shell:
nomura@raspberrypi5:~ $ sudo apt -y install influxdb influxdb-client
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  influxdb influxdb-client
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 6,517 kB of archives.
After this operation, 24.8 MB of additional disk space will be used.
Get:1 http://deb.debian.org/debian bookworm/main arm64 influxdb arm64 1.6.7~rc0-1+b13 [4,483 kB]
Get:2 http://deb.debian.org/debian bookworm/main arm64 influxdb-client arm64 1.6.7~rc0-1+b13 [2,034 kB]
Fetched 6,517 kB in 1s (4,734 kB/s)
Selecting previously unselected package influxdb.
(Reading database ... 147728 files and directories currently installed.)
Preparing to unpack .../influxdb_1.6.7~rc0-1+b13_arm64.deb ...
Unpacking influxdb (1.6.7~rc0-1+b13) ...
Selecting previously unselected package influxdb-client.
Preparing to unpack .../influxdb-client_1.6.7~rc0-1+b13_arm64.deb ...
Unpacking influxdb-client (1.6.7~rc0-1+b13) ...
Setting up influxdb-client (1.6.7~rc0-1+b13) ...
Setting up influxdb (1.6.7~rc0-1+b13) ...
Adding system user `influxdb' (UID 112) ...
Adding new user `influxdb' (UID 112) with group `nogroup' ...
Not creating home directory `/var/lib/influxdb'.
Adding group `influxdb' (GID 122) ...
Done.
Adding user `influxdb' to group `influxdb' ...
Done.
Created symlink /etc/systemd/system/influxd.service → /lib/systemd/system/influxdb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/influxdb.service → /lib/systemd/system/influxdb.service.
Processing triggers for man-db (2.11.2-2) ...
```
# インストール確認

systemctlコマンドを使ってInfluxDBの状態を確認します。

```shell:
nomura@raspberrypi5:~ $ sudo systemctl status influxdb.service
● influxdb.service - InfluxDB is an open-source, distributed, time series database
     Loaded: loaded (/lib/systemd/system/influxdb.service; enabled; preset: enabled)
     Active: active (running) since Sun 2024-06-02 13:54:07 JST; 6min ago
       Docs: man:influxd(1)
   Main PID: 2081 (influxd)
      Tasks: 10 (limit: 9252)
        CPU: 95ms
     CGroup: /system.slice/influxdb.service
             mq2081 /usr/bin/influxd -config /etc/influxdb/influxdb.conf

Jun 02 13:54:07 raspberrypi5 influxd[2081]: ts=2024-06-02T04:54:07.547619Z lvl=info msg="Registered diagnostics client" log_id=0pXpbRgG000 service=monitor name=system
Jun 02 13:54:07 raspberrypi5 influxd[2081]: ts=2024-06-02T04:54:07.547642Z lvl=info msg="Starting precreation service" log_id=0pXpbRgG000 service=shard-precreation check_inter>
Jun 02 13:54:07 raspberrypi5 influxd[2081]: ts=2024-06-02T04:54:07.547647Z lvl=info msg="Starting snapshot service" log_id=0pXpbRgG000 service=snapshot
Jun 02 13:54:07 raspberrypi5 influxd[2081]: ts=2024-06-02T04:54:07.547660Z lvl=info msg="Starting continuous query service" log_id=0pXpbRgG000 service=continuous_querier
Jun 02 13:54:07 raspberrypi5 influxd[2081]: ts=2024-06-02T04:54:07.547691Z lvl=info msg="Starting HTTP service" log_id=0pXpbRgG000 service=httpd authentication=false
Jun 02 13:54:07 raspberrypi5 influxd[2081]: ts=2024-06-02T04:54:07.547701Z lvl=info msg="opened HTTP access log" log_id=0pXpbRgG000 service=httpd path=stderr
Jun 02 13:54:07 raspberrypi5 influxd[2081]: ts=2024-06-02T04:54:07.547792Z lvl=info msg="Listening on HTTP" log_id=0pXpbRgG000 service=httpd addr=[::]:8086 https=false
Jun 02 13:54:07 raspberrypi5 influxd[2081]: ts=2024-06-02T04:54:07.547807Z lvl=info msg="Starting retention policy enforcement service" log_id=0pXpbRgG000 service=retention ch>
Jun 02 13:54:07 raspberrypi5 influxd[2081]: ts=2024-06-02T04:54:07.547802Z lvl=info msg="Storing statistics" log_id=0pXpbRgG000 service=monitor db_instance=_internal db_rp=mon>
Jun 02 13:54:07 raspberrypi5 influxd[2081]: ts=2024-06-02T04:54:07.547979Z lvl=info msg="Listening for signals" log_id=0pXpbRgG000
```
psコマンドでも確認してみましょう
```:shell
nomura@raspberrypi5:~ $ ps aux | grep influxdb
influxdb    2081  0.0  0.2 1429664 22016 ?       Ssl  13:54   0:00 /usr/bin/influxd -config /etc/influxdb/influxdb.conf
nomura      2155  0.0  0.0   6240  1536 pts/0    S+   14:02   0:00 grep --color=auto influxdb
```

# InfluxDBの初期設定

## データベースとユーザの作成

influxシェルを利用してデータベースとユーザを作成していきます。

```shell:Influxシェルの起動
nomura@raspberrypi5:~ $ influx
Connected to http://localhost:8086 version 1.6.7~rc0
InfluxDB shell version: 1.6.7~rc0
> 
```

以降はInfluxシェル上での操作になります。

```shell:データベースとユーザの作成
> create database omron_sensor
> use omron_sensor
Using database omron_sensor
> create user test_user with password 'test' with all privileges
> grant all privileges on omron_sensor to test_user
```

```shell:作成したユーザの確認
> show users
user      admin
----      -----
test_user true
```

Influxシェルを終了します。

```shell:Influxシェルの終了
> exit
```

### ライブラリのインストール

PythonでInfluxDBへの読み書きができるようにライブラリをインストールします。

```shell:ライブラリのインストール
nomura@raspberrypi5:~ $ sudo apt -y install python3-influxdb
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  python3-dateutil python3-msgpack
The following NEW packages will be installed:
  python3-dateutil python3-influxdb python3-msgpack
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 193 kB of archives.
After this operation, 972 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian bookworm/main arm64 python3-dateutil all 2.8.2-2 [78.3 kB]
Get:2 http://deb.debian.org/debian bookworm/main arm64 python3-msgpack arm64 1.0.3-2+b1 [62.6 kB]
Get:3 http://deb.debian.org/debian bookworm/main arm64 python3-influxdb all 5.3.1-3 [52.2 kB]
Fetched 193 kB in 0s (478 kB/s)
Selecting previously unselected package python3-dateutil.
(Reading database ... 147751 files and directories currently installed.)
Preparing to unpack .../python3-dateutil_2.8.2-2_all.deb ...
Unpacking python3-dateutil (2.8.2-2) ...
Selecting previously unselected package python3-msgpack.
Preparing to unpack .../python3-msgpack_1.0.3-2+b1_arm64.deb ...
Unpacking python3-msgpack (1.0.3-2+b1) ...
Selecting previously unselected package python3-influxdb.
Preparing to unpack .../python3-influxdb_5.3.1-3_all.deb ...
Unpacking python3-influxdb (5.3.1-3) ...
Setting up python3-dateutil (2.8.2-2) ...
Setting up python3-msgpack (1.0.3-2+b1) ...
Setting up python3-influxdb (5.3.1-3) ...
```