Skip to content

Instantly share code, notes, and snippets.

@chiva
Last active December 3, 2017 06:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chiva/5acb9802782b42b28fa9 to your computer and use it in GitHub Desktop.
Save chiva/5acb9802782b42b28fa9 to your computer and use it in GitHub Desktop.
#!/bin/bash
# How to make this work
#----------------------
# 1. Install required packages:
# sudo apt-get install curl
# 2. Copy this file to /etc
# 3 .Make this file executable:
# chmod +x rpi_stats.sh
# 4. Paste to crontab (command: crontab -e)
# */5 * * * * /etc/rpi_stats.sh > /dev/null 2>&1 # Update thingspeak every 5 minutes
# 5. Set the graphs at Thingspeak to:
# Days: 7
# Results: (empty)
# Average: 240
# 6. Update the API key with the one of your channel
api_key='YOUR_CHANNEL_KEY'
### CODE ###
# get cpu usage as a percent
cpu_load=`cat /proc/loadavg | awk '{ print $2 }'`
# get cpu temperature
cpu_temp=`vcgencmd measure_temp | egrep -o '[0-9]+.[0-9]+'`
# get ram use as a percent
ram_percent=`free -m | awk '/-\/+/ { printf("%.2f\n", 100*($3/($3+$4))) }'`
# get SD use as a percent
sd_percent=`df /dev/root | awk '/\/dev\/root/ { print substr($5,1,length($5)-1) }'`
# get USB use as a percent
usb_percent=`df /dev/sda1 | awk '/\/dev\/sda1/ { print substr($5,1,length($5)-1) }'`
# Thingspeak
curl -k --data "api_key=$api_key&field1=$cpu_temp&field2=$cpu_load&field3=$ram_percent&field4=$sd_percent&field5=$usb_percent" https://api.thingspeak.com/update
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment