Monitoring a Raspberry PI

While Raspberry Pi devices are versatile and powerful tools for various applications, their widespread use also makes them potential targets for security threats. As with any connected device, ensuring the security of a Raspberry Pi is paramount. Here's why security monitoring is essential:
Découvrir le cas en détails

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:

The script then concatenates the logs and stores them in a JSON format.

What it does:

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)
 

*/10 * * * * /usr/bin/sh /chemin/vers/le/fichier/my_script.sh
 

crontab -e
 

Need an easy and fast solution ?

Try BAGTOWER and see how easy it is
September 18, 2023

By exploring modern solutions like BAG·Tower, you can enhance the monitoring and management of your Raspberry Pi devices. Ultimately, optimize your digital projects with a simple script that handles monitoring for you.

Autres articles qui pourraient vous intéreser :

Ready to bring your asset monitoring solution to the next level ?

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
You have more questions ?
Contact us directly.
We can discuss your strategy together.