Subversion Repositories vnag

Rev

Rev 53 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php /* <ViaThinkSoftSignature>
  2. bNs6RDbDtvaplN+W0zEqFdF9ouhBy80juWD/zFr+tV5QgZ2n06p5jqHQLu+4xRaW9
  3. A6jqIg4xVnvN6OKBqBvDHuvkQTCQX3SpHN7YF/aPPDvWPeEbb5HodC/aHAgkl/aBW
  4. d8WDmR9tv0PuPWPLdWMwz/X9qYFc9rtVmTgR0Z2A5KQ0U/gjigJ6YN450hlKwn7v4
  5. f/RBOqb+2AJv77/eN7QZXfJIRhPz6k/QQi3woMjNflOzcbSyaVhQkpHU1MIIUbUGC
  6. Rty5Zs214LFSrBMGwVdIzZRl/ePlf/BvcvyBzrsfXwfGJKDJw/O6ezKYb4Vtu4DcS
  7. dt5ThQ7ZZhE9uEj09lmQHSR3aZWQy4cZ/C2/CAw933MgdQ/uiEHbD4ldfSbJXM6Qq
  8. Ef0iUSvVsVimqRrQmzE1MmgYb1oCjPJ+usAXsMfbi3r3hG1f+wlwkjRX0MarQZlup
  9. VljpKJwvCXyYqm47UGGTaE/ajqNuYWxnASpmhoCO+NvbEz6DzmMTDlvsJF9oJoyrU
  10. wyCzAij9adiK0FVnmAfB6jpyQskzP4RQkXKyIgKtzOPgGL3cSLAsBi9S2TPLr5CRX
  11. EJChPEWFFKlDvGHjg7XoerclvHYyuNc2v2Xby4El3CusDH2oCHME/wBUFjFYn7OoU
  12. 2vhbsi1fx6lX5t3eNmY9J9N4sUms63xOTr/UoeYOrPA/jzl5675V2qOx7cGZjnyYp
  13. YUipAnTepOt5FvnlW1i9TXMgRjT1Z62UJDJ5S7MLuntiwSxzyWSYE3NQ0CvwhlKQ2
  14. HZ8+j4r5qMxLevxbOZQAPDY65BNPpWVeyM/gPE+uTuM/XGJCEYazJJQ/h/DbdxH7e
  15. 4eBYQfvKTajwICmr5Jvg0tDrAsSRBBuGpwQxCPzS+f17yJT51IoWA4r+TilQFn0tM
  16. vk5CAgjjQdPMMk2/fyfE8ZV4izYIHPcMrNBYlo+RGuTQd6QkoTnb8sC2kc4oXoub9
  17. tFevAhA/Rm3oVGWIwCSH3rVfeyK/4cgSeyKHsd8xI8K7+XRvVrCE4P49yzBj/xd0P
  18. 0YQaJqONQNSUsFpEBn3ce6OR1Hg3v5ueZm+ZUS6rYWjJXO8h+KYV3olyDpa9Y4Ub2
  19. LfVQWMhNg+Z37Y087vB2AYsephcBamqbqL766cs/+Dw8vdIkVTnB4sSqOpbWtaiAl
  20. eIoyqDQdg7A9LBZz+7yh3NIIrgcoJ7NbliPfMAzscd+rTj6iRzt/+iJfaSiDEymOJ
  21. 5NKibqHD9a0t+gw3h6ZZ5fx8Bz8520xrpH2dLDV8llXLce/j27B0/VqKF07QIpn6w
  22. +Z5NHZZeVUredJlJhMOEyN1v5AUenSbT0si+u30f3m8UnALLbs3QfbZ2yniuRtITV
  23. w==
  24. </ViaThinkSoftSignature> */ ?>
  25. <?php
  26.  
  27. /*
  28.  * VNag - Nagios Framework for PHP
  29.  * Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
  30.  * Licensed under the terms of the Apache 2.0 license
  31.  *
  32.  * Revision 2023-10-13
  33.  */
  34.  
  35. declare(ticks=1);
  36.  
  37. class SmartCheck extends VNag {
  38.         protected $argType = null;
  39.  
  40.         public function __construct() {
  41.                 parent::__construct();
  42.  
  43.                 if ($this->is_http_mode()) {
  44.                         // Don't allow the standard arguments via $_REQUEST
  45.                         $this->registerExpectedStandardArguments('');
  46.                 } else {
  47.                         $this->registerExpectedStandardArguments('Vhtv');
  48.                 }
  49.  
  50.                 $this->getHelpManager()->setPluginName('vnag_smart');
  51.                 $this->getHelpManager()->setVersion('1.0');
  52.                 $this->getHelpManager()->setShortDescription('This plugin checks the contents of the SMART data and warns when a harddisk has failed.');
  53.                 $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
  54.                 $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-T <type>]');
  55.                 $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
  56.  
  57.                 // TODO: also add a command to check a single drive   [ -d /dev/sda,/dev/sdb ]
  58.                 // Individual (non-standard) arguments:
  59.                 $this->addExpectedArgument($this->argType = new VNagArgument('T', 'type', VNagArgument::VALUE_REQUIRED, 'type', 'Explicit drive type e.g. for RAID devices "sat+cciss,0" for drive 0.'));
  60.         }
  61.  
  62.         private function check_smart($dev) {
  63.                 if (!`which which`) {
  64.                         throw new VNagException("Program 'which' is not installed on your system");
  65.                 }
  66.  
  67.                 if (!`which smartctl`) {
  68.                         throw new VNagException("Program 'smartctl' (usually included in package smartmontools) is not installed on your system");
  69.                 }
  70.  
  71.                 $code = 0;
  72.                 $out = array();
  73.  
  74.                 if ($this->argType->getValue() != '') {
  75.                         // Note: Requires root
  76.                         exec('smartctl --all '.escapeshellarg($dev).' -d '.escapeshellarg($this->argType->getValue()), $out, $code);
  77.                 } else {
  78.                         // Note: Requires root
  79.                         exec('smartctl --all '.escapeshellarg($dev), $out, $code);
  80.                 }
  81.                 $cont = implode("\n", $out);
  82.  
  83.                 $msg = array();
  84.                 $status = -1;
  85.  
  86.                 if (stripos($cont, 'device lacks SMART capability') !== false)  {
  87.                         // At my system (Debian 9), I get exit code 4 (which is not fully accurate)
  88.                         $msg[] = 'Device lacks SMART capability';
  89.                         #$status = VNag::STATUS_UNKNOWN;
  90.                 } else if ($code == 0) {
  91.                         $status = VNag::STATUS_OK;
  92.                 } else {
  93.                         if ($code & 1) {
  94.                                 throw new VNagException("smartctl reports 'command line did not parse' (code $code).");
  95.                         }
  96.                         if ($code & 2) {
  97.                                 $msg[] = "Device open failed. It is either completely defective, or in low-power mode.";
  98.                                 $status = max($status, VNag::STATUS_CRITICAL);
  99.                         }
  100.                         if ($code & 4) {
  101.                                 $msg[] = "SMART command failed or checksum is wrong.";
  102.                                 $status = max($status, VNag::STATUS_WARNING);
  103.                         }
  104.                         if ($code & 8) {
  105.                                 $msg[] = "SMART status returns 'DISK FAILING'";
  106.                                 $status = max($status, VNag::STATUS_CRITICAL);
  107.                         }
  108.                         if ($code & 16) {
  109.                                 $msg[] = "SMART found prefail attributes below threshold";
  110.                                 $status = max($status, VNag::STATUS_WARNING);
  111.                         }
  112.                         if ($code & 32) {
  113.                                 $msg[] = "SMART status is 'OK' but usage/prefail attributes have been below threshold in the past.";
  114.                                 $status = max($status, VNag::STATUS_WARNING);
  115.                         }
  116.                         if ($code & 64) {
  117.                                 $msg[] = "The device error log contains records of errors.";
  118.                                 $status = max($status, VNag::STATUS_WARNING);
  119.                         }
  120.                         if ($code & 128) {
  121.                                 $msg[] = "The self-test logs contains records of errors.";
  122.                                 $status = max($status, VNag::STATUS_WARNING);
  123.                         }
  124.                 }
  125.  
  126.                 $messages = implode(", ", $msg);
  127.                 if ($messages != '') $messages = ": $messages";
  128.  
  129.                 if ($status == VNag::STATUS_CRITICAL) {
  130.                         $this->addVerboseMessage("$dev (Critical)$messages", VNag::VERBOSITY_SUMMARY);
  131.                 } else if ($status == VNag::STATUS_WARNING) {
  132.                         $this->addVerboseMessage("$dev (Warning)$messages", VNag::VERBOSITY_SUMMARY);
  133.                 } else if ($status == VNag::STATUS_OK) {
  134.                         $this->addVerboseMessage("$dev (OK)$messages", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
  135.                 } else {
  136.                         $status = VNag::STATUS_UNKNOWN;
  137.                         $this->addVerboseMessage("$dev (Unknown)$messages", VNag::VERBOSITY_SUMMARY);
  138.                 }
  139.                 $this->setStatus($status);
  140.                 return $status;
  141.         }
  142.  
  143.         protected function cbRun() {
  144.                 $devices = array();
  145.                 $devices = array_merge($devices, glob('/dev/sd?'));
  146.                 $devices = array_merge($devices, glob('/dev/hd?'));
  147.  
  148.                 if (count($devices) == 0) {
  149.                         throw new VNagException("No SDx or HDx drives found");
  150.                 }
  151.  
  152.                 if (strpos($this->argType->getValue(),'cciss') !== false) {
  153.                         $devices = array($devices[0]); // we just need a "fake" drive; the drive number is given as parameter to cciss
  154.                 }
  155.  
  156.                 $count_total = 0;
  157.                 $count_ok = 0;
  158.                 $count_warning = 0;
  159.                 $count_critical = 0;
  160.                 $count_unknown = 0;
  161.                 foreach ($devices as $dev) {
  162.                         $count_total++;
  163.                         switch ($this->check_smart($dev)) {
  164.                                 case VNag::STATUS_OK:
  165.                                         $count_ok++;
  166.                                         break;
  167.                                 case VNag::STATUS_WARNING:
  168.                                         $count_warning++;
  169.                                         break;
  170.                                 case VNag::STATUS_CRITICAL:
  171.                                         $count_critical++;
  172.                                         break;
  173.                                 case VNag::STATUS_UNKNOWN:
  174.                                         $count_unknown++;
  175.                                         break;
  176.                         }
  177.                 }
  178.  
  179.                 $this->setHeadline(sprintf('Checked %d drives (%d OK, %d warning, %d critical, %d unknown)', $count_total, $count_ok, $count_warning, $count_critical, $count_unknown));
  180.         }
  181. }
  182.