Improve Snmpsim usage to ease testing (#15471)

* Snmpsim use python venv
Patch to enable listening while minimizing output
Update lnms dev:simulate, tests, and ./scripts/save-test-data.php
removed old option to start snmpsim from older scripts, use lnms dev:simulate

* Apply fixes from StyleCI

* various fixes

* Remove patch official package is updated

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
This commit is contained in:
Tony Murray
2024-07-17 16:05:07 -05:00
committed by GitHub
parent 10669226fc
commit 1cceafb887
11 changed files with 100 additions and 212 deletions

View File

@@ -295,7 +295,7 @@ class CiHelper
$py_lint_cmd = [$this->checkPythonExec('pylint'), '-E', '-j', '0'];
$files = $this->flags['full']
? explode(PHP_EOL, rtrim(shell_exec("find . -name '*.py' -not -path './vendor/*' -not -path './tests/*'")))
? explode(PHP_EOL, rtrim(shell_exec("find . -name '*.py' -not -path './vendor/*' -not -path './tests/*' -not -path './.python_venvs/*'")))
: $this->changed['python'];
$py_lint_cmd = array_merge($py_lint_cmd, $files);

View File

@@ -560,17 +560,17 @@ class ModuleTestHelper
}
// Remove existing device in case it didn't get removed previously
if (($existing_device = device_by_name($snmpsim->getIp())) && isset($existing_device['device_id'])) {
if (($existing_device = device_by_name($snmpsim->ip)) && isset($existing_device['device_id'])) {
delete_device($existing_device['device_id']);
}
// Add the test device
try {
$new_device = new Device([
'hostname' => $snmpsim->getIp(),
'hostname' => $snmpsim->ip,
'version' => 'v2c',
'community' => $this->file_name,
'port' => $snmpsim->getPort(),
'port' => $snmpsim->port,
'disabled' => 1, // disable to block normal pollers
]);
(new ValidateDeviceAndCreate($new_device, true))->execute();
@@ -647,7 +647,7 @@ class ModuleTestHelper
$data = array_merge_recursive($data, $this->dumpDb($device_id, $polled_modules, 'poller'));
// Remove the test device, we don't need the debug from this
if ($device['hostname'] == $snmpsim->getIp()) {
if ($device['hostname'] == $snmpsim->ip) {
Debug::set(false);
delete_device($device_id);
}

View File

@@ -25,166 +25,70 @@
namespace LibreNMS\Util;
use App;
use LibreNMS\Config;
use LibreNMS\Proc;
use Symfony\Component\Process\Process;
class Snmpsim
class Snmpsim extends Process
{
private $snmprec_dir;
private $ip;
private $port;
private $log;
/** @var Proc */
private $proc;
public readonly string $snmprec_dir;
public function __construct($ip = '127.1.6.1', $port = 1161, $log = '/tmp/snmpsimd.log')
public function __construct(
public readonly string $ip = '127.1.6.1',
public readonly int $port = 1161,
public readonly ?string $log_method = null)
{
$this->ip = $ip;
$this->port = $port;
$this->log = $log;
$this->snmprec_dir = Config::get('install_dir') . '/tests/snmpsim/';
$this->snmprec_dir = base_path('tests/snmpsim');
$cmd = [
$this->getVenvPath('bin/snmpsim-command-responder-lite'),
"--data-dir={$this->snmprec_dir}",
"--agent-udpv4-endpoint={$this->ip}:{$this->port}",
'--log-level=error',
];
if ($this->log_method !== null) {
$cmd[] = "--logging-method=$this->log_method";
}
parent::__construct($cmd, base_path());
$this->setTimeout(null); // no timeout by default
}
/**
* Run snmpsimd and fork it into the background
* Captures all output to the log
*
* @param int $wait Wait for x seconds after starting before returning
*/
public function fork($wait = 2)
public function waitForStartup(): string
{
if ($this->isRunning()) {
echo "Snmpsim is already running!\n";
$listen = $this->ip . ':' . $this->port;
$this->waitUntil(function ($type, $buffer) use ($listen, &$last) {
$last = $buffer;
return;
}
return $type == Process::ERR && str_contains($buffer, $listen);
});
$cmd = $this->getCmd();
return trim($last);
}
if (App::runningInConsole()) {
echo "Starting snmpsim listening on {$this->ip}:{$this->port}... \n";
d_echo($cmd);
}
public function isVenvSetUp(): bool
{
return is_executable($this->getVenvPath('bin/snmpsim-command-responder-lite'));
}
$this->proc = new Proc($cmd);
public function setupVenv($print_output = false): void
{
$snmpsim_venv_path = $this->getVenvPath();
if ($wait) {
sleep($wait);
}
if (! $this->isVenvSetUp()) {
\Log::info('Setting up snmpsim virtual env in ' . $snmpsim_venv_path);
if (App::runningInConsole() && ! $this->proc->isRunning()) {
// if starting failed, run snmpsim again and output to the console and validate the data
passthru($this->getCmd(false) . ' --validate-data');
$setupProcess = new Process(['python', '-m', 'venv', $snmpsim_venv_path]);
$setupProcess->setTty($print_output);
$setupProcess->run();
if (! is_executable($this->findSnmpsimd())) {
echo "\nCould not find snmpsim, you can install it with 'pip install snmpsim-lextudio'. If it is already installed, make sure snmpsimd, snmpsim-command-responder or snmpsimd.py is in PATH\n";
} else {
echo "\nFailed to start Snmpsim. Scroll up for error.\n";
}
exit(1);
$installProcess = new Process([$snmpsim_venv_path . '/bin/pip', 'install', 'snmpsim']);
$installProcess->setTty($print_output);
$installProcess->run();
}
}
/**
* Stop and start the running snmpsim process
*/
public function restart()
public function getVenvPath(string $subdir = ''): string
{
$this->stop();
$this->proc = new Proc($this->getCmd());
}
public function stop()
{
if (isset($this->proc)) {
if ($this->proc->isRunning()) {
$this->proc->terminate();
}
unset($this->proc);
}
}
/**
* Run snmpsimd but keep it in the foreground
* Outputs to stdout
*/
public function run()
{
echo "Starting snmpsim listening on {$this->ip}:{$this->port}... \n";
shell_exec($this->getCmd(false));
}
public function isRunning()
{
if (isset($this->proc)) {
return $this->proc->isRunning();
}
return false;
}
/**
* @return string
*/
public function getDir()
{
return $this->snmprec_dir;
}
/**
* @return string
*/
public function getIp()
{
return $this->ip;
}
/**
* @return int
*/
public function getPort()
{
return $this->port;
}
/**
* Generate the command for snmpsimd
*
* @param bool $with_log
* @return string
*/
private function getCmd($with_log = true)
{
$cmd = $this->findSnmpsimd();
$cmd .= " --data-dir={$this->snmprec_dir} --agent-udpv4-endpoint={$this->ip}:{$this->port}";
if (is_null($this->log)) {
$cmd .= ' --logging-method=null';
} elseif ($with_log) {
$cmd .= " --logging-method=file:{$this->log}";
}
return $cmd;
}
public function __destruct()
{
// unset $this->proc to make sure it isn't referenced
unset($this->proc);
}
public function findSnmpsimd()
{
$cmd = Config::locateBinary('snmpsimd');
if (! is_executable($cmd)) {
$cmd = Config::locateBinary('snmpsim-command-responder');
if (! is_executable($cmd)) {
$cmd = Config::locateBinary('snmpsimd.py');
}
}
return $cmd;
return base_path('.python_venvs/snmpsim/' . $subdir);
}
}