Graphing CPU Temperatures in Linux

July 12th, 2009 by wd5gnr

I recently changed motherboards and as always mounting the fan and heatsink on the CPU makes me feel stupid. I have a large non-stock cooler and it is always a struggle to make it fit with all the other things motherboard makers stuff near the CPU these days. After some busted knuckles, I thought I had it. But the temperature was a little high for my taste and seemed to climb for no good reason.

I looked at the setup and realized the plate that touches the CPU was off center. So I decided to remount it. Well, turns out there was a reason the plate wouldn’t center, but I eventually solved it. But all this has given me an unsual interest in temperature the last few days.

I won’t tell you how to set up lmsensors since there are plenty of good references on how to do so (a quick google on “lmsensors howto” shows lots of pages including this one. I even used ksensor (I use KDE) to give a nice display. I was thinking how nice it would be to automatically collect the data and graph it. Then I thought, well, why not?

I wrote a quick and dirty script — you’d need to change it for your setup. I have an AMD 5600+ (overclocked though), so when I run “sensors” I get several devices. The one I wanted was the k8temp-pci-00c3 which shows the two core temperatures. Unfortunately, it isn’t in a great format for processing:

k8temp-pci-00c3
Adapter: PCI adapter
Core0 Temp:  +38.0°C
Core1 Temp:  +36.0°C

But certainly awk is up to the challenge of fixing this up. Here’s the quicky script:

#!/bin/bash
#usage: tempdata delay file
echo Press ^C to exit
echo "Date/Time,Core 0,Core 1">$2
while true
do
   echo -n `date +%Y%m%d%H%M%S,` >>$2
   sensors k8temp-pci-00c3 | awk 'NR==3 { gsub/.C/,""); c1=$3; } NR==4 { gsub(/.C/,""); print c1 "," $3; } >>$2
   sleep $1
done

That does it. The long line that starts with sensors might not show up right, so here it is broken up:

   sensors k8temp-pci-00c3 |
     awk 'NR==3 { gsub/.C/,""); c1=$3; }
          NR==4 { gsub(/.C/,""); print c1 "," $3; } >>$2


You wind up with a file that looks like this:

Date/Time,Core 0,Core 1
20090312075009,+38.0,+37.0
20090312075039,+38.0,+37.0
. . .

So all that’s left is to plot it. I thought about using gnuplot, but figured it would be easier to import the data into OpenOffice Calc and plot it that way. In Calc, you can import a text file by using Insert | Sheet from File. Pick the file and use a comma as a seperator. Then its simple enough to select the data and use Insert Chart to make any sort of graph you like. Here’s a short run of my system going from idle to playing Urban Terror, which usually stresses it about as much as running Xilinx ISE on a big CPU design.

CPU Temperature Plot

CPU Temperature Plot

So there you have it. A classic example of using little Linux tools to create something bigger.

Add This! BlogLines del.icio.us Digg Diigo DZone Facebook Google Google Reader Yahoo! MyWeb Netscape Netvouz reddit SlashDot Sphere StumbleUpon Technorati

2 Responses

  1. mie9iel

    In my machine k8temp-pci-00c3 senses my PCIe graphics card, CPU temps are read by a diferent sensor (it8716-isa-0228 in my case).

  2. mie9iel

    ps Nice job on the rest though

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.