Subversion Repositories vnag

Rev

Rev 80 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * VNag - Nagios Framework for PHP
  5.  * Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
  6.  * Licensed under the terms of the Apache 2.0 license
  7.  *
  8.  * Revision 2023-10-13
  9.  */
  10.  
  11. // TODO: Instead of "-d diskname[,diskname[,...]]", it would be nicer without comma, because then you could also use shell extensions /dev/sd*
  12.  
  13. declare(ticks=1);
  14.  
  15. class DiskRunningCheck extends VNag {
  16.         // Currently, we do not accept wildcards ('sd*' and 'hd*') because this plugin should monitor if disks become offline, e.g. horribly fail or get disconnected.
  17.         // If that would happen, the disk might vanish from the device-folder and therefore would not be detected by the wildcard anymore.
  18.         protected $argDisks = null;
  19.  
  20.         public function __construct() {
  21.                 parent::__construct();
  22.  
  23.                 if ($this->is_http_mode()) {
  24.                         // Don't allow the standard arguments via $_REQUEST
  25.                         $this->registerExpectedStandardArguments('');
  26.                 } else {
  27.                         $this->registerExpectedStandardArguments('Vhtv');
  28.                 }
  29.  
  30.                 $this->getHelpManager()->setPluginName('vnag_disk_running');
  31.                 $this->getHelpManager()->setVersion('2023-10-13');
  32.                 $this->getHelpManager()->setShortDescription('This plugin checks if a disk is running/online, even if no SMART functionality is available.');
  33.                 $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
  34.                 $this->getHelpManager()->setSyntax('$SCRIPTNAME$ -d diskname[,diskname[,...]]');
  35.                 $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
  36.  
  37.                 // Individual (non-standard) arguments:
  38.                 $this->addExpectedArgument($this->argDisks = new VNagArgument('d', 'disknames', VNagArgument::VALUE_REQUIRED, 'disks', 'Disks to be monitored, e.g. "sda,sdb"'));
  39.         }
  40.  
  41.         protected function cbRun() {
  42.                 $count_total = 0;
  43.                 $count_running = 0;
  44.                 $count_offline = 0;
  45.                 $count_warning = 0;
  46.                 $count_unknown = 0;
  47.  
  48.                 $disks = $this->argDisks->getValue();
  49.                 if (empty($disks)) {
  50.                         throw new VNagException("Please specify the disks you want to monitor.");
  51.                 }
  52.                 $disks = explode(',',$disks);
  53.  
  54.                 foreach ($disks as $disk) {
  55.                         $disk = preg_replace('@^/dev/@', '', $disk); // accept '/dev/' too
  56.  
  57.                         // We do not check the size, in case the user has more than 26 disks; https://rwmj.wordpress.com/2011/01/09/how-are-linux-drives-named-beyond-drive-26-devsdz/
  58.                         // But we check if everything is OK, and nothing nasty is done here
  59.                         $disk = preg_replace('@[^a-zA-Z0-9]@', '', $disk);
  60.  
  61.                         $disk = preg_replace('@^(...)\d+@', '\\1', $disk); // accept 'sdh1' too
  62.  
  63.                         $count_total++;
  64.                         if (!file_exists("/dev/$disk")) {
  65.                                 $this->addVerboseMessage("$disk : Drive does not exist", VNag::VERBOSITY_SUMMARY);
  66.                                 $this->setStatus(VNag::STATUS_CRITICAL);
  67.                                 $count_offline++;
  68.                         } else {
  69.                                 $state_file = "/sys/block/$disk/device/state";
  70.                                 if (!file_exists($state_file)) {
  71.                                         $this->addVerboseMessage("$disk : Cannot fetch state (Is this a valid block device?)", VNag::VERBOSITY_SUMMARY);
  72.                                         $this->setStatus(VNag::STATUS_CRITICAL);
  73.                                         $count_unknown++;
  74.                                 } else {
  75.                                         $cont = @file_get_contents($state_file);
  76.                                         if ($cont === false) throw new VNagException("Cannot read $state_file");
  77.                                         $state = trim($cont);
  78.                                         if ($state != 'running') {
  79.                                                 $this->addVerboseMessage("$disk : $state", VNag::VERBOSITY_SUMMARY);
  80.                                                 $this->setStatus(VNag::STATUS_CRITICAL);
  81.                                                 $count_offline++;
  82.                                         } else {
  83.                                                 #$ioerr_file = "/sys/block/$disk/device/ioerr_cnt";
  84.                                                 #if (file_exists($ioerr_file) && (($ioerr_cont = trim(file_get_contents($ioerr_file))) != '0x0')) {
  85.                                                 #       $this->addVerboseMessage("$disk : High IOERR count ($ioerr_cont), but state is reported as $state", VNag::VERBOSITY_SUMMARY);
  86.                                                 #       $this->setStatus(VNag::STATUS_WARNING);
  87.                                                 #       $count_warning++;
  88.                                                 #} else {
  89.                                                         $this->addVerboseMessage("$disk : $state", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
  90.                                                         $this->setStatus(VNag::STATUS_OK); // Note: This won't unset a previously set critical state
  91.                                                         $count_running++;
  92.                                                 #}
  93.                                         }
  94.                                 }
  95.                         }
  96.                 }
  97.  
  98.                 $this->setHeadline(sprintf('Checked %d disks (%d running, %d offline, %d warnings, %d unknown)', $count_total, $count_running, $count_offline, $count_warning, $count_unknown));
  99.         }
  100. }
  101.