- #!/usr/local/bin/php
- <?php
- // Sensors v0.1a (C) Scott Wilcox 2008
- // This script will read the output from the linux programs 'sensors' and then
- // optionally email or post a growl notification. If you want to use growl,
- // then you'll need to download and use the growl class, which can be downloaded
- // from http://code.google.com/p/php-growl/ then you'll need to change the require
- // function below too to match the path too.
- // Path to 'sensors'
- $path_sensors = "/usr/bin/sensors";
- // Use growl?
- $growl_use = 1;
- if ($growl_use) {
- require "path/to/growl/class.php";
- }
- $growl_address = "192.168.10.14";
- $growl_password = "password";
- // Email a report if unsafe?
- $mail_use = 1;
- $mail_to = "My Name <user@domain.tld>";
- $mail_from = "From: Server <server@host.domain.tld>";
- // Echo out message
- $echo_use = 1;
- // Safe Temperature
- $limit_low = -32;
- $limit_high = 50;
- // Grab the sensors output
- $output = trim(shell_exec($path_sensors));
- $data = explode("\n",$output);
- // Grab invdividual items
- $fan_case = $data[2]; $fan_cpu = $data[3]; $temp_sys = $data[6]; $temp_cpu = $data[7]; $temp_case = $data[8];
- // Format the numbers so we can use them
- $fan_case = trim(substr($fan_case,strpos($fan_case,": ")+1,strpos($fan_case,"RPM")-(strpos($fan_case,": ")+1)));
- $fan_cpu = trim(substr($fan_cpu,strpos($fan_cpu,": ")+1,strpos($fan_cpu,"RPM")-(strpos($fan_cpu,": ")+1)));
- $temp_sys = trim(substr($temp_sys,strpos($temp_sys,": ")+1,strpos($temp_sys,"C (")-(strpos($temp_sys,": ")+1)));
- $temp_cpu = trim(substr($temp_cpu,strpos($temp_cpu,": ")+1,strpos($temp_cpu,"C (")-(strpos($temp_cpu,": ")+1)));
- $temp_case = trim(substr($temp_case,strpos($temp_case,": ")+1,strpos($temp_case,"C (")-(strpos($temp_case,": ")+1)));
- // whether to email this time
- $unsafe = 0;
- // Motherboard Temp
- if (($temp_sys > $limit_low) && ($temp_sys < $limit_high)) {
- $temp_sys = $temp_sys."C [Safe]";
- } else {
- $unsafe = 1;
- $temp_sys = $temp_sys."C [UNSAFE]";
- }
- // CPU Temp
- if (($temp_cpu > $limit_low) && ($temp_cpu < $limit_high)) {
- $temp_cpu = $temp_cpu."C [Safe]";
- } else {
- $unsafe = 1;
- $temp_cpu = $temp_cpu."C [UNSAFE]";
- }
- // Case Temp
- if (($temp_case > $limit_low) && ($temp_case < $limit_high)) {
- $temp_case = $temp_case."C [Safe]";
- } else {
- $unsafe = 1;
- $temp_case = $temp_case."C [UNSAFE]";
- }
- // Generate the update message, its used for both mail and growl
- // notifications
- $update = "
- Case Fan: ".$fan_case."rpm
- CPU Fan: ".$fan_cpu."rpm
- System Temp: $temp_sys
- CPU Temp: $temp_cpu
- Case Temp: $temp_case";
- // If they want to use growl, send a notification message
- if ($growl_use) {
- // We'll also post a growl notification every time we're run too if
- // the user wants to.
- $g = new Growl("Server Notify");
- $g->setAddress($growl_address,$growl_password);
- $g->addNotification("Temperature Update");
- $g->register();
- $g->notify("Temperature Update", "Temperature Update", $update);
- }
- // If an unsafe flag was set, we'll email a report over.
- if ($mail_use && $unsafe) {
- mail($mail_to, "Server Temperature Alert", $update, $mail_from);
- }
- // Echo out if requested too
- if ($echo_use) {
- echo "Temperature Update: \n";
- echo $update."\n";
- }
- ?>
Download as Text : January 6th, 2009
This work is licensed under a