Using hardware sensors
path › xlifecolumns20020807

Using hardware sensors

Xlife column for August 7, 2002:
Summary:
Learn how to make use of the hardware (temperature, voltage, fan speed) sensors on your motherboard in order to monitor the health of your system.
  1. Introduction
  2. Get the required packages
  3. Enabling I2C
  4. Installing lm_sensors
  5. Configuring lm_sensors
  6. Using the sensors
  7. A tip about keeping the CPU cooler

Introduction

Hardware sensor chips are a common occurance nowadays on most motherboards. They follow minimal standard specifications, which means you can at least get the following information: CPU temperature, CPU cooler fan speed, several voltage values for CPU and motherboard. Some mobo's provide more, such as their temperature, inside the case temperature, open-case warning, the speeds of other fans beside the CPU cooler, interaction with other hardware monitoring chips, such as the ones placed on the newer hard-disks.

In order to benefit from this readouts you have to walk through the following:

  • Install the generic interface (I2C) to the hardware sensors on your system, either by installing a separate package or by activating it in your kernel (2.4.13+).
  • Install the sensor devices and the software that does the reading.
  • (Optional) Use an application that displays the readings in a nicer format (X apps).

More info is provided by the excellent and extensive LM78 FAQ. If you need to know what chips are supported check this out.

And before we start, let me thank Dave Serls from the BlackBox mailing list. The question he posted there a while back made me organize what info I had about the issue, which eventually turned into this column.

Get the required packages

In order to make use of the sensors readout software you have to get the lm_sensors. They rely on the I2C standard which is already included in 2.4.13+ kernels. If you have an older kernel you need the I2C package.

Note: it's not impossible to encounter problems even if you're using the I2C that came with the kernel. In such cases you'll need to download and install the separate I2C package anyway.

Enabling I2C

If you have a kernel version of 2.4.13 or later you'll probably want to use the embedded I2C support. You'll have to reconfigure your kernel and make sure you include the following in the "Character devices" section:

  • I2C
  • I2C bit-banging (algo)
  • I2C devices
  • /proc I2C support

Then you'll have to recompile and reinstall your kernel.

Note: I like setting the above features as loadable modules instead of built-in. This way you can load or unload them as needed and you are not forced to have them activated all the time. It is probably just good policy, but then again it's a good habit to play nice with your *n*x system.

If you don't have a proper kernel or don't want to use it for this, use the I2C package. Installing it is a simple matter of make; make install; depmod -a. This will compile the kernel modules, copy them to your /lib/modules/<kernel version>/ directory and update the dependency map.

Installing lm_sensors

Get the lm_sensors package and unpack it. Again, you can simply do a make; make install; depmod -a since the installation procedure is very similar to I2C. Again, this will compile the kernel modules, copy them to your /lib/modules/<kernel version>/ directory and update the dependency map.

The package doesn't come with a configure script, as you may have noticed, so if you want to tweak the installation directories you will have to modify the Makefile.

You can change variables like PREFIX (which affects the whole installation), MODDIR (where the kernel modules go -- default is /lib/modules/<kernel version>/), ETCDIR (the config file location), BINDIR (for the executable utilities).

For example, I chose to modifiy all of the above and thus got the complete install done to /opt/lm_sensors, so I could make a backup tarball for it. I still had to copy the config file to /etc, the modules to /lib/modules, and add /opt/lm_sensors/bin to the PATH, /opt/lm_sensors/man to MANPATH and /opt/lm_sensors/lib to /etc/ld.so.conf + run ldconfig. I run an /opt based LFS system, so this suited me fine, but you may just want to use the straightforward install which will place everything in the default locations.

Configuring lm_sensors

First you need to create the special devices needed to interface the sensors. The lm_sensors comes with a script called mkdev.sh which will do this for you. It would be a good idea to make a copy of the script, so you don't lose it after you get rid of the source package.

The configuration is as easy as running sensors-detect. This nice utility will probe the sensors, ask you a couple of questions to see how you'd rather have things set up and finally provide you with two things: a set of config lines that you must place in /etc/modules.conf (or /etc/conf.modules on older distributions); and the commands that you must run in order to load the sensors kernel modules.

This is needed because in order to do a readout of the sensors values, the utilities need the modules ("drivers") to be loaded. We already created the interface to the sensors in /dev earlier. But simply loading them (the second stuff provided by sensors-detect) is not enough, they need certain parameters which you provide through /etc/modules.conf.

So just follow the instructions the detection script offers, it's a simple copy and paste job. As for the modprobe commands you should place them in a script called something like "loadsensors" so you don't have to look them up time after time. I prefer to place them in /etc/rc.d/rc.local at the end so they are run at system startup and be done with it.

Important: after the modprobe commands you also need to run sensors -s to initialize the sensor readouts. Add this to the same place ("loadsensors" or rc.local).

Using the sensors

Just run the command "sensors". If everything went OK this should output a nice list with all the readouts available on your system: temperature, voltage, fan speeds.

You may want some nicer way to keep an eye out on your CPU heat, for one. There are many applications out there that can use the sensors interface once it is installed. What exactly you can use depends on your window manager (assuming you want this for X Windows).

A nice application that runs in many window managers is GKrellM, a compact panel full of various system readouts. GKrellM comes with a sensors plugin built-in, although you may not notice it as it only becomes available if you have the lm_sensors installed. There's also a third party plugin for GKrellM called gklmsensors which displays the readouts in a slightly different manner. I chose the plugin instead of the built-in version, for example, it just looked better.

Such applications are the equivalent of "sensors", just displaying the readouts in another form. This means they don't rely on the "sensors" script to get the values, they read them directly from the devices, which gives better performance.

A tip about keeping the CPU cooler

If you're gonna recompile the kernel, try including the "Make CPU idle calls when idle" option in the "General setup" section. Upon using the new kernel you will notice a process called "kapm-idled" which appears to hog all the available CPU. No, it doesn't actually use it, but instead forces it to "do nothing" when there's nothing to do.

In some cases, this helps cool the CPU a few degrees. In some cases, it doesn't have any effect. In some cases it won't work or break your system (see the help associated with this option in the kernel configuration utility for details).

Finally, one thing that it does for sure is mess up your CPU usage readout, since next to 0% usage will now become 100% thanks to kapm-idled, and so on. So unless you use a monitoring application that can be told to subtract kapm-idled from the CPU usage readout, you are gonna have to say goodbye to meaningful CPU usage statistics, or do the math yourself.

< Back to the top | Read other columns | Choose what column I should write next! >