Skip to content

Commit

Permalink
save-test-data.php: Print a message when we encounter a bad module na…
Browse files Browse the repository at this point in the history
…me (librenms#8274)

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

* Raise exception for invalid module name and handle it.
  • Loading branch information
murrant authored Feb 26, 2018
1 parent 6dcdd89 commit b2ce9b1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 23 deletions.
30 changes: 30 additions & 0 deletions LibreNMS/Exceptions/InvalidModuleException.php
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*/

namespace LibreNMS\Exceptions;

class InvalidModuleException extends \Exception
{
}
5 changes: 4 additions & 1 deletion LibreNMS/Util/ModuleTestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use LibreNMS\Config;
use LibreNMS\Exceptions\FileNotFoundException;
use LibreNMS\Exceptions\InvalidModuleException;
use Symfony\Component\Yaml\Yaml;

class ModuleTestHelper
Expand Down Expand Up @@ -57,6 +58,7 @@ class ModuleTestHelper
* @param array|string $modules
* @param string $os
* @param string $variant
* @throws InvalidModuleException
*/
public function __construct($modules, $os, $variant = '')
{
Expand Down Expand Up @@ -273,6 +275,7 @@ public static function extractVariant($os_file)
*
* @param array $modules
* @return array
* @throws InvalidModuleException
*/
private function resolveModuleDependencies($modules)
{
Expand All @@ -281,7 +284,7 @@ private function resolveModuleDependencies($modules)
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])) {
Expand Down
23 changes: 14 additions & 9 deletions scripts/collect-snmp-data.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env php
<?php

use LibreNMS\Exceptions\InvalidModuleException;
use LibreNMS\Util\ModuleTestHelper;
use LibreNMS\Util\Snmpsim;

Expand Down Expand Up @@ -104,17 +105,21 @@
}
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']);
$prefer_new_snmprec = isset($options['n']) || isset($options['prefer-new']);


echo "Capturing Data: ";
$capture->captureFromDevice($device['device_id'], true, $prefer_new_snmprec);
echo "Capturing Data: ";
$capture->captureFromDevice($device['device_id'], true, $prefer_new_snmprec);
} catch (InvalidModuleException $e) {
echo $e->getMessage() . PHP_EOL;
}
32 changes: 19 additions & 13 deletions scripts/save-test-data.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env php
<?php

use LibreNMS\Exceptions\InvalidModuleException;
use LibreNMS\Util\ModuleTestHelper;
use LibreNMS\Util\Snmpsim;

Expand Down Expand Up @@ -106,21 +107,26 @@
}


$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;
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);
$tester = new ModuleTestHelper($modules, $target_os, $target_variant);

$test_data = $tester->generateTestData($snmpsim, $no_save);
$test_data = $tester->generateTestData($snmpsim, $no_save);

if ($no_save) {
print_r($test_data);
if ($no_save) {
print_r($test_data);
}
}
} catch (InvalidModuleException $e) {
echo $e->getMessage() . PHP_EOL;
}
3 changes: 3 additions & 0 deletions tests/OSModulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use LibreNMS\Config;
use LibreNMS\Exceptions\FileNotFoundException;
use LibreNMS\Exceptions\InvalidModuleException;
use LibreNMS\Util\ModuleTestHelper;

class OSModulesTest extends DBTestCase
Expand Down Expand Up @@ -54,6 +55,8 @@ public function testOS($os, $variant, $modules)
$results = $helper->generateTestData($snmpsim, true);
} catch (FileNotFoundException $e) {
$this->fail($e->getMessage());
} catch (InvalidModuleException $e) {
$this->fail($e->getMessage());
}

if (is_null($results)) {
Expand Down

0 comments on commit b2ce9b1

Please sign in to comment.