Category Archives: bash

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

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!

Bash one liner: rDNS of failed ssh logins

Quickly get the rDNS of each IP that failed to login to your ssh :D

grep Failed /var/log/auth.log|grep -v pronto|sed 's/.*from //;s/ port.*//'|sort -u|while read host
do
        host "$host"
done
Host 196.108.80.114.in-addr.arpa not found: 2(SERVFAIL)
Host 71.102.207.124.in-addr.arpa not found: 2(SERVFAIL)
Host 192.190.210.125.in-addr.arpa. not found: 3(NXDOMAIN)
Host 248.196.211.125.in-addr.arpa. not found: 3(NXDOMAIN)
218.15.28.176.in-addr.arpa domain name pointer lvps176-28-15-218.dedicated.hosteurope.de.
Host 242.15.120.187.in-addr.arpa. not found: 3(NXDOMAIN)
Host 253.109.15.198.in-addr.arpa. not found: 3(NXDOMAIN)
Host 179.115.27.198.in-addr.arpa not found: 2(SERVFAIL)
8.197.61.198.in-addr.arpa domain name pointer 198-61-197-8.static.cloud-ips.com.
Host 159.203.61.198.in-addr.arpa. not found: 3(NXDOMAIN)
Host 60.82.71.198.in-addr.arpa. not found: 3(NXDOMAIN)
178.230.241.201.in-addr.arpa domain name pointer pc-178-230-241-201.cm.vtr.net.
Host 66.2.62.42.in-addr.arpa not found: 2(SERVFAIL)
82.179.79.4.in-addr.arpa domain name pointer scanning-service-4.nessus.org.
Host 61.226.49.64.in-addr.arpa. not found: 3(NXDOMAIN)
38.96.111.78.in-addr.arpa domain name pointer fire3.methosting.com.
7.138.17.85.in-addr.arpa domain name pointer hosted-by.leaseweb.com.
Host 72.129.211.95.in-addr.arpa. not found: 3(NXDOMAIN)

:D    also you can replace the “host “$host”  part with:    whois “$host” > $host    and quickly whois each IP as well, I recommend doing this in it’s own DIR though.  Then just do less *   and :n  to go to next file

Breakdown on the one liner for people new to linux/bash/celery
This part is pretty self explanatory, just greping auth.log for Failed, then grep -v is an inverse grep getting rid of my user name

grep Failed /var/log/auth.log|grep -v pronto

This part using is removing everything up to and including the word ‘from’ then everything and including the word ‘port’
the sed command is acutely doing two sed actions separated via a semicolon (no need to pipe sed to sed)

sed 's/.*from //;s/ port.*//'
the original line looks like:
Nov  9 08:22:56 tasty sshd[25254]: Failed password for root from 199.83.51.16 port 54268 ssh2
 
then end result is just "199.83.51.16"

for more useful sed one liners check out this page
this next part just sorts the massive list, and the -u flag only shows the unique ones

sort -u

 

Simple bash system monitor script

Uses nvclock (for nvidia gpu’s) and sensors
and chances are you’re gonna have to edit it some to get it to work :p

#!/bin/bash
heh=(`sensors|egrep "fan[1-3]|temp"|awk '{print $1$2}'&&
nvclock -i|egrep 'temp|RPM|PWM'|sed 's/PWM duty cycle://g; s/Fanspeed/Speed/g; s/ temperature//g; s/RPM\n/RPM/g'|sed ':q;N;s/RPM\n/RPM/g;t q'|sed 's/ /:/g'`)
for i in {0..8}
do
	out[$i]=`echo ${heh[$i]}|sed 's/:/: /g'`
done
echo -e "${out[0]}\t| ${out[3]}\t|${out[6]}\n${out[1]}\t| ${out[4]}\t|${out[7]}\n${out[2]}\t| ${out[5]}\t|${out[8]}"
unset heh out
output

Output