Skip to content

Instantly share code, notes, and snippets.

@FrangaL
Last active September 19, 2024 20:03
Show Gist options
  • Save FrangaL/8a311c55b845662d64107f8a85f96080 to your computer and use it in GitHub Desktop.
Save FrangaL/8a311c55b845662d64107f8a85f96080 to your computer and use it in GitHub Desktop.
Raspberry PI - CPU temperature rrdtool
#!/bin/bash -e
command rrdtool || sudo apt-get install rrdtool
command pngquant || sudo apt-get install pngquant
rrdtools_dir=/var/www/html/laravel/public/images/temp-sensor
rrdtools_db=cpu_temp_data.rrd
create(){
rrdtool create $rrdtools_dir/$rrdtools_db \
--start now \
--step 60 \
--no-overwrite \
DS:cpu_temp:GAUGE:120:U:U \
RRA:AVERAGE:0.5:1:525600
}
update_database(){
READ_TEMP=$(/opt/vc/bin/vcgencmd measure_temp)
TEMP_VALUE=${READ_TEMP:5:4}
TIMESTAMP=$(date +%s)
rrdtool update $rrdtools_dir/$rrdtools_db \
${TIMESTAMP}:${TEMP_VALUE}
}
generate_graph(){
rrdtool graph $rrdtools_dir/cpu_temp_${1}.png \
--start end-${1} \
--title "${2} Log" \
--vertical-label "Temperatura C" \
--width 1024 \
--height 576 \
--color GRID#C2C2D6 \
--color MGRID#E2E2E6 \
--dynamic-labels \
--grid-dash 1:1 \
--font TITLE:10 \
--font UNIT:9 \
--font LEGEND:8 \
--font AXIS:8 \
--font WATERMARK:8 \
--lazy \
--watermark "$(date -R)" \
DEF:cpu_temp=$rrdtools_dir/${rrdtools_db}:cpu_temp:AVERAGE \
AREA:cpu_temp#FF0000AA:"Raspberry Pi CPU Temperatura" \
LINE1:cpu_temp#FF0000
}
main(){
[ ! -f "$rrdtools_dir/$rrdtools_db" ] && create
update_database
sleep 5
INDEX=0
declare -a INTERVALS=("1h" "2h" "6h" "12h" "24h" "1w" "4w" "24w" "1y")
declare -a TITLES=("1 Hora" "2 Horas" "6 Horas" "12 Horas" "1 Dia" \
"1 Semana" "1 Mes" "6 Meses" "1 Año")
ARRAYLENGTH=${#INTERVALS[@]}
for ((i=1; i<${ARRAYLENGTH}+1; i++))
do
generate_graph "${INTERVALS[$i-1]}" "${TITLES[$i-1]}"
done
pngquant $rrdtools_dir/*.png --ext .png --force
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment