Monitor your Raspberry Pi

Salvador Guerrero
3 min readAug 27, 2023

--

I use these techniques to monitor my Raspberry Pi running a full bitcoin node, plus lightning.

Git Repo

  1. Create a new private git repository, you’re going to use this to upload the information you want from the rpi.
  2. Generate a new ssh key on a user not meant for running public processes on your rpi and add it as a deploy key to the repository.
    Deploy keys restrict access to only one repo.

Install Git

$ sudo apt install git --yes

I like storing the repo in my external drive because I will be writing to it daily and don’t want to waste the microSD life when I can.
(My repository is called dashboard)

$ cd /mnt/sda

# create the dashboard directory
$ sudo mkdir dashboard

# fix permissions (optional)
$ sudo chown :satoshi dashboard
$ sudo chmod g+w dashboard

Clone

$ git clone git@github.com:ObjSal/dashboard.git dashboard

Create a script file

$ cd /mnt/sda/dashboard

$ mkdir scripts

$ vi scripts/update_logs.sh

Now past the content below, in a gist it does the following:

  • Prints the date
  • Print the temperature
  • Copies the last 1/4 of a MB of the bitcoin’ debug.log
  • Copies all the content from the top, I like to watch if the swap memory is used or not, if it is I will recur to disabling SWAP or redirecting it to an external drive.
  • Creates a file called all.log and puts everything into it.
#!/bin/bash

cd /mnt/sda/dashboard

# copy the last 1/4 of a MB of bitcoin.log
sudo tail -c 250K /mnt/sda/bitcoin/debug.log > logs/bitcoin.log
sudo chown satoshi:satoshi logs/bitcoin.log

# save all the top info with -b
top -b -n 1 > logs/top.log

# create a file with a summary of everything named all.log
echo -e $"date: $(date)" > logs/all.log
echo -e $"temp: $(vcgencmd measure_temp)" >> logs/all.log

echo -e $"\n\n-==[ top ]==-" >> logs/all.log
head -n 10 logs/top.log >> logs/all.log

echo -e $"\n\n-==[ bitcoin's debug.log ]==-" >> logs/all.log
tail -c 1K logs/bitcoin.log >> logs/all.log

sudo -u satoshi git add -u
sudo -u satoshi git commit -m "update bitcoin and top logs"
sudo -u satoshi git push

Push it to your repository

# we most likely will get an error when running the "git add" commands
# to prevent that jsut mark it safe using the below command
$ git config --global --add safe.directory /mnt/sda/dashboard

$ git add /mnt/sda/dashboard/scripts/update_logs.sh
$ git commit -m "add update_logs.sh"
$ git push

If you run the above script from the external drive like I did you will get the following error

sudo: unable to execute ./update_logs.sh: Permission denied

This is most likely due to mounting the drive without execution permissions, to fix this add exec to the fstab entry for the drive.

PARTUUID=026ccff8-01 /mnt/sda ext4 defaults,auto,users,exec,rw,nofail 0 0

Cron Job

Set up a cron job so it executes it every day at 6 AM. We can play with time using this site.

$ crontab -e

and paste the following into the end of the file

# update logs every day at 6 AM
0 6 * * * sudo sh /mnt/sda/dashboard/scripts/update_logs.sh > /mnt/sda/dashboard/scripts/update_logs.sh.log

And that’s it, good luck monitoring! 😎

You can host a site that parses the logs and displays them beautifully on a website or anything you want 📊

If you have more ideas on what to upload feel free to add a comment!

TODO

  1. Add Tor logs using nyx or some other tool to all.log.

References

--

--

Salvador Guerrero

Computer Science Engineer, Cross-Platform App Developer, Open Source contributor. 🇲🇽🇺🇸