lm_sensors is a set of tools used on a Linux operating system to monitor the health of your hardware. I recently found it quite handy to monitor the core temperature of my file server at home. Due to the fact that I recently got married and had to start the process of domestication, I had to move the server into a less obtrusive location like the closet it now occupies, I noticed that the machine runs quite hot.
Here is the PHP script that will help sending an email when the temperature reaches a certain threshold. It requires only lm_sensors and grep to be installed. You can call it from crontab.
<?php
/*
This script will send warnings when the core temperature of the machine
is to high. It requires lm_sensors
Written by Wayne Swart
*/
// Some configuration
define('lm_sensors','/usr/bin/sensors'); // the path to the sensors binary
define('grep','/bin/grep'); // the path to the grep binary
define('threshold','30'); // triger a warning if the temperature is more than this in Degrees Celcius
define('admin','you@yourdomain.org'); // the email address that we will send the alerts to
define('from','you@yourdomain.org'); // default from address
define('hostname', 'my.server.org'); // The server hostname
// There shouldn't be any reason to change anything after this
$temp = exec(lm_sensors . ' |grep \'°\'');
$tempr = explode('+', $temp);
$tempval = preg_replace("/[^0-9,.]/", "", $tempr[1]);
if($tempval > threshold){
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: <'.admin.'>' . "\r\n";
$headers .= 'From: <'.from.'>' . "\r\n";
$message = 'The temperature for <strong>' . hostname . '</strong> is currently at <font color="red"><strong>' . $tempval . ' °C</strong></font> and over the threshold of ' . threshold . ' °C';
// Mail it
mail($admin, 'Temperature alert for ' . hostname, $message, $headers);
}
?>