I like to monitor every aspect of my linux servers and one of these aspects is the CPU temperature. Most common way to find out your CPU temperature for pretty much any linux distribution is to use the package lm_sensors. It’s quite easy to install it:

– if your linux distro is Debian based (like Debian, Ubuntu etc) you’ll issue this command: apt-get install lm-sensors
– if your linux distro is RedHat based (RedHat, CentOS, Fedora etc) you’ll issue this command: yum install lm_sensors
– if your linux distro is SuSe based (Suse, OpenSuse etc) you’ll issue this command: zypper install lm_sensors

For other distros you can check their official documentation, see what’s the default tool used to install packages.

I was able to find a few bash scripts based on lm_sensors but most of them where only taking into consideration the temperature from a single core of the CPU. My approach is a little different. Most of my servers have 2 CPUs so I would like to know the temperature on any of them. Also, if let’s say the server has 4 CPUs, then I want to be able to run the exact same script and get the temperature for each of the 4 CPUs.

Because I was not able to find anything to suit my needs, I’ve decided to write my own nagios plugin, in Python, based on lm_sensors package and the python pysensors. You cand find the plugin here: check_cpu_temp.

As I said, you’ll need to install lm_sensors package and the python module pysensors ( pip install pysensors).

As for most programs on linux, running the plugin with -h (help) will display some basic usage instructions:

Then simply run the plugin:
./check_cpu_temp

The output should be something like this:
OK - CPU(s) temperature(s): 29°C 33°C; high=80.0; crit=96.0

By default, if you run the pugin with no arguments, HIGH and CRIT temperatures are the default ones for your CPU(s). You can also use your own CUSTOM values if you like using the -w (warning) and -c (critical) arguments. Assuming you would want 50 degrees Celsius for HIGH and 60 degrees Celsius for CRIT, the syntax becomes this:
./check_cpu_temp -w 50 -c 60

I mostly use CentOS 7.x for my servers, so the example bellow is related to CentOS:

NRPE (nrpe.cfg):

Nagios command definition (commands.cfg):

Host service definition:

Enjoy!





Share.

About Author

I am a linux passionate and currently working as a Linux Senior System Administrator. I also am a freelancer and help people to complete different jobs. You can hire me on Freelancer.com