2017-12-20 08:36:49 -06:00
< ? php
/**
* Snmpsim . php
*
* Light wrapper around Snmpsim
*
* 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
2021-02-09 00:29:04 +01:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2017-12-20 08:36:49 -06:00
*
2021-02-09 00:29:04 +01:00
* @ link https :// www . librenms . org
2017-12-20 08:36:49 -06:00
* @ copyright 2017 Tony Murray
* @ author Tony Murray < murraytony @ gmail . com >
*/
namespace LibreNMS\Util ;
2021-05-13 07:18:54 -05:00
use App ;
2017-12-20 08:36:49 -06:00
use LibreNMS\Config ;
use LibreNMS\Proc ;
class Snmpsim
{
private $snmprec_dir ;
private $ip ;
private $port ;
private $log ;
2020-09-21 14:54:51 +02:00
/** @var Proc */
2017-12-20 08:36:49 -06:00
private $proc ;
public function __construct ( $ip = '127.1.6.1' , $port = 1161 , $log = '/tmp/snmpsimd.log' )
{
$this -> ip = $ip ;
$this -> port = $port ;
$this -> log = $log ;
2020-09-21 15:59:34 +02:00
$this -> snmprec_dir = Config :: get ( 'install_dir' ) . '/tests/snmpsim/' ;
2017-12-20 08:36:49 -06:00
}
2018-04-05 16:49:58 -05:00
/**
* Run snmpsimd and fork it into the background
* Captures all output to the log
*
2021-09-08 23:35:56 +02:00
* @ param int $wait Wait for x seconds after starting before returning
2018-04-05 16:49:58 -05:00
*/
public function fork ( $wait = 2 )
2017-12-20 08:36:49 -06:00
{
if ( $this -> isRunning ()) {
echo " Snmpsim is already running! \n " ;
2020-09-21 14:54:51 +02:00
2017-12-20 08:36:49 -06:00
return ;
}
$cmd = $this -> getCmd ();
2021-05-13 07:18:54 -05:00
if ( App :: runningInConsole ()) {
2017-12-20 08:36:49 -06:00
echo " Starting snmpsim listening on { $this -> ip } : { $this -> port } ... \n " ;
d_echo ( $cmd );
}
$this -> proc = new Proc ( $cmd );
2018-04-05 16:49:58 -05:00
if ( $wait ) {
sleep ( $wait );
}
2021-05-13 07:18:54 -05:00
if ( App :: runningInConsole () && ! $this -> proc -> isRunning ()) {
2018-05-09 06:53:45 -05:00
// if starting failed, run snmpsim again and output to the console and validate the data
passthru ( $this -> getCmd ( false ) . ' --validate-data' );
2020-09-21 14:54:51 +02:00
if ( ! is_executable ( $this -> findSnmpsimd ())) {
2019-01-09 06:29:22 -06:00
echo " \n Could not find snmpsim, you can install it with 'pip install snmpsim'. If it is already installed, make sure snmpsimd or snmpsimd.py is in PATH \n " ;
} else {
echo " \n Failed to start Snmpsim. Scroll up for error. \n " ;
}
2021-01-03 07:54:01 +01:00
exit ( 1 );
2017-12-20 08:36:49 -06:00
}
}
2018-04-05 16:49:58 -05:00
/**
* Stop and start the running snmpsim process
*/
2017-12-20 08:36:49 -06:00
public function restart ()
{
$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 );
}
}
2018-04-05 16:49:58 -05:00
/**
* Run snmpsimd but keep it in the foreground
* Outputs to stdout
*/
2017-12-20 08:36:49 -06:00
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 ;
}
2018-04-05 16:49:58 -05:00
/**
* Generate the command for snmpsimd
*
2021-09-08 23:35:56 +02:00
* @ param bool $with_log
2018-04-05 16:49:58 -05:00
* @ return string
*/
2017-12-20 08:36:49 -06:00
private function getCmd ( $with_log = true )
{
2019-01-09 06:29:22 -06:00
$cmd = $this -> findSnmpsimd ();
2018-08-04 16:26:50 -05:00
$cmd .= " --data-dir= { $this -> snmprec_dir } --agent-udpv4-endpoint= { $this -> ip } : { $this -> port } " ;
2017-12-20 08:36:49 -06:00
2018-05-09 06:53:45 -05:00
if ( is_null ( $this -> log )) {
2020-09-21 15:59:34 +02:00
$cmd .= ' --logging-method=null' ;
2018-05-09 06:53:45 -05:00
} elseif ( $with_log ) {
2017-12-20 08:36:49 -06:00
$cmd .= " --logging-method=file: { $this -> log } " ;
}
return $cmd ;
}
public function __destruct ()
{
// unset $this->proc to make sure it isn't referenced
unset ( $this -> proc );
}
2019-01-09 06:29:22 -06:00
2021-03-03 21:42:49 -06:00
public function findSnmpsimd ()
2019-01-09 06:29:22 -06:00
{
$cmd = Config :: locateBinary ( 'snmpsimd' );
2020-09-21 14:54:51 +02:00
if ( ! is_executable ( $cmd )) {
2019-01-09 06:29:22 -06:00
$cmd = Config :: locateBinary ( 'snmpsimd.py' );
}
2020-09-21 14:54:51 +02:00
2019-01-09 06:29:22 -06:00
return $cmd ;
}
2017-12-20 08:36:49 -06:00
}