Code / PHP Snippets / 'Sensors' Report Tool

  1. #!/usr/local/bin/php
  2. <?php
  3. // Sensors v0.1a (C) Scott Wilcox 2008
  4. // This script will read the output from the linux programs 'sensors' and then
  5. // optionally email or post a growl notification. If you want to use growl,
  6. // then you'll need to download and use the growl class, which can be downloaded
  7. // from http://code.google.com/p/php-growl/ then you'll need to change the require
  8. // function below too to match the path too.
  9.  
  10. // Path to 'sensors'
  11. $path_sensors = "/usr/bin/sensors";
  12.  
  13. // Use growl?
  14. $growl_use = 1;
  15. if ($growl_use) {
  16.         require "path/to/growl/class.php";
  17. }
  18. $growl_address = "192.168.10.14";
  19. $growl_password = "password";
  20.  
  21. // Email a report if unsafe?
  22. $mail_use = 1;
  23. $mail_to = "My Name <user@domain.tld>";
  24. $mail_from = "From: Server <server@host.domain.tld>";
  25.  
  26. //  Echo out message
  27. $echo_use = 1;
  28.  
  29. // Safe Temperature
  30. $limit_low = -32;
  31. $limit_high = 50;
  32.  
  33. // Grab the sensors output
  34. $output = trim(shell_exec($path_sensors));
  35. $data = explode("\n",$output);
  36.  
  37. // Grab invdividual items
  38. $fan_case = $data[2]; $fan_cpu = $data[3]; $temp_sys = $data[6]; $temp_cpu = $data[7]; $temp_case = $data[8];
  39.  
  40. // Format the numbers so we can use them
  41. $fan_case =  trim(substr($fan_case,strpos($fan_case,": ")+1,strpos($fan_case,"RPM")-(strpos($fan_case,": ")+1)));
  42. $fan_cpu =  trim(substr($fan_cpu,strpos($fan_cpu,": ")+1,strpos($fan_cpu,"RPM")-(strpos($fan_cpu,": ")+1)));
  43. $temp_sys =  trim(substr($temp_sys,strpos($temp_sys,": ")+1,strpos($temp_sys,"C  (")-(strpos($temp_sys,": ")+1)));
  44. $temp_cpu =  trim(substr($temp_cpu,strpos($temp_cpu,": ")+1,strpos($temp_cpu,"C  (")-(strpos($temp_cpu,": ")+1)));
  45. $temp_case =  trim(substr($temp_case,strpos($temp_case,": ")+1,strpos($temp_case,"C  (")-(strpos($temp_case,": ")+1)));
  46.  
  47. // whether to email this time
  48. $unsafe = 0;
  49.  
  50. // Motherboard Temp
  51. if (($temp_sys > $limit_low) && ($temp_sys < $limit_high)) {
  52.         $temp_sys = $temp_sys."C [Safe]";
  53. } else {
  54.         $unsafe = 1;
  55.         $temp_sys = $temp_sys."C [UNSAFE]";
  56. }
  57.  
  58. // CPU Temp
  59. if (($temp_cpu > $limit_low) && ($temp_cpu < $limit_high)) {
  60.         $temp_cpu = $temp_cpu."C [Safe]";
  61. } else {
  62.         $unsafe = 1;
  63.         $temp_cpu = $temp_cpu."C [UNSAFE]";
  64. }
  65.  
  66. // Case Temp
  67. if (($temp_case > $limit_low) && ($temp_case < $limit_high)) {
  68.         $temp_case = $temp_case."C [Safe]";
  69. } else {
  70.         $unsafe = 1;
  71.         $temp_case = $temp_case."C [UNSAFE]";
  72. }
  73.  
  74. // Generate the update message, its used for both mail and growl
  75. // notifications
  76. $update = "
  77. Case Fan: ".$fan_case."rpm
  78. CPU Fan: ".$fan_cpu."rpm
  79. System Temp: $temp_sys
  80. CPU Temp: $temp_cpu
  81. Case Temp: $temp_case";
  82.  
  83. // If they want to use growl, send a notification message
  84. if ($growl_use) {
  85.         // We'll also post a growl notification every time we're run too if
  86.         // the user wants to.
  87.         $g = new Growl("Server Notify");
  88.         $g->setAddress($growl_address,$growl_password);
  89.         $g->addNotification("Temperature Update");
  90.         $g->register();
  91.         $g->notify("Temperature Update", "Temperature Update", $update);
  92. }
  93.  
  94. // If an unsafe flag was set, we'll email a report over.
  95. if ($mail_use && $unsafe) {
  96.         mail($mail_to, "Server Temperature Alert", $update, $mail_from);
  97. }
  98.  
  99. // Echo out if requested too
  100. if ($echo_use) {
  101.         echo "Temperature Update: \n";
  102.         echo $update."\n";
  103. }
  104. ?>
  105.  

Download as Text : January 6th, 2009