7

So, I have the lm-sensors package installed, and I can run sensors and get details of my system temperature, like this:

acpitz-virtual-0
Adapter: Virtual device
temp1:        +45.0°C  (crit = +120.0°C)

k10temp-pci-00c3
Adapter: PCI adapter
temp1:        +46.5°C  (high = +70.0°C)
                       (crit = +115.0°C, hyst = +115.0°C)

What I'd like to do is be able to view a log (say, every 5 minutes) of what the temperature is. Is this possible?

  • 1
    did you install sensord ? packages.ubuntu.com/precise/sensord manpages.ubuntu.com/manpages/hardy/man8/sensord.8.html and do you want to check your logs or a graphical front end ? – Panther Jan 19 '12 at 22:35
  • @bodhi.zazen Didn't install sensord, and I don't care. Would prefer to check my logs. – jrg Jan 19 '12 at 22:36
  • With what you are asking for, unless someone has a better answer, I would write a script and send the output (with a time stamp) to a custom log and run it with cron. – Panther Jan 19 '12 at 22:41
  • Well, I'm not above installing sensord, it looks pretty cool. – jrg Jan 19 '12 at 22:45
6

Here is what I have done to log temps.

Prerequisites : lm-sensors dateutils gnuplot (gnuplot for visualization, not necessary)

One can install the above using the following command in terminal.

sudo apt install lm-sensors dateutils gnuplot

Coming to the main script :

#!/bin/bash 
# Log temperature over some time  interval given as days, hours, minutes or seconds.
# enter the variables according to your usage in the following seciton :
duration="$1"  #duration format is  ndnhnmns where n is some number and d is day,
# h is hours, m is minutes and s is seconds. For example, 4d , 4d5h30m , 5m30s, 6h30m30s are all valid.

step="$2"
#----------------------------------------------------------------------
#starting time taken as current
dt=$(date '+%Y-%m-%dT%H:%M:%S');
#et=$(date '+%Y-%m-%dT%H:%M:%S');

#----------------------------------------------------------------------
a=$(dateutils.dadd $dt  $duration )
b=$(dateutils.ddiff $dt $a -f '%S')
echo $a $b

ntimes=$((b/step))
echo $ntimes


echo "logging...";
rm t_log.txt
nms=0
while [  $nms -lt $ntimes ];  do
        sensors | grep -A 0  'Core' | cut -c18-21 |tr "\n" "\t" >> temp.txt
        let nms=nms+1
        sleep  $step
        now=$(date +"%m/%d/%Y %T")
#       echo $now
        echo -e "$(cat temp.txt)""\t$now"  >> t_log.txt
        rm temp.txt
done


#plotting using gnuplot to get a beautiful plot. 
day=86400 #different x-axis for different  time duration. A day = 86400 seconds

fcode=$(date "+%Y-%m-%d_%H%M%S") # generate a time stamp 
#echo $fcode
if [[ "$b" > "$day" ]]
then
        gnuplot -e "filename='temp_plot_$fcode'" plot_day
else
        gnuplot -e "filename='temp_plot_$fcode'" plot_time
fi
#end-of-script---------------------------------------------------------

The gnuplot command in the end needs two more files which are following.

file : plot_day

set terminal pngcairo size 1200,600 enhanced font 'Verdana'
set output sprintf('%s.png', filename)
set timefmt '%d/%m/%Y %H:%M:%S'
set xdata time
set format x '%m/%d'
set grid ytics lc rgb "#bbbbbb" lw 2 lt 1
set yr [20:100]
set title "Temperature log"
set xlabel "Date (month:day)"
set ylabel "degree Celcius"

plot "t_log.txt" using 7:1 with lines title "core:1" linewidth 3,\
     "t_log.txt" using 7:2 with lines title "core:2" linewidth 3 ,\
     "t_log.txt" using 7:3 with lines title "core:3" linewidth 3 ,\
     "t_log.txt" using 7:4 with lines title "core:4" linewidth 3 ,\
     "t_log.txt" using 7:5 with lines title "core:5" linewidth 3 ,\
     "t_log.txt" using 7:6 with lines title "core:6" linewidth 3
replot

file: plot_time

set terminal pngcairo size 1200,600 enhanced font 'Verdana'
set output sprintf('%s.png', filename)
set timefmt '%d/%m/%Y %H:%M:%S'
set xdata time
set format x '%H:%M:%S'
set grid ytics lc rgb "#bbbbbb" lw 2 lt 1
set yr [20:100]
set title "Temperature log"
set xlabel "Time (Hour:Minute:Second) "
set ylabel "degree Celcius"
plot "t_log.txt" using 7:1 with lines title "core:1" linewidth 3,\
     "t_log.txt" using 7:2 with lines title "core:2" linewidth 3 ,\
     "t_log.txt" using 7:3 with lines title "core:3" linewidth 3 ,\
     "t_log.txt" using 7:4 with lines title "core:4" linewidth 3 ,\
     "t_log.txt" using 7:5 with lines title "core:5" linewidth 3 ,\
     "t_log.txt" using 7:6 with lines title "core:6" linewidth 3 
replot

Running the script Save it, make it executable and run it as,

./script_name $nd$nh$nm$ns m

n is some number, while d=days, h=hours, m=minutes, s=seconds

m step size of measurement in seconds. Measurement will be taken after interval of m seconds

Example of usage.

./log_script.sh  3d12h 30

(explanation: log for 3 days 12 hrs with measurement at each 30 seconds)

./log_script.sh  12m30s 10

./log_script.sh  45m 2

./log_script.sh  55s 1

{It can be used with cron for regular logs. I use this script only when benchmarking and/or overclocking.}


Output

This scripts generates a log file as t_log.txt which has the temperature of the CPU cores. (For system with different cores there will be a small change. Edit the line in gnuplot script files which are plot_time and plot_day specifically, "t_log.txt" using last_column:column_for_each_core with lines title "core:6" linewidth 3).

The output in t_log.txt looks like following, (since this is a six core machine hence 6 columns of temps.)

28.0    28.0    27.0    27.0    27.0    27.0            12/18/2016 00:50:21
28.0    26.0    27.0    26.0    27.0    27.0            12/18/2016 00:50:23
27.0    27.0    26.0    26.0    27.0    26.0            12/18/2016 00:50:25
28.0    27.0    29.0    26.0    27.0    26.0            12/18/2016 00:50:27
26.0    27.0    26.0    28.0    27.0    26.0            12/18/2016 00:50:29
27.0    26.0    28.0    26.0    27.0    26.0            12/18/2016 00:50:31
27.0    26.0    26.0    26.0    27.0    26.0            12/18/2016 00:50:33
27.0    27.0    28.0    28.0    27.0    27.0            12/18/2016 00:50:35

And the plot generated looks like following :

Plot generated from gnuplot

The script can generate such plots for very long logs or many other parameters. It is also possible to plot in between logging which just needs modification of the script. The script can be improved a lot and logging of several other parameters like RAM usage, CPU load, HDD activity can be added. Enjoy !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.