Monitor your Nextcloud logs for suspicious activities with Grafana Loki
As an admin of a Nextcloud instance with tens of users, you usually don't know what's going on on the server. Nextcloud is generally known for its data thriftiness, which is why you can only access the users' data in a roundabout way.
However, Nextcloud is not necessarily a black box. With the help of Nextcloud's own audit logs, you can still look inside.
General design
On the Nextcloud instance we install
- Promtail as log collector
- Loki as backend for log monitoring
- Grafana as a dashboard for the logs
- Prometheus Alertmanager for alerting
We use a pre-built dashboard to monitor the activities on the Nextcloud instance and get notified if necessary.
At Nextcloud itself, we only enable the audit logs.
Features
- Login monitoring
- Rights changes in user and file context
- Access monitoring of public shares
- Password changes
Enable Nextcloud audit logs
The audit logs are only for the administrator and should not be freely accessible.
The configuration is simple and is done via an entry in the config.php of Nextcloud.
Reboot your Nginx/Apache2 instance for the change to become effective:
# NGINX
systemctl restart nginx
# Apache2 CentOS
systemctl restart httpd
# Apache2 Debian
systemctl restart apache2
Install Grafana, Alertmanager and Loki
As usual, we install Loki and Grafana as a Docker compose stack.
Create a new grafana folder and the following files and folders inside this folder:
.
│
├── docker-compose.yml
├── loki
│ └── alert
│ ├── chunks
│ ├── index
│ ├── rules
│ ├── wal
│ ├── loki-local-config.yaml
├── prom
│ └── alertmanager.yml
mkdir grafana
cd grafana
touch docker-compose.yml
mkdir {loki,prom}
cd loki
touch loki-local-config.yaml
mkdir {chunks,index,rules,wal}
touch ../prom/alertmanager.yml
Loki starts by default with the user UID and GID "10001:10001", so we need to adjust the file permissions for loki in the created loki folder:
Edit docker-compose.yml and add:
In the Loki configuration file, enter the following:
The loki folder is temporary storage for Loki. You can read the exact explanation on the Loki website.
Fill the alertmanager.qqyml file in the prom folder you created earlier with the following:
Start the containers with the command:
docker-compose up -d
Install Promtail for log aggregation
The Nextcloud logs have to get to Loki somehow. We do this via the free software Promtail.
While it would have been possible to install Promtail over Docker, trust me: you don't necessarily want to mess with docker networking if you can work around it.
Also, you can use Promtail + Loki in the longer term for other logs than just Nextcloud. Using Docker, you would have to additionally mount every single log you want to monitor. That's one more line per log.
Promtail is in the same git as Loki. You can find the latest version here.
At the time of this article, version 2.7.1 is current:
# apt system
wget https://github.com/grafana/loki/releases/download/v2.7.1/promtail_2.7.1_amd64.deb && dpkg -i promtail_2.7.1_amd64.deb
# rpm system
wget https://github.com/grafana/loki/releases/download/v2.7.1/promtail-2.7.1.aarch64.rpm && rpm -i promtail-2.7.1.aarch64.rpm
The installation creates the /etc/promtail-local-config.yaml file.
Change the configuration as follows. Remember to adjust the path for the Nextcloud audit logs that you specified in the Nextcloud configuration:
Start promtail:
systemctl enable --now promtail.service
Set Loki data source in Grafana
You can reach your Grafana instance via port 3000.
Default login for grafana is admin:admin
After login you have to add Loki as data source.
Since Loki belongs to the same Docker compose stack, you can simply address Loki via loki. Fill in the configuration as follows and also set custom http headers for websockets.
Connection: Upgrade
Upgrade: websocket
Create a dashboard for Nextcloud
This is the fun part because it's quick.
Import a new dashboard:
There is already a predefined dashboard under the ID 17987
Click on Load and set loki as the data source.
In the last step, only the variables from the dashboard need to be adjusted.
Click on the save icon in the upper right corner and make sure that the "Save current variable values as dashboard default" is checked.
The dashboard is ready. Take a look around and familiarize yourself with the dashboard.
Create alert rules for notification
Since we are not in front of the screen at all times, rules for alerting must be created.
To do this, we use the Prometheus alert manager previously installed via Docker.
The rules are created via YAML code in the loki/rules folder.
To do this, we must first create a new tenant folder for the rules. For some reason, Loki has adopted the name "fake".
cd loki/rule
mkdir fake
cd fake
touch nextcloud.yaml
Rules for notification are created in the nextcloud.yaml file.
Rules consist of:
- Name
- Rule
- Expression in LogQL syntax
- Label for severity
- annotation.
You can check out some sample rules on my git to get started.
The rules are enough to make a few things for yourself. Of course, you can also simply adopt them.
Save the file and make sure that every space is in its correct place (YAML syntax!).
Restart the container stack:
docker-compose down && docker-compse up -d
Continue on the Grafana interface and look at the item "Alert rules".
Grafana fetches the alert rules via API. You should see some new rules.
A few more things
I strongly advise to run Grafana, Loki and Promtail only behind a reverse proxy for security reasons.
The source code to the dashboard is freely available on Github.
The dashboard took a bit of a cue from Voidquark's dashboard, which however came to my attention relatively late.