HOWTO: Temperature Monitoring with coretemp in Sysinfo widget



  • I installed 2.0.1-RELEASE (amd64) two days ago and after everything had been set up (voip… vomit) I wanted to have a way to show the current CPU temp of my Atom N550 on the dashboard.
    Since this wasn't included in the original release I got creative and after searching the board I came up with this.
    I know that there are probably way better ways to do this and which are already included in a future version of pfsense but maybe this is useable for anyone else. :)
    (I like it. Simple, in style with pfsense and informative :p)

    My modification features:
    fetch temps from sysctl
    CPU Temperature @ sysinfo widget
    Multicore support

    What does it look like?

    Here is what you need to do:

    First of all install the coretemp module.
    for 32bit: http://files.pfsense.org/jimp/ko-8.1/i386/coretemp.ko
    for 64bit: http://files.pfsense.org/jimp/ko-8.1/amd64/coretemp.ko

    1. Copy the above downloaded appropriate (32 or 64) coretemp.ko into /boot/modules. (since I couldn't get wget to install because of a 404 error from the installation package I was forced to use WinSCP to copy it over)
    2. Edit /boot/loader.conf.local and add

    coretemp_load="YES"
    

    to the end.
    3. open a shell, execute:

    kldload coretemp
    

    Now 'sysctl -a | grep temperature' should throw you your cpu temps.

    To get this shown on the dashboard I modified these files:

    /usr/local/www/includes/functions.inc.php

    
    @@ -136,7 +136,10 @@
    
     function has_temp() {
    
    -	/* no known temp monitors available at present */
    +	/* Figure out if we support sysctl temperatures */
    +	if(`sysctl -a | grep temperature` <> "") {
    +		return true;
    +	}
    
     	/* should only reach here if there is no hardware monitor */
     	return false;
    @@ -148,12 +151,30 @@
     }
    
     function get_temp() {
    -	switch(get_hwtype()) {
    -		default:
    -			return;
    +
    +	//get our temperatures
    +	$temp = `sysctl -a | grep temperature | cut -d":" -f2`;
    +
    +	//remove empty lines
    +	$temp = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $temp);
    +
    +	//split on new lines
    +	$temp = preg_split( '/\r\n|\r|\n/', $temp );
    +
    +	for ($i=0; $i<sizeof($temp); $i++)="" {<br="">+		//check for empty array elements and remove them
    +		if ($temp[$i] == "") {
    +			unset($temp[$i]);
    +			continue;
    +		}
    +		//Cut "C" on each lines end
    +		$temp[$i] = substr_replace($temp[$i] ,"",-1);
    +
    +		//remove all whitespaces
    +		$temp[$i] = preg_replace('/\s+/', '', $temp[$i]);
     	}
    
    -	return $ret;
    +	return $temp;
     }
    
     function disk_usage()</sizeof($temp);> 
    

    /usr/local/www/widgets/widgets/system_information.widget.php

    @@ -232,12 +232,15 @@
     		if(has_temp()):
     ?>
    
    -			Temperature
    +			CPU Temperature
    
    -				
    -				![](./themes/<?= $g[)/images/misc/bar_left.gif" height="15" width="4" border="0" align="middle" alt="left bar" />![](./themes/<?= $g[)/images/misc/bar_blue.gif" height="15" name="tempwidtha" id="tempwidtha" width="" border="0" align="middle" alt="red bar" />![](./themes/<?= $g[)/images/misc/bar_gray.gif" height="15" name="tempwidthb" id="tempwidthb" width="" border="0" align="middle" alt="gray bar" />![](./themes/<?= $g[)/images/misc/bar_right.gif" height="15" width="5" border="0" align="middle" alt="right bar" />
    -				 
    -				" />
    +				+				for ($i=0; $i <sizeof($temp); $i++)="" {="" ?="">+					![](./themes/<?= $g[)/images/misc/bar_left.gif" height="15" width="4" border="0" align="middle" alt="left bar" />![](./themes/<?= $g[)/images/misc/bar_blue.gif" height="15" name="tempwidtha" id="tempwidtha" width="" border="0" align="middle" alt="red bar" />![](./themes/<?= $g[)/images/misc/bar_gray.gif" height="15" name="tempwidthb" id="tempwidthb" width="" border="0" align="middle" alt="gray bar" />![](./themes/<?= $g[)/images/misc/bar_right.gif" height="15" width="5" border="0" align="middle" alt="right bar" />
    +					 
    +					" />
    +					
    
    +</sizeof($temp);> 
    

    or just apply this patch:
    http://rserver8.de/pfsense/pfsense_coretemp_sysinfodashboard.patch

    This thread contains information I searched together from this forum as well as genuine stuff.
    Sources:
    http://forum.pfsense.org/index.php/topic,39595.0.html
    http://forum.pfsense.org/index.php?topic=49635.15
    My brain.



  • Hi,
    I have followed touy post and now I have this error message on webgui interface after copy/paste the "function has_temp" in my functions.inc.php :
    Parse error: syntax error, unexpected T_IF in /usr/local/www/includes/functions.inc.php on line 141
    The error is on this line :
    if (sysctl -a | grep temperature <> "") {
    An idea ?



  • @stratoscott:

    Hi,
    I have followed touy post and now I have this error message on webgui interface after copy/paste the "function has_temp" in my functions.inc.php :
    Parse error: syntax error, unexpected T_IF in /usr/local/www/includes/functions.inc.php on line 141
    The error is on this line :
    if (sysctl -a | grep temperature <> "") {
    An idea ?

    Give me your whole file after the patch was applied and I can tell you what went wrong.
    Usually something like this is caused by applying a patch to another version of the file. Mine was 2.0.1 amd64, so if you apply the patch to anything else, it is likely to break if the file has changed and you just blindly apply the patch :)



  • I followed the instructions here on an i386 install of 2.0.1 and it worked fine.  I did manually edit the files, but it looked like the patches would be the same for i386.



  • Thanks!! I followed your instructions and successfully added temperature display to sysinfo.  ;) :D ;D
    It seems that there is a "}" omitted in /usr/local/www/includes/functions.inc.php for the statement "switch(get_hwtype()) { …" in your post. ???

    I just changed cpu to core, since my system display one more value.. :o




  • @hisoka01:

    Thanks!! I followed your instructions and successfully added temperature display to sysinfo.  ;) :D ;D
    It seems that there is a "}" omitted in /usr/local/www/includes/functions.inc.php for the statement "switch(get_hwtype()) { …" in your post. ???

    I just changed cpu to core, since my system display one more value.. :o

    Glad it worked for you :)
    There is no } omitted. I posted the file modifications in the patch format, that means:

    -	switch(get_hwtype()) {
    -		default:
    -			return;
    

    Is to be removed.

    The first character in each line means:



    [+] (plus( = insert this line.


    (If you did everything right)

    You can check for your additional value with:

    sysctl -a | grep temperature
    

    This will show you what it is. Maybe your board has a pwm sensor or a nb sensor too.



  • I finally discover a { without+- under the for loop, sorry for that~ :o

    "sysctl -a | grep temperature" shows:

    
    hw.acpi.thermal.tz0.temperature: 26.8C
    dev.cpu.0.temperature: 40.0C
    dev.cpu.1.temperature: 40.0C
    dev.cpu.2.temperature: 45.0C
    dev.cpu.3.temperature: 45.0C
    
    

    If 26.8C indicate CPU temp by another sensor, it seems core temp should be more accurate… :o very big difference indeed :'(



  • Just configured this on my 2.0.1 amd64 install and its working perfectly  8)

    Thanks!



  • @show-p1984:

    Give me your whole file after the patch was applied and I can tell you what went wrong.
    Usually something like this is caused by applying a patch to another version of the file. Mine was 2.0.1 amd64, so if you apply the patch to anything else, it is likely to break if the file has changed and you just blindly apply the patch :)

    Hello,
    sorry but I was very busy, I send tot you a pm with the file. It's the same version.
    Thanks



  • Hi!

    I've done exactly like U wrote but it doesn't work…
    I attached my files so u can look if there is something wrong. I'm not getting any errors in any case.

    But when I'm running sysctl -a | grep temperature it doesnt do anything…

    [2.0.1-RELEASE][root@pfsense.localdomain]/root(14): sysctl -a | grep temperature
    [2.0.1-RELEASE][root@pfsense.localdomain]/root(15):
    
    

    I've allready wrote kldload coretemp but if I'm doing it again it will just print a message: File exists

    [2.0.1-RELEASE][root@pfsense.localdomain]/root(15): kldload coretemp
    kldload: can't load coretemp: File exists
    
    

    What's wrong? :)

    system_information.widget.php.txt
    functions.inc.php.txt


  • Netgate Administrator

    If you don't get a sysctl value at the command line the widget will never work.
    What cpu do you have?

    Steve



  • @stephenw10:

    If you don't get a sysctl value at the command line the widget will never work.
    What cpu do you have?

    Steve

    AMD Athlon™ 64 X2 Dual Core Processor 6000+


  • Netgate Administrator

    Not an Intel Core processor hence coretemp won't work.
    Instead you need the amdtemp.ko module (32bit or 64bit).

    Steve



  • @stephenw10:

    Not an Intel Core processor hence coretemp won't work.
    Instead you need the amdtemp.ko module (32bit or 64bit).

    Steve

    Thanks! I didn't realise it was for Intel :)
    Now it works like a charm! :)


  • Netgate Administrator

    Yes, the name is a little ambiguous. Glad it worked I've not ever run it myself.  ;)

    Steve



  • Works as described, I patched manually. Thanks great work show-p1984!



  • I tried this on my system, but it did not work :(. I have a AMD Athlon™ XP 3200+ and I used the amdtemp.ko module.

    Any sugestions???


  • Netgate Administrator

    The amdtemp module supports AMD K8 microarchitecture or newer. The Athlon XP is K7.

    To get the temperature of earlier CPUs you could try using mbmon. You will have to do some coding to make it work with this widget though.

    Steve



  • Thanks! This is great and should be included in the default install IMO!!

    Again, Thanks alot!

    Cheers
    E



  • Hello,
    I tried applying the patch that is located in the main post. I downloaded the package system patch and have tried it via command line with the same result. When I test the patch I get errors saying it cant be applied cleanly. Is this due to showp1984 stuff? should it be removed? not that experienced with troubleshooting patches..

    Output of full patch apply test:

    /usr/bin/patch –directory=/ -t -p4 -i /var/patches/50be92a047646.patch --check --forward

    Hmm...  Looks like a unified diff to me...
    The text leading up to this was:

    |diff -Naur /home/showp1984/Desktop/a/usr/local/www/includes/functions.inc.php /home/showp1984/Desktop/b/usr/local/www/includes/functions.inc.php
    |--- /home/showp1984/Desktop/a/usr/local/www/includes/functions.inc.php 2012-08-07 01:01:33.902057913 +0200

    +++ /home/showp1984/Desktop/b/usr/local/www/includes/functions.inc.php 2012-08-06 06:13:12.000000000 +0200

    No file to patch.  Skipping...
    Hunk #1 ignored at 136.
    Hunk #2 ignored at 151.
    2 out of 2 hunks ignored--saving rejects to a/usr/local/www/includes/functions.inc.php.rej
    Hmm...  The next patch looks like a unified diff to me...
    The text leading up to this was:

    |diff -Naur /home/showp1984/Desktop/a/usr/local/www/widgets/widgets/system_information.widget.php /home/showp1984/Desktop/b/usr/local/www/widgets/widgets/system_information.widget.php
    |--- /home/showp1984/Desktop/a/usr/local/www/widgets/widgets/system_information.widget.php 2012-08-07 01:01:27.446742521 +0200

    +++ /home/showp1984/Desktop/b/usr/local/www/widgets/widgets/system_information.widget.php 2012-08-06 06:10:46.000000000 +0200

    No file to patch.  Skipping...
    Hunk #1 ignored at 232.
    1 out of 1 hunks ignored--saving rejects to a/usr/local/www/widgets/widgets/system_information.widget.php.rej
    done


  • Netgate Administrator

    I haven't actually tried this but looking at the patch file it's obviously been built against two files on the OPs desktop machine.

    Since this is a relatively minor change to only two files I suggest you simply edit the two files manually.

    Steve



  • Stephen,
    I was able to do the manual edits prior to posting. I was just wondering why the patch wasn't working. It would have just saved me some time editing that's all.  Thought maybe i was doing something wrong with the patch.

    btw thanks to the OP for working on this. I do appreciate it. Now if i could just get this info on LCDProc everything would be golden. :-D



  • For some reason i can't get coretemp to automatically load upon reboot. I can ssh over and run kldload coretemp and i am up and running.  I did edit the loader.conf.local and added coretemp_load="yes".  The module is in /boot/modules as well.

    Am I missing something else to enable pfsense to load this on boot if i have to reboot the firewall?


  • Netgate Administrator

    File permissions perhaps?
    Looks like you're doing everything correctly.

    Steve



  • @stephenw10:

    File permissions perhaps?
    Looks like you're doing everything correctly.

    Steve

    What would i be looking for in relation to file permission? is there a way to test (fake boot process) to see if it would give me an error and why it wouldnt load?



  • should i add the line to loader.conf? or would that be bad? Wasnt sure if the .local may not be utilized for some reason? just thinking out loud.



  • @kilthro:

    should i add the line to loader.conf? or would that be bad? Wasnt sure if the .local may not be utilized for some reason? just thinking out loud.

    /boot/loader.conf may change on reinstall; /boot/loader.conf.local is guaranteed to remain untouched by a reinstall.

    You might see an error report on coretemp during the early boot (before the kernel is started).



  • Worked just fine on my Jetway MB with pfSense 2.0.2

    coretemp.ko was already in /boot/kernel folder

    Edited all files manually



  • will this work for 2.1 to?



  • I have it working on most recent version. Just do the edits manually.



  • I have it working on my Soekris 6501-50 with atom E6xx proc. and i386 pfSense 2.1.
    It shows the temp for 2 cores (hyper threading).
    I have manually edited both .php files
    Works great.



  • cool.

    thx



  • Thanks again.

    did get som errrors at first i missed some stuff. and some rows didnt match.
    you had some line more then me. but wasnt any biggie.



  • Works perfectly in release 2.0.3, which already has included coretemp modules! Only the PHP files need to be modified.

    I suggest to pull these in for everybody, by default in the installation media.


  • Netgate Administrator

    The coretemp modules and a dashboard temperature reading is included by default in 2.1. I don't think it is graphed though.

    Steve



  • OK thanks, didn't know that, I'm just upgrading to 2.0.3…



  • If anyone wants to graph these temperatures in Munin, using the munin-node on pfSense based on this, here's a plugin for that:

    #!/bin/sh
    
    # Plugin to monitor Jetway motherboard CPU temperatures on pfSense controller runtime data. Installation:
    # - copy this plugin script to /usr/local/share/munin/plugins/, make it executable
    # - symlink it to /usr/local/etc/munin/plugins/
    # - restart munin-service
    
    case $1 in
       config)
            cat <<'EOM'
    graph_title CPU Temperatures
    graph_vlabel Degrees C
    graph_category sensors
    cpu_0.label Core 0
    cpu_1.label Core 1
    cpu_2.label Core 2
    cpu_3.label Core 3
    
    EOM
            exit 0;;
    esac
    
    temp0=$(/sbin/sysctl dev.cpu.0.temperature|awk '{print $2}')
    echo -n 'cpu_0.value '; echo $temp0|awk '{gsub(/C/,"")};{print}'
    temp1=$(/sbin/sysctl dev.cpu.1.temperature|awk '{print $2}')
    echo -n 'cpu_1.value '; echo $temp1|awk '{gsub(/C/,"")};{print}'
    temp2=$(/sbin/sysctl dev.cpu.2.temperature|awk '{print $2}')
    echo -n 'cpu_2.value '; echo $temp2|awk '{gsub(/C/,"")};{print}'
    temp3=$(/sbin/sysctl dev.cpu.3.temperature|awk '{print $2}')
    echo -n 'cpu_3.value '; echo $temp3|awk '{gsub(/C/,"")};{print}'
    
    

    Btw, munin-node works fine on 2.0.3 too.



  • Munin Plugin updated for 8-core cpu temperature (tested on Supermicro A1SRi-2758F):

    #!/bin/sh
    
    # - copy this plugin script to /usr/local/share/munin/plugins/, make it executable
    # - symlink it to /usr/local/etc/munin/plugins/
    # - restart munin-service
    
    case $1 in
    config)
    cat <<'EOM'
    
    graph_title CPU Temperatures
    graph_vlabel Degrees C
    graph_category sensors
    cpu_0.label Core 0
    cpu_1.label Core 1
    cpu_2.label Core 2
    cpu_3.label Core 3
    cpu_4.label Core 4
    cpu_5.label Core 5
    cpu_6.label Core 6
    cpu_7.label Core 7
    
    EOM
    exit 0;;
    esac
    
    temps=$(/sbin/sysctl dev.cpu | grep temperature)
    echo -n 'cpu_0.value '; printf "$temps" | awk 'NR==1{print $2; exit}' | awk '{gsub(/C/,"")};{print}'
    echo -n 'cpu_1.value '; printf "$temps" | awk 'NR==2{print $2; exit}' | awk '{gsub(/C/,"")};{print}'
    echo -n 'cpu_2.value '; printf "$temps" | awk 'NR==3{print $2; exit}' | awk '{gsub(/C/,"")};{print}'
    echo -n 'cpu_3.value '; printf "$temps" | awk 'NR==4{print $2; exit}' | awk '{gsub(/C/,"")};{print}'
    echo -n 'cpu_4.value '; printf "$temps" | awk 'NR==5{print $2; exit}' | awk '{gsub(/C/,"")};{print}'
    echo -n 'cpu_5.value '; printf "$temps" | awk 'NR==6{print $2; exit}' | awk '{gsub(/C/,"")};{print}'
    echo -n 'cpu_6.value '; printf "$temps" | awk 'NR==7{print $2; exit}' | awk '{gsub(/C/,"")};{print}'
    echo -n 'cpu_7.value '; printf "$temps" | awk 'NR==8{print $2; exit}' | awk '{gsub(/C/,"")};{print}'