save-test-data.php: Print a message when we encounter a bad module name (#8274)

* Print a message when we encounter a bad module name (likely a typo)

* Raise exception for invalid module name and handle it.
This commit is contained in:
Tony Murray
2018-02-25 20:03:18 -06:00
committed by GitHub
parent 6dcdd89dd7
commit b2ce9b173b
5 changed files with 75 additions and 28 deletions

View File

@@ -0,0 +1,30 @@
<?php
/**
* InvalideModuleException.php
*
* Thrown when the given name isn't a valid discovery or poller module
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2018 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Exceptions;
class InvalidModuleException extends \Exception
{
}

View File

@@ -27,6 +27,7 @@ namespace LibreNMS\Util;
use LibreNMS\Config; use LibreNMS\Config;
use LibreNMS\Exceptions\FileNotFoundException; use LibreNMS\Exceptions\FileNotFoundException;
use LibreNMS\Exceptions\InvalidModuleException;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
class ModuleTestHelper class ModuleTestHelper
@@ -57,6 +58,7 @@ class ModuleTestHelper
* @param array|string $modules * @param array|string $modules
* @param string $os * @param string $os
* @param string $variant * @param string $variant
* @throws InvalidModuleException
*/ */
public function __construct($modules, $os, $variant = '') public function __construct($modules, $os, $variant = '')
{ {
@@ -273,6 +275,7 @@ class ModuleTestHelper
* *
* @param array $modules * @param array $modules
* @return array * @return array
* @throws InvalidModuleException
*/ */
private function resolveModuleDependencies($modules) private function resolveModuleDependencies($modules)
{ {
@@ -281,7 +284,7 @@ class ModuleTestHelper
foreach ($modules as $module) { foreach ($modules as $module) {
// only allow valid modules // only allow valid modules
if (!(Config::has("poller_modules.$module") || Config::has("discovery_modules.$module"))) { if (!(Config::has("poller_modules.$module") || Config::has("discovery_modules.$module"))) {
continue; throw new InvalidModuleException("Invalid module name: $module");
} }
if (isset($this->module_deps[$module])) { if (isset($this->module_deps[$module])) {

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env php #!/usr/bin/env php
<?php <?php
use LibreNMS\Exceptions\InvalidModuleException;
use LibreNMS\Util\ModuleTestHelper; use LibreNMS\Util\ModuleTestHelper;
use LibreNMS\Util\Snmpsim; use LibreNMS\Util\Snmpsim;
@@ -104,17 +105,21 @@ if ($variant) {
} }
echo PHP_EOL; echo PHP_EOL;
$capture = new ModuleTestHelper($modules, $target_os, $variant); try {
$capture = new ModuleTestHelper($modules, $target_os, $variant);
if (isset($options['f'])) { if (isset($options['f'])) {
$capture->setSnmprecSavePath($options['f']); $capture->setSnmprecSavePath($options['f']);
} elseif (isset($options['file'])) { } elseif (isset($options['file'])) {
$capture->setSnmprecSavePath($options['file']); $capture->setSnmprecSavePath($options['file']);
}
$prefer_new_snmprec = isset($options['n']) || isset($options['prefer-new']);
echo "Capturing Data: ";
$capture->captureFromDevice($device['device_id'], true, $prefer_new_snmprec);
} catch (InvalidModuleException $e) {
echo $e->getMessage() . PHP_EOL;
} }
$prefer_new_snmprec = isset($options['n']) || isset($options['prefer-new']);
echo "Capturing Data: ";
$capture->captureFromDevice($device['device_id'], true, $prefer_new_snmprec);

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env php #!/usr/bin/env php
<?php <?php
use LibreNMS\Exceptions\InvalidModuleException;
use LibreNMS\Util\ModuleTestHelper; use LibreNMS\Util\ModuleTestHelper;
use LibreNMS\Util\Snmpsim; use LibreNMS\Util\Snmpsim;
@@ -106,21 +107,26 @@ if (!$snmpsim->isRunning()) {
} }
$no_save = isset($options['n']) || isset($options['no-save']); try {
foreach ($os_list as $full_os_name => $parts) { $no_save = isset($options['n']) || isset($options['no-save']);
list($target_os, $target_variant) = $parts; foreach ($os_list as $full_os_name => $parts) {
echo "OS: $target_os\n"; list($target_os, $target_variant) = $parts;
echo "Module: $modules_input\n"; echo "OS: $target_os\n";
if ($target_variant) { echo "Module: $modules_input\n";
echo "Variant: $target_variant\n"; if ($target_variant) {
} echo "Variant: $target_variant\n";
echo PHP_EOL; }
echo PHP_EOL;
$tester = new ModuleTestHelper($modules, $target_os, $target_variant);
$test_data = $tester->generateTestData($snmpsim, $no_save); $tester = new ModuleTestHelper($modules, $target_os, $target_variant);
if ($no_save) { $test_data = $tester->generateTestData($snmpsim, $no_save);
print_r($test_data);
if ($no_save) {
print_r($test_data);
}
} }
} catch (InvalidModuleException $e) {
echo $e->getMessage() . PHP_EOL;
} }

View File

@@ -27,6 +27,7 @@ namespace LibreNMS\Tests;
use LibreNMS\Config; use LibreNMS\Config;
use LibreNMS\Exceptions\FileNotFoundException; use LibreNMS\Exceptions\FileNotFoundException;
use LibreNMS\Exceptions\InvalidModuleException;
use LibreNMS\Util\ModuleTestHelper; use LibreNMS\Util\ModuleTestHelper;
class OSModulesTest extends DBTestCase class OSModulesTest extends DBTestCase
@@ -54,6 +55,8 @@ class OSModulesTest extends DBTestCase
$results = $helper->generateTestData($snmpsim, true); $results = $helper->generateTestData($snmpsim, true);
} catch (FileNotFoundException $e) { } catch (FileNotFoundException $e) {
$this->fail($e->getMessage()); $this->fail($e->getMessage());
} catch (InvalidModuleException $e) {
$this->fail($e->getMessage());
} }
if (is_null($results)) { if (is_null($results)) {