Rev 77 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 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 | * |
||
77 | daniel-mar | 8 | * Revision 2023-10-13 |
2 | daniel-mar | 9 | */ |
10 | |||
6 | daniel-mar | 11 | // TODO: Anstelle "-d diskname[,diskname[,...]]" wäre es schöner ohne Komma, denn dann könnte man auch die Shell Erweiterung /dev/sd* nutzen |
12 | |||
2 | daniel-mar | 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'); |
||
79 | daniel-mar | 31 | $this->getHelpManager()->setVersion('2023-10-13'); |
2 | daniel-mar | 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; |
||
6 | daniel-mar | 45 | $count_warning = 0; |
2 | daniel-mar | 46 | $count_unknown = 0; |
47 | |||
48 | $disks = $this->argDisks->getValue(); |
||
49 | if (empty($disks)) { |
||
77 | daniel-mar | 50 | throw new VNagException("Please specify the disks you want to monitor."); |
2 | daniel-mar | 51 | } |
52 | $disks = explode(',',$disks); |
||
53 | |||
54 | foreach ($disks as $disk) { |
||
6 | daniel-mar | 55 | $disk = preg_replace('@^/dev/@', '', $disk); // accept '/dev/' too |
56 | |||
2 | daniel-mar | 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 | |||
6 | daniel-mar | 61 | $disk = preg_replace('@^(...)\d+@', '\\1', $disk); // accept 'sdh1' too |
62 | |||
2 | daniel-mar | 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 { |
||
77 | daniel-mar | 75 | $cont = @file_get_contents($state_file); |
76 | if ($cont === false) throw new VNagException("Cannot read $state_file"); |
||
77 | $state = trim($cont); |
||
2 | daniel-mar | 78 | if ($state != 'running') { |
79 | $this->addVerboseMessage("$disk : $state", VNag::VERBOSITY_SUMMARY); |
||
80 | $this->setStatus(VNag::STATUS_CRITICAL); |
||
81 | $count_offline++; |
||
82 | } else { |
||
31 | daniel-mar | 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 { |
||
6 | daniel-mar | 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++; |
||
31 | daniel-mar | 92 | #} |
2 | daniel-mar | 93 | } |
94 | } |
||
95 | } |
||
96 | } |
||
97 | |||
6 | daniel-mar | 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)); |
2 | daniel-mar | 99 | } |
100 | } |