From f32072e99781ccf073672ff3eccabd66f5299c5a Mon Sep 17 00:00:00 2001 From: Pieter Hollander Date: Mon, 25 Mar 2024 10:59:36 +0100 Subject: [PATCH] Add systemd configuration. --- README.md | 22 +++++++++++++++++-- systemd/opendtu-logger.service | 39 ++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 systemd/opendtu-logger.service diff --git a/README.md b/README.md index e2ae92d..3fe7d0e 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,30 @@ Preferably, run the Docker image using the Docker compose examples provided in t Go to the [releases page](https://git.hollander.online/energy/opendtu-logger/releases) and download the most recent stable release. -#### Running example +#### Running the binary as local user ```sh REMOTE_URL="opendtu:80" DB_URL="host=localhost port=5432 user=postgres password=secret dbname=dtu sslmode=disable" TIMESCALEDB_ENABLED="true" TZ="Europe/Amsterdam" ./opendtu-logger ``` +#### Creating a more permanent setup on Linux using systemd + +```sh +sudo adduser --system --group --disabled-login --home /opt/opendtu-logger opendtu-logger + +sudo mkdir /opt/opendtu-logger +sudo mkdir /opt/opendtu-logger/data +sudo mv ./opendtu-logger /opt/opendtu-logger/ + +sudo chown -R opendtu-logger:opendtu-logger /opt/opendtu-logger +``` + +```sh +sudo nano /etc/systemd/system/opendtu-logger.service +``` + +Add the contents of `systemd/opendtu-logger.service` and alter the `REMOTE_URL` and `DB_URL` Environment variables to match your setup. + ### PostgreSQL setup The OpenDTU logger logs to a PostgreSQL database and optionally supports TimescaleDB. Create a separate database and user using the following commands. @@ -64,4 +82,4 @@ The `grafana` folder contains example dashboards. ## Known issues - The logger will currently log every event posted by OpenDTU to the websocket. Checks still need to be added to determine the uniqueness of an event. For more information, see also [this OpenDTU Github issue](https://github.com/tbnobody/OpenDTU/issues/1800). -- Upon restart of the OpenDTU, OpenDTU Logger will need to be restarted manually as well. Automatic reconnection logic is a work in progress. +- Upon restart of the OpenDTU, OpenDTU Logger will need to be restarted manually as well. Automatic reconnection logic is a work in progress. Using the `systemd` service file provides a workaround for this. diff --git a/systemd/opendtu-logger.service b/systemd/opendtu-logger.service new file mode 100644 index 0000000..9c14fb6 --- /dev/null +++ b/systemd/opendtu-logger.service @@ -0,0 +1,39 @@ + GNU nano 7.2 /etc/systemd/system/opendtu-logger.service +[Unit] +Description=OpenDTU Logger +Documentation=https://git.hollander.online/energy/opendtu-logger +After=network.target auditd.service + +# If using a PostgreSQL DB on the local machine, uncomment the following additional "After" and "Require" sections. +## PostgreSQL +# After=postgresql.service +# Requires=postgresql.service + +[Service] +RestartSec=10s +Type=simple + +User=opendtu-logger +Group=opendtu-logger + +Environment="REMOTE_URL=opendtu.local:80" +Environment="DB_URL=host=localhost port=5432 user=postgres password=secret dbname=dtu sslmode=disable" +Environment="TIMESCALEDB_ENABLED=true" +Environment="TZ=Europe/Amsterdam" + +WorkingDirectory=/opt/opendtu-logger/ +ExecStart=/opt/opendtu-logger/opendtu-logger +Restart=always + +# Isolate OpenDTU Logger from the rest of the system +PrivateTmp=true +PrivateDevices=true +ProtectHome=true +NoNewPrivileges=true +ProtectSystem=strict + +# Only allow writes to the following directories +ReadWritePaths=/opt/opendtu-logger/data/ + +[Install] +WantedBy=multi-user.target