diff --git a/includes/functions.php b/includes/functions.php index e399a10ae5..c49857bca0 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1366,3 +1366,65 @@ function dnslookup($device,$type=false,$return=false) { $record = dns_get_record($device['hostname'],$type); return $record[0][$return]; }//end dnslookup + + +/** + * Reursive Filter Iterator to iterate directories and locate .rrd files. + * +**/ + +class RRDReursiveFilterIterator extends \RecursiveFilterIterator { + public function accept() { + $filename = $this->current()->getFilename(); + if ($filename[0] === '.') { + // Ignore hidden files and directories + return false; + } + if ($this->isDir()) { + // We want to search into directories + return true; + } + // Matches files with .rrd in the filename. + // We are only searching rrd folder, but there could be other files and we don't want to cause a stink. + return strpos($filename, '.rrd') !== false; + } +} + +/** + * Run rrdtool info on a file path + * + * @param string $path Path to pass to rrdtool info + * @param string $stdOutput Variable to recieve the output of STDOUT + * @param string $stdError Variable to recieve the output of STDERR + * + * @return int exit code + * +**/ + +function rrdtest($path, &$stdOutput, &$stdError) { + //rrdtool info + $command = $config['rrdtool'].' info '.escapeshellarg($path); + $process = proc_open( + $command, + array ( + 0 => array('pipe', 'r'), + 1 => array('pipe', 'w'), + 2 => array('pipe', 'w'), + ), + $pipes + ); + + if (!is_resource($process)) { + throw new \RuntimeException('Could not create a valid process'); + } + + $status = proc_get_status($process); + while($status['running']) { + $status = proc_get_status($process); + } + + $stdOutput = stream_get_contents($pipes[1]); + $stdError = stream_get_contents($pipes[2]); + proc_close($process); + return $status['exitcode']; +} diff --git a/validate.php b/validate.php index 13db33efd6..8d64262f7f 100755 --- a/validate.php +++ b/validate.php @@ -24,6 +24,7 @@ if (isset($options['h'])) { -m Any sub modules you want to run, comma separated: - mail: this will test your email settings (uses default_mail option even if default_only is not set). - dist-poller: this will test for the install running as a distributed poller. + - rrdcheck: this will check to see if your rrd files are corrupt Example: ./validate.php -m mail. @@ -267,6 +268,40 @@ foreach ($modules as $module) { } } } + break; + case 'rrdcheck': + + $directory = new RecursiveDirectoryIterator($config['rrd_dir']); + $filter = new RRDReursiveFilterIterator($directory); + $iterator = new RecursiveIteratorIterator($filter); + $total = iterator_count($iterator); + $iterator->rewind(); + + echo 'Scanning '.$total.' rrd files...'.PHP_EOL; + + $count = 0; + $screenpad = 0; + + foreach ($iterator as $filename => $file) { + + $testResult = rrdtest($filename, $output, $error); + + $count++; + if (($count % 100) == 0 ) { + echo "\033[".$screenpad.'D'; + $echo = 'Status: '.$count.'/'.$total; + echo $echo; + $screenpad = strlen($echo); + } + + if ($testResult > 0) { + echo "\033[".$screenpad.'D'; + echo PHP_EOL.'Error parsing "'.$filename.'" '.$error.PHP_EOL; + $screenpad = 0; + } + + } + break; }//end switch }//end foreach