Subversion Repositories vnag

Rev

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

  1. <?php /* <ViaThinkSoftSignature>
  2. mKTo94dd1u+i8HKvlQf3rPtiaEvY3sU5RuGBEWnFGFbKo1WomvGZiQ58cl1KqKOu6
  3. jf/DTuB6RmPuUku+7Y2oVS5IebUkUkt+TaXFBl7cUrOp9kkr8BjxTKDBkBvYTM6cG
  4. VUg9ceVBpDWkvmSvvkLCFabkOh8fmJsEifjyGrgIRjGCIhuY8DhwAjWz78+XosG/H
  5. nfYcf5Tpr6e/E7axGpEaVbBnb9otEG1CslG81GGDdlQp2/s35CqSMn3MBfL0qQxRw
  6. kvLHv5u345qDpgPmGL4mcK1bKQ7B4nNHFEn4qQQeuRPmZ1fLGrUHcmJGOH6Jy4hUM
  7. rnMe4yb9rpxxUPr6CHt0zI4QiW05pBTLwtQbmgRHll82LirDuSA27H4KtipGh+TuF
  8. MlZd1ixv7KcenO0YrVPU4SXV3XweaQT/luqLSVnajHVsXOIu4+FCCpV4AY07ppYtj
  9. bt9qbP6DcmbktbjiOFuHedAVC0L9448svSWHMDcOWdbAtpVhgajO9yTzMJblNvg55
  10. 8GrCR3YT+ol8SbU5h+iQCdPrtwETamtDGUw3p9kZIegGwbc6U+OjIbVnqKIPiNt13
  11. /4+uHcY8BZ+1BpGtPPfM1kR9c7yWkTJqkMEFFSp7ulsPfYN+pM66HFxMON8xQOOUv
  12. 0tVST1rgey7KtE8/xzwLLjTIW3nZYoS9MPkTSqzijC8KYszhR0OohEYyQYle3+L2Q
  13. TVd9ANakTCmKGhJ6t+dJeHzWXY/FFFeT019vVKTT03v51/olpUWV+/xs2joK9Dcb+
  14. ovTFvDQIMf3FTqlCCU1HOmj3F5wlvNJTZituEgo/9POFBmYdFSJnrX6JkPglDeJI7
  15. CmmPzxj+ZZxzaZjAOjAU4izd40N+xQx4RrI87++n44O7yBCv59UTrgIDEnhgKscQR
  16. 9yMoMK5sSqKfbEaXk7L0un5evTgKeP3ZRvS2zftKWsbsEgd2zl0EjCZz599at2N4W
  17. awoWndSN0H1RyPLYncSJG6wS5QB9MgWt8SuUJMZfFJy4lETtstjdP5XQvz9VBbNAr
  18. a3a+xRGzY8e4Z0JKzsV1jy2SDJ7cIQMW7Ci/NS2kghaKZl9T7CL+MITnbNeM2iPrQ
  19. YYKSmnHxZi5keTqZ72Ahr6rB8Zck676pUX9AucqxT5+7Ox2QrP+IfuBnTDCCPavJ2
  20. MFAlCHp9bGdrjT6lcC1sfYdb1WlhYjqwbwXjMRsbg7NMhiaxSQ+Ozz/eJM83wgmo6
  21. Ql/4cFyhwqmKUj7d/d4q/q0TFh2Rh4uuYqpQeAZkAJHgXlJt8mZigOU9ieA0m6Eha
  22. Emr22N2Ac0Fgjr4qIbNTabu4Cd6Or4h8mZcwGBcE0/2zZZmQWNfn6hxpZ6/usd0fc
  23. Q==
  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 2018-11-04
  33.  */
  34.  
  35. declare(ticks=1);
  36.  
  37. class MdStatCheck extends VNag {
  38.         public function __construct() {
  39.                 parent::__construct();
  40.  
  41.                 if ($this->is_http_mode()) {
  42.                         // Don't allow the standard arguments via $_REQUEST
  43.                         $this->registerExpectedStandardArguments('');
  44.                 } else {
  45.                         $this->registerExpectedStandardArguments('Vhtv');
  46.                 }
  47.  
  48.                 $this->getHelpManager()->setPluginName('vnag_mdstat');
  49.                 $this->getHelpManager()->setVersion('2.0');
  50.                 $this->getHelpManager()->setShortDescription('This plugin checks the contents of /proc/mdstat and warns when a harddisk has failed.');
  51.                 $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
  52.                 $this->getHelpManager()->setSyntax('$SCRIPTNAME$ (no additional arguments expected)');
  53.                 $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
  54.         }
  55.  
  56.         private function getDisks($device) {
  57.                 $disks = glob("/sys/block/$device/md/dev-*");
  58.                 foreach ($disks as &$disk) {
  59.                         $ary = explode('/', $disk);
  60.                         $disk = substr(array_pop($ary), 4);
  61.                 }
  62.                 return $disks;
  63.         }
  64.  
  65.         private function raidLevel($device) {
  66.                 $level_file = "/sys/block/$device/md/level";
  67.                 if (!file_exists($level_file)) {
  68.                         throw new VNagException("Kernel too old to fetch RAID level of array $device");
  69.                 }
  70.                 $level = file_exists($level_file) ? trim(file_get_contents($level_file)) : 'RAID?';
  71.                 return $level;
  72.         }
  73.  
  74.         private function raidState($device) {
  75.                 $state_file = "/sys/block/$device/md/array_state";
  76.                 if (!file_exists($state_file)) {
  77.                         throw new VNagException("Kernel too old to fetch state of array $device");
  78.                 }
  79.                 $state = trim(file_get_contents($state_file));
  80.                 return $state;
  81.         }
  82.  
  83.         private function check_disk_state($array, $disk) {
  84.                 $disk_state_file = "/sys/block/$array/md/dev-$disk/state";
  85.                 if (!file_exists($disk_state_file)) {
  86.                         throw new VNagException("Kernel too old to fetch state of disk $array:$disk");
  87.                 }
  88.                 $disk_states = trim(file_get_contents($disk_state_file));
  89.                 $disk_state_ary = explode(',', $disk_states);
  90.                 $disk_state_ary = array_map('trim', $disk_state_ary);
  91.  
  92.                 $status = VNag::STATUS_OK;
  93.                 $verbosity = VNag::VERBOSITY_ADDITIONAL_INFORMATION;
  94.  
  95.                 foreach ($disk_state_ary as $disk_state) {
  96.                         // https://www.kernel.org/doc/html/v4.15/admin-guide/md.html
  97.                         // CRIT faulty: device has been kicked from active use due to a detected fault, or it has unacknowledged bad blocks
  98.                         // OK   in_sync: device is a fully in-sync member of the array
  99.                         // OK   writemostly: device will only be subject to read requests if there are no other options. This applies only to raid1 arrays.
  100.                         // CRIT blocked: device has failed, and the failure hasn.t been acknowledged yet by the metadata handler. Writes that would write to this device if it were not faulty are blocked.
  101.                         // WARN spare: device is working, but not a full member. This includes spares that are in the process of being recovered to
  102.                         // WARN write_error: device has ever seen a write error.
  103.                         // WARN want_replacement: device is (mostly) working but probably should be replaced, either due to errors or due to user request.
  104.                         // OK   replacement: device is a replacement for another active device with same raid_disk.
  105.  
  106.                         if (($disk_state == 'faulty') || ($disk_state == 'blocked')) {
  107.                                 $status = max($status, VNag::STATUS_CRITICAL);
  108.                                 $verbosity = min($verbosity, VNag::VERBOSITY_SUMMARY);
  109.                         }
  110.                         if (($disk_state == 'spare') || ($disk_state == 'write_error') || ($disk_state == 'want_replacement')) {
  111.                                 $status = max($status, VNag::STATUS_WARNING);
  112.                                 $verbosity = min($verbosity, VNag::VERBOSITY_SUMMARY);
  113.                         }
  114.                 }
  115.  
  116.                 return array($status, $verbosity, $disk_states);
  117.         }
  118.  
  119.         private function get_raid_arrays() {
  120.                 $arrays = array();
  121.                 $devices = glob('/dev/md/*');
  122.                 foreach ($devices as $device) {
  123.                         $ary = explode('/', $device);
  124.                         $arrays[] = 'md'.array_pop($ary);
  125.                 }
  126.                 return $arrays;
  127.         }
  128.  
  129.         protected function cbRun() {
  130.                 $disks_total = 0;
  131.                 $disks_critical = 0;
  132.                 $disks_warning = 0;
  133.  
  134.                 $arrays = $this->get_raid_arrays();
  135.                 foreach ($arrays as $array) {
  136.                         $level = $this->raidLevel($array);
  137.                         $state = $this->raidState($array);
  138.  
  139.                         $disk_texts = array();
  140.                         $verbosity = VNag::VERBOSITY_ADDITIONAL_INFORMATION;
  141.                         $disks = $this->getDisks($array);
  142.                         foreach ($disks as $disk) {
  143.                                 $disks_total++;
  144.                                 list($status, $verbosity_, $disk_states) = $this->check_disk_state($array, $disk);
  145.                                 $verbosity = min($verbosity, $verbosity_);
  146.                                 $this->setStatus($status);
  147.                                 if ($status == VNag::STATUS_WARNING) $disks_warning++;
  148.                                 if ($status == VNag::STATUS_CRITICAL) $disks_critical++;
  149.                                 $status_text = VNagLang::status($status, VNag::STATUSMODEL_SERVICE);
  150.                                 $disk_texts[] = "$disk ($status_text: $disk_states)";
  151.                         }
  152.  
  153.                         # Example output:
  154.                         # Array md0 (raid1, degraded): sda1 (Warning: faulty, blocked), sdb1 (OK: in_sync)
  155.                         $this->addVerboseMessage("Array $array ($level, $state): ".implode(', ', $disk_texts), $verbosity);
  156.                 }
  157.  
  158.                 $this->setHeadline(sprintf('%s disks in %s arrays (%s warnings, %s critical)', $disks_total, count($arrays), $disks_warning, $disks_critical));
  159.         }
  160. }
  161.