Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
52 | daniel-mar | 1 | <?php /* <ViaThinkSoftSignature> |
2 | XWco4Mm3VUQ05meRqXThUvcQghmYl9HrK3nXhjgzTIBDPuVi1WSTixqUVnhfoOrEw |
||
3 | aub01DrcC3Whb4UV436HDtzdusCfXzO2twiV6Ll9Jyym6Ue4PSG5CIKlc5izAawDw |
||
4 | ZCO4j/r6P3jubB9XUuBPZ3v4nbPGf+vQpwsRaYTQyV3wFzn+A4ytI4EtK8+1WNYAA |
||
5 | aa7uygPUeX6nQ8UDU0GwqgNgvYuG3uwB/oKWOUX+u7hSKmRO6qT7GnMlHypb/iMlM |
||
6 | iNJdAacqZsge0/A5ht+Vpa9JJyJ9HeTW3tBHh7U5pwBvm2L3Pt3BexOP/YXOysKVL |
||
7 | SiKMHpa/Ub1HpdipbBQvsO7nA9HmxjKApTm9lklZpP1vKccceEb9+s/RxqZsi5CR8 |
||
8 | PQoO/zCebrPi8AlohDc4BF6uc6CDXuGLS/gLACnoA7a/Zl60IxjoxUqjznuk7lVSR |
||
9 | wSAhSrRbihpmp7vMM/PIK5xbbMFEOJa+AYlfY480bXYHJiboqrNfRmcanWhN+fcli |
||
10 | WneZ9gFSiHWM/2r3Xiq5PR4cQ2LUiT7jxIyEeKynyf+LQ9OJ+z8cC9Qa9/98KpHaV |
||
11 | yFOaI2yRKabyvFSsbSJuP6rorjxAv2YeKGAkOHVt596qQYZzkOfAg/1GEi2+OfaB8 |
||
12 | Gv1d+FaHAEAodi1z/MiuWH+SgJM7QjeiQSaSs+cm+zlWxlRQM4UhPjdckg1Ua/vYY |
||
13 | UJV5iwPKc/800ieC2b1xWqPrFs31N4oeBVf52VYSmTtj8NspePbjx6Fd77aI2ik2e |
||
14 | KfgiE3Vb+5naPgEwPRi5yHLxD5DmcLwQTD5wraUWbB63GtFMRJr+tY5NqE4Id1Okt |
||
15 | fJqwX6w//viS8dCRUGAGj7zpKcMah4SYGxvWwTFutKcmU9kKdUXzQsJjarpPXgqid |
||
16 | WwXwt0olEe+6Z+Lw1dMAKm8msIZrRNbDvhii88AwIFGN5I62epg2012+G0sMg/dgU |
||
17 | lJwt2sRD1IQQfePvdF1CeCfJINnXKtK5EDArDv1p4M7jKlTkMS1dk4kmtJKZoy1Ge |
||
18 | G19Ie3+UiNskQoz3v2+4MBcIjtnzdMBa3SzhhVI2IIbsTShin68Nb742jem6vzVNk |
||
19 | KJtsWZylRPhI+Nw26Hnb/Fvnr9WB+fiJYih1J9ANS733E8I18xmzUjIxFt4UaeuL1 |
||
20 | 3XVpjZjToGPHyaXvmIgnoky8e7UMfmE2pb2I6VI8lqaDjN3RZrg0aGhpNLeQ9DvSw |
||
21 | msuIjyhTE7MiTjasbSZ47NOINf/OjZANlAK+Mn1Xm2kQZtsLSzfVeag2GUpqIbv3c |
||
22 | acyHtjO4RjccrZJscrNtYftqujahtf2N9oOozGii8aNSdW7qxnN9F7SOsBxAEOLCf |
||
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 2022-06-06 |
||
33 | */ |
||
34 | |||
35 | declare(ticks=1); |
||
36 | |||
37 | define('OUTPUT_UOM', 'MB'); |
||
38 | define('ROUND_TO', 0); |
||
39 | |||
40 | class OpenDeletedFilesCheck extends VNag { |
||
41 | protected $argDir = null; |
||
42 | |||
43 | public function __construct() { |
||
44 | parent::__construct(); |
||
45 | |||
46 | $this->registerExpectedStandardArguments('Vhtwc'); |
||
47 | |||
48 | $this->getHelpManager()->setPluginName('open_deleted_files'); |
||
49 | $this->getHelpManager()->setVersion('1.0'); |
||
50 | $this->getHelpManager()->setShortDescription('This plugin checks for open deleted files (which require space but are not visible/accessible anymore).'); |
||
51 | $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.'); |
||
52 | $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-d directory] [-w warnSizeKB] [-c critSizeKB]'); |
||
53 | $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com'); |
||
54 | |||
55 | # $this->warningSingleValueRangeBehaviors[0] = self::SINGLEVALUE_RANGE_VAL_GT_X_BAD; |
||
56 | # $this->criticalSingleValueRangeBehaviors[0] = self::SINGLEVALUE_RANGE_VAL_GT_X_BAD; |
||
57 | |||
58 | // Individual (non-standard) arguments: |
||
59 | $this->addExpectedArgument($this->argDir = new VNagArgument('d', 'directory', VNagArgument::VALUE_REQUIRED, 'directory', 'Directory to check (e.g. /tmp)Directory to check (e.g. /tmp)')); |
||
60 | } |
||
61 | |||
62 | protected static function check_open_deleted_files($dir_to_check = '/') { |
||
63 | // TODO: For some reason, it does not work if "sudo" is added to the command section in icinga2.conf! |
||
64 | exec('sudo lsof -n', $lines, $ec); |
||
65 | if ($ec != 0) return false; |
||
66 | |||
67 | /* |
||
68 | $lines = explode("\n", |
||
69 | 'COMMAND PID TID TASKCMD USER FD TYPE DEVICE SIZE/OFF NODE NAME |
||
70 | php-cgi 430 oidplus 3u REG 0,42 0 1502217042 /tmp/.ZendSem.uhCRtC (deleted) |
||
71 | apache2 838 root 150u REG 0,42 0 1499023202 /tmp/.ZendSem.RFcTM9 (deleted) |
||
72 | postgres 1060 gitlab-psql txt REG 9,0 9291488 47189384 /opt/gitlab/embedded/postgresql/12/bin/postgres (deleted) |
||
73 | php-cgi 1573 owncloud 3u REG 0,42 0 1499024339 /tmp/.ZendSem.2Qh70x (deleted) |
||
74 | php-fpm7. 1738 root 3u REG 0,42 0 434907183 /tmp/.ZendSem.unGJqF |
||
75 | php-fpm7. 1739 www-data 3u REG 0,42 0 434907183 /tmp/.ZendSem.unGJqF (deleted) |
||
76 | php-fpm7. 1740 www-data 3u REG 0,42 0 434907183 /tmp/.ZendSem.unGJqF (deleted) |
||
77 | runsvdir 1932 root txt REG 9,0 27104 45351338 /opt/gitlab/embedded/bin/runsvdir (deleted) |
||
78 | '); |
||
79 | */ |
||
80 | |||
81 | $line_desc = array_shift($lines); |
||
82 | $p_name = strpos($line_desc, 'NAME'); |
||
83 | if ($p_name === false) return false; |
||
84 | |||
85 | $nodes = array(); |
||
86 | |||
87 | foreach ($lines as $line) { |
||
88 | if (trim($line) == '') continue; |
||
89 | |||
90 | $name = substr($line, $p_name); |
||
91 | |||
92 | preg_match('@.+\s(\d+)\$@ism', substr($line, 0, $p_name-1), $m); |
||
93 | $tmp = rtrim(substr($line, 0, $p_name-1)); |
||
94 | $tmp = explode(" ", $tmp); |
||
95 | $node = end($tmp); |
||
96 | |||
97 | $tmp = rtrim(substr($line, 0, $p_name-strlen($node)-1)); |
||
98 | $tmp = explode(" ", $tmp); |
||
99 | $size = end($tmp); |
||
100 | |||
101 | if (substr($name, 0, strlen($dir_to_check)) !== $dir_to_check) continue; |
||
102 | |||
103 | if (strpos($name, ' (deleted)') === false) continue; |
||
104 | if ($size == 0) continue; |
||
105 | |||
106 | $nodes[$node] = $size; |
||
107 | } |
||
108 | |||
109 | $size_total = 0; |
||
110 | foreach ($nodes as $node => $size) { |
||
111 | $size_total += $size; |
||
112 | } |
||
113 | return $size_total; |
||
114 | } |
||
115 | |||
116 | protected function cbRun($optional_args=array()) { |
||
117 | $dir = $this->argDir->getValue(); |
||
118 | if (empty($dir)) $dir = '/'; |
||
119 | $dir = realpath($dir) === false ? $dir : realpath($dir); |
||
120 | if (substr($dir,-1) !== '/') $dir .= '/'; |
||
121 | |||
122 | $size = self::check_open_deleted_files($dir); |
||
123 | if ($size === false) throw new Exception("Cannot get information from 'lsof'"); |
||
124 | |||
125 | $this->checkAgainstWarningRange( array($size.'B'), false, true, 0); |
||
126 | $this->checkAgainstCriticalRange(array($size.'B'), false, true, 0); |
||
127 | |||
128 | $m = (new VNagValueUomPair($size.'B')); |
||
129 | $m->roundTo = ROUND_TO; |
||
130 | $sizeOut = $m->normalize(OUTPUT_UOM); |
||
131 | |||
132 | $msg = "$sizeOut opened deleted files in $dir"; |
||
133 | $this->setHeadline($msg); |
||
134 | } |
||
135 | } |