Files
librenms-librenms/scripts/save-test-data.php
Tony Murray fff66d3c00 Feature: Generic discovery and poller tests (#7873)
* Processor Tests!

* Capture data from live devices easily.

* fix up some stuff, remove powerconnect things as they seem to be just broken.

* formatting, fix missing assignment
add netonix processor data

* fix multi-line, always add sysDescr and sysObjectID
ios cpm test file

* revert composer change and fix whitespace issues

* add help text

* missed help text

* tighter debug output

* handle empty strings properly and mibs with numbers

* use keys for sorting as intended

* fix type with empty data

* oops :)

* whitespace fix

* try installing fping

* Fix TestCase collision + cleanup

* mark TestCase as abstract
don't run two instances of snmpsim

* better database dumps, improved capture

* style fixes

* fix quotes add a few more tables

* add --prefer-new, properly merge data

* Support separate discovery and poller data. But don't waste space if they aren't needed.

* refactor to use class
collects all code in one place for reusability

* reorganize

* Print out when not saving.

* Support for running multiple (or all) modules at once.

* tidy

* Change unit test to a generic name and test all modules we have data for.

* Add documentation and a few more tidies

* whitespace fixes

* Fix tests, add a couple more modules, more docs

* More docs updates

* Fix scrutinizer issues

* add bgp-peers
2017-12-20 08:36:49 -06:00

142 lines
3.4 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
use LibreNMS\Config;
use LibreNMS\Proc;
use LibreNMS\Util\ModuleTestHelper;
use LibreNMS\Util\Snmpsim;
global $device;
$install_dir = realpath(__DIR__ . '/..');
chdir($install_dir);
$options = getopt(
'h:dnm:o:v:f:',
array(
'debug',
'no-save',
'prefer-new',
'hostname:',
'help',
'module:',
'os:',
'variant:',
'file:',
'snmpsim',
)
);
$init_modules = array('discovery', 'polling');
require $install_dir . '/includes/init.php';
$debug = (isset($options['d']) || isset($options['debug']));
$vdebug = $debug;
if (isset($options['snmpsim'])) {
$snmpsim = new Snmpsim();
$snmpsim->run();
exit;
}
if (isset($options['h'])) {
$hostname = $options['h'];
} elseif (isset($options['hostname'])) {
$hostname = $options['hostname'];
}
$target_os = '';
if (isset($options['o'])) {
$target_os = $options['o'];
} elseif (isset($options['os'])) {
$target_os = $options['os'];
}
if (isset($hostname)) {
if (is_numeric($hostname)) {
$device = device_by_id_cache($hostname);
} elseif (!empty($hostname)) {
$device = device_by_name($hostname);
}
if (isset($device['os']) && $device['os'] != 'generic') {
$target_os = $device['os'];
} else {
echo "OS (-o, --os) required because device is generic.\n";
exit;
}
}
if (isset($options['help']) || empty($target_os)) {
echo "Script to extract test data from devices or update test data
Usage:
You must specify a valid hostname or os.
-h, --hostname ID, IP, or hostname of the device to extract data from
If this is not given, the existing snmp data will be used
-o, --os Name of the OS to save test data for
-v, --variant The variant of the OS to use, usually the device model
-m, --modules The discovery/poller module(s) to collect data for, comma delimited
-f, --file File to save the database entries to. Default is in tests/data/
-d, --debug Enable debug output
-n, --prefer-new Prefer new snmprec data over existing data
--no-save Don't save database entries, print them out instead
--snmpsim Just run snmpsimd.py for manual testing.
";
exit;
}
if (isset($options['m'])) {
$modules_input = $options['m'];
$modules = explode(',', $modules_input);
} elseif (isset($options['modules'])) {
$modules_input = $options['modules'];
$modules = explode(',', $modules_input);
} else {
$modules_input = 'all';
$modules = array();
}
$variant = '';
if (isset($options['v'])) {
$variant = $options['v'];
} elseif (isset($options['variant'])) {
$variant = $options['variant'];
}
echo "OS: $target_os\n";
echo "Module: $modules_input\n";
if ($variant) {
echo "Variant: $variant\n";
}
echo PHP_EOL;
$tester = new ModuleTestHelper($modules, $target_os, $variant);
// Capture snmp data
if ($device) {
echo "Capturing Data: ";
$prefer_new_snmprec = isset($options['n']) || isset($options['prefer-new']);
$tester->captureFromDevice($device['device_id'], true, $prefer_new_snmprec);
echo PHP_EOL;
}
// Now use the saved data to update the saved database data
$snmpsim = new Snmpsim();
$snmpsim->fork();
$snmpsim_ip = $snmpsim->getIp();
$snmpsim_port = $snmpsim->getPort();
$no_save = isset($options['no-save']);
$test_data = $tester->generateTestData($snmpsim, $no_save);
if ($no_save) {
print_r($test_data);
}