There are dozens of tools to monitor your Raspberry Pi, including well-known ones like Prometheus, Zabbix, and of course, BAG·Tower. Sometimes, a simple bash script can do the job. Here's the script they propose:
1. The Script
#!/bin/bash
# watch processor temp
temperature=$(vcgencmd measure_temp | awk -F"=" '{print $2}')
cpu_temp=$(echo $temperature | sed 's/[^0-9.]//g')
# watch cpu and memory's load
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')
memory_usage=$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2}')
# monitor disk space
disk_usage=$(df -h / | awk '/\//{print $5}')
# monitor network usage
network_connections=$(netstat -tuln | awk 'NR>2{print $6}' | sort | uniq -c | awk '{print $2":"$1}')
# get last login (usefuyll to know who accessed your device)
last_logins=$(last -n 5)
# las system event
recent_events=$(journalctl --since "10 minutes ago" --no-pager -p err..emerg)
# Is security up to date ?
security_updates=$(apt list --upgradable 2>/dev/null | grep -i security)
# log concatenation
json_output='{
"temperature": "'$cpu_temp'",
"cpu_usage": "'$cpu_usage'",
"memory_usage": "'$memory_usage'",
"disk_usage": "'$disk_usage'",
"network_connections": "'$network_connections'",
"recent_events": "'$recent_events'",
"last_logins": "'$last_logins'",
"security_updates": "'$security_updates'"
}'
# Stockage du log
echo $json_output > monitoring.json
echo "system ed"
The script monitors:
- CPU temperature using the vcgencmd measure_temp command.
- CPU and memory usage using the top -bn1 command.
- Storage space usage with the df -h / command.
- Network connections using the netstat -tuln command.
- Recent system log events from the last 10 minutes using journalctl.
- Last user logins with the last -n 5 command.
- Pending security updates using the apt list --upgradable command.
The script then concatenates the logs and stores them in a JSON format.
What it does:
- Monitors temperature: The script uses the vcgencmd measure_temp command to get the current CPU temperature of the Raspberry Pi.
- Monitors CPU and memory usage: The top -bn1 command provides information on current CPU and memory usage.
- Monitors storage space: The df -h / command provides information on the usage of the root filesystem storage space.
- Monitors network connections: The netstat -tuln command lists active network connections.
- Retrieves system log events: The journalctl command extracts system log events from the past 10 minutes with severity levels from "error" to "emergency".
- Retrieves last user logins: The last -n 5 command fetches the last five user logins.
- Checks for pending security updates: The apt list --upgradable command checks for available updates.
In summary, monitoring and maintaining a Raspberry Pi can be simplified with a custom shell script that collects and organizes key data. This script provides an instant overview of your system's health and security. It should work seamlessly on Raspberry Pi 3, Raspberry Pi 4, and even newer models like Raspberry Pi 400 or Raspberry Pi Pico. However, feel free to customize it to best suit your needs.
2. Bonus: Push your Raspberry Pi logs to BAG·Tower:
Modify the script to send logs to BAG·Tower.
# Envoie du log sur BAG·Tower
API_KEY='YOUR_API_KEY'
curl -s -X POST "https://api.bagtower.bag-era.fr/v2/logs" -H "Content-Type: application/json" -H "x-api-key: $API_KEY" -d $json_output)
- Add a cron job to automatically push the information every 10 minutes.
*/10 * * * * /usr/bin/sh /chemin/vers/le/fichier/my_script.sh
crontab -e