From edb61975c0dc84366ae13a6e876451119b64e4b3 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 1 Dec 2017 16:28:02 +0100 Subject: [PATCH] MibParser: prepare pre-validation --- library/Snmp/MibParser.php | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/library/Snmp/MibParser.php b/library/Snmp/MibParser.php index 10a5deb..f108777 100644 --- a/library/Snmp/MibParser.php +++ b/library/Snmp/MibParser.php @@ -9,6 +9,69 @@ class MibParser { + protected static $lastValidationErrors; + + public static function preValidateFile($filename) + { + $binary = '/usr/bin/smilint'; + if (! file_exists($binary)) { + throw new IcingaException('%s not found', $binary); + } + + $command = sprintf( + "exec %s '%s' -l 2", + $binary, + escapeshellarg($filename) + ); + + $loop = Loop::create(); + $process = new Process($command); + $process->start($loop); + $buffer = ''; + $timer = $loop->addTimer(10, function () use ($process) { + $process->terminate(); + }); + $process->stdout->on('data', function ($string) use (& $buffer) { + $buffer .= $string; + }); + $process->stderr->on('data', function ($string) use (& $buffer) { + $buffer .= $string; + }); + $process->on('exit', function ($exitCode, $termSignal) use ($timer) { + $timer->cancel(); + if ($exitCode === null) { + if ($termSignal === null) { + throw new IcingaException( + 'Fuck, I have no idea how the validator got killed' + ); + } else { + throw new IcingaException( + "They killed the validator with $termSignal" + ); + } + } else { + if ($exitCode !== 0) { + throw new IcingaException("Validator exited with $exitCode"); + } + } + }); + + $loop->run(); + + if (empty($buffer)) { + return true; + } else { + self::$lastValidationErrors = $buffer; + + return false; + } + } + + public static function getLastValidationError() + { + return self::$lastValidationErrors; + } + public static function parseString($string) { $loop = Loop::create();