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 | |||
11 | declare(ticks=1); |
||
12 | |||
13 | define('OUTPUT_UOM', 'GB'); |
||
14 | define('ROUND_TO', 0); |
||
15 | |||
16 | class VirtualMemCheck extends VNag { |
||
17 | public function __construct() { |
||
18 | parent::__construct(); |
||
19 | |||
20 | $this->registerExpectedStandardArguments('Vhtwc'); |
||
21 | |||
22 | $this->getHelpManager()->setPluginName('check_virtual_mem'); |
||
79 | daniel-mar | 23 | $this->getHelpManager()->setVersion('2023-10-13'); |
2 | daniel-mar | 24 | $this->getHelpManager()->setShortDescription('This plugin checks the amount of free virtual memory (real memory + swap combined).'); |
25 | $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.'); |
||
26 | $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-w freeMemKB|%] [-c freeMemKB|%]'); |
||
27 | $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com'); |
||
28 | |||
52 | daniel-mar | 29 | // In this context, when the user writes "-w 10GB" then they probably mean "-w @10GB". Make sure that the user doesn't do it wrong |
2 | daniel-mar | 30 | $this->warningSingleValueRangeBehaviors[0] = self::SINGLEVALUE_RANGE_VAL_LT_X_BAD; |
31 | $this->criticalSingleValueRangeBehaviors[0] = self::SINGLEVALUE_RANGE_VAL_LT_X_BAD; |
||
32 | } |
||
33 | |||
34 | protected function cbRun($optional_args=array()) { |
||
51 | daniel-mar | 35 | if (!file_exists($meminfo_file = '/proc/meminfo')) { |
36 | throw new VNagException("Cannot find $meminfo_file"); |
||
2 | daniel-mar | 37 | } |
38 | |||
51 | daniel-mar | 39 | $meminfo_attrs = array(); |
40 | foreach (file($meminfo_file) as $line) { |
||
41 | $line = trim($line); |
||
42 | if ($line == '') continue; |
||
43 | list($key, $value) = explode(':', $line, 2); |
||
44 | if ((!strstr($key,'Pages')) && (strtolower(substr($line,-2)) !== 'kb')) { |
||
77 | daniel-mar | 45 | throw new VNagException("meminfo shall contain Kilobytes!"); |
51 | daniel-mar | 46 | } |
47 | $meminfo_attrs[$key] = (int)trim(substr($value, 0, strlen($value)-2)); |
||
48 | } |
||
49 | |||
50 | if (!isset($meminfo_attrs['MemAvailable'])) { |
||
51 | $meminfo_attrs['MemAvailable'] = $meminfo_attrs['MemFree']; |
||
52 | if (isset($meminfo_attrs['Buffers'])) $meminfo_attrs['MemAvailable'] += $meminfo_attrs['Buffers']; |
||
53 | if (isset($meminfo_attrs['Cached'])) $meminfo_attrs['MemAvailable'] += $meminfo_attrs['Cached']; |
||
54 | } |
||
55 | |||
56 | $totalKB = $meminfo_attrs['MemTotal'] + $meminfo_attrs['SwapTotal']; |
||
57 | $freeKB = $meminfo_attrs['MemAvailable'] + $meminfo_attrs['SwapFree']; |
||
2 | daniel-mar | 58 | $freePercent = $freeKB/$totalKB*100; |
59 | |||
60 | $this->checkAgainstWarningRange( array($freeKB.'KB', $freePercent.'%'), false, true, 0); |
||
61 | $this->checkAgainstCriticalRange(array($freeKB.'KB', $freePercent.'%'), false, true, 0); |
||
62 | |||
63 | $m = (new VNagValueUomPair($freeKB.'KB')); |
||
64 | $m->roundTo = ROUND_TO; |
||
65 | $freeKB = $m->normalize(OUTPUT_UOM); |
||
66 | |||
67 | $m = (new VNagValueUomPair($totalKB.'KB')); |
||
68 | $m->roundTo = ROUND_TO; |
||
69 | $totalKB = $m->normalize(OUTPUT_UOM); |
||
70 | |||
71 | $msg = "$freeKB free of $totalKB (".round($freePercent,ROUND_TO)."% free)"; |
||
72 | $this->setHeadline($msg); |
||
73 | } |
||
74 | } |