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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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\Exceptions\FileNotFoundException;
use LibreNMS\Exceptions\InvalidModuleException;
use Symfony\Component\Yaml\Yaml;
class ModuleTestHelper
@ -57,6 +58,7 @@ class ModuleTestHelper
* @param array|string $modules
* @param string $os
* @param string $variant
* @throws InvalidModuleException
*/
public function __construct($modules, $os, $variant = '')
{
@ -273,6 +275,7 @@ class ModuleTestHelper
*
* @param array $modules
* @return array
* @throws InvalidModuleException
*/
private function resolveModuleDependencies($modules)
{
@ -281,7 +284,7 @@ class ModuleTestHelper
foreach ($modules as $module) {
// only allow valid modules
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])) {

View File

@ -1,6 +1,7 @@
#!/usr/bin/env php
<?php
use LibreNMS\Exceptions\InvalidModuleException;
use LibreNMS\Util\ModuleTestHelper;
use LibreNMS\Util\Snmpsim;
@ -104,17 +105,21 @@ if ($variant) {
}
echo PHP_EOL;
$capture = new ModuleTestHelper($modules, $target_os, $variant);
try {
$capture = new ModuleTestHelper($modules, $target_os, $variant);
if (isset($options['f'])) {
$capture->setSnmprecSavePath($options['f']);
} elseif (isset($options['file'])) {
$capture->setSnmprecSavePath($options['file']);
if (isset($options['f'])) {
$capture->setSnmprecSavePath($options['f']);
} elseif (isset($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
<?php
use LibreNMS\Exceptions\InvalidModuleException;
use LibreNMS\Util\ModuleTestHelper;
use LibreNMS\Util\Snmpsim;
@ -106,21 +107,26 @@ if (!$snmpsim->isRunning()) {
}
$no_save = isset($options['n']) || isset($options['no-save']);
foreach ($os_list as $full_os_name => $parts) {
list($target_os, $target_variant) = $parts;
echo "OS: $target_os\n";
echo "Module: $modules_input\n";
if ($target_variant) {
echo "Variant: $target_variant\n";
}
echo PHP_EOL;
$tester = new ModuleTestHelper($modules, $target_os, $target_variant);
$test_data = $tester->generateTestData($snmpsim, $no_save);
if ($no_save) {
print_r($test_data);
try {
$no_save = isset($options['n']) || isset($options['no-save']);
foreach ($os_list as $full_os_name => $parts) {
list($target_os, $target_variant) = $parts;
echo "OS: $target_os\n";
echo "Module: $modules_input\n";
if ($target_variant) {
echo "Variant: $target_variant\n";
}
echo PHP_EOL;
$tester = new ModuleTestHelper($modules, $target_os, $target_variant);
$test_data = $tester->generateTestData($snmpsim, $no_save);
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\Exceptions\FileNotFoundException;
use LibreNMS\Exceptions\InvalidModuleException;
use LibreNMS\Util\ModuleTestHelper;
class OSModulesTest extends DBTestCase
@ -54,6 +55,8 @@ class OSModulesTest extends DBTestCase
$results = $helper->generateTestData($snmpsim, true);
} catch (FileNotFoundException $e) {
$this->fail($e->getMessage());
} catch (InvalidModuleException $e) {
$this->fail($e->getMessage());
}
if (is_null($results)) {