Raspberry Pi GnuPlot Temperature

So, I have a raspberry Pi, and I wanted to chart it’s CPU temp

Ex: temp log hosted on RPI itself   | backup if the the RPI is down

To do this I needed to do a few things:

  1. Log the CPU temp (I’m doing every 5 minutes)
  2. Prepare the data
  3. Chart
  4. Show it to the world

1) Log data via 5 minute cron:

$ cat templog.sh 
temp=` cat /sys/class/thermal/thermal_zone0/temp| awk -v FS=" " '{print $1/1000""}'` 
echo "$temp | `date` ">>~/temp.log

Then a cronjob as normal user

*/5 * * * * bash ~/templog.sh

now a gnuplot script (plot.pg)

#!/usr/bin/gnuplot
reset
set nokey
plot "/home/pi/temp500" u 0:1
MAX=GPVAL_Y_MAX
min_y = GPVAL_DATA_Y_MIN
max_y = GPVAL_DATA_Y_MAX
f(x) = mean_y
fit f(x) '/home/pi/temp500'  u 0:1 via mean_y
 
set label 1 gprintf("Min = %g", min_y) at 52, 21 tc rgb "#00FFFF"
set label 2 gprintf("Max = %g", max_y) at 0, 21  tc rgb "#FF0000"
set label 3 gprintf("Mean = %g", mean_y) at 102, 21 tc lt 2
set terminal png size 1400,400
set style data linespoints
set yrange [20:MAX+(MAX*.1)]
set grid
set style line 1 lt 1 lw 3 pt 3 linecolor rgb "blue"
set object 1 rectangle from graph 0,0 to graph 1,1 behind fc rgbcolor "#10000f" lw 0
set pointsize .5
plot  \
"/home/pi/temp500" using 0:1 title "cpu temp" pt 5 ps .5  lc rgb "#FF0000" , \
min_y with filledcurves y1=min_y lt 1 lw 2 lc rgb "#00FFFF"  , \
max_y with filledcurves y1=max_y lt 2 lw 2 lc rgb "#FF0000", \
mean_y with filledcurves y1=mean_y lt 3 lw 2 lc 2,\
 "/home/pi/temp500" using 0:1 title "cpu temp" pt 5 ps .10  lc rgb "#0FfFFF"

now a script to run it all: (plot.sh)

#!/bin/bash
cat /home/pi/temp.log|tail -n 500 > /home/pi/temp500A
sed '/-/d'  /home/pi/temp500A  > /home/pi/temp500
chown pi:pi /home/pi * 
/root/plot.pg > /var/www/temp.png

Python function to update a var in a list of tuple

As part of my new ssh-fail script, now written in python I found myself needing to update a var in a list of a list, but you can’t just do list_a[3][3]= ‘new thing’ :(

so i wrote this function:

def tuple_update(touple, varloc, newval):
    temp = []
    for a in range(len(tuple)):
        if varloc != a:
            temp.append(tuple[a])
        else:
            temp.append(newval)
    return temp

and you can use it like this:

>>>ip_test=[('8.8.8.8', 423, None, 0), ('4.2.2.2', 64, None, 3), ('42.42.42.42', 23, None, 10)]
 
>>> ip_test[1]
('4.2.2.2', 64, None, 3)
>>> ip_test[1]=tuple_update(ip_test[1],1,38)
>>> ip_test[1]
['4.2.2.2', 38, None, 3]
>>> ip_test[2]=tuple_update(ip_test[2],0,'100.100.2.3')
>>> ip_test[2]
['100.100.2.3', 23, None, 10]
>>> ip_test
[['9.9.9.9', 423, None, 0], ['4.2.2.2', 38, None, 3], ['100.100.2.3', 23, None, 10]]

that’s: tuple_update(list,location,newval)

SourceForge direct download with clean filename

If you ever wget files from source forge you’ll know how the file name will be annoying with a bunch of extra crap

so…:

 

#!/bin/bash
file=`echo $1 | sed 's,.*/,,g; s/?.*//'`
echo $file
wget "$1" -O ./$file

for:

% ~/sf.sh "http://downloads.sourceforge.net/project/gns-3/GNS3/0.8.3.1/GNS3-0.8.3.1-src.tar.bz2?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fgns-3%2F%3Fsource%3Ddirectory&ts=1354547913&use_mirror=voxel"
GNS3-0.8.3.1-src.tar.bz2
--2012-12-03 10:18:49--  http://downloads.sourceforge.net/project/gns-3/GNS3/0.8.3.1/GNS3-0.8.3.1-src.tar.bz2?r=http%3A%2F%
<>

Length: 2611269 (2.5M) [application/x-bzip2]
Saving to: `./GNS3-0.8.3.1-src.tar.bz2'

100%[=======================================================================================================================================================================>] 2,611,269   2.37M/s   in 1.1s    

2012-12-03 10:19:16 (2.37 MB/s) - `./GNS3-0.8.3.1-src.tar.bz2' saved [2611269/2611269]
 
( ~/del/sftest )% ls
GNS3-0.8.3.1-src.tar.bz2

yay!